api.c 2.4 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #endif
  215. #ifndef NO_SIG_WRAPPER
  216. #include <wolfssl/wolfcrypt/signature.h>
  217. #endif
  218. #ifdef HAVE_AESCCM
  219. #include <wolfssl/wolfcrypt/aes.h>
  220. #endif
  221. #ifdef HAVE_PKCS7
  222. #include <wolfssl/wolfcrypt/pkcs7.h>
  223. #include <wolfssl/wolfcrypt/asn.h>
  224. #ifdef HAVE_LIBZ
  225. #include <wolfssl/wolfcrypt/compress.h>
  226. #endif
  227. #endif
  228. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  229. #include <wolfssl/wolfcrypt/asn.h>
  230. #endif
  231. #ifndef NO_DSA
  232. #include <wolfssl/wolfcrypt/dsa.h>
  233. #ifndef ONEK_BUF
  234. #define ONEK_BUF 1024
  235. #endif
  236. #ifndef TWOK_BUF
  237. #define TWOK_BUF 2048
  238. #endif
  239. #ifndef FOURK_BUF
  240. #define FOURK_BUF 4096
  241. #endif
  242. #ifndef DSA_SIG_SIZE
  243. #define DSA_SIG_SIZE 40
  244. #endif
  245. #ifndef MAX_DSA_PARAM_SIZE
  246. #define MAX_DSA_PARAM_SIZE 256
  247. #endif
  248. #endif
  249. #ifdef WOLFSSL_CMAC
  250. #include <wolfssl/wolfcrypt/cmac.h>
  251. #endif
  252. #ifdef HAVE_ED25519
  253. #include <wolfssl/wolfcrypt/ed25519.h>
  254. #endif
  255. #ifdef HAVE_CURVE25519
  256. #include <wolfssl/wolfcrypt/curve25519.h>
  257. #endif
  258. #ifdef HAVE_ED448
  259. #include <wolfssl/wolfcrypt/ed448.h>
  260. #endif
  261. #ifdef HAVE_CURVE448
  262. #include <wolfssl/wolfcrypt/curve448.h>
  263. #endif
  264. #ifdef HAVE_PKCS12
  265. #include <wolfssl/wolfcrypt/pkcs12.h>
  266. #endif
  267. #include <wolfssl/wolfcrypt/logging.h>
  268. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  269. #include <wolfssl/openssl/ssl.h>
  270. #ifndef NO_ASN
  271. /* for ASN_COMMON_NAME DN_tags enum */
  272. #include <wolfssl/wolfcrypt/asn.h>
  273. #endif
  274. #ifdef HAVE_OCSP
  275. #include <wolfssl/openssl/ocsp.h>
  276. #endif
  277. #endif
  278. #ifdef OPENSSL_EXTRA
  279. #include <wolfssl/openssl/cmac.h>
  280. #include <wolfssl/openssl/x509v3.h>
  281. #include <wolfssl/openssl/asn1.h>
  282. #include <wolfssl/openssl/crypto.h>
  283. #include <wolfssl/openssl/pkcs12.h>
  284. #include <wolfssl/openssl/evp.h>
  285. #include <wolfssl/openssl/dh.h>
  286. #include <wolfssl/openssl/bn.h>
  287. #include <wolfssl/openssl/buffer.h>
  288. #include <wolfssl/openssl/pem.h>
  289. #include <wolfssl/openssl/ec.h>
  290. #include <wolfssl/openssl/ecdh.h>
  291. #include <wolfssl/openssl/engine.h>
  292. #include <wolfssl/openssl/hmac.h>
  293. #include <wolfssl/openssl/objects.h>
  294. #include <wolfssl/openssl/rand.h>
  295. #include <wolfssl/openssl/modes.h>
  296. #include <wolfssl/openssl/fips_rand.h>
  297. #include <wolfssl/openssl/kdf.h>
  298. #ifdef OPENSSL_ALL
  299. #include <wolfssl/openssl/txt_db.h>
  300. #include <wolfssl/openssl/lhash.h>
  301. #endif
  302. #ifndef NO_AES
  303. #include <wolfssl/openssl/aes.h>
  304. #endif
  305. #ifndef NO_DES3
  306. #include <wolfssl/openssl/des.h>
  307. #endif
  308. #ifndef NO_RC4
  309. #include <wolfssl/openssl/rc4.h>
  310. #endif
  311. #ifdef HAVE_ECC
  312. #include <wolfssl/openssl/ecdsa.h>
  313. #endif
  314. #ifdef HAVE_PKCS7
  315. #include <wolfssl/openssl/pkcs7.h>
  316. #endif
  317. #ifdef HAVE_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) || defined(WOLFSSL_TLS13)
  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. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  349. * have default (hidden) visibility, and in the absence of visibility, it's
  350. * benign to mask out the library implementation.
  351. */
  352. #define WOLFSSL_MISC_INCLUDED
  353. #include <wolfcrypt/src/misc.c>
  354. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  355. /* FIPS build has replaced ecc.h. */
  356. #define wc_ecc_key_get_priv(key) (&((key)->k))
  357. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  358. #endif
  359. typedef struct testVector {
  360. const char* input;
  361. const char* output;
  362. size_t inLen;
  363. size_t outLen;
  364. } testVector;
  365. #if defined(HAVE_PKCS7)
  366. typedef struct {
  367. const byte* content;
  368. word32 contentSz;
  369. int contentOID;
  370. int encryptOID;
  371. int keyWrapOID;
  372. int keyAgreeOID;
  373. byte* cert;
  374. size_t certSz;
  375. byte* privateKey;
  376. word32 privateKeySz;
  377. } pkcs7EnvelopedVector;
  378. #ifndef NO_PKCS7_ENCRYPTED_DATA
  379. typedef struct {
  380. const byte* content;
  381. word32 contentSz;
  382. int contentOID;
  383. int encryptOID;
  384. byte* encryptionKey;
  385. word32 encryptionKeySz;
  386. } pkcs7EncryptedVector;
  387. #endif
  388. #endif /* HAVE_PKCS7 */
  389. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  390. typedef int (*ssl_cb)(WOLFSSL* ssl);
  391. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  392. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  393. typedef struct test_ssl_cbf {
  394. method_provider method;
  395. ctx_cb ctx_ready;
  396. ssl_cb ssl_ready;
  397. ssl_cb on_result;
  398. ctx_cb on_ctx_cleanup;
  399. ssl_cb on_cleanup;
  400. hs_cb on_handshake;
  401. WOLFSSL_CTX* ctx;
  402. const char* caPemFile;
  403. const char* certPemFile;
  404. const char* keyPemFile;
  405. const char* crlPemFile;
  406. #ifdef WOLFSSL_STATIC_MEMORY
  407. byte* mem;
  408. word32 memSz;
  409. wolfSSL_method_func method_ex;
  410. #endif
  411. int devId;
  412. int return_code;
  413. int last_err;
  414. unsigned char isSharedCtx:1;
  415. unsigned char loadToSSL:1;
  416. unsigned char ticNoInit:1;
  417. unsigned char doUdp:1;
  418. } test_ssl_cbf;
  419. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  420. typedef struct test_ssl_memio_ctx {
  421. WOLFSSL_CTX* s_ctx;
  422. WOLFSSL_CTX* c_ctx;
  423. WOLFSSL* s_ssl;
  424. WOLFSSL* c_ssl;
  425. const char* c_ciphers;
  426. const char* s_ciphers;
  427. char* c_msg;
  428. int c_msglen;
  429. char* s_msg;
  430. int s_msglen;
  431. test_ssl_cbf s_cb;
  432. test_ssl_cbf c_cb;
  433. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  434. int c_len;
  435. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  436. int s_len;
  437. } test_ssl_memio_ctx;
  438. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  439. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  440. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  441. const char* currentTestName;
  442. char tmpDirName[16];
  443. int tmpDirNameSet = 0;
  444. #endif
  445. /*----------------------------------------------------------------------------*
  446. | Constants
  447. *----------------------------------------------------------------------------*/
  448. /* Test result constants and macros. */
  449. /* Test succeeded. */
  450. #define TEST_SUCCESS (1)
  451. /* Test failed. */
  452. #define TEST_FAIL (0)
  453. /* Test skipped - not run. */
  454. #define TEST_SKIPPED (-7777)
  455. /* Returns the result based on whether check is true.
  456. *
  457. * @param [in] check Condition for success.
  458. * @return When condition is true: TEST_SUCCESS.
  459. * @return When condition is false: TEST_FAIL.
  460. */
  461. #ifdef DEBUG_WOLFSSL_VERBOSE
  462. #define XSTRINGIFY(s) STRINGIFY(s)
  463. #define STRINGIFY(s) #s
  464. #define TEST_RES_CHECK(check) ({ \
  465. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  466. if (_ret == TEST_FAIL) { \
  467. fprintf(stderr, " check \"%s\" at %d ", \
  468. XSTRINGIFY(check), __LINE__); \
  469. } \
  470. _ret; })
  471. #else
  472. #define TEST_RES_CHECK(check) \
  473. ((check) ? TEST_SUCCESS : TEST_FAIL)
  474. #endif /* DEBUG_WOLFSSL_VERBOSE */
  475. #define TEST_STRING "Everyone gets Friday off."
  476. #define TEST_STRING_SZ 25
  477. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  478. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  479. #define TEST_RSA_BITS 1024
  480. #else
  481. #define TEST_RSA_BITS 2048
  482. #endif
  483. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  484. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  485. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  486. static const char* bogusFile =
  487. #ifdef _WIN32
  488. "NUL"
  489. #else
  490. "/dev/null"
  491. #endif
  492. ;
  493. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  494. enum {
  495. TESTING_RSA = 1,
  496. TESTING_ECC = 2
  497. };
  498. #ifdef WOLFSSL_QNX_CAAM
  499. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  500. static int testDevId = WOLFSSL_CAAM_DEVID;
  501. #else
  502. static int testDevId = INVALID_DEVID;
  503. #endif
  504. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  505. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  506. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  507. #define HAVE_IO_TESTS_DEPENDENCIES
  508. #endif
  509. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  510. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  511. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  512. #endif
  513. /*----------------------------------------------------------------------------*
  514. | BIO with fixed read/write size
  515. *----------------------------------------------------------------------------*/
  516. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  517. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  518. int len)
  519. {
  520. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  521. len = 0;
  522. }
  523. else {
  524. if (bio->wrSz - bio->wrIdx < len) {
  525. len = bio->wrSz - bio->wrIdx;
  526. }
  527. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  528. bio->wrIdx += len;
  529. }
  530. return len;
  531. }
  532. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  533. {
  534. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  535. len = 0;
  536. }
  537. else {
  538. if (bio->wrSz - bio->rdIdx < len) {
  539. len = bio->wrSz - bio->rdIdx;
  540. }
  541. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  542. bio->rdIdx += len;
  543. }
  544. return len;
  545. }
  546. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  547. {
  548. static WOLFSSL_BIO_METHOD meth;
  549. meth.type = WOLFSSL_BIO_BIO;
  550. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  551. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  552. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  553. return &meth;
  554. }
  555. #endif
  556. /*----------------------------------------------------------------------------*
  557. | Setup
  558. *----------------------------------------------------------------------------*/
  559. static int test_wolfSSL_Init(void)
  560. {
  561. EXPECT_DECLS;
  562. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  563. return EXPECT_RESULT();
  564. }
  565. static int test_wolfSSL_Cleanup(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  569. return EXPECT_RESULT();
  570. }
  571. /* Initialize the wolfCrypt state.
  572. * POST: 0 success.
  573. */
  574. static int test_wolfCrypt_Init(void)
  575. {
  576. EXPECT_DECLS;
  577. ExpectIntEQ(wolfCrypt_Init(), 0);
  578. return EXPECT_RESULT();
  579. } /* END test_wolfCrypt_Init */
  580. static int test_wolfCrypt_Cleanup(void)
  581. {
  582. EXPECT_DECLS;
  583. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  584. return EXPECT_RESULT();
  585. }
  586. /*----------------------------------------------------------------------------*
  587. | Platform dependent function test
  588. *----------------------------------------------------------------------------*/
  589. static int test_fileAccess(void)
  590. {
  591. EXPECT_DECLS;
  592. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  593. const char *fname[] = {
  594. svrCertFile, svrKeyFile, caCertFile,
  595. eccCertFile, eccKeyFile, eccRsaCertFile,
  596. cliCertFile, cliCertDerFile, cliKeyFile,
  597. dhParamFile,
  598. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  599. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  600. NULL
  601. };
  602. const char derfile[] = "./certs/server-cert.der";
  603. XFILE f = XBADFILE;
  604. size_t sz;
  605. byte *buff = NULL;
  606. int i;
  607. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  608. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  609. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  610. XFCLOSE(f);
  611. }
  612. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  613. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  614. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  615. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  616. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  617. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  618. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  619. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  620. XFCLOSE(f);
  621. #endif
  622. return EXPECT_RESULT();
  623. }
  624. /*----------------------------------------------------------------------------*
  625. | Method Allocators
  626. *----------------------------------------------------------------------------*/
  627. static int test_wolfSSL_Method_Allocators(void)
  628. {
  629. EXPECT_DECLS;
  630. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  631. do { \
  632. WOLFSSL_METHOD *method = NULL; \
  633. condition(method = allocator()); \
  634. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  635. } while (0)
  636. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  637. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  638. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  639. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  640. #ifndef NO_OLD_TLS
  641. #ifdef WOLFSSL_ALLOW_SSLV3
  642. #ifndef NO_WOLFSSL_SERVER
  643. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  644. #endif
  645. #ifndef NO_WOLFSSL_CLIENT
  646. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  647. #endif
  648. #endif
  649. #ifdef WOLFSSL_ALLOW_TLSV10
  650. #ifndef NO_WOLFSSL_SERVER
  651. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  652. #endif
  653. #ifndef NO_WOLFSSL_CLIENT
  654. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  655. #endif
  656. #endif
  657. #ifndef NO_WOLFSSL_SERVER
  658. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  659. #endif
  660. #ifndef NO_WOLFSSL_CLIENT
  661. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  662. #endif
  663. #endif /* !NO_OLD_TLS */
  664. #ifndef WOLFSSL_NO_TLS12
  665. #ifndef NO_WOLFSSL_SERVER
  666. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  667. #endif
  668. #ifndef NO_WOLFSSL_CLIENT
  669. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  670. #endif
  671. #endif /* !WOLFSSL_NO_TLS12 */
  672. #ifdef WOLFSSL_TLS13
  673. #ifndef NO_WOLFSSL_SERVER
  674. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  675. #endif
  676. #ifndef NO_WOLFSSL_CLIENT
  677. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  678. #endif
  679. #endif /* WOLFSSL_TLS13 */
  680. #ifndef NO_WOLFSSL_SERVER
  681. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  682. #endif
  683. #ifndef NO_WOLFSSL_CLIENT
  684. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  685. #endif
  686. #ifdef WOLFSSL_DTLS
  687. #ifndef NO_OLD_TLS
  688. #ifndef NO_WOLFSSL_SERVER
  689. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  690. #endif
  691. #ifndef NO_WOLFSSL_CLIENT
  692. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  693. #endif
  694. #endif
  695. #ifndef WOLFSSL_NO_TLS12
  696. #ifndef NO_WOLFSSL_SERVER
  697. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  698. #endif
  699. #ifndef NO_WOLFSSL_CLIENT
  700. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  701. #endif
  702. #endif
  703. #endif /* WOLFSSL_DTLS */
  704. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  705. /* Stubs */
  706. #ifndef NO_WOLFSSL_SERVER
  707. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  708. #endif
  709. #ifndef NO_WOLFSSL_CLIENT
  710. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  711. #endif
  712. #endif
  713. /* Test Either Method (client or server) */
  714. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  715. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  716. #ifndef NO_OLD_TLS
  717. #ifdef WOLFSSL_ALLOW_TLSV10
  718. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  719. #endif
  720. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  721. #endif /* !NO_OLD_TLS */
  722. #ifndef WOLFSSL_NO_TLS12
  723. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  724. #endif /* !WOLFSSL_NO_TLS12 */
  725. #ifdef WOLFSSL_TLS13
  726. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  727. #endif /* WOLFSSL_TLS13 */
  728. #ifdef WOLFSSL_DTLS
  729. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  730. #ifndef NO_OLD_TLS
  731. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  732. #endif /* !NO_OLD_TLS */
  733. #ifndef WOLFSSL_NO_TLS12
  734. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  735. #endif /* !WOLFSSL_NO_TLS12 */
  736. #endif /* WOLFSSL_DTLS */
  737. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  738. return EXPECT_RESULT();
  739. }
  740. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  741. /*----------------------------------------------------------------------------*
  742. | Dual algorithm Certificate Tests
  743. *----------------------------------------------------------------------------*/
  744. #define LARGE_TEMP_SZ 4096
  745. /* To better understand this, please see the X9.146 example in wolfssl-examples
  746. * repo. */
  747. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  748. char *sapkiFile, char *altPrivFile)
  749. {
  750. EXPECT_DECLS;
  751. FILE* file = NULL;
  752. Cert newCert;
  753. DecodedCert preTBS;
  754. byte caKeyBuf[LARGE_TEMP_SZ];
  755. word32 caKeySz = LARGE_TEMP_SZ;
  756. byte sapkiBuf[LARGE_TEMP_SZ];
  757. word32 sapkiSz = LARGE_TEMP_SZ;
  758. byte altPrivBuf[LARGE_TEMP_SZ];
  759. word32 altPrivSz = LARGE_TEMP_SZ;
  760. byte altSigAlgBuf[LARGE_TEMP_SZ];
  761. word32 altSigAlgSz = LARGE_TEMP_SZ;
  762. byte scratchBuf[LARGE_TEMP_SZ];
  763. word32 scratchSz = LARGE_TEMP_SZ;
  764. byte preTbsBuf[LARGE_TEMP_SZ];
  765. word32 preTbsSz = LARGE_TEMP_SZ;
  766. byte altSigValBuf[LARGE_TEMP_SZ];
  767. word32 altSigValSz = LARGE_TEMP_SZ;
  768. byte *outBuf = NULL;
  769. word32 outSz = LARGE_TEMP_SZ;
  770. WC_RNG rng;
  771. RsaKey caKey;
  772. ecc_key altCaKey;
  773. word32 idx = 0;
  774. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  775. DYNAMIC_TYPE_TMP_BUFFER));
  776. ExpectIntEQ(wc_InitRng(&rng), 0);
  777. XMEMSET(caKeyBuf, 0, caKeySz);
  778. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  779. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  780. fclose(file);
  781. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  782. idx = 0;
  783. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  784. 0);
  785. XMEMSET(sapkiBuf, 0, sapkiSz);
  786. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  787. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  788. fclose(file);
  789. XMEMSET(altPrivBuf, 0, altPrivSz);
  790. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  791. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  792. fclose(file);
  793. wc_ecc_init(&altCaKey);
  794. idx = 0;
  795. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  796. (word32)altPrivSz), 0);
  797. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  798. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  799. oidSigType, 0), 0);
  800. wc_InitCert(&newCert);
  801. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  802. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  803. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  804. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  805. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  806. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  807. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  808. newCert.sigType = CTC_SHA256wRSA;
  809. newCert.isCA = 1;
  810. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  811. (const byte *)"This is NOT a critical extension", 32), 0);
  812. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  813. sapkiSz), 0);
  814. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  815. altSigAlgSz), 0);
  816. XMEMSET(scratchBuf, 0, scratchSz);
  817. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  818. &caKey, &rng), 0);
  819. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  820. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  821. XMEMSET(preTbsBuf, 0, preTbsSz);
  822. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  823. XMEMSET(altSigValBuf, 0, altSigValSz);
  824. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  825. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  826. &rng), 0);
  827. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  828. altSigValSz), 0);
  829. /* Finally, generate the new certificate. */
  830. XMEMSET(outBuf, 0, outSz);
  831. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  832. 0);
  833. *out = outBuf;
  834. wc_FreeRsaKey(&caKey);
  835. wc_FreeRng(&rng);
  836. return outSz;
  837. }
  838. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  839. char *sapkiFile, char *altPrivFile,
  840. char *serverKeyFile,
  841. byte *caCertBuf, int caCertSz)
  842. {
  843. EXPECT_DECLS;
  844. FILE* file = NULL;
  845. Cert newCert;
  846. DecodedCert preTBS;
  847. byte serverKeyBuf[LARGE_TEMP_SZ];
  848. word32 serverKeySz = LARGE_TEMP_SZ;
  849. byte caKeyBuf[LARGE_TEMP_SZ];
  850. word32 caKeySz = LARGE_TEMP_SZ;
  851. byte sapkiBuf[LARGE_TEMP_SZ];
  852. word32 sapkiSz = LARGE_TEMP_SZ;
  853. byte altPrivBuf[LARGE_TEMP_SZ];
  854. word32 altPrivSz = LARGE_TEMP_SZ;
  855. byte altSigAlgBuf[LARGE_TEMP_SZ];
  856. word32 altSigAlgSz = LARGE_TEMP_SZ;
  857. byte scratchBuf[LARGE_TEMP_SZ];
  858. word32 scratchSz = LARGE_TEMP_SZ;
  859. byte preTbsBuf[LARGE_TEMP_SZ];
  860. word32 preTbsSz = LARGE_TEMP_SZ;
  861. byte altSigValBuf[LARGE_TEMP_SZ];
  862. word32 altSigValSz = LARGE_TEMP_SZ;
  863. byte *outBuf = NULL;
  864. word32 outSz = LARGE_TEMP_SZ;
  865. WC_RNG rng;
  866. RsaKey caKey;
  867. RsaKey serverKey;
  868. ecc_key altCaKey;
  869. word32 idx = 0;
  870. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  871. DYNAMIC_TYPE_TMP_BUFFER));
  872. ExpectIntEQ(wc_InitRng(&rng), 0);
  873. XMEMSET(serverKeyBuf, 0, serverKeySz);
  874. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  875. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  876. 0);
  877. fclose(file);
  878. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  879. idx = 0;
  880. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  881. (word32)serverKeySz), 0);
  882. XMEMSET(caKeyBuf, 0, caKeySz);
  883. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  884. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  885. fclose(file);
  886. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  887. idx = 0;
  888. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  889. (word32)caKeySz), 0);
  890. XMEMSET(sapkiBuf, 0, sapkiSz);
  891. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  892. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  893. fclose(file);
  894. XMEMSET(altPrivBuf, 0, altPrivSz);
  895. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  896. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  897. fclose(file);
  898. wc_ecc_init(&altCaKey);
  899. idx = 0;
  900. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  901. (word32)altPrivSz), 0);
  902. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  903. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  904. oidSigType, 0), 0);
  905. wc_InitCert(&newCert);
  906. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  907. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  908. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  909. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  910. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  911. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  912. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  913. newCert.sigType = CTC_SHA256wRSA;
  914. newCert.isCA = 0;
  915. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  916. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  917. (const byte *)"This is NOT a critical extension", 32), 0);
  918. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  919. sapkiSz), 0);
  920. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  921. altSigAlgSz), 0);
  922. XMEMSET(scratchBuf, 0, scratchSz);
  923. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  924. &rng), 0);
  925. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  926. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  927. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  928. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  929. XMEMSET(preTbsBuf, 0, preTbsSz);
  930. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  931. XMEMSET(altSigValBuf, 0, altSigValSz);
  932. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  933. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  934. &rng), 0);
  935. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  936. altSigValBuf, altSigValSz), 0);
  937. /* Finally, generate the new certificate. */
  938. XMEMSET(outBuf, 0, outSz);
  939. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  940. 0);
  941. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  942. outSz, &caKey, NULL, &rng), 0);
  943. *out = outBuf;
  944. wc_FreeRsaKey(&caKey);
  945. wc_FreeRsaKey(&serverKey);
  946. wc_FreeRng(&rng);
  947. return outSz;
  948. }
  949. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  950. byte *serverCert, word32 serverCertSz,
  951. byte *serverKey, word32 serverKeySz,
  952. int negative_test)
  953. {
  954. EXPECT_DECLS;
  955. WOLFSSL_CTX *ctx_c = NULL;
  956. WOLFSSL_CTX *ctx_s = NULL;
  957. WOLFSSL *ssl_c = NULL;
  958. WOLFSSL *ssl_s = NULL;
  959. struct test_memio_ctx test_ctx;
  960. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  961. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  962. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  963. caCert, caCertSz, serverCert, serverCertSz,
  964. serverKey, serverKeySz), 0);
  965. if (negative_test) {
  966. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  967. }
  968. else {
  969. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  970. }
  971. wolfSSL_free(ssl_c);
  972. wolfSSL_free(ssl_s);
  973. wolfSSL_CTX_free(ctx_c);
  974. wolfSSL_CTX_free(ctx_s);
  975. return EXPECT_RESULT();
  976. }
  977. static int extCount = 0;
  978. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  979. const unsigned char* der, word32 derSz)
  980. {
  981. (void) oid;
  982. (void) oidSz;
  983. (void) crit;
  984. (void) der;
  985. (void) derSz;
  986. extCount ++;
  987. /* Accept all extensions. This is only a test. Normally we would be much more
  988. * careful about critical extensions. */
  989. return 1;
  990. }
  991. static int test_dual_alg_support(void)
  992. {
  993. EXPECT_DECLS;
  994. /* Root CA and server keys will be the same. This is only appropriate for
  995. * testing. */
  996. char keyFile[] = "./certs/ca-key.der";
  997. char sapkiFile[] = "./certs/ecc-keyPub.der";
  998. char altPrivFile[] = "./certs/ecc-key.der";
  999. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1000. byte *serverKey = NULL;
  1001. size_t serverKeySz = 0;
  1002. byte *root = NULL;
  1003. int rootSz = 0;
  1004. byte *server = NULL;
  1005. int serverSz = 0;
  1006. WOLFSSL_CERT_MANAGER* cm = NULL;
  1007. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1008. /* Base normal case. */
  1009. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1010. ExpectNotNull(root);
  1011. ExpectIntGT(rootSz, 0);
  1012. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1013. altPrivFile, keyFile, root, rootSz);
  1014. ExpectNotNull(server);
  1015. ExpectIntGT(serverSz, 0);
  1016. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1017. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1018. TEST_SUCCESS);
  1019. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1020. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1021. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1022. * the alternative signature and then set negative_test to true for the
  1023. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1024. * because the signature won't verify. */
  1025. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1026. ExpectNotNull(root);
  1027. ExpectIntGT(rootSz, 0);
  1028. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1029. wrongPrivFile, keyFile, root, rootSz);
  1030. ExpectNotNull(server);
  1031. ExpectIntGT(serverSz, 0);
  1032. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1033. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1034. TEST_SUCCESS);
  1035. /* Lets see if CertManager can find the new extensions */
  1036. extCount = 0;
  1037. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1038. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1039. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1040. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1041. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1042. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1043. /* There is only 1 unknown exension (1.2.3.4.5). The other ones are known
  1044. * because they are for the dual alg extensions. */
  1045. ExpectIntEQ(extCount, 1);
  1046. wolfSSL_CertManagerFree(cm);
  1047. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1048. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1049. free(serverKey);
  1050. return EXPECT_RESULT();
  1051. }
  1052. #else
  1053. static int test_dual_alg_support(void)
  1054. {
  1055. return TEST_SKIPPED;
  1056. }
  1057. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1058. /*----------------------------------------------------------------------------*
  1059. | Context
  1060. *----------------------------------------------------------------------------*/
  1061. #ifndef NO_WOLFSSL_SERVER
  1062. static int test_wolfSSL_CTX_new(void)
  1063. {
  1064. EXPECT_DECLS;
  1065. WOLFSSL_CTX *ctx;
  1066. WOLFSSL_METHOD* method;
  1067. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1068. ExpectNotNull(method = wolfSSLv23_server_method());
  1069. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1070. wolfSSL_CTX_free(ctx);
  1071. return EXPECT_RESULT();
  1072. }
  1073. #endif
  1074. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1075. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1076. static int test_for_double_Free(void)
  1077. {
  1078. EXPECT_DECLS;
  1079. WOLFSSL_CTX* ctx = NULL;
  1080. WOLFSSL* ssl = NULL;
  1081. int skipTest = 0;
  1082. const char* testCertFile;
  1083. const char* testKeyFile;
  1084. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1085. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1086. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1087. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1088. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1089. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1090. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1091. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1092. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1093. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1094. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1095. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1096. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1097. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1098. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1099. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1100. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1101. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1102. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1103. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1104. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1105. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1106. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1107. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1108. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1109. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1110. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1111. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1112. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1113. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1114. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1115. #ifdef OPENSSL_EXTRA
  1116. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1117. "!SRP:!kRSA:!aNULL:!eNULL";
  1118. #endif
  1119. #ifndef NO_RSA
  1120. testCertFile = svrCertFile;
  1121. testKeyFile = svrKeyFile;
  1122. #elif defined(HAVE_ECC)
  1123. testCertFile = eccCertFile;
  1124. testKeyFile = eccKeyFile;
  1125. #else
  1126. skipTest = 1;
  1127. #endif
  1128. if (skipTest != 1) {
  1129. #ifndef NO_WOLFSSL_SERVER
  1130. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1131. #else
  1132. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1133. #endif
  1134. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1135. WOLFSSL_FILETYPE_PEM));
  1136. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1137. WOLFSSL_FILETYPE_PEM));
  1138. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1139. /* First test freeing SSL, then CTX */
  1140. wolfSSL_free(ssl);
  1141. ssl = NULL;
  1142. wolfSSL_CTX_free(ctx);
  1143. ctx = NULL;
  1144. #ifndef NO_WOLFSSL_CLIENT
  1145. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1146. #else
  1147. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1148. #endif
  1149. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1150. WOLFSSL_FILETYPE_PEM));
  1151. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1152. WOLFSSL_FILETYPE_PEM));
  1153. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1154. /* Next test freeing CTX then SSL */
  1155. wolfSSL_CTX_free(ctx);
  1156. ctx = NULL;
  1157. wolfSSL_free(ssl);
  1158. ssl = NULL;
  1159. #ifndef NO_WOLFSSL_SERVER
  1160. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1161. #else
  1162. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1163. #endif
  1164. /* Test setting ciphers at ctx level */
  1165. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1166. WOLFSSL_FILETYPE_PEM));
  1167. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1168. WOLFSSL_FILETYPE_PEM));
  1169. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1170. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1171. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1172. /* only update TLSv13 suites */
  1173. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1174. #endif
  1175. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1176. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1177. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1178. /* only update pre-TLSv13 suites */
  1179. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1180. "ECDHE-RSA-AES128-GCM-SHA256"));
  1181. #endif
  1182. #ifdef OPENSSL_EXTRA
  1183. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1184. #endif
  1185. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1186. wolfSSL_CTX_free(ctx);
  1187. ctx = NULL;
  1188. wolfSSL_free(ssl);
  1189. ssl = NULL;
  1190. #ifndef NO_WOLFSSL_CLIENT
  1191. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1192. #else
  1193. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1194. #endif
  1195. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1196. WOLFSSL_FILETYPE_PEM));
  1197. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1198. WOLFSSL_FILETYPE_PEM));
  1199. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1200. /* test setting ciphers at SSL level */
  1201. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1202. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1203. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1204. /* only update TLSv13 suites */
  1205. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1206. #endif
  1207. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1208. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1209. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1210. /* only update pre-TLSv13 suites */
  1211. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1212. #endif
  1213. wolfSSL_CTX_free(ctx);
  1214. ctx = NULL;
  1215. wolfSSL_free(ssl);
  1216. ssl = NULL;
  1217. }
  1218. return EXPECT_RESULT();
  1219. }
  1220. #endif
  1221. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1222. {
  1223. EXPECT_DECLS;
  1224. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1225. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1226. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1227. const char* testCertFile;
  1228. const char* testKeyFile;
  1229. WOLFSSL_CTX* ctx = NULL;
  1230. WOLFSSL* ssl = NULL;
  1231. const byte cipherList[] =
  1232. {
  1233. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1234. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1235. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1236. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1237. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1238. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1239. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1240. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1241. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1242. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1243. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1244. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1245. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1246. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1247. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1248. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1249. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1250. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1251. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1252. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1253. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1254. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1255. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1256. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1257. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1258. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1259. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1260. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1261. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1262. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1263. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1264. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1265. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1266. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1267. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1268. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1269. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1270. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1271. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1272. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1273. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1274. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1275. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1276. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1277. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1278. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1279. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1280. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1281. /* SHA256 */
  1282. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1283. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1284. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1285. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1286. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1287. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1288. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1289. /* SHA384 */
  1290. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1291. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1292. /* AES-GCM */
  1293. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1294. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1295. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1296. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1297. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1298. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1299. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1300. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1301. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1302. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1303. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1304. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1305. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1306. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1307. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1308. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1309. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1310. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1311. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1312. * also, in some of the other AES-CCM suites
  1313. * there will be second byte number conflicts
  1314. * with non-ECC AES-GCM */
  1315. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1316. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1317. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1318. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1319. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1320. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1321. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1322. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1323. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1324. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1325. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1326. /* Camellia */
  1327. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1328. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1329. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1330. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1331. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1332. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1333. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1334. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1335. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1336. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1337. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1338. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1339. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1340. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1341. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1342. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1343. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1344. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1345. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1346. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1347. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1348. /* TLS v1.3 cipher suites */
  1349. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1350. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1351. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1352. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1353. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1354. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1355. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1356. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1357. };
  1358. #ifndef NO_RSA
  1359. testCertFile = svrCertFile;
  1360. testKeyFile = svrKeyFile;
  1361. #elif defined(HAVE_ECC)
  1362. testCertFile = eccCertFile;
  1363. testKeyFile = eccKeyFile;
  1364. #endif
  1365. #ifndef NO_WOLFSSL_SERVER
  1366. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1367. #else
  1368. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1369. #endif
  1370. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1371. sizeof(cipherList)));
  1372. wolfSSL_CTX_free(ctx);
  1373. ctx = NULL;
  1374. #ifndef NO_WOLFSSL_SERVER
  1375. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1376. #else
  1377. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1378. #endif
  1379. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1380. WOLFSSL_FILETYPE_PEM));
  1381. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1382. WOLFSSL_FILETYPE_PEM));
  1383. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1384. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1385. sizeof(cipherList)));
  1386. wolfSSL_free(ssl);
  1387. wolfSSL_CTX_free(ctx);
  1388. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1389. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1390. return EXPECT_RESULT();
  1391. }
  1392. static int test_wolfSSL_CTX_use_certificate_file(void)
  1393. {
  1394. EXPECT_DECLS;
  1395. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1396. WOLFSSL_CTX *ctx = NULL;
  1397. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1398. /* invalid context */
  1399. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1400. WOLFSSL_FILETYPE_PEM));
  1401. /* invalid cert file */
  1402. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1403. WOLFSSL_FILETYPE_PEM));
  1404. /* invalid cert type */
  1405. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1406. #ifdef NO_RSA
  1407. /* rsa needed */
  1408. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1409. WOLFSSL_FILETYPE_PEM));
  1410. #else
  1411. /* success */
  1412. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1413. WOLFSSL_FILETYPE_PEM));
  1414. #endif
  1415. wolfSSL_CTX_free(ctx);
  1416. #endif
  1417. return EXPECT_RESULT();
  1418. }
  1419. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1420. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1421. {
  1422. EXPECT_DECLS;
  1423. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1424. WOLFSSL_CTX* ctx = NULL;
  1425. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1426. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1427. server_cert_der_2048), WOLFSSL_SUCCESS);
  1428. wolfSSL_CTX_free(ctx);
  1429. #endif
  1430. return EXPECT_RESULT();
  1431. }
  1432. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1433. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1434. * context using buffer.
  1435. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1436. * --enable-testcert flag.
  1437. */
  1438. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1439. {
  1440. EXPECT_DECLS;
  1441. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1442. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1443. WOLFSSL_CTX* ctx = NULL;
  1444. int ret;
  1445. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1446. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1447. server_cert_der_2048, sizeof_server_cert_der_2048,
  1448. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1449. wolfSSL_CTX_free(ctx);
  1450. #endif
  1451. return EXPECT_RESULT();
  1452. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1453. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1454. {
  1455. EXPECT_DECLS;
  1456. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1457. WOLFSSL_CTX *ctx = NULL;
  1458. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1459. /* invalid context */
  1460. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1461. WOLFSSL_FILETYPE_PEM));
  1462. /* invalid key file */
  1463. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1464. WOLFSSL_FILETYPE_PEM));
  1465. /* invalid key type */
  1466. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1467. /* success */
  1468. #ifdef NO_RSA
  1469. /* rsa needed */
  1470. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1471. WOLFSSL_FILETYPE_PEM));
  1472. #else
  1473. /* success */
  1474. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1475. WOLFSSL_FILETYPE_PEM));
  1476. #endif
  1477. wolfSSL_CTX_free(ctx);
  1478. #endif
  1479. return EXPECT_RESULT();
  1480. }
  1481. /* test both file and buffer versions along with unloading trusted peer certs */
  1482. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1483. {
  1484. EXPECT_DECLS;
  1485. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1486. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1487. WOLFSSL_CTX *ctx = NULL;
  1488. WOLFSSL* ssl = NULL;
  1489. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1490. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1491. #if !defined(NO_FILESYSTEM)
  1492. /* invalid file */
  1493. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1494. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1495. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1496. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1497. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1498. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1499. /* success */
  1500. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1501. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1502. /* unload cert */
  1503. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1504. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1505. /* invalid file */
  1506. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1507. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1508. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1509. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1510. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1511. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1512. /* success */
  1513. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1514. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1515. #ifdef WOLFSSL_LOCAL_X509_STORE
  1516. /* unload cert */
  1517. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1518. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1519. #endif
  1520. #endif
  1521. /* Test of loading certs from buffers */
  1522. /* invalid buffer */
  1523. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1524. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1525. /* success */
  1526. #ifdef USE_CERT_BUFFERS_1024
  1527. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1528. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1529. #endif
  1530. #ifdef USE_CERT_BUFFERS_2048
  1531. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1532. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1533. #endif
  1534. /* unload cert */
  1535. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1536. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1537. wolfSSL_free(ssl);
  1538. wolfSSL_CTX_free(ctx);
  1539. #endif
  1540. return EXPECT_RESULT();
  1541. }
  1542. static int test_wolfSSL_CTX_load_verify_locations(void)
  1543. {
  1544. EXPECT_DECLS;
  1545. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1546. WOLFSSL_CTX *ctx = NULL;
  1547. #ifndef NO_RSA
  1548. WOLFSSL_CERT_MANAGER* cm = NULL;
  1549. #ifdef PERSIST_CERT_CACHE
  1550. int cacheSz = 0;
  1551. unsigned char* cache = NULL;
  1552. int used = 0;
  1553. #ifndef NO_FILESYSTEM
  1554. const char* cacheFile = "./tests/cert_cache.tmp";
  1555. #endif
  1556. int i;
  1557. int t;
  1558. int* p;
  1559. #endif
  1560. #endif
  1561. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1562. const char* load_certs_path = "./certs/external";
  1563. const char* load_no_certs_path = "./examples";
  1564. const char* load_expired_path = "./certs/test/expired";
  1565. #endif
  1566. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1567. /* invalid arguments */
  1568. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1569. WOLFSSL_FAILURE);
  1570. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1571. WOLFSSL_FAILURE);
  1572. /* invalid ca file */
  1573. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1574. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1575. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1576. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1577. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1578. /* invalid path */
  1579. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1580. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1581. #endif
  1582. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1583. /* test ignoring the invalid path */
  1584. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1585. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1586. #endif
  1587. /* load ca cert */
  1588. #ifdef NO_RSA
  1589. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1590. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1591. #else /* Skip the following test without RSA certs. */
  1592. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1593. WOLFSSL_SUCCESS);
  1594. #ifdef PERSIST_CERT_CACHE
  1595. /* Get cert cache size */
  1596. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1597. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1598. DYNAMIC_TYPE_TMP_BUFFER));
  1599. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1600. BAD_FUNC_ARG);
  1601. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1602. BAD_FUNC_ARG);
  1603. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1604. BAD_FUNC_ARG);
  1605. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1606. BAD_FUNC_ARG);
  1607. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1608. BAD_FUNC_ARG);
  1609. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1610. BAD_FUNC_ARG);
  1611. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1612. BAD_FUNC_ARG);
  1613. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1614. BAD_FUNC_ARG);
  1615. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1616. BAD_FUNC_ARG);
  1617. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1618. BUFFER_E);
  1619. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1620. ExpectIntEQ(cacheSz, used);
  1621. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1622. BAD_FUNC_ARG);
  1623. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1624. BAD_FUNC_ARG);
  1625. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1626. BAD_FUNC_ARG);
  1627. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1628. BAD_FUNC_ARG);
  1629. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1630. BAD_FUNC_ARG);
  1631. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1632. BAD_FUNC_ARG);
  1633. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1634. BAD_FUNC_ARG);
  1635. /* Smaller than header. */
  1636. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1637. for (i = 1; i < cacheSz; i++) {
  1638. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1639. BUFFER_E);
  1640. }
  1641. if (EXPECT_SUCCESS()) {
  1642. /* Modify header for bad results! */
  1643. p = (int*)cache;
  1644. /* version */
  1645. t = p[0]; p[0] = 0xff;
  1646. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1647. CACHE_MATCH_ERROR);
  1648. p[0] = t; p++;
  1649. /* rows */
  1650. t = p[0]; p[0] = 0xff;
  1651. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1652. CACHE_MATCH_ERROR);
  1653. p[0] = t; p++;
  1654. /* columns[0] */
  1655. t = p[0]; p[0] = -1;
  1656. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1657. PARSE_ERROR);
  1658. p[0] = t; p += CA_TABLE_SIZE;
  1659. /* signerSz*/
  1660. t = p[0]; p[0] = 0xff;
  1661. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1662. CACHE_MATCH_ERROR);
  1663. p[0] = t;
  1664. }
  1665. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1666. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1667. #ifndef NO_FILESYSTEM
  1668. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1669. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1670. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1671. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1672. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1673. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1674. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1675. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1676. WOLFSSL_BAD_FILE);
  1677. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1678. /* File contents is not a cache. */
  1679. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1680. CACHE_MATCH_ERROR);
  1681. #endif
  1682. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1683. #endif
  1684. /* Test unloading CA's */
  1685. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1686. #ifdef PERSIST_CERT_CACHE
  1687. /* Verify no certs (result is less than cacheSz) */
  1688. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1689. #endif
  1690. /* load ca cert again */
  1691. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1692. WOLFSSL_SUCCESS);
  1693. /* Test getting CERT_MANAGER */
  1694. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1695. /* Test unloading CA's using CM */
  1696. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1697. #ifdef PERSIST_CERT_CACHE
  1698. /* Verify no certs (result is less than cacheSz) */
  1699. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1700. #endif
  1701. #endif
  1702. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1703. /* Test loading CA certificates using a path */
  1704. #ifdef NO_RSA
  1705. /* failure here okay since certs in external directory are RSA */
  1706. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1707. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1708. #else
  1709. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1710. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1711. #endif
  1712. /* Test loading path with no files */
  1713. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1714. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1715. /* Test loading expired CA certificates */
  1716. #ifdef NO_RSA
  1717. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1718. load_expired_path,
  1719. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1720. WOLFSSL_SUCCESS);
  1721. #else
  1722. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1723. load_expired_path,
  1724. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1725. WOLFSSL_SUCCESS);
  1726. #endif
  1727. /* Test loading CA certificates and ignoring all errors */
  1728. #ifdef NO_RSA
  1729. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1730. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1731. #else
  1732. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1733. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1734. #endif
  1735. #endif
  1736. wolfSSL_CTX_free(ctx);
  1737. #endif
  1738. return EXPECT_RESULT();
  1739. }
  1740. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1741. {
  1742. int res = TEST_SKIPPED;
  1743. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1744. (!defined(NO_RSA) || defined(HAVE_ECC))
  1745. WOLFSSL_CTX* ctx;
  1746. byte dirValid = 0;
  1747. int ret = 0;
  1748. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1749. if (ctx == NULL) {
  1750. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1751. ret = -1;
  1752. }
  1753. if (ret == 0) {
  1754. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1755. dirValid = 1;
  1756. #else
  1757. word32 numDirs;
  1758. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1759. if (caDirs == NULL || numDirs == 0) {
  1760. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1761. ret = -1;
  1762. }
  1763. else {
  1764. ReadDirCtx dirCtx;
  1765. word32 i;
  1766. for (i = 0; i < numDirs; ++i) {
  1767. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1768. /* Directory isn't empty. */
  1769. dirValid = 1;
  1770. wc_ReadDirClose(&dirCtx);
  1771. break;
  1772. }
  1773. }
  1774. }
  1775. #endif
  1776. }
  1777. /*
  1778. * If the directory isn't empty, we should be able to load CA
  1779. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1780. * usable.
  1781. */
  1782. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1783. WOLFSSL_SUCCESS) {
  1784. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1785. ret = -1;
  1786. }
  1787. #ifdef OPENSSL_EXTRA
  1788. if (ret == 0 &&
  1789. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1790. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1791. ret = -1;
  1792. }
  1793. #endif /* OPENSSL_EXTRA */
  1794. wolfSSL_CTX_free(ctx);
  1795. res = TEST_RES_CHECK(ret == 0);
  1796. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1797. return res;
  1798. }
  1799. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1800. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1801. int file_type)
  1802. {
  1803. int ret;
  1804. WOLFSSL_CERT_MANAGER* cm;
  1805. cm = wolfSSL_CertManagerNew();
  1806. if (cm == NULL) {
  1807. fprintf(stderr, "test_cm_load_ca failed\n");
  1808. return -1;
  1809. }
  1810. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1811. wolfSSL_CertManagerFree(cm);
  1812. return ret;
  1813. }
  1814. static int test_cm_load_ca_file(const char* ca_cert_file)
  1815. {
  1816. int ret = 0;
  1817. byte* cert_buf = NULL;
  1818. size_t cert_sz = 0;
  1819. #if defined(WOLFSSL_PEM_TO_DER)
  1820. DerBuffer* pDer = NULL;
  1821. #endif
  1822. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1823. if (ret == 0) {
  1824. /* normal test */
  1825. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1826. if (ret == WOLFSSL_SUCCESS) {
  1827. /* test including null terminator in length */
  1828. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1829. if (tmp == NULL) {
  1830. ret = MEMORY_E;
  1831. }
  1832. else {
  1833. cert_buf = tmp;
  1834. cert_buf[cert_sz] = '\0';
  1835. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1836. WOLFSSL_FILETYPE_PEM);
  1837. }
  1838. }
  1839. #if defined(WOLFSSL_PEM_TO_DER)
  1840. if (ret == WOLFSSL_SUCCESS) {
  1841. /* test loading DER */
  1842. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1843. if (ret == 0 && pDer != NULL) {
  1844. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1845. WOLFSSL_FILETYPE_ASN1);
  1846. wc_FreeDer(&pDer);
  1847. }
  1848. }
  1849. #endif
  1850. }
  1851. free(cert_buf);
  1852. return ret;
  1853. }
  1854. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1855. int file_type, word32 flags)
  1856. {
  1857. int ret;
  1858. WOLFSSL_CERT_MANAGER* cm;
  1859. cm = wolfSSL_CertManagerNew();
  1860. if (cm == NULL) {
  1861. fprintf(stderr, "test_cm_load_ca failed\n");
  1862. return -1;
  1863. }
  1864. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1865. 0, flags);
  1866. wolfSSL_CertManagerFree(cm);
  1867. return ret;
  1868. }
  1869. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1870. {
  1871. int ret = 0;
  1872. byte* cert_buf = NULL;
  1873. size_t cert_sz = 0;
  1874. #if defined(WOLFSSL_PEM_TO_DER)
  1875. DerBuffer* pDer = NULL;
  1876. #endif
  1877. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1878. if (ret == 0) {
  1879. /* normal test */
  1880. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1881. WOLFSSL_FILETYPE_PEM, flags);
  1882. if (ret == WOLFSSL_SUCCESS) {
  1883. /* test including null terminator in length */
  1884. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1885. if (tmp == NULL) {
  1886. ret = MEMORY_E;
  1887. }
  1888. else {
  1889. cert_buf = tmp;
  1890. cert_buf[cert_sz] = '\0';
  1891. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1892. WOLFSSL_FILETYPE_PEM, flags);
  1893. }
  1894. }
  1895. #if defined(WOLFSSL_PEM_TO_DER)
  1896. if (ret == WOLFSSL_SUCCESS) {
  1897. /* test loading DER */
  1898. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1899. if (ret == 0 && pDer != NULL) {
  1900. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1901. WOLFSSL_FILETYPE_ASN1, flags);
  1902. wc_FreeDer(&pDer);
  1903. }
  1904. }
  1905. #endif
  1906. }
  1907. free(cert_buf);
  1908. return ret;
  1909. }
  1910. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1911. static int test_wolfSSL_CertManagerAPI(void)
  1912. {
  1913. EXPECT_DECLS;
  1914. #ifndef NO_CERTS
  1915. WOLFSSL_CERT_MANAGER* cm = NULL;
  1916. unsigned char c;
  1917. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1918. wolfSSL_CertManagerFree(NULL);
  1919. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1920. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1921. #ifdef WOLFSSL_TRUST_PEER_CERT
  1922. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1923. #endif
  1924. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1925. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1926. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1927. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1928. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1929. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1930. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1931. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1932. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1933. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1934. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1935. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1936. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1937. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1938. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1939. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1940. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1941. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1942. WOLFSSL_BAD_FILETYPE);
  1943. #endif
  1944. #if !defined(NO_FILESYSTEM)
  1945. {
  1946. const char* ca_cert = "./certs/ca-cert.pem";
  1947. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1948. const char* ca_cert_der = "./certs/ca-cert.der";
  1949. #endif
  1950. const char* ca_path = "./certs";
  1951. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1952. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1953. BAD_FUNC_ARG);
  1954. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1955. BAD_FUNC_ARG);
  1956. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1957. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1958. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1959. WOLFSSL_BAD_FILETYPE);
  1960. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1961. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1962. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  1963. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  1964. #endif
  1965. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  1966. WOLFSSL_FATAL_ERROR);
  1967. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  1968. WOLFSSL_FATAL_ERROR);
  1969. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  1970. WOLFSSL_FATAL_ERROR);
  1971. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  1972. WOLFSSL_FATAL_ERROR);
  1973. }
  1974. #endif
  1975. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  1976. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  1977. #elif !defined(HAVE_CRL)
  1978. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  1979. #endif
  1980. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  1981. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  1982. #ifdef HAVE_CRL
  1983. /* Test APIs when CRL is disabled. */
  1984. #ifdef HAVE_CRL_IO
  1985. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  1986. #endif
  1987. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  1988. sizeof_server_cert_der_2048), 1);
  1989. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  1990. #endif
  1991. /* OCSP */
  1992. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  1993. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  1994. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  1995. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  1996. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1997. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1998. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  1999. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2000. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2001. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2002. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2003. #endif
  2004. #ifdef HAVE_OCSP
  2005. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2006. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2007. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2008. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2009. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2010. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2011. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2012. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2013. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2014. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2015. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2016. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2017. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2018. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2019. BAD_FUNC_ARG);
  2020. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2021. BAD_FUNC_ARG);
  2022. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2023. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2024. BAD_FUNC_ARG);
  2025. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2026. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2027. /* Test APIs when OCSP is disabled. */
  2028. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2029. NULL, NULL, NULL, NULL), 1);
  2030. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2031. #endif
  2032. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2033. wolfSSL_CertManagerFree(cm);
  2034. wolfSSL_CertManagerFree(cm);
  2035. cm = NULL;
  2036. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2037. #ifdef HAVE_OCSP
  2038. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2039. WOLFSSL_OCSP_CHECKALL), 1);
  2040. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2041. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2042. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2043. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2044. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2045. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2046. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2047. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2048. #endif
  2049. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2050. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2051. #endif
  2052. #ifdef WOLFSSL_TRUST_PEER_CERT
  2053. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2054. #endif
  2055. wolfSSL_CertManagerFree(cm);
  2056. #endif
  2057. return EXPECT_RESULT();
  2058. }
  2059. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2060. {
  2061. EXPECT_DECLS;
  2062. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2063. const char* ca_cert = "./certs/ca-cert.pem";
  2064. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2065. int ret;
  2066. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2067. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2068. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2069. #elif defined(NO_RSA)
  2070. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2071. #else
  2072. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2073. #endif
  2074. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2075. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2076. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2077. #elif defined(NO_RSA)
  2078. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2079. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2080. !defined(NO_ASN_TIME)
  2081. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2082. #else
  2083. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2084. #endif
  2085. #endif
  2086. return EXPECT_RESULT();
  2087. }
  2088. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2089. {
  2090. EXPECT_DECLS;
  2091. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2092. const char* ca_cert = "./certs/ca-cert.pem";
  2093. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2094. int ret;
  2095. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2096. 1);
  2097. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2098. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2099. #elif defined(NO_RSA)
  2100. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2101. #else
  2102. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2103. #endif
  2104. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2105. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2106. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2107. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2108. #elif defined(NO_RSA)
  2109. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2110. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2111. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2112. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2113. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2114. #else
  2115. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2116. #endif
  2117. #endif
  2118. return EXPECT_RESULT();
  2119. }
  2120. static int test_wolfSSL_CertManagerGetCerts(void)
  2121. {
  2122. EXPECT_DECLS;
  2123. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2124. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2125. defined(WOLFSSL_SIGNER_DER_CERT)
  2126. WOLFSSL_CERT_MANAGER* cm = NULL;
  2127. WOLFSSL_STACK* sk = NULL;
  2128. X509* x509 = NULL;
  2129. X509* cert1 = NULL;
  2130. FILE* file1 = NULL;
  2131. #ifdef DEBUG_WOLFSSL_VERBOSE
  2132. WOLFSSL_BIO* bio = NULL;
  2133. #endif
  2134. int i = 0;
  2135. int ret = 0;
  2136. const byte* der = NULL;
  2137. int derSz = 0;
  2138. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2139. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2140. if (file1 != NULL) {
  2141. fclose(file1);
  2142. }
  2143. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2144. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2145. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2146. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2147. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2148. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2149. * and full OpenSSL compatibility */
  2150. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2151. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2152. #else
  2153. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2154. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2155. #endif
  2156. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2157. "./certs/ca-cert.pem", NULL));
  2158. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2159. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2160. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2161. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2162. #ifdef DEBUG_WOLFSSL_VERBOSE
  2163. bio = BIO_new(wolfSSL_BIO_s_file());
  2164. if (bio != NULL) {
  2165. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2166. X509_print(bio, x509);
  2167. BIO_free(bio);
  2168. }
  2169. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2170. }
  2171. wolfSSL_X509_free(cert1);
  2172. sk_X509_pop_free(sk, NULL);
  2173. wolfSSL_CertManagerFree(cm);
  2174. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2175. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2176. defined(WOLFSSL_SIGNER_DER_CERT) */
  2177. return EXPECT_RESULT();
  2178. }
  2179. static int test_wolfSSL_CertManagerSetVerify(void)
  2180. {
  2181. EXPECT_DECLS;
  2182. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2183. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2184. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2185. WOLFSSL_CERT_MANAGER* cm = NULL;
  2186. int tmp = myVerifyAction;
  2187. const char* ca_cert = "./certs/ca-cert.pem";
  2188. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2189. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2190. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2191. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2192. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2193. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2194. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2195. #else
  2196. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2197. WOLFSSL_SUCCESS);
  2198. #endif
  2199. /* Use the test CB that always accepts certs */
  2200. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2201. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2202. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2203. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2204. {
  2205. const char* verifyCert = "./certs/server-cert.der";
  2206. /* Use the test CB that always fails certs */
  2207. myVerifyAction = VERIFY_FORCE_FAIL;
  2208. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2209. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2210. }
  2211. #endif
  2212. wolfSSL_CertManagerFree(cm);
  2213. myVerifyAction = tmp;
  2214. #endif
  2215. return EXPECT_RESULT();
  2216. }
  2217. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2218. defined(DEBUG_UNIT_TEST_CERTS)
  2219. /* Used when debugging name constraint tests. Not static to allow use in
  2220. * multiple locations with complex define guards. */
  2221. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2222. {
  2223. BIO* out = BIO_new_file(fileName, "wb");
  2224. if (out != NULL) {
  2225. PEM_write_bio_X509(out, x509);
  2226. BIO_free(out);
  2227. }
  2228. }
  2229. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2230. {
  2231. BIO* out = BIO_new_file(fileName, "wb");
  2232. if (out != NULL) {
  2233. BIO_write(out, der, derSz);
  2234. BIO_free(out);
  2235. }
  2236. }
  2237. #else
  2238. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2239. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2240. #endif
  2241. static int test_wolfSSL_CertManagerNameConstraint(void)
  2242. {
  2243. EXPECT_DECLS;
  2244. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2245. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2246. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2247. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2248. !defined(NO_SHA256)
  2249. WOLFSSL_CERT_MANAGER* cm = NULL;
  2250. WOLFSSL_EVP_PKEY *priv = NULL;
  2251. WOLFSSL_X509_NAME* name = NULL;
  2252. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2253. const char* server_cert = "./certs/test/server-goodcn.pem";
  2254. int i = 0;
  2255. static const byte extNameConsOid[] = {85, 29, 30};
  2256. RsaKey key;
  2257. WC_RNG rng;
  2258. byte *der = NULL;
  2259. int derSz = 0;
  2260. word32 idx = 0;
  2261. byte *pt;
  2262. WOLFSSL_X509 *x509 = NULL;
  2263. WOLFSSL_X509 *ca = NULL;
  2264. wc_InitRng(&rng);
  2265. /* load in CA private key for signing */
  2266. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2267. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2268. sizeof_server_key_der_2048), 0);
  2269. /* get ca certificate then alter it */
  2270. ExpectNotNull(der =
  2271. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2272. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2273. WOLFSSL_FILETYPE_ASN1));
  2274. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2275. if (EXPECT_SUCCESS() && (der != NULL)) {
  2276. XMEMCPY(der, pt, derSz);
  2277. /* find the name constraint extension and alter it */
  2278. pt = der;
  2279. for (i = 0; i < derSz - 3; i++) {
  2280. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2281. pt += 3;
  2282. break;
  2283. }
  2284. pt++;
  2285. }
  2286. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2287. /* go to the length value and set it to 0 */
  2288. while (i < derSz && *pt != 0x81) {
  2289. pt++;
  2290. i++;
  2291. }
  2292. ExpectIntNE(i, derSz); /* did not place to alter */
  2293. pt++;
  2294. *pt = 0x00;
  2295. }
  2296. /* resign the altered certificate */
  2297. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2298. FOURK_BUF, &key, NULL, &rng)), 0);
  2299. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2300. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2301. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2302. wolfSSL_CertManagerFree(cm);
  2303. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2304. wolfSSL_X509_free(x509);
  2305. wc_FreeRsaKey(&key);
  2306. wc_FreeRng(&rng);
  2307. /* add email alt name to satisfy constraint */
  2308. pt = (byte*)server_key_der_2048;
  2309. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2310. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2311. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2312. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2313. WOLFSSL_FILETYPE_ASN1));
  2314. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2315. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2316. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2317. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2318. /* Good cert test with proper alt email name */
  2319. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2320. WOLFSSL_FILETYPE_PEM));
  2321. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2322. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2323. name = NULL;
  2324. ExpectNotNull(name = X509_NAME_new());
  2325. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2326. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2327. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2328. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2329. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2330. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2331. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2332. X509_NAME_free(name);
  2333. name = NULL;
  2334. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2335. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2336. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2337. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2338. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2339. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2340. wolfSSL_X509_free(x509);
  2341. x509 = NULL;
  2342. /* Cert with bad alt name list */
  2343. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2344. WOLFSSL_FILETYPE_PEM));
  2345. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2346. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2347. name = NULL;
  2348. ExpectNotNull(name = X509_NAME_new());
  2349. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2350. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2351. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2352. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2353. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2354. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2355. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2356. X509_NAME_free(name);
  2357. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2358. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2359. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2360. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2361. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2362. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2363. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2364. wolfSSL_CertManagerFree(cm);
  2365. wolfSSL_X509_free(x509);
  2366. wolfSSL_X509_free(ca);
  2367. wolfSSL_EVP_PKEY_free(priv);
  2368. #endif
  2369. return EXPECT_RESULT();
  2370. }
  2371. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2372. {
  2373. EXPECT_DECLS;
  2374. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2375. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2376. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2377. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2378. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2379. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2380. const char* server_cert = "./certs/server-cert.pem";
  2381. WOLFSSL_CERT_MANAGER* cm = NULL;
  2382. WOLFSSL_X509 *x509 = NULL;
  2383. WOLFSSL_X509 *ca = NULL;
  2384. const unsigned char *der = NULL;
  2385. const unsigned char *pt;
  2386. WOLFSSL_EVP_PKEY *priv = NULL;
  2387. WOLFSSL_X509_NAME* name = NULL;
  2388. int derSz = 0;
  2389. /* C=US*/
  2390. char altName[] = {
  2391. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2392. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2393. };
  2394. /* C=ID */
  2395. char altNameFail[] = {
  2396. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2397. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2398. };
  2399. /* C=US ST=California*/
  2400. char altNameExc[] = {
  2401. 0x30, 0x22,
  2402. 0x31, 0x0B,
  2403. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2404. 0x31, 0x13,
  2405. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2406. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2407. };
  2408. /* load in CA private key for signing */
  2409. pt = ca_key_der_2048;
  2410. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2411. sizeof_ca_key_der_2048));
  2412. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2413. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2414. WOLFSSL_FILETYPE_ASN1));
  2415. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2416. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2417. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2418. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2419. WOLFSSL_FILETYPE_PEM));
  2420. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2421. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2422. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2423. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2424. #else
  2425. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2426. #endif
  2427. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2428. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2429. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2430. /* add in matching DIR alt name and resign */
  2431. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2432. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2433. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2434. #else
  2435. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2436. #endif
  2437. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2438. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2439. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2440. wolfSSL_X509_free(x509);
  2441. x509 = NULL;
  2442. /* check verify fail */
  2443. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2444. WOLFSSL_FILETYPE_PEM));
  2445. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2446. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2447. /* add in miss matching DIR alt name and resign */
  2448. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2449. ASN_DIR_TYPE);
  2450. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2451. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2452. #else
  2453. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2454. #endif
  2455. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2456. #ifndef WOLFSSL_NO_ASN_STRICT
  2457. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2458. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2459. #else
  2460. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2461. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2462. #endif
  2463. /* check that it still fails if one bad altname and one good altname is in
  2464. * the certificate */
  2465. wolfSSL_X509_free(x509);
  2466. x509 = NULL;
  2467. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2468. WOLFSSL_FILETYPE_PEM));
  2469. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2470. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2471. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2472. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2473. ASN_DIR_TYPE);
  2474. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2475. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2476. #else
  2477. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2478. #endif
  2479. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2480. #ifndef WOLFSSL_NO_ASN_STRICT
  2481. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2482. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2483. #else
  2484. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2485. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2486. #endif
  2487. /* check it fails with switching position of bad altname */
  2488. wolfSSL_X509_free(x509);
  2489. x509 = NULL;
  2490. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2491. WOLFSSL_FILETYPE_PEM));
  2492. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2493. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2494. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2495. ASN_DIR_TYPE);
  2496. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2497. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2498. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2499. #else
  2500. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2501. #endif
  2502. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2503. #ifndef WOLFSSL_NO_ASN_STRICT
  2504. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2505. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2506. #else
  2507. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2508. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2509. #endif
  2510. wolfSSL_CertManagerFree(cm);
  2511. wolfSSL_X509_free(x509);
  2512. x509 = NULL;
  2513. wolfSSL_X509_free(ca);
  2514. ca = NULL;
  2515. /* now test with excluded name constraint */
  2516. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2517. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2518. WOLFSSL_FILETYPE_ASN1));
  2519. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2520. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2521. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2522. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2523. WOLFSSL_FILETYPE_PEM));
  2524. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2525. ASN_DIR_TYPE);
  2526. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2527. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2528. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2529. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2530. #else
  2531. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2532. #endif
  2533. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2534. #ifndef WOLFSSL_NO_ASN_STRICT
  2535. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2536. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2537. #else
  2538. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2539. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2540. #endif
  2541. wolfSSL_CertManagerFree(cm);
  2542. wolfSSL_X509_free(x509);
  2543. wolfSSL_X509_free(ca);
  2544. wolfSSL_EVP_PKEY_free(priv);
  2545. #endif
  2546. return EXPECT_RESULT();
  2547. }
  2548. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2549. {
  2550. EXPECT_DECLS;
  2551. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2552. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2553. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2554. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2555. !defined(NO_SHA256)
  2556. WOLFSSL_CERT_MANAGER* cm = NULL;
  2557. WOLFSSL_EVP_PKEY *priv = NULL;
  2558. WOLFSSL_X509_NAME* name = NULL;
  2559. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2560. const char* server_cert = "./certs/test/server-goodcn.pem";
  2561. byte *der = NULL;
  2562. int derSz = 0;
  2563. byte *pt;
  2564. WOLFSSL_X509 *x509 = NULL;
  2565. WOLFSSL_X509 *ca = NULL;
  2566. pt = (byte*)server_key_der_2048;
  2567. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2568. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2569. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2570. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2571. WOLFSSL_FILETYPE_ASN1));
  2572. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2573. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2574. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2575. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2576. /* check satisfying .wolfssl.com constraint passes */
  2577. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2578. WOLFSSL_FILETYPE_PEM));
  2579. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2580. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2581. name = NULL;
  2582. ExpectNotNull(name = X509_NAME_new());
  2583. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2584. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2585. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2586. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2587. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2588. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2589. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2590. X509_NAME_free(name);
  2591. name = NULL;
  2592. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2593. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2594. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2595. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2596. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2597. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2598. wolfSSL_X509_free(x509);
  2599. x509 = NULL;
  2600. /* check satisfying .random.com constraint passes */
  2601. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2602. WOLFSSL_FILETYPE_PEM));
  2603. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2604. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2605. name = NULL;
  2606. ExpectNotNull(name = X509_NAME_new());
  2607. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2608. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2609. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2610. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2611. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2612. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2613. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2614. X509_NAME_free(name);
  2615. name = NULL;
  2616. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2617. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2618. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2619. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2620. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2621. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2622. wolfSSL_X509_free(x509);
  2623. x509 = NULL;
  2624. /* check fail case when neither constraint is matched */
  2625. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2626. WOLFSSL_FILETYPE_PEM));
  2627. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2628. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2629. name = NULL;
  2630. ExpectNotNull(name = X509_NAME_new());
  2631. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2632. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2633. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2634. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2635. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2636. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2637. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2638. X509_NAME_free(name);
  2639. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2640. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2641. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2642. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2643. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2644. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2645. wolfSSL_CertManagerFree(cm);
  2646. wolfSSL_X509_free(x509);
  2647. wolfSSL_X509_free(ca);
  2648. wolfSSL_EVP_PKEY_free(priv);
  2649. #endif
  2650. return EXPECT_RESULT();
  2651. }
  2652. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2653. {
  2654. EXPECT_DECLS;
  2655. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2656. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2657. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2658. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2659. !defined(NO_SHA256)
  2660. WOLFSSL_CERT_MANAGER* cm = NULL;
  2661. WOLFSSL_EVP_PKEY *priv = NULL;
  2662. WOLFSSL_X509_NAME* name = NULL;
  2663. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2664. const char* server_cert = "./certs/test/server-goodcn.pem";
  2665. byte *der = NULL;
  2666. int derSz;
  2667. byte *pt;
  2668. WOLFSSL_X509 *x509 = NULL;
  2669. WOLFSSL_X509 *ca = NULL;
  2670. pt = (byte*)server_key_der_2048;
  2671. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2672. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2673. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2674. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2675. WOLFSSL_FILETYPE_ASN1));
  2676. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2677. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2678. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2679. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2680. /* check satisfying wolfssl.com constraint passes */
  2681. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2682. WOLFSSL_FILETYPE_PEM));
  2683. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2684. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2685. name = NULL;
  2686. ExpectNotNull(name = X509_NAME_new());
  2687. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2688. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2689. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2690. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2691. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2692. X509_NAME_free(name);
  2693. name = NULL;
  2694. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2695. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2696. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2697. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2698. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2699. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2700. wolfSSL_X509_free(x509);
  2701. x509 = NULL;
  2702. /* check satisfying example.com constraint passes */
  2703. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2704. WOLFSSL_FILETYPE_PEM));
  2705. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2706. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2707. name = NULL;
  2708. ExpectNotNull(name = X509_NAME_new());
  2709. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2710. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2711. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2712. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2713. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2714. X509_NAME_free(name);
  2715. name = NULL;
  2716. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2717. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2718. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2719. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2720. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2721. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2722. wolfSSL_X509_free(x509);
  2723. x509 = NULL;
  2724. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2725. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2726. WOLFSSL_FILETYPE_PEM));
  2727. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2728. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2729. name = NULL;
  2730. ExpectNotNull(name = X509_NAME_new());
  2731. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2732. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2733. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2734. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2735. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2736. X509_NAME_free(name);
  2737. name = NULL;
  2738. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2739. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2740. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2741. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2742. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2743. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2744. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2745. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2746. wolfSSL_X509_free(x509);
  2747. x509 = NULL;
  2748. /* check fail when one DNS in the list is bad */
  2749. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2750. WOLFSSL_FILETYPE_PEM));
  2751. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2752. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2753. name = NULL;
  2754. ExpectNotNull(name = X509_NAME_new());
  2755. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2756. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2757. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2758. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2759. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2760. X509_NAME_free(name);
  2761. name = NULL;
  2762. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2763. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2764. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2765. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2766. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2767. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2768. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2769. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2770. wolfSSL_X509_free(x509);
  2771. x509 = NULL;
  2772. /* check fail case when neither constraint is matched */
  2773. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2774. WOLFSSL_FILETYPE_PEM));
  2775. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2776. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2777. name = NULL;
  2778. ExpectNotNull(name = X509_NAME_new());
  2779. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2780. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2781. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2782. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2783. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2784. X509_NAME_free(name);
  2785. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2786. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2787. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2788. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2789. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2790. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2791. wolfSSL_CertManagerFree(cm);
  2792. wolfSSL_X509_free(x509);
  2793. wolfSSL_X509_free(ca);
  2794. wolfSSL_EVP_PKEY_free(priv);
  2795. #endif
  2796. return EXPECT_RESULT();
  2797. }
  2798. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2799. {
  2800. EXPECT_DECLS;
  2801. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2802. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2803. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2804. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2805. !defined(NO_SHA256)
  2806. WOLFSSL_CERT_MANAGER* cm = NULL;
  2807. WOLFSSL_EVP_PKEY *priv = NULL;
  2808. WOLFSSL_X509_NAME* name = NULL;
  2809. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2810. const char* server_cert = "./certs/test/server-goodcn.pem";
  2811. byte *der = NULL;
  2812. int derSz;
  2813. byte *pt;
  2814. WOLFSSL_X509 *x509 = NULL;
  2815. WOLFSSL_X509 *ca = NULL;
  2816. pt = (byte*)server_key_der_2048;
  2817. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2818. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2819. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2820. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2821. WOLFSSL_FILETYPE_ASN1));
  2822. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2823. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2824. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2825. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2826. /* check satisfying wolfssl.com constraint passes */
  2827. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2828. WOLFSSL_FILETYPE_PEM));
  2829. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2830. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2831. name = NULL;
  2832. ExpectNotNull(name = X509_NAME_new());
  2833. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2834. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2835. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2836. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2837. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2838. X509_NAME_free(name);
  2839. name = NULL;
  2840. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2841. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2842. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2843. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2844. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2845. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2846. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2847. wolfSSL_X509_free(x509);
  2848. x509 = NULL;
  2849. /* fail with DNS check because of common name */
  2850. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2851. WOLFSSL_FILETYPE_PEM));
  2852. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2853. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2854. name = NULL;
  2855. ExpectNotNull(name = X509_NAME_new());
  2856. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2857. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2858. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2859. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2860. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2861. X509_NAME_free(name);
  2862. name = NULL;
  2863. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2864. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2865. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2866. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2867. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2868. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2869. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2870. wolfSSL_X509_free(x509);
  2871. x509 = NULL;
  2872. /* fail on permitted DNS name constraint */
  2873. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2874. WOLFSSL_FILETYPE_PEM));
  2875. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2876. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2877. name = NULL;
  2878. ExpectNotNull(name = X509_NAME_new());
  2879. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2880. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2881. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2882. X509_NAME_free(name);
  2883. name = NULL;
  2884. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2885. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2886. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2887. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2888. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2889. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2890. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2891. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2892. wolfSSL_X509_free(x509);
  2893. x509 = NULL;
  2894. /* fail on permitted email name constraint */
  2895. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2896. WOLFSSL_FILETYPE_PEM));
  2897. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2898. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2899. name = NULL;
  2900. ExpectNotNull(name = X509_NAME_new());
  2901. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2902. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2903. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2904. X509_NAME_free(name);
  2905. name = NULL;
  2906. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2907. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2908. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2909. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2910. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2911. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2912. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2913. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2914. wolfSSL_X509_free(x509);
  2915. x509 = NULL;
  2916. /* success with empty email name */
  2917. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2918. WOLFSSL_FILETYPE_PEM));
  2919. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2920. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2921. name = NULL;
  2922. ExpectNotNull(name = X509_NAME_new());
  2923. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2924. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2925. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2926. X509_NAME_free(name);
  2927. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2928. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2929. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2930. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2931. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2932. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2933. wolfSSL_X509_free(x509);
  2934. wolfSSL_CertManagerFree(cm);
  2935. wolfSSL_X509_free(ca);
  2936. wolfSSL_EVP_PKEY_free(priv);
  2937. #endif
  2938. return EXPECT_RESULT();
  2939. }
  2940. static int test_wolfSSL_CertManagerCRL(void)
  2941. {
  2942. EXPECT_DECLS;
  2943. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2944. !defined(NO_RSA)
  2945. const char* ca_cert = "./certs/ca-cert.pem";
  2946. const char* crl1 = "./certs/crl/crl.pem";
  2947. const char* crl2 = "./certs/crl/crl2.pem";
  2948. #ifdef WC_RSA_PSS
  2949. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  2950. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  2951. #endif
  2952. const unsigned char crl_buff[] = {
  2953. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2954. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2955. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2956. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2957. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2958. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2959. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2960. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  2961. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  2962. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  2963. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  2964. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  2965. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  2966. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  2967. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  2968. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  2969. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  2970. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  2971. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  2972. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  2973. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  2974. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  2975. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  2976. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  2977. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  2978. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  2979. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  2980. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  2981. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  2982. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  2983. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  2984. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  2985. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  2986. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  2987. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  2988. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  2989. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  2990. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  2991. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  2992. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  2993. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  2994. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  2995. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  2996. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  2997. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  2998. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  2999. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3000. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3001. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3002. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3003. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3004. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3005. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3006. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3007. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3008. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3009. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3010. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3011. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3012. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3013. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3014. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3015. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3016. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3017. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3018. };
  3019. WOLFSSL_CERT_MANAGER* cm = NULL;
  3020. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3021. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3022. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3023. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3024. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3025. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3026. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3027. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3028. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3029. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3030. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3031. BAD_FUNC_ARG);
  3032. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3033. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3034. BAD_FUNC_ARG);
  3035. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3036. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3037. BAD_FUNC_ARG);
  3038. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3039. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3040. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3041. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3042. #ifdef HAVE_CRL_IO
  3043. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3044. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3045. #endif
  3046. #ifndef NO_FILESYSTEM
  3047. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3048. 0), BAD_FUNC_ARG);
  3049. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3050. 0), BAD_FUNC_ARG);
  3051. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3052. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3053. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3054. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3055. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3056. BAD_FUNC_ARG);
  3057. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3058. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3059. ASN_PARSE_E);
  3060. #endif
  3061. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3062. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3063. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3064. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3065. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3066. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3067. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3068. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3069. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3070. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3071. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3072. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3073. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3074. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3075. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3076. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3077. ExpectIntEQ(WOLFSSL_SUCCESS,
  3078. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3079. ExpectIntEQ(WOLFSSL_SUCCESS,
  3080. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3081. ExpectIntEQ(WOLFSSL_SUCCESS,
  3082. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3083. wolfSSL_CertManagerFreeCRL(cm);
  3084. ExpectIntEQ(WOLFSSL_SUCCESS,
  3085. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3086. ExpectIntEQ(WOLFSSL_SUCCESS,
  3087. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3088. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3089. sizeof_server_cert_der_2048), CRL_MISSING);
  3090. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3091. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3092. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3093. WOLFSSL_FILETYPE_ASN1), 1);
  3094. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3095. /* loading should fail without the CA set */
  3096. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3097. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3098. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3099. ExpectIntEQ(WOLFSSL_SUCCESS,
  3100. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3101. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3102. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3103. #endif
  3104. wolfSSL_CertManagerFree(cm);
  3105. #endif
  3106. return EXPECT_RESULT();
  3107. }
  3108. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3109. {
  3110. EXPECT_DECLS;
  3111. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3112. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3113. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3114. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3115. defined(HAVE_LIGHTY)
  3116. WOLFSSL_CERT_MANAGER* cm = NULL;
  3117. /* Raw OCSP response bytes captured using the following setup:
  3118. * - Run responder with
  3119. * openssl ocsp -port 9999 -ndays 9999
  3120. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3121. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3122. * -rkey certs/ocsp/ocsp-responder-key.pem
  3123. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3124. * - Run client with
  3125. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3126. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3127. * -cert certs/ocsp/server1-cert.pem
  3128. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3129. * - Select the response packet in Wireshark, and export it using
  3130. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3131. * packets only". After importing into the editor, remove the initial
  3132. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3133. */
  3134. static const byte response[] = {
  3135. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3136. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3137. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3138. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3139. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3140. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3141. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3142. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3143. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3144. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3145. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3146. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3147. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3148. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3149. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3150. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3151. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3152. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3153. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3154. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3155. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3156. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3157. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3158. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3159. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3160. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3161. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3162. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3163. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3164. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3165. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3166. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3167. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3168. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3169. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3170. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3171. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3172. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3173. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3174. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3175. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3176. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3177. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3178. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3179. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3180. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3181. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3182. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3183. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3184. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3185. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3186. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3187. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3188. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3189. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3190. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3191. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3192. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3193. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3194. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3195. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3196. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3197. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3198. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3199. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3200. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3201. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3202. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3203. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3204. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3205. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3206. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3207. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3208. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3209. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3210. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3211. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3212. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3213. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3214. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3215. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3216. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3217. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3218. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3219. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3220. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3221. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3222. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3223. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3224. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3225. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3226. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3227. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3228. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3229. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3230. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3231. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3232. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3233. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3234. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3235. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3236. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3237. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3238. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3239. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3240. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3241. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3242. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3243. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3244. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3245. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3246. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3247. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3248. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3249. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3250. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3251. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3252. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3253. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3254. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3255. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3256. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3257. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3258. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3259. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3260. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3261. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3262. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3263. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3264. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3265. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3266. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3267. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3268. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3269. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3270. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3271. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3272. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3273. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3274. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3275. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3276. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3277. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3278. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3279. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3280. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3281. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3282. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3283. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3284. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3285. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3286. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3287. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3288. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3289. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3290. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3291. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3292. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3293. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3294. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3295. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3296. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3297. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3298. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3299. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3300. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3301. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3302. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3303. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3304. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3305. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3306. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3307. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3308. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3309. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3310. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3311. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3312. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3313. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3314. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3315. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3316. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3317. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3318. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3319. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3320. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3321. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3322. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3323. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3324. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3325. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3326. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3327. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3328. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3329. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3330. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3331. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3332. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3333. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3334. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3335. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3336. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3337. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3338. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3339. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3340. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3341. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3342. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3343. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3344. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3345. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3346. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3347. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3348. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3349. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3350. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3351. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3352. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3353. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3354. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3355. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3356. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3357. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3358. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3359. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3360. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3361. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3362. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3363. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3364. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3365. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3366. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3367. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3368. };
  3369. OcspEntry entry[1];
  3370. CertStatus status[1];
  3371. OcspRequest* request = NULL;
  3372. #ifndef NO_FILESYSTEM
  3373. const char* ca_cert = "./certs/ca-cert.pem";
  3374. #endif
  3375. byte serial[] = {0x05};
  3376. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3377. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3378. XMEMSET(entry, 0, sizeof(OcspEntry));
  3379. XMEMSET(status, 0, sizeof(CertStatus));
  3380. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3381. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3382. DYNAMIC_TYPE_OCSP_REQUEST));
  3383. if ((request != NULL) && (request->serial != NULL)) {
  3384. request->serialSz = sizeof(serial);
  3385. XMEMCPY(request->serial, serial, sizeof(serial));
  3386. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3387. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3388. }
  3389. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3390. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3391. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3392. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3393. /* Response should be valid. */
  3394. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3395. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3396. /* Flip a byte in the request serial number, response should be invalid
  3397. * now. */
  3398. if ((request != NULL) && (request->serial != NULL))
  3399. request->serial[0] ^= request->serial[0];
  3400. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3401. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3402. #ifndef NO_FILESYSTEM
  3403. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3404. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3405. ExpectIntEQ(WOLFSSL_SUCCESS,
  3406. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3407. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3408. sizeof(server_cert_der_2048)), 1);
  3409. #endif
  3410. wolfSSL_OCSP_REQUEST_free(request);
  3411. wolfSSL_CertManagerFree(cm);
  3412. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3413. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3414. #endif /* HAVE_OCSP */
  3415. return EXPECT_RESULT();
  3416. }
  3417. static int test_wolfSSL_CheckOCSPResponse(void)
  3418. {
  3419. EXPECT_DECLS;
  3420. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3421. defined(OPENSSL_ALL)
  3422. const char* responseFile = "./certs/ocsp/test-response.der";
  3423. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3424. const char* responseNoInternFile =
  3425. "./certs/ocsp/test-response-nointern.der";
  3426. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3427. OcspResponse* res = NULL;
  3428. byte data[4096];
  3429. const unsigned char* pt;
  3430. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3431. * gcc sanitizer with --enable-heapmath.
  3432. */
  3433. XFILE f = XBADFILE;
  3434. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3435. WOLFSSL_X509_STORE* st = NULL;
  3436. WOLFSSL_X509* issuer = NULL;
  3437. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3438. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3439. if (f != XBADFILE) {
  3440. XFCLOSE(f);
  3441. f = XBADFILE;
  3442. }
  3443. pt = data;
  3444. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3445. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3446. SSL_FILETYPE_PEM));
  3447. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3448. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3449. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3450. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3451. wolfSSL_OCSP_BASICRESP_free(bs);
  3452. bs = NULL;
  3453. wolfSSL_OCSP_RESPONSE_free(res);
  3454. res = NULL;
  3455. wolfSSL_X509_STORE_free(st);
  3456. st = NULL;
  3457. wolfSSL_X509_free(issuer);
  3458. issuer = NULL;
  3459. /* check loading a response with optional certs */
  3460. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3461. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3462. if (f != XBADFILE)
  3463. XFCLOSE(f);
  3464. f = XBADFILE;
  3465. pt = data;
  3466. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3467. wolfSSL_OCSP_RESPONSE_free(res);
  3468. res = NULL;
  3469. /* check loading a response with multiple certs */
  3470. {
  3471. WOLFSSL_CERT_MANAGER* cm = NULL;
  3472. OcspEntry *entry = NULL;
  3473. CertStatus* status = NULL;
  3474. OcspRequest* request = NULL;
  3475. byte serial1[] = {0x01};
  3476. byte serial[] = {0x02};
  3477. byte issuerHash[] = {
  3478. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3479. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3480. 0x37, 0x15, 0x8A, 0x88
  3481. };
  3482. byte issuerKeyHash[] = {
  3483. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3484. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3485. 0x7E, 0x72, 0x15, 0x21
  3486. };
  3487. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3488. DYNAMIC_TYPE_OPENSSL));
  3489. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3490. DYNAMIC_TYPE_OPENSSL));
  3491. if (entry != NULL)
  3492. XMEMSET(entry, 0, sizeof(OcspEntry));
  3493. if (status != NULL)
  3494. XMEMSET(status, 0, sizeof(CertStatus));
  3495. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3496. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3497. DYNAMIC_TYPE_OCSP_REQUEST));
  3498. if (request != NULL && request->serial != NULL) {
  3499. request->serialSz = sizeof(serial);
  3500. XMEMCPY(request->serial, serial, sizeof(serial));
  3501. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3502. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3503. sizeof(issuerKeyHash));
  3504. }
  3505. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3506. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3507. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3508. WOLFSSL_SUCCESS);
  3509. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3510. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3511. if (f != XBADFILE)
  3512. XFCLOSE(f);
  3513. f = XBADFILE;
  3514. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3515. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3516. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3517. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3518. ExpectNotNull(entry->status);
  3519. if (request != NULL && request->serial != NULL)
  3520. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3521. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3522. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3523. /* store both status's in the entry to check that "next" is not
  3524. * overwritten */
  3525. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3526. status->next = entry->status;
  3527. entry->status = status;
  3528. }
  3529. if (request != NULL && request->serial != NULL)
  3530. XMEMCPY(request->serial, serial, sizeof(serial));
  3531. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3532. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3533. ExpectNotNull(entry->status->next);
  3534. /* compare the status found */
  3535. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3536. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3537. status->serialSz), 0);
  3538. if (status != NULL && entry != NULL && entry->status != status) {
  3539. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3540. }
  3541. wolfSSL_OCSP_CERTID_free(entry);
  3542. wolfSSL_OCSP_REQUEST_free(request);
  3543. wolfSSL_CertManagerFree(cm);
  3544. }
  3545. #if defined(WC_RSA_PSS)
  3546. {
  3547. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3548. /* check loading a response with RSA-PSS signature */
  3549. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3550. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3551. if (f != XBADFILE)
  3552. XFCLOSE(f);
  3553. pt = data;
  3554. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3555. /* try to verify the response */
  3556. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3557. SSL_FILETYPE_PEM));
  3558. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3559. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3560. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3561. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3562. WOLFSSL_SUCCESS);
  3563. wolfSSL_OCSP_BASICRESP_free(bs);
  3564. wolfSSL_OCSP_RESPONSE_free(res);
  3565. wolfSSL_X509_STORE_free(st);
  3566. wolfSSL_X509_free(issuer);
  3567. }
  3568. #endif
  3569. #endif /* HAVE_OCSP */
  3570. return EXPECT_RESULT();
  3571. }
  3572. static int test_wolfSSL_FPKI(void)
  3573. {
  3574. EXPECT_DECLS;
  3575. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3576. XFILE f = XBADFILE;
  3577. const char* fpkiCert = "./certs/fpki-cert.der";
  3578. DecodedCert cert;
  3579. byte buf[4096];
  3580. byte* uuid = NULL;
  3581. byte* fascn = NULL;
  3582. word32 fascnSz;
  3583. word32 uuidSz;
  3584. int bytes = 0;
  3585. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3586. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3587. if (f != XBADFILE)
  3588. XFCLOSE(f);
  3589. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3590. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3591. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3592. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3593. DYNAMIC_TYPE_TMP_BUFFER));
  3594. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3595. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3596. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3597. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3598. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3599. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3600. wc_FreeDecodedCert(&cert);
  3601. #endif
  3602. return EXPECT_RESULT();
  3603. }
  3604. /* use RID in confuncture with other names to test parsing of unknown other
  3605. * names */
  3606. static int test_wolfSSL_OtherName(void)
  3607. {
  3608. EXPECT_DECLS;
  3609. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3610. XFILE f = XBADFILE;
  3611. const char* ridCert = "./certs/rid-cert.der";
  3612. DecodedCert cert;
  3613. byte buf[4096];
  3614. int bytes = 0;
  3615. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3616. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3617. if (f != XBADFILE)
  3618. XFCLOSE(f);
  3619. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3620. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3621. wc_FreeDecodedCert(&cert);
  3622. #endif
  3623. return EXPECT_RESULT();
  3624. }
  3625. static int test_wolfSSL_CertRsaPss(void)
  3626. {
  3627. EXPECT_DECLS;
  3628. /* FIPS v2 and below don't support long salts. */
  3629. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3630. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3631. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3632. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3633. XFILE f = XBADFILE;
  3634. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3635. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3636. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3637. RSA_MAX_SIZE >= 3072
  3638. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3639. #endif
  3640. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3641. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3642. #endif
  3643. DecodedCert cert;
  3644. byte buf[4096];
  3645. int bytes = 0;
  3646. WOLFSSL_CERT_MANAGER* cm = NULL;
  3647. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3648. ExpectIntEQ(WOLFSSL_SUCCESS,
  3649. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3650. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3651. ExpectIntEQ(WOLFSSL_SUCCESS,
  3652. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3653. #endif
  3654. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3655. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3656. if (f != XBADFILE) {
  3657. XFCLOSE(f);
  3658. f = XBADFILE;
  3659. }
  3660. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3661. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3662. wc_FreeDecodedCert(&cert);
  3663. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3664. RSA_MAX_SIZE >= 3072
  3665. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3666. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3667. if (f != XBADFILE)
  3668. XFCLOSE(f);
  3669. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3670. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3671. wc_FreeDecodedCert(&cert);
  3672. #endif
  3673. wolfSSL_CertManagerFree(cm);
  3674. #endif
  3675. return EXPECT_RESULT();
  3676. }
  3677. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3678. {
  3679. EXPECT_DECLS;
  3680. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3681. !defined(NO_WOLFSSL_CLIENT)
  3682. WOLFSSL_CTX* ctx = NULL;
  3683. const char* ca_cert = "./certs/ca-cert.pem";
  3684. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3685. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3686. /* test good CA */
  3687. ExpectTrue(WOLFSSL_SUCCESS ==
  3688. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3689. WOLFSSL_LOAD_FLAG_NONE));
  3690. /* test expired CA */
  3691. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3692. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3693. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3694. #else
  3695. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3696. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3697. #endif
  3698. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3699. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3700. wolfSSL_CTX_free(ctx);
  3701. #endif
  3702. return EXPECT_RESULT();
  3703. }
  3704. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3705. {
  3706. EXPECT_DECLS;
  3707. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3708. defined(USE_CERT_BUFFERS_2048)
  3709. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3710. WOLFSSL_CTX* ctx;
  3711. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3712. byte ca_expired_cert[TWOK_BUF];
  3713. word32 sizeof_ca_expired_cert = 0;
  3714. XFILE fp = XBADFILE;
  3715. #ifndef NO_WOLFSSL_CLIENT
  3716. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3717. #else
  3718. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3719. #endif
  3720. ExpectNotNull(ctx);
  3721. /* test good CA */
  3722. ExpectTrue(WOLFSSL_SUCCESS ==
  3723. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3724. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3725. WOLFSSL_LOAD_FLAG_NONE));
  3726. /* load expired CA */
  3727. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3728. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3729. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3730. sizeof(ca_expired_cert), fp), 0);
  3731. if (fp != XBADFILE)
  3732. XFCLOSE(fp);
  3733. /* test expired CA failure */
  3734. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3735. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3736. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3737. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3738. #else
  3739. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3740. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3741. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3742. #endif
  3743. /* test expired CA success */
  3744. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3745. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3746. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3747. wolfSSL_CTX_free(ctx);
  3748. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3749. #endif
  3750. return EXPECT_RESULT();
  3751. }
  3752. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3753. {
  3754. EXPECT_DECLS;
  3755. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3756. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3757. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3758. WOLFSSL_CTX* ctx = NULL;
  3759. #ifndef NO_WOLFSSL_CLIENT
  3760. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3761. #else
  3762. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3763. #endif
  3764. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3765. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3766. WOLFSSL_FILETYPE_ASN1));
  3767. wolfSSL_CTX_free(ctx);
  3768. #endif
  3769. return EXPECT_RESULT();
  3770. }
  3771. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3772. {
  3773. EXPECT_DECLS;
  3774. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3775. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3776. WOLFSSL_CTX* ctx;
  3777. WOLFSSL* ssl = NULL;
  3778. const char *certChain[] = {
  3779. "./certs/intermediate/client-int-cert.pem",
  3780. "./certs/intermediate/ca-int2-cert.pem",
  3781. "./certs/intermediate/ca-int-cert.pem",
  3782. "./certs/ca-cert.pem",
  3783. NULL
  3784. };
  3785. const char** cert;
  3786. WOLFSSL_X509* x509 = NULL;
  3787. WOLF_STACK_OF(X509)* chain = NULL;
  3788. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3789. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3790. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3791. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3792. WOLFSSL_FILETYPE_PEM));
  3793. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3794. X509_free(x509);
  3795. x509 = NULL;
  3796. }
  3797. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3798. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3799. WOLFSSL_FILETYPE_PEM));
  3800. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3801. X509_free(x509);
  3802. x509 = NULL;
  3803. }
  3804. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3805. ExpectIntEQ(sk_X509_num(chain), 3);
  3806. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3807. ExpectIntEQ(sk_X509_num(chain), 3);
  3808. SSL_free(ssl);
  3809. SSL_CTX_free(ctx);
  3810. #endif
  3811. return EXPECT_RESULT();
  3812. }
  3813. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3814. {
  3815. EXPECT_DECLS;
  3816. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3817. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3818. const char* server_chain_der = "./certs/server-cert-chain.der";
  3819. const char* client_single_pem = "./certs/client-cert.pem";
  3820. WOLFSSL_CTX* ctx;
  3821. (void)server_chain_der;
  3822. (void)client_single_pem;
  3823. (void)ctx;
  3824. #ifndef NO_WOLFSSL_CLIENT
  3825. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3826. #else
  3827. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3828. #endif
  3829. ExpectNotNull(ctx);
  3830. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3831. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3832. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3833. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3834. wolfSSL_CTX_free(ctx);
  3835. #endif
  3836. return EXPECT_RESULT();
  3837. }
  3838. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3839. {
  3840. EXPECT_DECLS;
  3841. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3842. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3843. WOLFSSL_CTX *ctx = NULL;
  3844. (void)ctx;
  3845. #ifndef NO_WOLFSSL_CLIENT
  3846. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3847. #else
  3848. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3849. #endif
  3850. /* invalid context */
  3851. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3852. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3853. /* invalid dhParamFile file */
  3854. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3855. NULL, WOLFSSL_FILETYPE_PEM));
  3856. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3857. bogusFile, WOLFSSL_FILETYPE_PEM));
  3858. /* success */
  3859. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3860. WOLFSSL_FILETYPE_PEM));
  3861. wolfSSL_CTX_free(ctx);
  3862. #endif
  3863. return EXPECT_RESULT();
  3864. }
  3865. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3866. {
  3867. EXPECT_DECLS;
  3868. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3869. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3870. WOLFSSL_CTX *ctx = NULL;
  3871. #ifndef NO_WOLFSSL_CLIENT
  3872. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3873. #else
  3874. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3875. #endif
  3876. /* invalid context */
  3877. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3878. dh_key_der_2048, sizeof_dh_key_der_2048,
  3879. WOLFSSL_FILETYPE_ASN1));
  3880. /* invalid dhParamFile file */
  3881. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3882. 0, WOLFSSL_FILETYPE_ASN1));
  3883. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3884. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3885. WOLFSSL_FILETYPE_ASN1));
  3886. /* success */
  3887. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3888. dh_key_der_2048, sizeof_dh_key_der_2048,
  3889. WOLFSSL_FILETYPE_ASN1));
  3890. wolfSSL_CTX_free(ctx);
  3891. #endif
  3892. return EXPECT_RESULT();
  3893. }
  3894. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3895. {
  3896. EXPECT_DECLS;
  3897. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3898. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3899. WOLFSSL_CTX *ctx;
  3900. (void)ctx;
  3901. #ifndef NO_WOLFSSL_CLIENT
  3902. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3903. #else
  3904. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3905. #endif
  3906. ExpectNotNull(ctx);
  3907. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3908. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3909. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3910. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3911. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3912. dh_key_der_2048, sizeof_dh_key_der_2048,
  3913. WOLFSSL_FILETYPE_ASN1));
  3914. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3915. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3916. dh_key_der_2048, sizeof_dh_key_der_2048,
  3917. WOLFSSL_FILETYPE_ASN1));
  3918. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3919. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3920. dh_key_der_2048, sizeof_dh_key_der_2048,
  3921. WOLFSSL_FILETYPE_ASN1));
  3922. wolfSSL_CTX_free(ctx);
  3923. #endif
  3924. return EXPECT_RESULT();
  3925. }
  3926. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3927. {
  3928. EXPECT_DECLS;
  3929. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3930. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3931. WOLFSSL_CTX* ctx = NULL;
  3932. const char* derCert = "./certs/server-cert.der";
  3933. const char* nullPath = NULL;
  3934. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3935. const char* emptyPath = "";
  3936. /* der load Case 1 ctx NULL */
  3937. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3938. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3939. #ifndef NO_WOLFSSL_CLIENT
  3940. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3941. #else
  3942. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3943. #endif
  3944. /* Case 2 filePath NULL */
  3945. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3946. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3947. /* Case 3 invalid format */
  3948. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3949. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3950. /* Case 4 filePath not valid */
  3951. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3952. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3953. /* Case 5 filePath empty */
  3954. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3955. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3956. #ifndef NO_RSA
  3957. /* Case 6 success case */
  3958. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3959. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3960. #endif
  3961. wolfSSL_CTX_free(ctx);
  3962. #endif
  3963. return EXPECT_RESULT();
  3964. }
  3965. static int test_wolfSSL_CTX_enable_disable(void)
  3966. {
  3967. EXPECT_DECLS;
  3968. #ifndef NO_CERTS
  3969. WOLFSSL_CTX* ctx = NULL;
  3970. #ifdef HAVE_CRL
  3971. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  3972. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  3973. #endif
  3974. #ifdef HAVE_OCSP
  3975. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  3976. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  3977. #endif
  3978. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3979. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3980. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  3981. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  3982. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3983. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3984. #endif
  3985. #ifndef NO_WOLFSSL_CLIENT
  3986. #ifdef HAVE_EXTENDED_MASTER
  3987. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3988. #endif
  3989. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3990. #ifdef HAVE_EXTENDED_MASTER
  3991. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3992. #endif
  3993. #elif !defined(NO_WOLFSSL_SERVER)
  3994. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3995. #endif
  3996. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3997. #ifdef HAVE_CRL
  3998. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3999. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4000. #endif
  4001. #ifdef HAVE_OCSP
  4002. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4003. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4004. WOLFSSL_SUCCESS);
  4005. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4006. WOLFSSL_SUCCESS);
  4007. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4008. WOLFSSL_SUCCESS);
  4009. #endif
  4010. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4011. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4012. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4013. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4014. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4015. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4016. #endif
  4017. wolfSSL_CTX_free(ctx);
  4018. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4019. #endif /* NO_CERTS */
  4020. return EXPECT_RESULT();
  4021. }
  4022. static int test_wolfSSL_CTX_ticket_API(void)
  4023. {
  4024. EXPECT_DECLS;
  4025. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4026. WOLFSSL_CTX* ctx = NULL;
  4027. void *userCtx = (void*)"this is my ctx";
  4028. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4029. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4030. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4031. wolfSSL_CTX_free(ctx);
  4032. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4033. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4034. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4035. return EXPECT_RESULT();
  4036. }
  4037. static int test_wolfSSL_set_minmax_proto_version(void)
  4038. {
  4039. EXPECT_DECLS;
  4040. #ifdef OPENSSL_EXTRA
  4041. WOLFSSL_CTX *ctx = NULL;
  4042. WOLFSSL *ssl = NULL;
  4043. (void)ssl;
  4044. #ifndef NO_WOLFSSL_CLIENT
  4045. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4046. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4047. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4048. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4049. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4050. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4051. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4052. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4053. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4054. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4055. wolfSSL_free(ssl);
  4056. wolfSSL_CTX_free(ctx);
  4057. ctx = NULL;
  4058. #endif
  4059. #ifndef NO_WOLFSSL_SERVER
  4060. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4061. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4062. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4063. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4064. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4065. wolfSSL_CTX_free(ctx);
  4066. #endif
  4067. #endif
  4068. return EXPECT_RESULT();
  4069. }
  4070. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4071. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4072. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4073. {
  4074. EXPECT_DECLS;
  4075. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4076. return EXPECT_RESULT();
  4077. }
  4078. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4079. {
  4080. EXPECT_DECLS;
  4081. /* Set TLS 1.2 */
  4082. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4083. WOLFSSL_SUCCESS);
  4084. return EXPECT_RESULT();
  4085. }
  4086. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4087. * what was set at ctx creation. */
  4088. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4089. {
  4090. EXPECT_DECLS;
  4091. test_ssl_cbf client_cbs;
  4092. test_ssl_cbf server_cbs;
  4093. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4094. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4095. client_cbs.method = wolfTLS_client_method;
  4096. server_cbs.method = wolfTLS_server_method;
  4097. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4098. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4099. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4100. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4101. &server_cbs, NULL), TEST_SUCCESS);
  4102. return EXPECT_RESULT();
  4103. }
  4104. #else
  4105. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4106. {
  4107. return TEST_SKIPPED;
  4108. }
  4109. #endif
  4110. /*----------------------------------------------------------------------------*
  4111. | SSL
  4112. *----------------------------------------------------------------------------*/
  4113. static int test_server_wolfSSL_new(void)
  4114. {
  4115. EXPECT_DECLS;
  4116. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4117. !defined(NO_WOLFSSL_SERVER)
  4118. WOLFSSL_CTX *ctx = NULL;
  4119. WOLFSSL_CTX *ctx_nocert = NULL;
  4120. WOLFSSL *ssl = NULL;
  4121. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4122. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4123. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4124. WOLFSSL_FILETYPE_PEM));
  4125. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4126. WOLFSSL_FILETYPE_PEM));
  4127. /* invalid context */
  4128. ExpectNull(ssl = wolfSSL_new(NULL));
  4129. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4130. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4131. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4132. #endif
  4133. /* success */
  4134. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4135. wolfSSL_free(ssl);
  4136. wolfSSL_CTX_free(ctx);
  4137. wolfSSL_CTX_free(ctx_nocert);
  4138. #endif
  4139. return EXPECT_RESULT();
  4140. }
  4141. static int test_client_wolfSSL_new(void)
  4142. {
  4143. EXPECT_DECLS;
  4144. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4145. !defined(NO_WOLFSSL_CLIENT)
  4146. WOLFSSL_CTX *ctx = NULL;
  4147. WOLFSSL_CTX *ctx_nocert = NULL;
  4148. WOLFSSL *ssl = NULL;
  4149. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4150. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4151. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4152. /* invalid context */
  4153. ExpectNull(ssl = wolfSSL_new(NULL));
  4154. /* success */
  4155. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4156. wolfSSL_free(ssl);
  4157. ssl = NULL;
  4158. /* success */
  4159. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4160. wolfSSL_free(ssl);
  4161. wolfSSL_CTX_free(ctx);
  4162. wolfSSL_CTX_free(ctx_nocert);
  4163. #endif
  4164. return EXPECT_RESULT();
  4165. }
  4166. static int test_wolfSSL_SetTmpDH_file(void)
  4167. {
  4168. EXPECT_DECLS;
  4169. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4170. !defined(NO_WOLFSSL_SERVER)
  4171. WOLFSSL_CTX *ctx = NULL;
  4172. WOLFSSL *ssl = NULL;
  4173. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4174. #ifndef NO_RSA
  4175. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4176. WOLFSSL_FILETYPE_PEM));
  4177. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4178. WOLFSSL_FILETYPE_PEM));
  4179. #elif defined(HAVE_ECC)
  4180. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4181. WOLFSSL_FILETYPE_PEM));
  4182. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4183. WOLFSSL_FILETYPE_PEM));
  4184. #elif defined(HAVE_ED25519)
  4185. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4186. WOLFSSL_FILETYPE_PEM));
  4187. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4188. WOLFSSL_FILETYPE_PEM));
  4189. #elif defined(HAVE_ED448)
  4190. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4191. WOLFSSL_FILETYPE_PEM));
  4192. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4193. WOLFSSL_FILETYPE_PEM));
  4194. #endif
  4195. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4196. /* invalid ssl */
  4197. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4198. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4199. /* invalid dhParamFile file */
  4200. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4201. NULL, WOLFSSL_FILETYPE_PEM));
  4202. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4203. bogusFile, WOLFSSL_FILETYPE_PEM));
  4204. /* success */
  4205. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4206. WOLFSSL_FILETYPE_PEM));
  4207. wolfSSL_free(ssl);
  4208. wolfSSL_CTX_free(ctx);
  4209. #endif
  4210. return EXPECT_RESULT();
  4211. }
  4212. static int test_wolfSSL_SetTmpDH_buffer(void)
  4213. {
  4214. EXPECT_DECLS;
  4215. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4216. WOLFSSL_CTX *ctx = NULL;
  4217. WOLFSSL *ssl = NULL;
  4218. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4219. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4220. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4221. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4222. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4223. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4224. /* invalid ssl */
  4225. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4226. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4227. /* invalid dhParamFile file */
  4228. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4229. 0, WOLFSSL_FILETYPE_ASN1));
  4230. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4231. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4232. /* success */
  4233. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4234. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4235. wolfSSL_free(ssl);
  4236. wolfSSL_CTX_free(ctx);
  4237. #endif
  4238. return EXPECT_RESULT();
  4239. }
  4240. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4241. {
  4242. EXPECT_DECLS;
  4243. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4244. WOLFSSL_CTX *ctx = NULL;
  4245. WOLFSSL_CTX *ctx2 = NULL;
  4246. WOLFSSL *ssl = NULL;
  4247. WOLFSSL *ssl2 = NULL;
  4248. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4249. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4250. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4251. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4252. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4253. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4254. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4255. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4256. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4257. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4258. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4259. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4260. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4261. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4262. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4263. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4264. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4265. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4266. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4267. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4268. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4269. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4270. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4271. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4272. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4273. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4274. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4275. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4276. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4277. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4278. wolfSSL_free(ssl2);
  4279. wolfSSL_CTX_free(ctx2);
  4280. wolfSSL_free(ssl);
  4281. wolfSSL_CTX_free(ctx);
  4282. #endif
  4283. return EXPECT_RESULT();
  4284. }
  4285. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4286. * allowed.
  4287. * POST: return 1 on success.
  4288. */
  4289. static int test_wolfSSL_SetMinVersion(void)
  4290. {
  4291. int res = TEST_SKIPPED;
  4292. #ifndef NO_WOLFSSL_CLIENT
  4293. int failFlag = WOLFSSL_SUCCESS;
  4294. WOLFSSL_CTX* ctx = NULL;
  4295. WOLFSSL* ssl = NULL;
  4296. int itr;
  4297. #ifndef NO_OLD_TLS
  4298. const int versions[] = {
  4299. #ifdef WOLFSSL_ALLOW_TLSV10
  4300. WOLFSSL_TLSV1,
  4301. #endif
  4302. WOLFSSL_TLSV1_1,
  4303. WOLFSSL_TLSV1_2};
  4304. #elif !defined(WOLFSSL_NO_TLS12)
  4305. const int versions[] = { WOLFSSL_TLSV1_2 };
  4306. #else
  4307. const int versions[] = { WOLFSSL_TLSV1_3 };
  4308. #endif
  4309. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4310. ssl = wolfSSL_new(ctx);
  4311. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4312. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4313. failFlag = WOLFSSL_FAILURE;
  4314. }
  4315. }
  4316. wolfSSL_free(ssl);
  4317. wolfSSL_CTX_free(ctx);
  4318. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4319. #endif
  4320. return res;
  4321. } /* END test_wolfSSL_SetMinVersion */
  4322. #ifdef OPENSSL_EXTRA
  4323. static int test_ED25519(void)
  4324. {
  4325. EXPECT_DECLS;
  4326. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4327. defined(WOLFSSL_KEY_GEN)
  4328. byte priv[ED25519_PRV_KEY_SIZE];
  4329. unsigned int privSz = (unsigned int)sizeof(priv);
  4330. byte pub[ED25519_PUB_KEY_SIZE];
  4331. unsigned int pubSz = (unsigned int)sizeof(pub);
  4332. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4333. const char* msg = TEST_STRING;
  4334. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4335. byte sig[ED25519_SIG_SIZE];
  4336. unsigned int sigSz = (unsigned int)sizeof(sig);
  4337. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4338. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4339. WOLFSSL_SUCCESS);
  4340. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4341. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4342. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4343. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4344. &sigSz), WOLFSSL_SUCCESS);
  4345. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4346. #ifdef HAVE_ED25519_VERIFY
  4347. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4348. sigSz), WOLFSSL_SUCCESS);
  4349. #endif /* HAVE_ED25519_VERIFY */
  4350. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4351. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4352. return EXPECT_RESULT();
  4353. }
  4354. static int test_ED448(void)
  4355. {
  4356. EXPECT_DECLS;
  4357. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4358. defined(WOLFSSL_KEY_GEN)
  4359. byte priv[ED448_PRV_KEY_SIZE];
  4360. unsigned int privSz = (unsigned int)sizeof(priv);
  4361. byte pub[ED448_PUB_KEY_SIZE];
  4362. unsigned int pubSz = (unsigned int)sizeof(pub);
  4363. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4364. const char* msg = TEST_STRING;
  4365. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4366. byte sig[ED448_SIG_SIZE];
  4367. unsigned int sigSz = (unsigned int)sizeof(sig);
  4368. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4369. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  4370. WOLFSSL_SUCCESS);
  4371. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4372. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4373. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4374. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4375. &sigSz), WOLFSSL_SUCCESS);
  4376. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4377. #ifdef HAVE_ED448_VERIFY
  4378. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4379. sigSz), WOLFSSL_SUCCESS);
  4380. #endif /* HAVE_ED448_VERIFY */
  4381. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4382. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4383. return EXPECT_RESULT();
  4384. }
  4385. #endif /* OPENSSL_EXTRA */
  4386. #include <wolfssl/openssl/pem.h>
  4387. /*----------------------------------------------------------------------------*
  4388. | EVP
  4389. *----------------------------------------------------------------------------*/
  4390. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4391. {
  4392. EXPECT_DECLS;
  4393. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4394. WOLFSSL_BIO* rbio = NULL;
  4395. WOLFSSL_BIO* wbio = NULL;
  4396. WOLFSSL_EVP_PKEY* pkey = NULL;
  4397. char line[256] = { 0 };
  4398. char line1[256] = { 0 };
  4399. int i = 0;
  4400. /* test error cases */
  4401. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4402. /*
  4403. * test RSA public key print
  4404. * in this test, pass '3' for indent
  4405. */
  4406. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4407. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4408. sizeof_client_keypub_der_1024));
  4409. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4410. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4411. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4412. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4413. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4414. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4415. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4416. strcpy(line1, " Modulus:\n");
  4417. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4418. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4419. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4420. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4421. /* skip to the end of modulus element*/
  4422. for (i = 0; i < 8 ;i++) {
  4423. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4424. }
  4425. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4426. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4427. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4428. /* should reach EOF */
  4429. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4430. EVP_PKEY_free(pkey);
  4431. pkey = NULL;
  4432. BIO_free(rbio);
  4433. BIO_free(wbio);
  4434. rbio = NULL;
  4435. wbio = NULL;
  4436. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4437. /*
  4438. * test DSA public key print
  4439. */
  4440. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4441. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4442. sizeof_dsa_pub_key_der_2048));
  4443. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4444. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4445. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4446. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4447. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4448. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4449. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4450. strcpy(line1, "pub:\n");
  4451. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4452. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4453. strcpy(line1,
  4454. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4455. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4456. /* skip to the end of pub element*/
  4457. for (i = 0; i < 17 ;i++) {
  4458. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4459. }
  4460. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4461. strcpy(line1, "P:\n");
  4462. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4463. /* skip to the end of P element*/
  4464. for (i = 0; i < 18 ;i++) {
  4465. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4466. }
  4467. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4468. strcpy(line1, "Q:\n");
  4469. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4470. /* skip to the end of Q element*/
  4471. for (i = 0; i < 3 ;i++) {
  4472. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4473. }
  4474. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4475. strcpy(line1, "G:\n");
  4476. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4477. /* skip to the end of G element*/
  4478. for (i = 0; i < 18 ;i++) {
  4479. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4480. }
  4481. /* should reach EOF */
  4482. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4483. EVP_PKEY_free(pkey);
  4484. pkey = NULL;
  4485. BIO_free(rbio);
  4486. BIO_free(wbio);
  4487. rbio = NULL;
  4488. wbio = NULL;
  4489. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4490. /*
  4491. * test ECC public key print
  4492. */
  4493. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4494. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4495. sizeof_ecc_clikeypub_der_256));
  4496. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4497. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4498. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4499. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4500. strcpy(line1, "Public-Key: (256 bit)\n");
  4501. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4502. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4503. strcpy(line1, "pub:\n");
  4504. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4505. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4506. strcpy(line1,
  4507. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4508. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4509. /* skip to the end of pub element*/
  4510. for (i = 0; i < 4 ;i++) {
  4511. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4512. }
  4513. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4514. strcpy(line1, "ASN1 OID: prime256v1\n");
  4515. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4516. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4517. strcpy(line1, "NIST CURVE: P-256\n");
  4518. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4519. /* should reach EOF */
  4520. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4521. EVP_PKEY_free(pkey);
  4522. pkey = NULL;
  4523. BIO_free(rbio);
  4524. BIO_free(wbio);
  4525. rbio = NULL;
  4526. wbio = NULL;
  4527. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4528. /*
  4529. * test DH public key print
  4530. */
  4531. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4532. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4533. sizeof_dh_pub_key_der_2048));
  4534. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4535. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4536. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4537. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4538. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4539. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4540. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4541. strcpy(line1, "public-key:\n");
  4542. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4543. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4544. strcpy(line1,
  4545. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4546. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4547. /* skip to the end of public-key element*/
  4548. for (i = 0; i < 17 ;i++) {
  4549. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4550. }
  4551. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4552. strcpy(line1, "prime:\n");
  4553. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4554. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4555. strcpy(line1,
  4556. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4557. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4558. /* skip to the end of prime element*/
  4559. for (i = 0; i < 17 ;i++) {
  4560. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4561. }
  4562. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4563. strcpy(line1, "generator: 2 (0x02)\n");
  4564. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4565. /* should reach EOF */
  4566. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4567. EVP_PKEY_free(pkey);
  4568. pkey = NULL;
  4569. BIO_free(rbio);
  4570. BIO_free(wbio);
  4571. rbio = NULL;
  4572. wbio = NULL;
  4573. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4574. /* to prevent "unused variable" warning */
  4575. (void)pkey;
  4576. (void)wbio;
  4577. (void)rbio;
  4578. (void)line;
  4579. (void)line1;
  4580. (void)i;
  4581. #endif /* OPENSSL_EXTRA */
  4582. return EXPECT_RESULT();
  4583. }
  4584. /* Test functions for base64 encode/decode */
  4585. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4586. {
  4587. EXPECT_DECLS;
  4588. #if defined(OPENSSL_EXTRA) && \
  4589. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4590. EVP_ENCODE_CTX* ctx = NULL;
  4591. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4592. ExpectIntEQ(ctx->remaining,0);
  4593. ExpectIntEQ(ctx->data[0],0);
  4594. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4595. EVP_ENCODE_CTX_free(ctx);
  4596. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4597. return EXPECT_RESULT();
  4598. }
  4599. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4600. {
  4601. EXPECT_DECLS;
  4602. #if defined(OPENSSL_EXTRA) && \
  4603. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4604. EVP_ENCODE_CTX* ctx = NULL;
  4605. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4606. EVP_ENCODE_CTX_free(ctx);
  4607. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4608. return EXPECT_RESULT();
  4609. }
  4610. static int test_wolfSSL_EVP_EncodeInit(void)
  4611. {
  4612. EXPECT_DECLS;
  4613. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4614. EVP_ENCODE_CTX* ctx = NULL;
  4615. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4616. ExpectIntEQ(ctx->remaining, 0);
  4617. ExpectIntEQ(ctx->data[0], 0);
  4618. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4619. if (ctx != NULL) {
  4620. /* make ctx dirty */
  4621. ctx->remaining = 10;
  4622. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4623. }
  4624. EVP_EncodeInit(ctx);
  4625. ExpectIntEQ(ctx->remaining, 0);
  4626. ExpectIntEQ(ctx->data[0], 0);
  4627. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4628. EVP_ENCODE_CTX_free(ctx);
  4629. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4630. return EXPECT_RESULT();
  4631. }
  4632. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4633. {
  4634. EXPECT_DECLS;
  4635. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4636. int outl;
  4637. int total;
  4638. const unsigned char plain0[] = {"Th"};
  4639. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4640. const unsigned char plain2[] = {"This is additional data."};
  4641. const unsigned char encBlock0[] = {"VGg="};
  4642. const unsigned char enc0[] = {"VGg=\n"};
  4643. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4644. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4645. const unsigned char enc2[] =
  4646. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4647. unsigned char encOutBuff[300];
  4648. EVP_ENCODE_CTX* ctx = NULL;
  4649. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4650. EVP_EncodeInit(ctx);
  4651. /* illegal parameter test */
  4652. ExpectIntEQ(
  4653. EVP_EncodeUpdate(
  4654. NULL, /* pass NULL as ctx */
  4655. encOutBuff,
  4656. &outl,
  4657. plain1,
  4658. sizeof(plain1)-1),
  4659. 0 /* expected result code 0: fail */
  4660. );
  4661. ExpectIntEQ(
  4662. EVP_EncodeUpdate(
  4663. ctx,
  4664. NULL, /* pass NULL as out buff */
  4665. &outl,
  4666. plain1,
  4667. sizeof(plain1)-1),
  4668. 0 /* expected result code 0: fail */
  4669. );
  4670. ExpectIntEQ(
  4671. EVP_EncodeUpdate(
  4672. ctx,
  4673. encOutBuff,
  4674. NULL, /* pass NULL as outl */
  4675. plain1,
  4676. sizeof(plain1)-1),
  4677. 0 /* expected result code 0: fail */
  4678. );
  4679. ExpectIntEQ(
  4680. EVP_EncodeUpdate(
  4681. ctx,
  4682. encOutBuff,
  4683. &outl,
  4684. NULL, /* pass NULL as in */
  4685. sizeof(plain1)-1),
  4686. 0 /* expected result code 0: fail */
  4687. );
  4688. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  4689. /* meaningless parameter test */
  4690. ExpectIntEQ(
  4691. EVP_EncodeUpdate(
  4692. ctx,
  4693. encOutBuff,
  4694. &outl,
  4695. plain1,
  4696. 0), /* pass zero input */
  4697. 1 /* expected result code 1: success */
  4698. );
  4699. /* very small data encoding test */
  4700. EVP_EncodeInit(ctx);
  4701. ExpectIntEQ(
  4702. EVP_EncodeUpdate(
  4703. ctx,
  4704. encOutBuff,
  4705. &outl,
  4706. plain0,
  4707. sizeof(plain0)-1),
  4708. 1 /* expected result code 1: success */
  4709. );
  4710. ExpectIntEQ(outl,0);
  4711. if (EXPECT_SUCCESS()) {
  4712. EVP_EncodeFinal(
  4713. ctx,
  4714. encOutBuff + outl,
  4715. &outl);
  4716. }
  4717. ExpectIntEQ( outl, sizeof(enc0)-1);
  4718. ExpectIntEQ(
  4719. XSTRNCMP(
  4720. (const char*)encOutBuff,
  4721. (const char*)enc0,sizeof(enc0) ),
  4722. 0);
  4723. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4724. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  4725. sizeof(encBlock0)-1);
  4726. ExpectStrEQ(encOutBuff, encBlock0);
  4727. /* pass small size( < 48bytes ) input, then make sure they are not
  4728. * encoded and just stored in ctx
  4729. */
  4730. EVP_EncodeInit(ctx);
  4731. total = 0;
  4732. outl = 0;
  4733. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4734. ExpectIntEQ(
  4735. EVP_EncodeUpdate(
  4736. ctx,
  4737. encOutBuff, /* buffer for output */
  4738. &outl, /* size of output */
  4739. plain1, /* input */
  4740. sizeof(plain1)-1), /* size of input */
  4741. 1); /* expected result code 1:success */
  4742. total += outl;
  4743. ExpectIntEQ(outl, 0); /* no output expected */
  4744. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  4745. ExpectTrue(
  4746. XSTRNCMP((const char*)(ctx->data),
  4747. (const char*)plain1,
  4748. ctx->remaining) ==0 );
  4749. ExpectTrue(encOutBuff[0] == 0);
  4750. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  4751. * the stored data and the new input together
  4752. */
  4753. ExpectIntEQ(
  4754. EVP_EncodeUpdate(
  4755. ctx,
  4756. encOutBuff + outl, /* buffer for output */
  4757. &outl, /* size of output */
  4758. plain2, /* additional input */
  4759. sizeof(plain2) -1), /* size of additional input */
  4760. 1); /* expected result code 1:success */
  4761. total += outl;
  4762. ExpectIntNE(outl, 0); /* some output is expected this time*/
  4763. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  4764. ExpectIntEQ(
  4765. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  4766. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  4767. EVP_EncodeFinal(
  4768. ctx,
  4769. encOutBuff + outl,
  4770. &outl);
  4771. total += outl;
  4772. ExpectIntNE(total,0);
  4773. ExpectIntNE(outl,0);
  4774. ExpectIntEQ(XSTRNCMP(
  4775. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  4776. /* test with illeagal parameters */
  4777. outl = 1;
  4778. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  4779. ExpectIntEQ(outl, 0);
  4780. outl = 1;
  4781. EVP_EncodeFinal(ctx, NULL, &outl);
  4782. ExpectIntEQ(outl, 0);
  4783. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  4784. EVP_EncodeFinal(NULL, NULL, NULL);
  4785. EVP_ENCODE_CTX_free(ctx);
  4786. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4787. return EXPECT_RESULT();
  4788. }
  4789. static int test_wolfSSL_EVP_EncodeFinal(void)
  4790. {
  4791. int res = TEST_SKIPPED;
  4792. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4793. /* tests for wolfSSL_EVP_EncodeFinal are included in
  4794. * test_wolfSSL_EVP_EncodeUpdate
  4795. */
  4796. res = TEST_SUCCESS;
  4797. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4798. return res;
  4799. }
  4800. static int test_wolfSSL_EVP_DecodeInit(void)
  4801. {
  4802. EXPECT_DECLS;
  4803. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4804. EVP_ENCODE_CTX* ctx = NULL;
  4805. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  4806. ExpectIntEQ( ctx->remaining,0);
  4807. ExpectIntEQ( ctx->data[0],0);
  4808. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4809. if (ctx != NULL) {
  4810. /* make ctx dirty */
  4811. ctx->remaining = 10;
  4812. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  4813. }
  4814. EVP_DecodeInit(ctx);
  4815. ExpectIntEQ( ctx->remaining,0);
  4816. ExpectIntEQ( ctx->data[0],0);
  4817. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4818. EVP_ENCODE_CTX_free(ctx);
  4819. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4820. return EXPECT_RESULT();
  4821. }
  4822. static int test_wolfSSL_EVP_DecodeUpdate(void)
  4823. {
  4824. EXPECT_DECLS;
  4825. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4826. int outl;
  4827. unsigned char decOutBuff[300];
  4828. EVP_ENCODE_CTX* ctx = NULL;
  4829. static const unsigned char enc1[] =
  4830. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4831. /* const unsigned char plain1[] =
  4832. {"This is a base64 decoding test."} */
  4833. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4834. EVP_DecodeInit(ctx);
  4835. /* illegal parameter tests */
  4836. /* pass NULL as ctx */
  4837. ExpectIntEQ(
  4838. EVP_DecodeUpdate(
  4839. NULL, /* pass NULL as ctx */
  4840. decOutBuff,
  4841. &outl,
  4842. enc1,
  4843. sizeof(enc1)-1),
  4844. -1 /* expected result code -1: fail */
  4845. );
  4846. ExpectIntEQ( outl, 0);
  4847. /* pass NULL as output */
  4848. ExpectIntEQ(
  4849. EVP_DecodeUpdate(
  4850. ctx,
  4851. NULL, /* pass NULL as out buff */
  4852. &outl,
  4853. enc1,
  4854. sizeof(enc1)-1),
  4855. -1 /* expected result code -1: fail */
  4856. );
  4857. ExpectIntEQ( outl, 0);
  4858. /* pass NULL as outl */
  4859. ExpectIntEQ(
  4860. EVP_DecodeUpdate(
  4861. ctx,
  4862. decOutBuff,
  4863. NULL, /* pass NULL as outl */
  4864. enc1,
  4865. sizeof(enc1)-1),
  4866. -1 /* expected result code -1: fail */
  4867. );
  4868. /* pass NULL as input */
  4869. ExpectIntEQ(
  4870. EVP_DecodeUpdate(
  4871. ctx,
  4872. decOutBuff,
  4873. &outl,
  4874. NULL, /* pass NULL as in */
  4875. sizeof(enc1)-1),
  4876. -1 /* expected result code -1: fail */
  4877. );
  4878. ExpectIntEQ( outl, 0);
  4879. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4880. /* pass zero length input */
  4881. ExpectIntEQ(
  4882. EVP_DecodeUpdate(
  4883. ctx,
  4884. decOutBuff,
  4885. &outl,
  4886. enc1,
  4887. 0), /* pass zero as input len */
  4888. 1 /* expected result code 1: success */
  4889. );
  4890. /* decode correct base64 string */
  4891. {
  4892. static const unsigned char enc2[] =
  4893. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4894. static const unsigned char plain2[] =
  4895. {"This is a base64 decoding test."};
  4896. EVP_EncodeInit(ctx);
  4897. ExpectIntEQ(
  4898. EVP_DecodeUpdate(
  4899. ctx,
  4900. decOutBuff,
  4901. &outl,
  4902. enc2,
  4903. sizeof(enc2)-1),
  4904. 0 /* expected result code 0: success */
  4905. );
  4906. ExpectIntEQ(outl,sizeof(plain2) -1);
  4907. ExpectIntEQ(
  4908. EVP_DecodeFinal(
  4909. ctx,
  4910. decOutBuff + outl,
  4911. &outl),
  4912. 1 /* expected result code 1: success */
  4913. );
  4914. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  4915. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4916. sizeof(plain2) -1 ),0);
  4917. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4918. sizeof(plain2)-1);
  4919. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4920. sizeof(plain2) -1 ),0);
  4921. }
  4922. /* decode correct base64 string which does not have '\n' in its last*/
  4923. {
  4924. static const unsigned char enc3[] =
  4925. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4926. static const unsigned char plain3[] =
  4927. {"This is a base64 decoding test."}; /* 31 chars */
  4928. EVP_EncodeInit(ctx);
  4929. ExpectIntEQ(
  4930. EVP_DecodeUpdate(
  4931. ctx,
  4932. decOutBuff,
  4933. &outl,
  4934. enc3,
  4935. sizeof(enc3)-1),
  4936. 0 /* expected result code 0: success */
  4937. );
  4938. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4939. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4940. sizeof(plain3) -1 ),0);
  4941. ExpectIntEQ(
  4942. EVP_DecodeFinal(
  4943. ctx,
  4944. decOutBuff + outl,
  4945. &outl),
  4946. 1 /* expected result code 1: success */
  4947. );
  4948. ExpectIntEQ(outl,0 );
  4949. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4950. sizeof(plain3)-1);
  4951. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4952. sizeof(plain3) -1 ),0);
  4953. }
  4954. /* decode string which has a padding char ('=') in the illegal position*/
  4955. {
  4956. static const unsigned char enc4[] =
  4957. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4958. EVP_EncodeInit(ctx);
  4959. ExpectIntEQ(
  4960. EVP_DecodeUpdate(
  4961. ctx,
  4962. decOutBuff,
  4963. &outl,
  4964. enc4,
  4965. sizeof(enc4)-1),
  4966. -1 /* expected result code -1: error */
  4967. );
  4968. ExpectIntEQ(outl,0);
  4969. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4970. }
  4971. /* small data decode test */
  4972. {
  4973. static const unsigned char enc00[] = {"VG"};
  4974. static const unsigned char enc01[] = {"g=\n"};
  4975. static const unsigned char plain4[] = {"Th"};
  4976. EVP_EncodeInit(ctx);
  4977. ExpectIntEQ(
  4978. EVP_DecodeUpdate(
  4979. ctx,
  4980. decOutBuff,
  4981. &outl,
  4982. enc00,
  4983. sizeof(enc00)-1),
  4984. 1 /* expected result code 1: success */
  4985. );
  4986. ExpectIntEQ(outl,0);
  4987. ExpectIntEQ(
  4988. EVP_DecodeUpdate(
  4989. ctx,
  4990. decOutBuff + outl,
  4991. &outl,
  4992. enc01,
  4993. sizeof(enc01)-1),
  4994. 0 /* expected result code 0: success */
  4995. );
  4996. ExpectIntEQ(outl,sizeof(plain4)-1);
  4997. /* test with illegal parameters */
  4998. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4999. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5000. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5001. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5002. if (EXPECT_SUCCESS()) {
  5003. EVP_DecodeFinal(
  5004. ctx,
  5005. decOutBuff + outl,
  5006. &outl);
  5007. }
  5008. ExpectIntEQ( outl, 0);
  5009. ExpectIntEQ(
  5010. XSTRNCMP(
  5011. (const char*)decOutBuff,
  5012. (const char*)plain4,sizeof(plain4)-1 ),
  5013. 0);
  5014. }
  5015. EVP_ENCODE_CTX_free(ctx);
  5016. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5017. return EXPECT_RESULT();
  5018. }
  5019. static int test_wolfSSL_EVP_DecodeFinal(void)
  5020. {
  5021. int res = TEST_SKIPPED;
  5022. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5023. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5024. * test_wolfSSL_EVP_DecodeUpdate
  5025. */
  5026. res = TEST_SUCCESS;
  5027. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5028. return res;
  5029. }
  5030. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5031. */
  5032. #ifdef OPENSSL_EXTRA
  5033. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5034. {
  5035. EXPECT_DECLS;
  5036. #ifndef NO_AES
  5037. const WOLFSSL_EVP_CIPHER* c;
  5038. c = wolfSSL_EVP_get_cipherbynid(419);
  5039. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5040. defined(WOLFSSL_AES_128)
  5041. ExpectNotNull(c);
  5042. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5043. #else
  5044. ExpectNull(c);
  5045. #endif
  5046. c = wolfSSL_EVP_get_cipherbynid(423);
  5047. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5048. defined(WOLFSSL_AES_192)
  5049. ExpectNotNull(c);
  5050. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5051. #else
  5052. ExpectNull(c);
  5053. #endif
  5054. c = wolfSSL_EVP_get_cipherbynid(427);
  5055. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5056. defined(WOLFSSL_AES_256)
  5057. ExpectNotNull(c);
  5058. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5059. #else
  5060. ExpectNull(c);
  5061. #endif
  5062. c = wolfSSL_EVP_get_cipherbynid(904);
  5063. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5064. ExpectNotNull(c);
  5065. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5066. #else
  5067. ExpectNull(c);
  5068. #endif
  5069. c = wolfSSL_EVP_get_cipherbynid(905);
  5070. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5071. ExpectNotNull(c);
  5072. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5073. #else
  5074. ExpectNull(c);
  5075. #endif
  5076. c = wolfSSL_EVP_get_cipherbynid(906);
  5077. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5078. ExpectNotNull(c);
  5079. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5080. #else
  5081. ExpectNull(c);
  5082. #endif
  5083. c = wolfSSL_EVP_get_cipherbynid(418);
  5084. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5085. ExpectNotNull(c);
  5086. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5087. #else
  5088. ExpectNull(c);
  5089. #endif
  5090. c = wolfSSL_EVP_get_cipherbynid(422);
  5091. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5092. ExpectNotNull(c);
  5093. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5094. #else
  5095. ExpectNull(c);
  5096. #endif
  5097. c = wolfSSL_EVP_get_cipherbynid(426);
  5098. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5099. ExpectNotNull(c);
  5100. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5101. #else
  5102. ExpectNull(c);
  5103. #endif
  5104. #endif /* !NO_AES */
  5105. #ifndef NO_DES3
  5106. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5107. #ifdef WOLFSSL_DES_ECB
  5108. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5109. #endif
  5110. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5111. #ifdef WOLFSSL_DES_ECB
  5112. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5113. #endif
  5114. #endif /* !NO_DES3 */
  5115. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5116. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5117. #endif
  5118. /* test for nid is out of range */
  5119. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5120. return EXPECT_RESULT();
  5121. }
  5122. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5123. {
  5124. EXPECT_DECLS;
  5125. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5126. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5127. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5128. const EVP_CIPHER *test;
  5129. byte key[AES_BLOCK_SIZE] = {0};
  5130. byte iv[AES_BLOCK_SIZE] = {0};
  5131. ExpectNotNull(ctx);
  5132. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5133. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5134. test = EVP_CIPHER_CTX_cipher(ctx);
  5135. ExpectTrue(init == test);
  5136. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5137. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5138. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5139. EVP_CIPHER_CTX_free(ctx);
  5140. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5141. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5142. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5143. return EXPECT_RESULT();
  5144. }
  5145. #endif /* OPENSSL_EXTRA */
  5146. /*----------------------------------------------------------------------------*
  5147. | IO
  5148. *----------------------------------------------------------------------------*/
  5149. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5150. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5151. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5152. #ifdef WC_SHA512_DIGEST_SIZE
  5153. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5154. #else
  5155. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5156. #endif
  5157. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5158. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5159. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5160. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5161. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5162. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5163. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5164. defined(HAVE_AES_CBC)
  5165. typedef struct openssl_key_ctx {
  5166. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5167. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5168. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5169. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5170. } openssl_key_ctx;
  5171. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5172. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5173. static WC_INLINE int OpenSSLTicketInit(void)
  5174. {
  5175. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5176. if (ret != 0) return ret;
  5177. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5178. sizeof(myOpenSSLKey_ctx.name));
  5179. if (ret != 0) return ret;
  5180. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5181. sizeof(myOpenSSLKey_ctx.key));
  5182. if (ret != 0) return ret;
  5183. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5184. sizeof(myOpenSSLKey_ctx.hmacKey));
  5185. if (ret != 0) return ret;
  5186. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5187. sizeof(myOpenSSLKey_ctx.iv));
  5188. if (ret != 0) return ret;
  5189. return 0;
  5190. }
  5191. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5192. byte name[WOLFSSL_TICKET_NAME_SZ],
  5193. byte iv[WOLFSSL_TICKET_IV_SZ],
  5194. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5195. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5196. (void)ssl;
  5197. if (enc) {
  5198. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5199. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5200. }
  5201. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5202. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5203. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5204. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5205. return 0;
  5206. }
  5207. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5208. if (enc)
  5209. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5210. else
  5211. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5212. return 1;
  5213. }
  5214. static WC_INLINE void OpenSSLTicketCleanup(void)
  5215. {
  5216. wc_FreeRng(&myOpenSSLKey_rng);
  5217. }
  5218. #endif
  5219. #endif
  5220. /* helper functions */
  5221. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5222. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5223. void *ctx)
  5224. {
  5225. struct test_ssl_memio_ctx *test_ctx;
  5226. byte *buf;
  5227. int *len;
  5228. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5229. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5230. buf = test_ctx->c_buff;
  5231. len = &test_ctx->c_len;
  5232. }
  5233. else {
  5234. buf = test_ctx->s_buff;
  5235. len = &test_ctx->s_len;
  5236. }
  5237. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5238. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5239. XMEMCPY(buf + *len, data, sz);
  5240. *len += sz;
  5241. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5242. {
  5243. /* This can be imported into Wireshark by transforming the file with
  5244. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5245. * And then loading test_output.dump.hex into Wireshark using the
  5246. * "Import from Hex Dump..." option ion and selecting the TCP
  5247. * encapsulation option. */
  5248. char dump_file_name[64];
  5249. WOLFSSL_BIO *dump_file;
  5250. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5251. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5252. if (dump_file != NULL) {
  5253. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5254. wolfSSL_BIO_free(dump_file);
  5255. }
  5256. }
  5257. #endif
  5258. return sz;
  5259. }
  5260. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5261. void *ctx)
  5262. {
  5263. struct test_ssl_memio_ctx *test_ctx;
  5264. int read_sz;
  5265. byte *buf;
  5266. int *len;
  5267. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5268. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5269. buf = test_ctx->s_buff;
  5270. len = &test_ctx->s_len;
  5271. }
  5272. else {
  5273. buf = test_ctx->c_buff;
  5274. len = &test_ctx->c_len;
  5275. }
  5276. if (*len == 0)
  5277. return WOLFSSL_CBIO_ERR_WANT_READ;
  5278. read_sz = sz < *len ? sz : *len;
  5279. XMEMCPY(data, buf, read_sz);
  5280. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5281. *len -= read_sz;
  5282. return read_sz;
  5283. }
  5284. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5285. {
  5286. EXPECT_DECLS;
  5287. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5288. int c_sharedCtx = 0;
  5289. int s_sharedCtx = 0;
  5290. #endif
  5291. const char* clientCertFile = cliCertFile;
  5292. const char* clientKeyFile = cliKeyFile;
  5293. const char* serverCertFile = svrCertFile;
  5294. const char* serverKeyFile = svrKeyFile;
  5295. /********************************
  5296. * Create WOLFSSL_CTX for client.
  5297. ********************************/
  5298. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5299. if (ctx->c_ctx != NULL) {
  5300. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5301. }
  5302. else
  5303. #endif
  5304. {
  5305. WOLFSSL_METHOD* method = NULL;
  5306. if (ctx->c_cb.method != NULL) {
  5307. method = ctx->c_cb.method();
  5308. }
  5309. else {
  5310. method = wolfSSLv23_client_method();
  5311. }
  5312. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5313. }
  5314. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5315. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5316. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5317. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5318. #endif
  5319. if (ctx->c_cb.caPemFile != NULL)
  5320. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5321. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5322. else
  5323. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5324. caCertFile, 0), WOLFSSL_SUCCESS);
  5325. if (ctx->c_cb.certPemFile != NULL) {
  5326. clientCertFile = ctx->c_cb.certPemFile;
  5327. }
  5328. if (ctx->c_cb.keyPemFile != NULL) {
  5329. clientKeyFile = ctx->c_cb.keyPemFile;
  5330. }
  5331. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5332. if (!c_sharedCtx)
  5333. #endif
  5334. {
  5335. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5336. clientCertFile), WOLFSSL_SUCCESS);
  5337. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5338. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5339. }
  5340. #ifdef HAVE_CRL
  5341. if (ctx->c_cb.crlPemFile != NULL) {
  5342. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5343. WOLFSSL_SUCCESS);
  5344. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5345. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5346. }
  5347. #endif
  5348. if (ctx->c_ciphers != NULL) {
  5349. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5350. WOLFSSL_SUCCESS);
  5351. }
  5352. if (ctx->c_cb.ctx_ready != NULL) {
  5353. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5354. }
  5355. /********************************
  5356. * Create WOLFSSL_CTX for server.
  5357. ********************************/
  5358. if (ctx->s_ctx != NULL) {
  5359. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5360. s_sharedCtx = 1;
  5361. #endif
  5362. ctx->s_cb.isSharedCtx = 1;
  5363. }
  5364. else
  5365. {
  5366. WOLFSSL_METHOD* method = NULL;
  5367. if (ctx->s_cb.method != NULL) {
  5368. method = ctx->s_cb.method();
  5369. }
  5370. else {
  5371. method = wolfSSLv23_server_method();
  5372. }
  5373. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5374. ctx->s_cb.isSharedCtx = 0;
  5375. }
  5376. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5377. #if defined(HAVE_SESSION_TICKET) && \
  5378. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5379. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5380. OpenSSLTicketInit();
  5381. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5382. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5383. TicketInit();
  5384. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5385. #endif
  5386. #endif
  5387. }
  5388. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5389. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5390. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5391. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5392. if (ctx->s_cb.caPemFile != NULL)
  5393. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5394. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5395. else
  5396. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5397. cliCertFile, 0), WOLFSSL_SUCCESS);
  5398. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5399. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5400. #endif
  5401. if (ctx->s_cb.certPemFile != NULL) {
  5402. serverCertFile = ctx->s_cb.certPemFile;
  5403. }
  5404. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5405. if (!s_sharedCtx)
  5406. #endif
  5407. {
  5408. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5409. serverCertFile), WOLFSSL_SUCCESS);
  5410. }
  5411. if (ctx->s_cb.keyPemFile != NULL) {
  5412. serverKeyFile = ctx->s_cb.keyPemFile;
  5413. }
  5414. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5415. if (!s_sharedCtx)
  5416. #endif
  5417. {
  5418. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5419. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5420. }
  5421. if (ctx->s_ciphers != NULL) {
  5422. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5423. WOLFSSL_SUCCESS);
  5424. }
  5425. if (ctx->s_cb.ctx_ready != NULL) {
  5426. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5427. }
  5428. /****************************
  5429. * Create WOLFSSL for client.
  5430. ****************************/
  5431. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5432. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5433. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5434. if (0
  5435. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5436. || c_sharedCtx
  5437. #endif
  5438. )
  5439. {
  5440. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5441. clientCertFile), WOLFSSL_SUCCESS);
  5442. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5443. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5444. }
  5445. if (ctx->c_cb.ssl_ready != NULL) {
  5446. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5447. }
  5448. /****************************
  5449. * Create WOLFSSL for server.
  5450. ****************************/
  5451. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5452. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5453. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5454. if (0
  5455. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5456. || s_sharedCtx
  5457. #endif
  5458. )
  5459. {
  5460. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5461. serverCertFile), WOLFSSL_SUCCESS);
  5462. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5463. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5464. }
  5465. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5466. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5467. #elif !defined(NO_DH)
  5468. /* will repick suites with DHE, higher priority than PSK */
  5469. SetDH(ctx->s_ssl);
  5470. #endif
  5471. if (ctx->s_cb.ssl_ready != NULL) {
  5472. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5473. }
  5474. return EXPECT_RESULT();
  5475. }
  5476. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5477. int* rounds)
  5478. {
  5479. int handshake_complete = 0;
  5480. int hs_c = 0;
  5481. int hs_s = 0;
  5482. int failing_s = 0;
  5483. int failing_c = 0;
  5484. int ret;
  5485. int err;
  5486. if (rounds != NULL) {
  5487. *rounds = 0;
  5488. }
  5489. while ((!handshake_complete) && (max_rounds > 0)) {
  5490. if (!hs_c) {
  5491. wolfSSL_SetLoggingPrefix("client");
  5492. ret = wolfSSL_connect(ctx->c_ssl);
  5493. wolfSSL_SetLoggingPrefix(NULL);
  5494. if (ret == WOLFSSL_SUCCESS) {
  5495. hs_c = 1;
  5496. }
  5497. else {
  5498. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5499. if (err != WOLFSSL_ERROR_WANT_READ &&
  5500. err != WOLFSSL_ERROR_WANT_WRITE) {
  5501. char buff[WOLFSSL_MAX_ERROR_SZ];
  5502. fprintf(stderr, "error = %d, %s\n", err,
  5503. wolfSSL_ERR_error_string(err, buff));
  5504. failing_c = 1;
  5505. hs_c = 1;
  5506. if (failing_c && failing_s) {
  5507. break;
  5508. }
  5509. }
  5510. }
  5511. }
  5512. if (!hs_s) {
  5513. wolfSSL_SetLoggingPrefix("server");
  5514. ret = wolfSSL_accept(ctx->s_ssl);
  5515. wolfSSL_SetLoggingPrefix(NULL);
  5516. if (ret == WOLFSSL_SUCCESS) {
  5517. hs_s = 1;
  5518. }
  5519. else {
  5520. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5521. if (err != WOLFSSL_ERROR_WANT_READ &&
  5522. err != WOLFSSL_ERROR_WANT_WRITE) {
  5523. char buff[WOLFSSL_MAX_ERROR_SZ];
  5524. fprintf(stderr, "error = %d, %s\n", err,
  5525. wolfSSL_ERR_error_string(err, buff));
  5526. failing_s = 1;
  5527. hs_s = 1;
  5528. if (failing_c && failing_s) {
  5529. break;
  5530. }
  5531. }
  5532. }
  5533. }
  5534. handshake_complete = hs_c && hs_s;
  5535. max_rounds--;
  5536. if (rounds != NULL) {
  5537. *rounds += 1;
  5538. }
  5539. }
  5540. if (!handshake_complete || failing_c || failing_s) {
  5541. return TEST_FAIL;
  5542. }
  5543. return TEST_SUCCESS;
  5544. }
  5545. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5546. {
  5547. EXPECT_DECLS;
  5548. char input[1024];
  5549. int idx = 0;
  5550. const char* msg_c = "hello wolfssl!";
  5551. int msglen_c = (int)XSTRLEN(msg_c);
  5552. const char* msg_s = "I hear you fa shizzle!";
  5553. int msglen_s = (int)XSTRLEN(msg_s);
  5554. if (ctx->c_msg != NULL) {
  5555. msg_c = ctx->c_msg;
  5556. msglen_c = ctx->c_msglen;
  5557. }
  5558. if (ctx->s_msg != NULL) {
  5559. msg_s = ctx->s_msg;
  5560. msglen_s = ctx->s_msglen;
  5561. }
  5562. wolfSSL_SetLoggingPrefix("client");
  5563. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5564. wolfSSL_SetLoggingPrefix("server");
  5565. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5566. if (idx >= 0) {
  5567. input[idx] = '\0';
  5568. }
  5569. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5570. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5571. ctx->s_cb.return_code = EXPECT_RESULT();
  5572. wolfSSL_SetLoggingPrefix("client");
  5573. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5574. wolfSSL_SetLoggingPrefix(NULL);
  5575. if (idx >= 0) {
  5576. input[idx] = '\0';
  5577. }
  5578. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5579. ctx->c_cb.return_code = EXPECT_RESULT();
  5580. if (ctx->c_cb.on_result != NULL) {
  5581. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5582. }
  5583. if (ctx->s_cb.on_result != NULL) {
  5584. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5585. }
  5586. return EXPECT_RESULT();
  5587. }
  5588. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5589. {
  5590. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5591. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5592. if (ctx->c_cb.on_cleanup != NULL) {
  5593. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5594. }
  5595. if (ctx->s_cb.on_cleanup != NULL) {
  5596. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5597. }
  5598. wolfSSL_shutdown(ctx->s_ssl);
  5599. wolfSSL_shutdown(ctx->c_ssl);
  5600. wolfSSL_free(ctx->s_ssl);
  5601. wolfSSL_free(ctx->c_ssl);
  5602. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  5603. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  5604. }
  5605. if (!ctx->c_cb.isSharedCtx) {
  5606. wolfSSL_CTX_free(ctx->c_ctx);
  5607. ctx->c_ctx = NULL;
  5608. }
  5609. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  5610. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  5611. }
  5612. if (!ctx->s_cb.isSharedCtx) {
  5613. wolfSSL_CTX_free(ctx->s_ctx);
  5614. ctx->s_ctx = NULL;
  5615. }
  5616. if (!ctx->s_cb.ticNoInit) {
  5617. #if defined(HAVE_SESSION_TICKET) && \
  5618. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5619. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5620. OpenSSLTicketCleanup();
  5621. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5622. TicketCleanup();
  5623. #endif
  5624. #endif
  5625. }
  5626. }
  5627. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5628. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5629. {
  5630. EXPECT_DECLS;
  5631. struct test_ssl_memio_ctx test_ctx;
  5632. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5633. size_t msg_len;
  5634. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5635. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5636. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5637. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5638. test_ctx.c_ctx = client_cb->ctx;
  5639. test_ctx.s_ctx = server_cb->ctx;
  5640. test_ctx.c_cb.return_code = TEST_FAIL;
  5641. test_ctx.s_cb.return_code = TEST_FAIL;
  5642. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5643. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5644. if (client_on_handshake != NULL) {
  5645. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5646. TEST_SUCCESS);
  5647. }
  5648. if (client_cb->on_handshake != NULL) {
  5649. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  5650. TEST_SUCCESS);
  5651. }
  5652. if (server_cb->on_handshake != NULL) {
  5653. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  5654. TEST_SUCCESS);
  5655. }
  5656. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5657. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5658. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  5659. MD_MAX_SIZE);
  5660. ExpectIntGE(msg_len, 0);
  5661. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5662. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  5663. MD_MAX_SIZE);
  5664. ExpectIntGE(msg_len, 0);
  5665. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5666. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  5667. test_ssl_memio_cleanup(&test_ctx);
  5668. client_cb->return_code = test_ctx.c_cb.return_code;
  5669. client_cb->last_err = test_ctx.c_cb.last_err;
  5670. server_cb->return_code = test_ctx.s_cb.return_code;
  5671. server_cb->last_err = test_ctx.s_cb.last_err;
  5672. return EXPECT_RESULT();
  5673. }
  5674. #endif
  5675. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  5676. #ifdef WOLFSSL_SESSION_EXPORT
  5677. #ifdef WOLFSSL_DTLS
  5678. /* set up function for sending session information */
  5679. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  5680. {
  5681. WOLFSSL_CTX* ctx = NULL;
  5682. WOLFSSL* ssl = NULL;
  5683. AssertNotNull(inSsl);
  5684. AssertNotNull(buf);
  5685. AssertIntNE(0, sz);
  5686. /* Set ctx to DTLS 1.2 */
  5687. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  5688. AssertNotNull(ctx);
  5689. ssl = wolfSSL_new(ctx);
  5690. AssertNotNull(ssl);
  5691. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  5692. wolfSSL_free(ssl);
  5693. wolfSSL_CTX_free(ctx);
  5694. (void)userCtx;
  5695. return 0;
  5696. }
  5697. #endif
  5698. /* returns negative value on fail and positive (including 0) on success */
  5699. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  5700. {
  5701. int ret, err, loop_count, count, timeout = 10;
  5702. char msg[] = "I hear you fa shizzle!";
  5703. char input[1024];
  5704. loop_count = ((func_args*)args)->argc;
  5705. #ifdef WOLFSSL_ASYNC_CRYPT
  5706. err = 0; /* Reset error */
  5707. #endif
  5708. do {
  5709. #ifdef WOLFSSL_ASYNC_CRYPT
  5710. if (err == WC_PENDING_E) {
  5711. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5712. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5713. }
  5714. #endif
  5715. ret = wolfSSL_accept(ssl);
  5716. err = wolfSSL_get_error(ssl, 0);
  5717. if (err == WOLFSSL_ERROR_WANT_READ ||
  5718. err == WOLFSSL_ERROR_WANT_WRITE) {
  5719. int select_ret;
  5720. err = WC_PENDING_E;
  5721. select_ret = tcp_select(*sockfd, timeout);
  5722. if (select_ret == TEST_TIMEOUT) {
  5723. return WOLFSSL_FATAL_ERROR;
  5724. }
  5725. }
  5726. } while (err == WC_PENDING_E);
  5727. if (ret != WOLFSSL_SUCCESS) {
  5728. char buff[WOLFSSL_MAX_ERROR_SZ];
  5729. fprintf(stderr, "error = %d, %s\n", err,
  5730. wolfSSL_ERR_error_string(err, buff));
  5731. return ret;
  5732. }
  5733. for (count = 0; count < loop_count; count++) {
  5734. int select_ret;
  5735. select_ret = tcp_select(*sockfd, timeout);
  5736. if (select_ret == TEST_TIMEOUT) {
  5737. ret = WOLFSSL_FATAL_ERROR;
  5738. break;
  5739. }
  5740. do {
  5741. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5742. if (ret > 0) {
  5743. input[ret] = '\0';
  5744. fprintf(stderr, "Client message: %s\n", input);
  5745. }
  5746. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5747. do {
  5748. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  5749. return WOLFSSL_FATAL_ERROR;
  5750. }
  5751. err = wolfSSL_get_error(ssl, ret);
  5752. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5753. }
  5754. return ret;
  5755. }
  5756. #endif /* WOLFSSL_SESSION_EXPORT */
  5757. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  5758. {
  5759. SOCKET_T sockfd = 0;
  5760. SOCKET_T clientfd = 0;
  5761. word16 port;
  5762. callback_functions* cbf;
  5763. WOLFSSL_CTX* ctx = NULL;
  5764. WOLFSSL* ssl = NULL;
  5765. func_args* opts = (func_args*)args;
  5766. char msg[] = "I hear you fa shizzle!";
  5767. char input[1024];
  5768. int idx;
  5769. int ret, err = 0;
  5770. int sharedCtx = 0;
  5771. int doUdp = 0;
  5772. SOCKADDR_IN_T cliAddr;
  5773. socklen_t cliLen;
  5774. const char* certFile = svrCertFile;
  5775. const char* keyFile = svrKeyFile;
  5776. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5777. size_t msg_len = 0;
  5778. #endif
  5779. wolfSSL_SetLoggingPrefix("server");
  5780. #ifdef WOLFSSL_TIRTOS
  5781. fdOpenSession(Task_self());
  5782. #endif
  5783. opts->return_code = TEST_FAIL;
  5784. cbf = opts->callbacks;
  5785. if (cbf != NULL && cbf->ctx) {
  5786. ctx = cbf->ctx;
  5787. sharedCtx = 1;
  5788. }
  5789. else
  5790. {
  5791. WOLFSSL_METHOD* method = NULL;
  5792. if (cbf != NULL && cbf->method != NULL) {
  5793. method = cbf->method();
  5794. }
  5795. else {
  5796. method = wolfSSLv23_server_method();
  5797. }
  5798. ctx = wolfSSL_CTX_new(method);
  5799. }
  5800. if (ctx == NULL) {
  5801. /* Release the wait for TCP ready. */
  5802. signal_ready(opts->signal);
  5803. goto done;
  5804. }
  5805. if (cbf == NULL || !cbf->ticNoInit) {
  5806. #if defined(HAVE_SESSION_TICKET) && \
  5807. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5808. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5809. OpenSSLTicketInit();
  5810. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  5811. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5812. TicketInit();
  5813. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  5814. #endif
  5815. #endif
  5816. }
  5817. #if defined(USE_WINDOWS_API)
  5818. port = opts->signal->port;
  5819. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5820. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5821. /* Let tcp_listen assign port */
  5822. port = 0;
  5823. #else
  5824. /* Use default port */
  5825. port = wolfSSLPort;
  5826. #endif
  5827. if (cbf != NULL)
  5828. doUdp = cbf->doUdp;
  5829. /* do it here to detect failure */
  5830. tcp_accept(
  5831. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  5832. if (doUdp) {
  5833. cliLen = sizeof(cliAddr);
  5834. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  5835. (struct sockaddr*)&cliAddr, &cliLen);
  5836. AssertIntGT(idx, 0);
  5837. }
  5838. else {
  5839. CloseSocket(sockfd);
  5840. }
  5841. wolfSSL_CTX_set_verify(ctx,
  5842. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5843. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5844. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5845. #endif
  5846. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5847. != WOLFSSL_SUCCESS) {
  5848. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5849. goto done;
  5850. }
  5851. if (cbf != NULL && cbf->certPemFile != NULL)
  5852. certFile = cbf->certPemFile;
  5853. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5854. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5855. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5856. #else
  5857. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5858. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5859. #endif
  5860. /*err_sys("can't load server cert chain file, "
  5861. "Please run from wolfSSL home dir");*/
  5862. goto done;
  5863. }
  5864. if (cbf != NULL && cbf->keyPemFile != NULL)
  5865. keyFile = cbf->keyPemFile;
  5866. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5867. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5868. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5869. #else
  5870. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5871. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5872. #endif
  5873. /*err_sys("can't load server key file, "
  5874. "Please run from wolfSSL home dir");*/
  5875. goto done;
  5876. }
  5877. #ifdef HAVE_CRL
  5878. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5879. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5880. goto done;
  5881. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5882. != WOLFSSL_SUCCESS)
  5883. goto done;
  5884. }
  5885. #endif
  5886. /* call ctx setup callback */
  5887. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5888. cbf->ctx_ready(ctx);
  5889. }
  5890. ssl = wolfSSL_new(ctx);
  5891. if (ssl == NULL) {
  5892. goto done;
  5893. }
  5894. if (doUdp) {
  5895. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5896. if (err != WOLFSSL_SUCCESS)
  5897. goto done;
  5898. }
  5899. #ifdef WOLFSSL_SESSION_EXPORT
  5900. /* only add in more complex nonblocking case with session export tests */
  5901. if (args && opts->argc > 0) {
  5902. /* set as nonblock and time out for waiting on read/write */
  5903. tcp_set_nonblocking(&clientfd);
  5904. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5905. }
  5906. #endif
  5907. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5908. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5909. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5910. #else
  5911. if (wolfSSL_use_certificate_file(ssl, certFile,
  5912. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5913. #endif
  5914. /*err_sys("can't load server cert chain file, "
  5915. "Please run from wolfSSL home dir");*/
  5916. goto done;
  5917. }
  5918. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5919. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5920. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5921. #else
  5922. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5923. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5924. #endif
  5925. /*err_sys("can't load server key file, "
  5926. "Please run from wolfSSL home dir");*/
  5927. goto done;
  5928. }
  5929. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5930. /*err_sys("SSL_set_fd failed");*/
  5931. goto done;
  5932. }
  5933. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5934. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5935. #elif !defined(NO_DH)
  5936. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5937. #endif
  5938. /* call ssl setup callback */
  5939. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5940. cbf->ssl_ready(ssl);
  5941. }
  5942. #ifdef WOLFSSL_SESSION_EXPORT
  5943. /* only add in more complex nonblocking case with session export tests */
  5944. if (opts->argc > 0) {
  5945. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5946. if (ret >= 0) {
  5947. opts->return_code = TEST_SUCCESS;
  5948. }
  5949. #ifdef WOLFSSL_TIRTOS
  5950. Task_yield();
  5951. #endif
  5952. goto done;
  5953. }
  5954. #endif
  5955. #ifdef WOLFSSL_ASYNC_CRYPT
  5956. err = 0; /* Reset error */
  5957. #endif
  5958. do {
  5959. #ifdef WOLFSSL_ASYNC_CRYPT
  5960. if (err == WC_PENDING_E) {
  5961. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5962. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5963. }
  5964. #endif
  5965. ret = wolfSSL_negotiate(ssl);
  5966. err = wolfSSL_get_error(ssl, 0);
  5967. } while (err == WC_PENDING_E);
  5968. if (ret != WOLFSSL_SUCCESS) {
  5969. char buff[WOLFSSL_MAX_ERROR_SZ];
  5970. fprintf(stderr, "error = %d, %s\n", err,
  5971. wolfSSL_ERR_error_string(err, buff));
  5972. /*err_sys("SSL_accept failed");*/
  5973. goto done;
  5974. }
  5975. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5976. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5977. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  5978. AssertIntGE(msg_len, 0);
  5979. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5980. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  5981. AssertIntGE(msg_len, 0);
  5982. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5983. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5984. if (idx > 0) {
  5985. input[idx] = '\0';
  5986. fprintf(stderr, "Client message: %s\n", input);
  5987. }
  5988. else if (idx < 0) {
  5989. goto done;
  5990. }
  5991. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5992. /*err_sys("SSL_write failed");*/
  5993. goto done;
  5994. }
  5995. if (cbf != NULL && cbf->on_result != NULL)
  5996. cbf->on_result(ssl);
  5997. #ifdef WOLFSSL_TIRTOS
  5998. Task_yield();
  5999. #endif
  6000. opts->return_code = TEST_SUCCESS;
  6001. done:
  6002. if (cbf != NULL)
  6003. cbf->last_err = err;
  6004. if (cbf != NULL && cbf->on_cleanup != NULL)
  6005. cbf->on_cleanup(ssl);
  6006. wolfSSL_shutdown(ssl);
  6007. wolfSSL_free(ssl);
  6008. if (!sharedCtx)
  6009. wolfSSL_CTX_free(ctx);
  6010. CloseSocket(clientfd);
  6011. #ifdef WOLFSSL_TIRTOS
  6012. fdCloseSession(Task_self());
  6013. #endif
  6014. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6015. && defined(HAVE_THREAD_LS)
  6016. wc_ecc_fp_free(); /* free per thread cache */
  6017. #endif
  6018. if (cbf == NULL || !cbf->ticNoInit) {
  6019. #if defined(HAVE_SESSION_TICKET) && \
  6020. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6021. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6022. OpenSSLTicketCleanup();
  6023. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6024. TicketCleanup();
  6025. #endif
  6026. #endif
  6027. }
  6028. wolfSSL_SetLoggingPrefix(NULL);
  6029. WOLFSSL_RETURN_FROM_THREAD(0);
  6030. }
  6031. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6032. !defined(WOLFSSL_NO_TLS12)
  6033. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6034. {
  6035. SOCKET_T sockfd = 0;
  6036. SOCKET_T clientfd = 0;
  6037. word16 port;
  6038. callback_functions* cbf;
  6039. WOLFSSL_CTX* ctx = 0;
  6040. WOLFSSL* ssl = 0;
  6041. char msg[] = "I hear you fa shizzle!";
  6042. char input[1024];
  6043. int idx;
  6044. int ret, err = 0;
  6045. int sharedCtx = 0;
  6046. func_args* opts = (func_args*)args;
  6047. int loop_count = opts->argc;
  6048. int count = 0;
  6049. #ifdef WOLFSSL_TIRTOS
  6050. fdOpenSession(Task_self());
  6051. #endif
  6052. opts->return_code = TEST_FAIL;
  6053. cbf = opts->callbacks;
  6054. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6055. if (cbf != NULL && cbf->ctx) {
  6056. ctx = cbf->ctx;
  6057. sharedCtx = 1;
  6058. }
  6059. else
  6060. #endif
  6061. {
  6062. WOLFSSL_METHOD* method = NULL;
  6063. if (cbf != NULL && cbf->method != NULL) {
  6064. method = cbf->method();
  6065. }
  6066. else {
  6067. method = wolfSSLv23_server_method();
  6068. }
  6069. ctx = wolfSSL_CTX_new(method);
  6070. }
  6071. #if defined(USE_WINDOWS_API)
  6072. port = opts->signal->port;
  6073. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6074. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6075. /* Let tcp_listen assign port */
  6076. port = 0;
  6077. #else
  6078. /* Use default port */
  6079. port = wolfSSLPort;
  6080. #endif
  6081. wolfSSL_CTX_set_verify(ctx,
  6082. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6083. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6084. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6085. #endif
  6086. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6087. != WOLFSSL_SUCCESS) {
  6088. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6089. /* Release the wait for TCP ready. */
  6090. signal_ready(opts->signal);
  6091. goto done;
  6092. }
  6093. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6094. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6095. /*err_sys("can't load server cert chain file, "
  6096. "Please run from wolfSSL home dir");*/
  6097. /* Release the wait for TCP ready. */
  6098. signal_ready(opts->signal);
  6099. goto done;
  6100. }
  6101. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6102. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6103. /*err_sys("can't load server key file, "
  6104. "Please run from wolfSSL home dir");*/
  6105. /* Release the wait for TCP ready. */
  6106. signal_ready(opts->signal);
  6107. goto done;
  6108. }
  6109. /* call ctx setup callback */
  6110. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6111. cbf->ctx_ready(ctx);
  6112. }
  6113. while (count != loop_count) {
  6114. ssl = wolfSSL_new(ctx);
  6115. if (ssl == NULL) {
  6116. signal_ready(opts->signal);
  6117. goto done;
  6118. }
  6119. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6120. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6121. /*err_sys("can't load server cert chain file, "
  6122. "Please run from wolfSSL home dir");*/
  6123. /* Release the wait for TCP ready. */
  6124. signal_ready(opts->signal);
  6125. goto done;
  6126. }
  6127. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6128. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6129. /*err_sys("can't load server key file, "
  6130. "Please run from wolfSSL home dir");*/
  6131. /* Release the wait for TCP ready. */
  6132. signal_ready(opts->signal);
  6133. goto done;
  6134. }
  6135. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6136. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6137. #elif !defined(NO_DH)
  6138. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6139. #endif
  6140. /* call ssl setup callback */
  6141. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6142. cbf->ssl_ready(ssl);
  6143. }
  6144. /* do it here to detect failure */
  6145. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6146. 0);
  6147. CloseSocket(sockfd);
  6148. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6149. /*err_sys("SSL_set_fd failed");*/
  6150. goto done;
  6151. }
  6152. #ifdef WOLFSSL_ASYNC_CRYPT
  6153. err = 0; /* Reset error */
  6154. #endif
  6155. do {
  6156. #ifdef WOLFSSL_ASYNC_CRYPT
  6157. if (err == WC_PENDING_E) {
  6158. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6159. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6160. }
  6161. #endif
  6162. ret = wolfSSL_accept(ssl);
  6163. err = wolfSSL_get_error(ssl, 0);
  6164. } while (err == WC_PENDING_E);
  6165. if (ret != WOLFSSL_SUCCESS) {
  6166. char buff[WOLFSSL_MAX_ERROR_SZ];
  6167. fprintf(stderr, "error = %d, %s\n", err,
  6168. wolfSSL_ERR_error_string(err, buff));
  6169. /*err_sys("SSL_accept failed");*/
  6170. goto done;
  6171. }
  6172. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6173. if (idx > 0) {
  6174. input[idx] = '\0';
  6175. fprintf(stderr, "Client message: %s\n", input);
  6176. }
  6177. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6178. /*err_sys("SSL_write failed");*/
  6179. goto done;
  6180. }
  6181. /* free ssl for this connection */
  6182. wolfSSL_shutdown(ssl);
  6183. wolfSSL_free(ssl); ssl = NULL;
  6184. CloseSocket(clientfd);
  6185. count++;
  6186. }
  6187. #ifdef WOLFSSL_TIRTOS
  6188. Task_yield();
  6189. #endif
  6190. opts->return_code = TEST_SUCCESS;
  6191. done:
  6192. if (ssl != NULL) {
  6193. wolfSSL_shutdown(ssl);
  6194. wolfSSL_free(ssl);
  6195. }
  6196. if (!sharedCtx)
  6197. wolfSSL_CTX_free(ctx);
  6198. CloseSocket(clientfd);
  6199. #ifdef WOLFSSL_TIRTOS
  6200. fdCloseSession(Task_self());
  6201. #endif
  6202. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6203. && defined(HAVE_THREAD_LS)
  6204. wc_ecc_fp_free(); /* free per thread cache */
  6205. #endif
  6206. WOLFSSL_RETURN_FROM_THREAD(0);
  6207. }
  6208. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6209. static int test_client_nofail(void* args, cbType cb)
  6210. {
  6211. #if !defined(NO_WOLFSSL_CLIENT)
  6212. SOCKET_T sockfd = 0;
  6213. callback_functions* cbf;
  6214. WOLFSSL_CTX* ctx = 0;
  6215. WOLFSSL* ssl = 0;
  6216. WOLFSSL_CIPHER* cipher;
  6217. char msg[64] = "hello wolfssl!";
  6218. char reply[1024];
  6219. int input;
  6220. int msgSz = (int)XSTRLEN(msg);
  6221. int ret, err = 0;
  6222. int cipherSuite;
  6223. int sharedCtx = 0;
  6224. int doUdp = 0;
  6225. const char* cipherName1, *cipherName2;
  6226. wolfSSL_SetLoggingPrefix("client");
  6227. #ifdef WOLFSSL_TIRTOS
  6228. fdOpenSession(Task_self());
  6229. #endif
  6230. ((func_args*)args)->return_code = TEST_FAIL;
  6231. cbf = ((func_args*)args)->callbacks;
  6232. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6233. if (cbf != NULL && cbf->ctx) {
  6234. ctx = cbf->ctx;
  6235. sharedCtx = cbf->isSharedCtx;
  6236. }
  6237. else
  6238. #endif
  6239. {
  6240. WOLFSSL_METHOD* method = NULL;
  6241. if (cbf != NULL && cbf->method != NULL) {
  6242. method = cbf->method();
  6243. }
  6244. else {
  6245. method = wolfSSLv23_client_method();
  6246. }
  6247. ctx = wolfSSL_CTX_new(method);
  6248. }
  6249. if (cbf != NULL)
  6250. doUdp = cbf->doUdp;
  6251. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6252. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6253. #endif
  6254. /* Do connect here so server detects failures */
  6255. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6256. doUdp, 0, NULL);
  6257. /* Connect the socket so that we don't have to set the peer later on */
  6258. if (doUdp)
  6259. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6260. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6261. {
  6262. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6263. goto done;
  6264. }
  6265. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6266. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6267. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6268. #else
  6269. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6270. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6271. #endif
  6272. /*err_sys("can't load client cert file, "
  6273. "Please run from wolfSSL home dir");*/
  6274. goto done;
  6275. }
  6276. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6277. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6278. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6279. #else
  6280. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6281. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6282. #endif
  6283. /*err_sys("can't load client key file, "
  6284. "Please run from wolfSSL home dir");*/
  6285. goto done;
  6286. }
  6287. #ifdef HAVE_CRL
  6288. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6289. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6290. goto done;
  6291. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6292. != WOLFSSL_SUCCESS)
  6293. goto done;
  6294. }
  6295. #endif
  6296. /* call ctx setup callback */
  6297. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6298. cbf->ctx_ready(ctx);
  6299. }
  6300. ssl = wolfSSL_new(ctx);
  6301. if (ssl == NULL) {
  6302. goto done;
  6303. }
  6304. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6305. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6306. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6307. #else
  6308. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6309. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6310. #endif
  6311. /*err_sys("can't load client cert file, "
  6312. "Please run from wolfSSL home dir");*/
  6313. goto done;
  6314. }
  6315. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6316. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6317. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6318. #else
  6319. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6320. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6321. #endif
  6322. /*err_sys("can't load client key file, "
  6323. "Please run from wolfSSL home dir");*/
  6324. goto done;
  6325. }
  6326. if (!doUdp) {
  6327. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6328. /*err_sys("SSL_set_fd failed");*/
  6329. goto done;
  6330. }
  6331. }
  6332. else {
  6333. #ifdef WOLFSSL_DTLS
  6334. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6335. /*err_sys("SSL_set_fd failed");*/
  6336. goto done;
  6337. }
  6338. #else
  6339. goto done;
  6340. #endif
  6341. }
  6342. /* call ssl setup callback */
  6343. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6344. cbf->ssl_ready(ssl);
  6345. }
  6346. #ifdef WOLFSSL_ASYNC_CRYPT
  6347. err = 0; /* Reset error */
  6348. #endif
  6349. do {
  6350. #ifdef WOLFSSL_ASYNC_CRYPT
  6351. if (err == WC_PENDING_E) {
  6352. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6353. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6354. }
  6355. #endif
  6356. ret = wolfSSL_negotiate(ssl);
  6357. err = wolfSSL_get_error(ssl, 0);
  6358. } while (err == WC_PENDING_E);
  6359. if (ret != WOLFSSL_SUCCESS) {
  6360. char buff[WOLFSSL_MAX_ERROR_SZ];
  6361. fprintf(stderr, "error = %d, %s\n", err,
  6362. wolfSSL_ERR_error_string(err, buff));
  6363. /*err_sys("SSL_connect failed");*/
  6364. goto done;
  6365. }
  6366. /* test the various get cipher methods */
  6367. /* Internal cipher suite names */
  6368. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6369. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6370. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6371. (cipherSuite >> 8), cipherSuite & 0xFF);
  6372. AssertStrEQ(cipherName1, cipherName2);
  6373. /* IANA Cipher Suites Names */
  6374. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6375. then it's the internal cipher suite name */
  6376. cipher = wolfSSL_get_current_cipher(ssl);
  6377. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6378. cipherName2 = wolfSSL_get_cipher(ssl);
  6379. AssertStrEQ(cipherName1, cipherName2);
  6380. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6381. !defined(WOLFSSL_QT)
  6382. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6383. (cipherSuite >> 8), cipherSuite & 0xFF);
  6384. AssertStrEQ(cipherName1, cipherName2);
  6385. #endif
  6386. if (cb != NULL)
  6387. (cb)(ctx, ssl);
  6388. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6389. /*err_sys("SSL_write failed");*/
  6390. goto done;
  6391. }
  6392. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6393. if (input > 0) {
  6394. reply[input] = '\0';
  6395. fprintf(stderr, "Server response: %s\n", reply);
  6396. }
  6397. if (cbf != NULL && cbf->on_result != NULL)
  6398. cbf->on_result(ssl);
  6399. ((func_args*)args)->return_code = TEST_SUCCESS;
  6400. done:
  6401. if (cbf != NULL)
  6402. cbf->last_err = err;
  6403. if (cbf != NULL && cbf->on_cleanup != NULL)
  6404. cbf->on_cleanup(ssl);
  6405. wolfSSL_free(ssl);
  6406. if (!sharedCtx)
  6407. wolfSSL_CTX_free(ctx);
  6408. CloseSocket(sockfd);
  6409. #ifdef WOLFSSL_TIRTOS
  6410. fdCloseSession(Task_self());
  6411. #endif
  6412. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6413. && defined(HAVE_THREAD_LS)
  6414. wc_ecc_fp_free(); /* free per thread cache */
  6415. #endif
  6416. #else
  6417. (void)args;
  6418. (void)cb;
  6419. #endif /* !NO_WOLFSSL_CLIENT */
  6420. wolfSSL_SetLoggingPrefix(NULL);
  6421. return 0;
  6422. }
  6423. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6424. callback_functions* server_cb, cbType client_on_handshake)
  6425. {
  6426. func_args client_args;
  6427. func_args server_args;
  6428. tcp_ready ready;
  6429. THREAD_TYPE serverThread;
  6430. XMEMSET(&client_args, 0, sizeof(func_args));
  6431. XMEMSET(&server_args, 0, sizeof(func_args));
  6432. #ifdef WOLFSSL_TIRTOS
  6433. fdOpenSession(Task_self());
  6434. #endif
  6435. StartTCP();
  6436. InitTcpReady(&ready);
  6437. #if defined(USE_WINDOWS_API)
  6438. /* use RNG to get random port if using windows */
  6439. ready.port = GetRandomPort();
  6440. #endif
  6441. server_args.signal = &ready;
  6442. server_args.callbacks = server_cb;
  6443. client_args.signal = &ready;
  6444. client_args.callbacks = client_cb;
  6445. start_thread(test_server_nofail, &server_args, &serverThread);
  6446. wait_tcp_ready(&server_args);
  6447. test_client_nofail(&client_args, client_on_handshake);
  6448. join_thread(serverThread);
  6449. client_cb->return_code = client_args.return_code;
  6450. server_cb->return_code = server_args.return_code;
  6451. FreeTcpReady(&ready);
  6452. #ifdef WOLFSSL_TIRTOS
  6453. fdOpenSession(Task_self());
  6454. #endif
  6455. }
  6456. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6457. callback_functions* server_cb)
  6458. {
  6459. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6460. }
  6461. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6462. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6463. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6464. void* server_args)
  6465. {
  6466. SOCKET_T sockfd = 0;
  6467. callback_functions* cbf;
  6468. WOLFSSL_CTX* ctx = 0;
  6469. WOLFSSL* ssl = 0;
  6470. WOLFSSL_SESSION* session = NULL;
  6471. char msg[64] = "hello wolfssl!";
  6472. char reply[1024];
  6473. int input;
  6474. int msgSz = (int)XSTRLEN(msg);
  6475. int ret, err = 0;
  6476. int sharedCtx = 0;
  6477. #ifdef WOLFSSL_TIRTOS
  6478. fdOpenSession(Task_self());
  6479. #endif
  6480. ((func_args*)args)->return_code = TEST_FAIL;
  6481. cbf = ((func_args*)args)->callbacks;
  6482. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6483. if (cbf != NULL && cbf->ctx) {
  6484. ctx = cbf->ctx;
  6485. sharedCtx = 1;
  6486. }
  6487. else
  6488. #endif
  6489. {
  6490. WOLFSSL_METHOD* method = NULL;
  6491. if (cbf != NULL && cbf->method != NULL) {
  6492. method = cbf->method();
  6493. }
  6494. else {
  6495. method = wolfSSLv23_client_method();
  6496. }
  6497. ctx = wolfSSL_CTX_new(method);
  6498. }
  6499. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6500. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6501. #endif
  6502. /* Do connect here so server detects failures */
  6503. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6504. 0, 0, NULL);
  6505. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6506. WOLFSSL_SUCCESS) {
  6507. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6508. goto done;
  6509. }
  6510. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6511. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6512. /*err_sys("can't load client cert file, "
  6513. "Please run from wolfSSL home dir");*/
  6514. goto done;
  6515. }
  6516. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6517. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6518. /*err_sys("can't load client key file, "
  6519. "Please run from wolfSSL home dir");*/
  6520. goto done;
  6521. }
  6522. /* call ctx setup callback */
  6523. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6524. cbf->ctx_ready(ctx);
  6525. }
  6526. ssl = wolfSSL_new(ctx);
  6527. if (ssl == NULL) {
  6528. goto done;
  6529. }
  6530. /* keep handshake resources for re-using WOLFSSL obj */
  6531. wolfSSL_KeepArrays(ssl);
  6532. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6533. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6534. goto done;
  6535. }
  6536. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6537. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6538. /*err_sys("can't load client cert file, "
  6539. "Please run from wolfSSL home dir");*/
  6540. goto done;
  6541. }
  6542. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6543. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6544. /*err_sys("can't load client key file, "
  6545. "Please run from wolfSSL home dir");*/
  6546. goto done;
  6547. }
  6548. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6549. /*err_sys("SSL_set_fd failed");*/
  6550. goto done;
  6551. }
  6552. /* call ssl setup callback */
  6553. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6554. cbf->ssl_ready(ssl);
  6555. }
  6556. #ifdef WOLFSSL_ASYNC_CRYPT
  6557. err = 0; /* Reset error */
  6558. #endif
  6559. do {
  6560. #ifdef WOLFSSL_ASYNC_CRYPT
  6561. if (err == WC_PENDING_E) {
  6562. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6563. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6564. }
  6565. #endif
  6566. ret = wolfSSL_connect(ssl);
  6567. err = wolfSSL_get_error(ssl, 0);
  6568. } while (err == WC_PENDING_E);
  6569. if (ret != WOLFSSL_SUCCESS) {
  6570. char buff[WOLFSSL_MAX_ERROR_SZ];
  6571. fprintf(stderr, "error = %d, %s\n", err,
  6572. wolfSSL_ERR_error_string(err, buff));
  6573. /*err_sys("SSL_connect failed");*/
  6574. goto done;
  6575. }
  6576. /* Build first session */
  6577. if (cb != NULL)
  6578. cb(ctx, ssl);
  6579. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6580. /*err_sys("SSL_write failed");*/
  6581. goto done;
  6582. }
  6583. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6584. if (input > 0) {
  6585. reply[input] = '\0';
  6586. fprintf(stderr, "Server response: %s\n", reply);
  6587. }
  6588. /* Session Resumption by re-using WOLFSSL object */
  6589. wolfSSL_set_quiet_shutdown(ssl, 1);
  6590. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6591. /* err_sys ("SSL shutdown failed"); */
  6592. goto done;
  6593. }
  6594. session = wolfSSL_get1_session(ssl);
  6595. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6596. wolfSSL_SESSION_free(session);
  6597. /* err_sys ("SSL_clear failed"); */
  6598. goto done;
  6599. }
  6600. wolfSSL_set_session(ssl, session);
  6601. wolfSSL_SESSION_free(session);
  6602. session = NULL;
  6603. /* close socket once */
  6604. CloseSocket(sockfd);
  6605. sockfd = 0;
  6606. /* wait until server ready */
  6607. wait_tcp_ready((func_args*)server_args);
  6608. fprintf(stderr, "session resumption\n");
  6609. /* Do re-connect */
  6610. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6611. 0, 0, NULL);
  6612. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6613. /*err_sys("SSL_set_fd failed");*/
  6614. goto done;
  6615. }
  6616. #ifdef WOLFSSL_ASYNC_CRYPT
  6617. err = 0; /* Reset error */
  6618. #endif
  6619. do {
  6620. #ifdef WOLFSSL_ASYNC_CRYPT
  6621. if (err == WC_PENDING_E) {
  6622. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6623. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6624. }
  6625. #endif
  6626. ret = wolfSSL_connect(ssl);
  6627. err = wolfSSL_get_error(ssl, 0);
  6628. } while (err == WC_PENDING_E);
  6629. if (ret != WOLFSSL_SUCCESS) {
  6630. char buff[WOLFSSL_MAX_ERROR_SZ];
  6631. fprintf(stderr, "error = %d, %s\n", err,
  6632. wolfSSL_ERR_error_string(err, buff));
  6633. /*err_sys("SSL_connect failed");*/
  6634. goto done;
  6635. }
  6636. /* Build first session */
  6637. if (cb != NULL)
  6638. cb(ctx, ssl);
  6639. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6640. /*err_sys("SSL_write failed");*/
  6641. goto done;
  6642. }
  6643. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6644. if (input > 0) {
  6645. reply[input] = '\0';
  6646. fprintf(stderr, "Server response: %s\n", reply);
  6647. }
  6648. ((func_args*)args)->return_code = TEST_SUCCESS;
  6649. done:
  6650. wolfSSL_free(ssl);
  6651. if (!sharedCtx)
  6652. wolfSSL_CTX_free(ctx);
  6653. CloseSocket(sockfd);
  6654. #ifdef WOLFSSL_TIRTOS
  6655. fdCloseSession(Task_self());
  6656. #endif
  6657. return;
  6658. }
  6659. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6660. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  6661. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  6662. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  6663. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  6664. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  6665. #define HAVE_ALPN_PROTOS_SUPPORT
  6666. #endif
  6667. /* Generic TLS client / server with callbacks for API unit tests
  6668. * Used by SNI / ALPN / crypto callback helper functions */
  6669. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  6670. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  6671. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  6672. #define ENABLE_TLS_CALLBACK_TEST
  6673. #endif
  6674. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  6675. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  6676. /* TLS server for API unit testing - generic */
  6677. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  6678. {
  6679. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6680. WOLFSSL_CTX* ctx = NULL;
  6681. WOLFSSL* ssl = NULL;
  6682. SOCKET_T sfd = 0;
  6683. SOCKET_T cfd = 0;
  6684. word16 port;
  6685. char msg[] = "I hear you fa shizzle!";
  6686. int len = (int) XSTRLEN(msg);
  6687. char input[1024];
  6688. int idx;
  6689. int ret, err = 0;
  6690. ((func_args*)args)->return_code = TEST_FAIL;
  6691. #if defined(USE_WINDOWS_API)
  6692. port = ((func_args*)args)->signal->port;
  6693. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6694. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6695. /* Let tcp_listen assign port */
  6696. port = 0;
  6697. #else
  6698. /* Use default port */
  6699. port = wolfSSLPort;
  6700. #endif
  6701. #ifdef WOLFSSL_DTLS
  6702. if (callbacks->method == wolfDTLS_server_method
  6703. #ifdef WOLFSSL_STATIC_MEMORY
  6704. || callbacks->method_ex == wolfDTLS_server_method_ex
  6705. #endif
  6706. #ifndef NO_OLD_TLS
  6707. || callbacks->method == wolfDTLSv1_server_method
  6708. #ifdef WOLFSSL_STATIC_MEMORY
  6709. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  6710. #endif
  6711. #endif
  6712. #ifndef WOLFSSL_NO_TLS12
  6713. || callbacks->method == wolfDTLSv1_2_server_method
  6714. #ifdef WOLFSSL_STATIC_MEMORY
  6715. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  6716. #endif
  6717. #endif
  6718. #ifdef WOLFSSL_DTLS13
  6719. || callbacks->method == wolfDTLSv1_3_server_method
  6720. #ifdef WOLFSSL_STATIC_MEMORY
  6721. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  6722. #endif
  6723. #endif
  6724. ) {
  6725. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  6726. }
  6727. else
  6728. #endif
  6729. {
  6730. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6731. }
  6732. #ifdef WOLFSSL_STATIC_MEMORY
  6733. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6734. callbacks->memSz > 0) {
  6735. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6736. callbacks->mem, callbacks->memSz, 0, 1);
  6737. if (ret != WOLFSSL_SUCCESS) {
  6738. fprintf(stderr, "CTX static new failed %d\n", ret);
  6739. goto cleanup;
  6740. }
  6741. }
  6742. #else
  6743. ctx = wolfSSL_CTX_new(callbacks->method());
  6744. #endif
  6745. if (ctx == NULL) {
  6746. fprintf(stderr, "CTX new failed\n");
  6747. goto cleanup;
  6748. }
  6749. /* set defaults */
  6750. if (callbacks->caPemFile == NULL)
  6751. callbacks->caPemFile = cliCertFile;
  6752. if (callbacks->certPemFile == NULL)
  6753. callbacks->certPemFile = svrCertFile;
  6754. if (callbacks->keyPemFile == NULL)
  6755. callbacks->keyPemFile = svrKeyFile;
  6756. #ifdef WOLFSSL_TIRTOS
  6757. fdOpenSession(Task_self());
  6758. #endif
  6759. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6760. wolfSSL_CTX_set_verify(ctx,
  6761. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6762. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6763. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6764. #endif
  6765. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  6766. if (callbacks->method == wolfDTLSv1_2_server_method) {
  6767. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  6768. goto cleanup;
  6769. }
  6770. #endif
  6771. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6772. WOLFSSL_SUCCESS) {
  6773. goto cleanup;
  6774. }
  6775. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6776. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6777. goto cleanup;
  6778. }
  6779. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6780. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6781. goto cleanup;
  6782. }
  6783. #ifdef HAVE_CRL
  6784. if (callbacks->crlPemFile != NULL) {
  6785. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6786. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6787. goto cleanup;
  6788. }
  6789. }
  6790. #endif
  6791. if (callbacks->ctx_ready)
  6792. callbacks->ctx_ready(ctx);
  6793. ssl = wolfSSL_new(ctx);
  6794. if (ssl == NULL) {
  6795. fprintf(stderr, "SSL new failed\n");
  6796. goto cleanup;
  6797. }
  6798. if (wolfSSL_dtls(ssl)) {
  6799. SOCKADDR_IN_T cliAddr;
  6800. socklen_t cliLen;
  6801. cliLen = sizeof(cliAddr);
  6802. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  6803. (struct sockaddr*)&cliAddr, &cliLen);
  6804. if (idx <= 0) {
  6805. goto cleanup;
  6806. }
  6807. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6808. }
  6809. else {
  6810. CloseSocket(sfd);
  6811. }
  6812. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  6813. goto cleanup;
  6814. }
  6815. if (callbacks->loadToSSL) {
  6816. wolfSSL_SetDevId(ssl, callbacks->devId);
  6817. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6818. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6819. goto cleanup;
  6820. }
  6821. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6822. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6823. goto cleanup;
  6824. }
  6825. }
  6826. #ifdef NO_PSK
  6827. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6828. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6829. #elif !defined(NO_DH)
  6830. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6831. #endif
  6832. #endif
  6833. if (callbacks->ssl_ready)
  6834. callbacks->ssl_ready(ssl);
  6835. #ifdef WOLFSSL_ASYNC_CRYPT
  6836. err = 0; /* Reset error */
  6837. #endif
  6838. do {
  6839. #ifdef WOLFSSL_ASYNC_CRYPT
  6840. if (err == WC_PENDING_E) {
  6841. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6842. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6843. }
  6844. #endif
  6845. ret = wolfSSL_accept(ssl);
  6846. err = wolfSSL_get_error(ssl, ret);
  6847. } while (err == WC_PENDING_E);
  6848. if (ret != WOLFSSL_SUCCESS) {
  6849. char buff[WOLFSSL_MAX_ERROR_SZ];
  6850. fprintf(stderr, "accept error = %d, %s\n", err,
  6851. wolfSSL_ERR_error_string(err, buff));
  6852. /*err_sys("SSL_accept failed");*/
  6853. }
  6854. else {
  6855. #ifdef WOLFSSL_ASYNC_CRYPT
  6856. err = 0; /* Reset error */
  6857. #endif
  6858. do {
  6859. #ifdef WOLFSSL_ASYNC_CRYPT
  6860. if (err == WC_PENDING_E) {
  6861. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6862. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6863. }
  6864. #endif
  6865. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6866. err = wolfSSL_get_error(ssl, idx);
  6867. } while (err == WC_PENDING_E);
  6868. if (idx > 0) {
  6869. input[idx] = 0;
  6870. fprintf(stderr, "Client message: %s\n", input);
  6871. }
  6872. #ifdef WOLFSSL_ASYNC_CRYPT
  6873. err = 0; /* Reset error */
  6874. #endif
  6875. do {
  6876. #ifdef WOLFSSL_ASYNC_CRYPT
  6877. if (err == WC_PENDING_E) {
  6878. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6879. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6880. }
  6881. #endif
  6882. ret = wolfSSL_write(ssl, msg, len);
  6883. err = wolfSSL_get_error(ssl, ret);
  6884. } while (err == WC_PENDING_E);
  6885. if (len != ret) {
  6886. goto cleanup;
  6887. }
  6888. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6889. defined(WOLFSSL_DTLS)
  6890. if (wolfSSL_dtls(ssl)) {
  6891. byte* import;
  6892. word32 sz;
  6893. wolfSSL_dtls_export(ssl, NULL, &sz);
  6894. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6895. if (import == NULL) {
  6896. goto cleanup;
  6897. }
  6898. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6899. if (idx < 0) {
  6900. goto cleanup;
  6901. }
  6902. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6903. goto cleanup;
  6904. }
  6905. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6906. }
  6907. #endif
  6908. #ifdef WOLFSSL_TIRTOS
  6909. Task_yield();
  6910. #endif
  6911. ((func_args*)args)->return_code = TEST_SUCCESS;
  6912. }
  6913. if (callbacks->on_result)
  6914. callbacks->on_result(ssl);
  6915. wolfSSL_shutdown(ssl);
  6916. cleanup:
  6917. wolfSSL_free(ssl);
  6918. wolfSSL_CTX_free(ctx);
  6919. CloseSocket(cfd);
  6920. #ifdef WOLFSSL_TIRTOS
  6921. fdCloseSession(Task_self());
  6922. #endif
  6923. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6924. && defined(HAVE_THREAD_LS)
  6925. wc_ecc_fp_free(); /* free per thread cache */
  6926. #endif
  6927. WOLFSSL_RETURN_FROM_THREAD(0);
  6928. }
  6929. /* TLS Client for API unit testing - generic */
  6930. static void run_wolfssl_client(void* args)
  6931. {
  6932. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6933. WOLFSSL_CTX* ctx = NULL;
  6934. WOLFSSL* ssl = NULL;
  6935. SOCKET_T sfd = 0;
  6936. char msg[] = "hello wolfssl server!";
  6937. int len = (int) XSTRLEN(msg);
  6938. char input[1024];
  6939. int ret, err = 0;
  6940. ((func_args*)args)->return_code = TEST_FAIL;
  6941. /* set defaults */
  6942. if (callbacks->caPemFile == NULL)
  6943. callbacks->caPemFile = caCertFile;
  6944. if (callbacks->certPemFile == NULL)
  6945. callbacks->certPemFile = cliCertFile;
  6946. if (callbacks->keyPemFile == NULL)
  6947. callbacks->keyPemFile = cliKeyFile;
  6948. #ifdef WOLFSSL_STATIC_MEMORY
  6949. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6950. callbacks->memSz > 0) {
  6951. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6952. callbacks->mem, callbacks->memSz, 0, 1);
  6953. if (ret != WOLFSSL_SUCCESS) {
  6954. fprintf(stderr, "CTX static new failed %d\n", ret);
  6955. goto cleanup;
  6956. }
  6957. }
  6958. #else
  6959. ctx = wolfSSL_CTX_new(callbacks->method());
  6960. #endif
  6961. if (ctx == NULL) {
  6962. fprintf(stderr, "CTX new failed\n");
  6963. goto cleanup;
  6964. }
  6965. #ifdef WOLFSSL_TIRTOS
  6966. fdOpenSession(Task_self());
  6967. #endif
  6968. if (!callbacks->loadToSSL) {
  6969. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6970. }
  6971. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6972. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6973. #endif
  6974. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6975. WOLFSSL_SUCCESS) {
  6976. goto cleanup;
  6977. }
  6978. if (!callbacks->loadToSSL) {
  6979. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6980. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6981. goto cleanup;
  6982. }
  6983. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6984. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6985. goto cleanup;
  6986. }
  6987. }
  6988. #ifdef HAVE_CRL
  6989. if (callbacks->crlPemFile != NULL) {
  6990. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6991. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6992. goto cleanup;
  6993. }
  6994. }
  6995. #endif
  6996. if (callbacks->ctx_ready)
  6997. callbacks->ctx_ready(ctx);
  6998. ssl = wolfSSL_new(ctx);
  6999. if (wolfSSL_dtls(ssl)) {
  7000. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7001. 1, 0, ssl);
  7002. }
  7003. else {
  7004. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7005. 0, 0, ssl);
  7006. }
  7007. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7008. goto cleanup;
  7009. }
  7010. if (callbacks->loadToSSL) {
  7011. wolfSSL_SetDevId(ssl, callbacks->devId);
  7012. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7013. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7014. goto cleanup;
  7015. }
  7016. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7017. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7018. goto cleanup;
  7019. }
  7020. }
  7021. if (callbacks->ssl_ready)
  7022. callbacks->ssl_ready(ssl);
  7023. #ifdef WOLFSSL_ASYNC_CRYPT
  7024. err = 0; /* Reset error */
  7025. #endif
  7026. do {
  7027. #ifdef WOLFSSL_ASYNC_CRYPT
  7028. if (err == WC_PENDING_E) {
  7029. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7030. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7031. }
  7032. #endif
  7033. ret = wolfSSL_connect(ssl);
  7034. err = wolfSSL_get_error(ssl, ret);
  7035. } while (err == WC_PENDING_E);
  7036. if (ret != WOLFSSL_SUCCESS) {
  7037. char buff[WOLFSSL_MAX_ERROR_SZ];
  7038. fprintf(stderr, "error = %d, %s\n", err,
  7039. wolfSSL_ERR_error_string(err, buff));
  7040. /*err_sys("SSL_connect failed");*/
  7041. }
  7042. else {
  7043. #ifdef WOLFSSL_ASYNC_CRYPT
  7044. err = 0; /* Reset error */
  7045. #endif
  7046. do {
  7047. #ifdef WOLFSSL_ASYNC_CRYPT
  7048. if (err == WC_PENDING_E) {
  7049. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7050. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7051. }
  7052. #endif
  7053. ret = wolfSSL_write(ssl, msg, len);
  7054. err = wolfSSL_get_error(ssl, ret);
  7055. } while (err == WC_PENDING_E);
  7056. if (len != ret)
  7057. goto cleanup;
  7058. #ifdef WOLFSSL_ASYNC_CRYPT
  7059. err = 0; /* Reset error */
  7060. #endif
  7061. do {
  7062. #ifdef WOLFSSL_ASYNC_CRYPT
  7063. if (err == WC_PENDING_E) {
  7064. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7065. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7066. }
  7067. #endif
  7068. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7069. err = wolfSSL_get_error(ssl, ret);
  7070. } while (err == WC_PENDING_E);
  7071. if (ret > 0) {
  7072. input[ret] = '\0'; /* null term */
  7073. fprintf(stderr, "Server response: %s\n", input);
  7074. }
  7075. ((func_args*)args)->return_code = TEST_SUCCESS;
  7076. }
  7077. if (callbacks->on_result)
  7078. callbacks->on_result(ssl);
  7079. cleanup:
  7080. wolfSSL_free(ssl);
  7081. wolfSSL_CTX_free(ctx);
  7082. CloseSocket(sfd);
  7083. #ifdef WOLFSSL_TIRTOS
  7084. fdCloseSession(Task_self());
  7085. #endif
  7086. }
  7087. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7088. static int test_wolfSSL_read_write(void)
  7089. {
  7090. /* The unit testing for read and write shall happen simultaneously, since
  7091. * one can't do anything with one without the other. (Except for a failure
  7092. * test case.) This function will call all the others that will set up,
  7093. * execute, and report their test findings.
  7094. *
  7095. * Set up the success case first. This function will become the template
  7096. * for the other tests. This should eventually be renamed
  7097. *
  7098. * The success case isn't interesting, how can this fail?
  7099. * - Do not give the client context a CA certificate. The connect should
  7100. * fail. Do not need server for this?
  7101. * - Using NULL for the ssl object on server. Do not need client for this.
  7102. * - Using NULL for the ssl object on client. Do not need server for this.
  7103. * - Good ssl objects for client and server. Client write() without server
  7104. * read().
  7105. * - Good ssl objects for client and server. Server write() without client
  7106. * read().
  7107. * - Forgetting the password callback?
  7108. */
  7109. tcp_ready ready;
  7110. func_args client_args;
  7111. func_args server_args;
  7112. THREAD_TYPE serverThread;
  7113. EXPECT_DECLS;
  7114. XMEMSET(&client_args, 0, sizeof(func_args));
  7115. XMEMSET(&server_args, 0, sizeof(func_args));
  7116. #ifdef WOLFSSL_TIRTOS
  7117. fdOpenSession(Task_self());
  7118. #endif
  7119. StartTCP();
  7120. InitTcpReady(&ready);
  7121. #if defined(USE_WINDOWS_API)
  7122. /* use RNG to get random port if using windows */
  7123. ready.port = GetRandomPort();
  7124. #endif
  7125. server_args.signal = &ready;
  7126. client_args.signal = &ready;
  7127. start_thread(test_server_nofail, &server_args, &serverThread);
  7128. wait_tcp_ready(&server_args);
  7129. test_client_nofail(&client_args, NULL);
  7130. join_thread(serverThread);
  7131. ExpectTrue(client_args.return_code);
  7132. ExpectTrue(server_args.return_code);
  7133. FreeTcpReady(&ready);
  7134. #ifdef WOLFSSL_TIRTOS
  7135. fdOpenSession(Task_self());
  7136. #endif
  7137. return EXPECT_RESULT();
  7138. }
  7139. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7140. {
  7141. EXPECT_DECLS;
  7142. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7143. !defined(WOLFSSL_NO_TLS12)
  7144. /* The unit test for session resumption by re-using WOLFSSL object.
  7145. * WOLFSSL object is not cleared after first session. It reuse the object
  7146. * for second connection.
  7147. */
  7148. tcp_ready ready;
  7149. func_args client_args;
  7150. func_args server_args;
  7151. THREAD_TYPE serverThread;
  7152. callback_functions client_cbf;
  7153. callback_functions server_cbf;
  7154. XMEMSET(&client_args, 0, sizeof(func_args));
  7155. XMEMSET(&server_args, 0, sizeof(func_args));
  7156. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7157. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7158. #ifdef WOLFSSL_TIRTOS
  7159. fdOpenSession(Task_self());
  7160. #endif
  7161. StartTCP();
  7162. InitTcpReady(&ready);
  7163. #if defined(USE_WINDOWS_API)
  7164. /* use RNG to get random port if using windows */
  7165. ready.port = GetRandomPort();
  7166. #endif
  7167. client_cbf.method = wolfTLSv1_2_client_method;
  7168. server_cbf.method = wolfTLSv1_2_server_method;
  7169. client_args.callbacks = &client_cbf;
  7170. server_args.callbacks = &server_cbf;
  7171. server_args.signal = &ready;
  7172. client_args.signal = &ready;
  7173. /* the var is used for loop number */
  7174. server_args.argc = 2;
  7175. start_thread(test_server_loop, &server_args, &serverThread);
  7176. wait_tcp_ready(&server_args);
  7177. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7178. join_thread(serverThread);
  7179. ExpectTrue(client_args.return_code);
  7180. ExpectTrue(server_args.return_code);
  7181. FreeTcpReady(&ready);
  7182. #ifdef WOLFSSL_TIRTOS
  7183. fdOpenSession(Task_self());
  7184. #endif
  7185. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7186. * !defined(WOLFSSL_TLS13) */
  7187. return EXPECT_RESULT();
  7188. }
  7189. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7190. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7191. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7192. WOLFSSL_CTX* ctx)
  7193. {
  7194. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7195. myVerifyAction = VERIFY_USE_PREVERFIY;
  7196. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7197. return TEST_SUCCESS;
  7198. }
  7199. #endif
  7200. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7201. {
  7202. EXPECT_DECLS;
  7203. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7204. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7205. test_ssl_cbf client_cbf;
  7206. test_ssl_cbf server_cbf;
  7207. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7208. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7209. #ifdef WOLFSSL_TLS13
  7210. client_cbf.method = wolfTLSv1_3_client_method;
  7211. #endif /* WOLFSSL_TLS13 */
  7212. client_cbf.ctx_ready =
  7213. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7214. /* test case 1 verify depth is equal to peer chain */
  7215. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7216. &server_cbf, NULL), TEST_SUCCESS);
  7217. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7218. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7219. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7220. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7221. return EXPECT_RESULT();
  7222. }
  7223. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7224. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7225. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7226. WOLFSSL_CTX* ctx)
  7227. {
  7228. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7229. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7230. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7231. return TEST_SUCCESS;
  7232. }
  7233. #endif
  7234. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7235. {
  7236. EXPECT_DECLS;
  7237. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7238. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7239. test_ssl_cbf client_cbf;
  7240. test_ssl_cbf server_cbf;
  7241. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7242. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7243. #ifdef WOLFSSL_TLS13
  7244. client_cbf.method = wolfTLSv1_3_client_method;
  7245. #endif /* WOLFSSL_TLS13 */
  7246. client_cbf.ctx_ready =
  7247. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7248. /* test case 2
  7249. * verify depth is zero, number of peer's chain is 2.
  7250. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7251. * callback.
  7252. */
  7253. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7254. &server_cbf, NULL), TEST_SUCCESS);
  7255. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7256. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7257. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7258. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7259. return EXPECT_RESULT();
  7260. }
  7261. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7262. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7263. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7264. WOLFSSL_CTX* ctx)
  7265. {
  7266. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7267. myVerifyAction = VERIFY_USE_PREVERFIY;
  7268. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7269. return TEST_SUCCESS;
  7270. }
  7271. #endif
  7272. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7273. {
  7274. EXPECT_DECLS;
  7275. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7276. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7277. test_ssl_cbf client_cbf;
  7278. test_ssl_cbf server_cbf;
  7279. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7280. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7281. #ifdef WOLFSSL_TLS13
  7282. client_cbf.method = wolfTLSv1_3_client_method;
  7283. #endif /* WOLFSSL_TLS13 */
  7284. client_cbf.ctx_ready =
  7285. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7286. /* test case 3
  7287. * verify depth is zero, number of peer's chain is 2
  7288. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7289. * therefore, handshake becomes failure.
  7290. */
  7291. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7292. &server_cbf, NULL), TEST_FAIL);
  7293. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7294. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7295. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7296. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7297. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7298. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7299. return EXPECT_RESULT();
  7300. }
  7301. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7302. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7303. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7304. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7305. {
  7306. EXPECT_DECLS;
  7307. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7308. return EXPECT_RESULT();
  7309. }
  7310. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7311. {
  7312. EXPECT_DECLS;
  7313. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7314. return EXPECT_RESULT();
  7315. }
  7316. #endif
  7317. static int test_wolfSSL_CTX_set_cipher_list(void)
  7318. {
  7319. EXPECT_DECLS;
  7320. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7321. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7322. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7323. WOLFSSL_CTX* ctxClient = NULL;
  7324. WOLFSSL* sslClient = NULL;
  7325. test_ssl_cbf client_cbf;
  7326. test_ssl_cbf server_cbf;
  7327. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7328. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7329. server_cbf.method = wolfTLSv1_2_server_method;
  7330. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7331. client_cbf.method = wolfTLSv1_2_client_method;
  7332. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7333. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7334. &server_cbf, NULL), TEST_SUCCESS);
  7335. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7336. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7337. /* check with cipher string that has '+' */
  7338. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7339. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7340. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7341. /* check for the existence of an ECDHE ECDSA cipher suite */
  7342. if (EXPECT_SUCCESS()) {
  7343. int i = 0;
  7344. int found = 0;
  7345. const char* suite;
  7346. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7347. WOLFSSL_CIPHER* current;
  7348. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7349. do {
  7350. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7351. if (current) {
  7352. suite = wolfSSL_CIPHER_get_name(current);
  7353. if (suite && XSTRSTR(suite, "ECDSA")) {
  7354. found = 1;
  7355. break;
  7356. }
  7357. }
  7358. } while (current);
  7359. ExpectIntEQ(found, 1);
  7360. }
  7361. wolfSSL_free(sslClient);
  7362. wolfSSL_CTX_free(ctxClient);
  7363. #endif
  7364. return EXPECT_RESULT();
  7365. }
  7366. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7367. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7368. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7369. WOLFSSL* ssl)
  7370. {
  7371. EXPECT_DECLS;
  7372. size_t msg_len;
  7373. (void)ctx;
  7374. /* get_finished test */
  7375. /* 1. get own sent message */
  7376. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7377. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7378. ExpectIntGE(msg_len, 0);
  7379. /* 2. get peer message */
  7380. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7381. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7382. ExpectIntGE(msg_len, 0);
  7383. return EXPECT_RESULT();
  7384. }
  7385. #endif
  7386. static int test_wolfSSL_get_finished(void)
  7387. {
  7388. EXPECT_DECLS;
  7389. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7390. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7391. test_ssl_cbf client_cbf;
  7392. test_ssl_cbf server_cbf;
  7393. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7394. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7395. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7396. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7397. TEST_SUCCESS);
  7398. /* test received msg vs sent msg */
  7399. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7400. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7401. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7402. return EXPECT_RESULT();
  7403. }
  7404. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7405. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7406. !defined(NO_SESSION_CACHE)
  7407. /* Sessions to restore/store */
  7408. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7409. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7410. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7411. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7412. {
  7413. /* Don't store sessions. Lookup is still enabled. */
  7414. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7415. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7416. #ifdef OPENSSL_EXTRA
  7417. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7418. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7419. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7420. #endif
  7421. /* Require both peers to provide certs */
  7422. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7423. }
  7424. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7425. {
  7426. WOLFSSL_SESSION** sess;
  7427. if (wolfSSL_is_server(ssl))
  7428. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7429. else
  7430. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7431. if (*sess == NULL) {
  7432. #ifdef NO_SESSION_CACHE_REF
  7433. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7434. #else
  7435. /* Test for backwards compatibility */
  7436. if (wolfSSL_is_server(ssl)) {
  7437. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7438. }
  7439. else {
  7440. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7441. }
  7442. #endif
  7443. /* Now save the session in the internal store to make it available
  7444. * for lookup. For TLS 1.3, we can't save the session without
  7445. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7446. * session from cache. */
  7447. if (wolfSSL_is_server(ssl)
  7448. #ifndef WOLFSSL_TICKET_HAVE_ID
  7449. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7450. #endif
  7451. )
  7452. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7453. *sess), WOLFSSL_SUCCESS);
  7454. }
  7455. else {
  7456. /* If we have a session retrieved then remaining connections should be
  7457. * resuming on that session */
  7458. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7459. }
  7460. /* Save CTX to be able to decrypt tickets */
  7461. if (wolfSSL_is_server(ssl) &&
  7462. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7463. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7464. = wolfSSL_get_SSL_CTX(ssl));
  7465. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7466. WOLFSSL_SUCCESS);
  7467. }
  7468. #ifdef SESSION_CERTS
  7469. #ifndef WOLFSSL_TICKET_HAVE_ID
  7470. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7471. wolfSSL_session_reused(ssl))
  7472. #endif
  7473. {
  7474. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7475. * for all connections. TLS 1.3 only has tickets so if we don't
  7476. * include the session id in the ticket then the certificates
  7477. * will not be available on resumption. */
  7478. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7479. AssertNotNull(peer);
  7480. wolfSSL_X509_free(peer);
  7481. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7482. #ifdef OPENSSL_EXTRA
  7483. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7484. #endif
  7485. }
  7486. #endif /* SESSION_CERTS */
  7487. }
  7488. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7489. {
  7490. /* Set the session to reuse for the client */
  7491. AssertIntEQ(wolfSSL_set_session(ssl,
  7492. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7493. }
  7494. #endif
  7495. static int test_wolfSSL_CTX_add_session(void)
  7496. {
  7497. EXPECT_DECLS;
  7498. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7499. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7500. !defined(NO_SESSION_CACHE)
  7501. tcp_ready ready;
  7502. func_args client_args;
  7503. func_args server_args;
  7504. THREAD_TYPE serverThread;
  7505. callback_functions client_cb;
  7506. callback_functions server_cb;
  7507. method_provider methods[][2] = {
  7508. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7509. !defined(NO_DES3))
  7510. /* Without AES there are almost no ciphersuites available. This leads
  7511. * to no ciphersuites being available and an error. */
  7512. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7513. #endif
  7514. #ifndef WOLFSSL_NO_TLS12
  7515. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7516. #endif
  7517. /* Needs the default ticket callback since it is tied to the
  7518. * connection context and this makes it easy to carry over the ticket
  7519. * crypto context between connections */
  7520. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7521. defined(HAVE_SESSION_TICKET)
  7522. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7523. #endif
  7524. };
  7525. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7526. size_t i, j;
  7527. for (i = 0; i < methodsLen; i++) {
  7528. /* First run creates a connection while the second+ run will attempt
  7529. * to resume the connection. The trick is that the internal cache
  7530. * is turned off. wolfSSL_CTX_add_session should put the session in
  7531. * the cache anyway. */
  7532. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7533. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7534. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7535. #ifdef NO_SESSION_CACHE_REF
  7536. for (j = 0; j < 4; j++) {
  7537. #else
  7538. /* The session may be overwritten in this case. Do only one resumption
  7539. * to stop this test from failing intermittently. */
  7540. for (j = 0; j < 2; j++) {
  7541. #endif
  7542. #ifdef WOLFSSL_TIRTOS
  7543. fdOpenSession(Task_self());
  7544. #endif
  7545. StartTCP();
  7546. InitTcpReady(&ready);
  7547. XMEMSET(&client_args, 0, sizeof(func_args));
  7548. XMEMSET(&server_args, 0, sizeof(func_args));
  7549. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7550. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7551. client_cb.method = methods[i][0];
  7552. server_cb.method = methods[i][1];
  7553. server_args.signal = &ready;
  7554. server_args.callbacks = &server_cb;
  7555. client_args.signal = &ready;
  7556. client_args.callbacks = &client_cb;
  7557. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7558. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7559. server_cb.isSharedCtx = 1;
  7560. }
  7561. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7562. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7563. if (j != 0)
  7564. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7565. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7566. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7567. server_cb.ticNoInit = 1; /* Use default builtin */
  7568. start_thread(test_server_nofail, &server_args, &serverThread);
  7569. wait_tcp_ready(&server_args);
  7570. test_client_nofail(&client_args, NULL);
  7571. join_thread(serverThread);
  7572. ExpectTrue(client_args.return_code);
  7573. ExpectTrue(server_args.return_code);
  7574. FreeTcpReady(&ready);
  7575. if (EXPECT_FAIL())
  7576. break;
  7577. }
  7578. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7579. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7580. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7581. if (EXPECT_FAIL())
  7582. break;
  7583. }
  7584. #endif
  7585. return EXPECT_RESULT();
  7586. }
  7587. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7588. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7589. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7590. defined(HAVE_SESSION_TICKET) && \
  7591. !defined(TITAN_SESSION_CACHE) && \
  7592. !defined(HUGE_SESSION_CACHE) && \
  7593. !defined(BIG_SESSION_CACHE) && \
  7594. !defined(MEDIUM_SESSION_CACHE)
  7595. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7596. /* Sessions to restore/store */
  7597. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7598. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7599. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7600. static int twcase_new_session_called = 0;
  7601. static int twcase_remove_session_called = 0;
  7602. static int twcase_get_session_called = 0;
  7603. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7604. #define SESSION_CACHE_SIZE 33
  7605. typedef struct {
  7606. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7607. WOLFSSL_SESSION* value;
  7608. } hashTable_entry;
  7609. typedef struct {
  7610. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7611. size_t capacity; /* size of entries */
  7612. size_t length; /* number of items in the hash table */
  7613. wolfSSL_Mutex htLock; /* lock */
  7614. }hashTable;
  7615. static hashTable server_sessionCache;
  7616. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7617. {
  7618. int i;
  7619. unsigned int len;
  7620. (void)ssl;
  7621. /*
  7622. * This example uses a hash table.
  7623. * Steps you should take for a non-demo code:
  7624. * - acquire a lock for the file named according to the session id
  7625. * - open the file
  7626. * - encrypt and write the SSL_SESSION object to the file
  7627. * - release the lock
  7628. *
  7629. * Return:
  7630. * 0: The callback does not wish to hold a reference of the sess
  7631. * 1: The callback wants to hold a reference of the sess. The callback is
  7632. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7633. */
  7634. if (sess == NULL)
  7635. return 0;
  7636. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7637. return 0;
  7638. }
  7639. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7640. if (server_sessionCache.entries[i].value == NULL) {
  7641. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7642. server_sessionCache.entries[i].value = sess;
  7643. server_sessionCache.length++;
  7644. break;
  7645. }
  7646. }
  7647. ++twcase_new_session_called;
  7648. wc_UnLockMutex(&server_sessionCache.htLock);
  7649. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  7650. twcase_new_session_called);
  7651. return 1;
  7652. }
  7653. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  7654. {
  7655. int i;
  7656. (void)ctx;
  7657. (void)sess;
  7658. if (sess == NULL)
  7659. return;
  7660. /*
  7661. * This example uses a hash table.
  7662. * Steps you should take for a non-demo code:
  7663. * - acquire a lock for the file named according to the session id
  7664. * - remove the file
  7665. * - release the lock
  7666. */
  7667. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7668. return;
  7669. }
  7670. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7671. if (server_sessionCache.entries[i].key != NULL &&
  7672. XMEMCMP(server_sessionCache.entries[i].key,
  7673. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7674. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7675. server_sessionCache.entries[i].value = NULL;
  7676. server_sessionCache.entries[i].key = NULL;
  7677. server_sessionCache.length--;
  7678. break;
  7679. }
  7680. }
  7681. ++twcase_remove_session_called;
  7682. wc_UnLockMutex(&server_sessionCache.htLock);
  7683. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  7684. twcase_remove_session_called);
  7685. }
  7686. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  7687. const unsigned char *id, int len, int *ref)
  7688. {
  7689. int i;
  7690. (void)ssl;
  7691. (void)id;
  7692. (void)len;
  7693. /*
  7694. * This example uses a hash table.
  7695. * Steps you should take for a non-demo code:
  7696. * - acquire a lock for the file named according to the session id in the
  7697. * 2nd arg
  7698. * - read and decrypt contents of file and create a new SSL_SESSION
  7699. * - object release the lock
  7700. * - return the new session object
  7701. */
  7702. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  7703. ++twcase_get_session_called);
  7704. /* This callback want to retain a copy of the object. If we want wolfSSL to
  7705. * be responsible for the pointer then set to 0. */
  7706. *ref = 1;
  7707. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7708. if (server_sessionCache.entries[i].key != NULL &&
  7709. XMEMCMP(server_sessionCache.entries[i].key, id,
  7710. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7711. return server_sessionCache.entries[i].value;
  7712. }
  7713. }
  7714. return NULL;
  7715. }
  7716. static int twcase_get_sessionCb_cleanup(void)
  7717. {
  7718. int i;
  7719. int cnt = 0;
  7720. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  7721. * for freeing sessions */
  7722. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7723. if (server_sessionCache.entries[i].value != NULL) {
  7724. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7725. cnt++;
  7726. }
  7727. }
  7728. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  7729. cnt);
  7730. return TEST_SUCCESS;
  7731. }
  7732. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  7733. {
  7734. EXPECT_DECLS;
  7735. /* off - Disable internal cache */
  7736. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7737. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7738. #ifdef OPENSSL_EXTRA
  7739. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7740. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7741. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7742. #endif
  7743. /* off - Do not setup external cache */
  7744. /* Require both peers to provide certs */
  7745. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7746. return EXPECT_RESULT();
  7747. }
  7748. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  7749. {
  7750. /* on - internal cache is on by default */
  7751. /* off - Do not setup external cache */
  7752. /* Require both peers to provide certs */
  7753. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7754. return TEST_SUCCESS;
  7755. }
  7756. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  7757. {
  7758. EXPECT_DECLS;
  7759. /* off - Disable internal cache */
  7760. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7761. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7762. #ifdef OPENSSL_EXTRA
  7763. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7764. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7765. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7766. #endif
  7767. /* on - Enable external cache */
  7768. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7769. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7770. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7771. /* Require both peers to provide certs */
  7772. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7773. return EXPECT_RESULT();
  7774. }
  7775. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  7776. {
  7777. /* on - internal cache is on by default */
  7778. /* on - Enable external cache */
  7779. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7780. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7781. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7782. /* Require both peers to provide certs */
  7783. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7784. return TEST_SUCCESS;
  7785. }
  7786. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  7787. {
  7788. /* on - internal cache is on by default */
  7789. /* on - Enable external cache */
  7790. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7791. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7792. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7793. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  7794. /* Require both peers to provide certs */
  7795. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7796. return TEST_SUCCESS;
  7797. }
  7798. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7799. {
  7800. EXPECT_DECLS;
  7801. WOLFSSL_SESSION** sess;
  7802. if (wolfSSL_is_server(ssl))
  7803. sess = &twcase_server_first_session_ptr;
  7804. else
  7805. return TEST_SUCCESS;
  7806. if (*sess == NULL) {
  7807. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7808. /* Now save the session in the internal store to make it available
  7809. * for lookup. For TLS 1.3, we can't save the session without
  7810. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7811. * session from cache. */
  7812. if (wolfSSL_is_server(ssl)
  7813. #ifndef WOLFSSL_TICKET_HAVE_ID
  7814. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7815. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7816. #endif
  7817. ) {
  7818. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7819. *sess), WOLFSSL_SUCCESS);
  7820. }
  7821. }
  7822. /* Save CTX to be able to decrypt tickets */
  7823. if (twcase_server_current_ctx_ptr == NULL) {
  7824. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7825. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7826. WOLFSSL_SUCCESS);
  7827. }
  7828. #ifdef SESSION_CERTS
  7829. #ifndef WOLFSSL_TICKET_HAVE_ID
  7830. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7831. wolfSSL_session_reused(ssl))
  7832. #endif
  7833. {
  7834. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7835. * for all connections. TLS 1.3 only has tickets so if we don't
  7836. * include the session id in the ticket then the certificates
  7837. * will not be available on resumption. */
  7838. WOLFSSL_X509* peer = NULL;
  7839. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7840. wolfSSL_X509_free(peer);
  7841. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7842. }
  7843. #endif
  7844. return EXPECT_RESULT();
  7845. }
  7846. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7847. {
  7848. EXPECT_DECLS;
  7849. WOLFSSL_SESSION** sess;
  7850. sess = &twcase_client_first_session_ptr;
  7851. if (*sess == NULL) {
  7852. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7853. }
  7854. else {
  7855. /* If we have a session retrieved then remaining connections should be
  7856. * resuming on that session */
  7857. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7858. }
  7859. #ifdef SESSION_CERTS
  7860. #ifndef WOLFSSL_TICKET_HAVE_ID
  7861. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7862. wolfSSL_session_reused(ssl))
  7863. #endif
  7864. {
  7865. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7866. ExpectNotNull(peer);
  7867. wolfSSL_X509_free(peer);
  7868. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7869. #ifdef OPENSSL_EXTRA
  7870. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7871. #endif
  7872. }
  7873. #endif
  7874. return EXPECT_RESULT();
  7875. }
  7876. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7877. {
  7878. EXPECT_DECLS;
  7879. /* Set the session to reuse for the client */
  7880. ExpectNotNull(ssl);
  7881. ExpectNotNull(twcase_client_first_session_ptr);
  7882. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7883. WOLFSSL_SUCCESS);
  7884. return EXPECT_RESULT();
  7885. }
  7886. struct test_add_session_ext_params {
  7887. method_provider client_meth;
  7888. method_provider server_meth;
  7889. const char* tls_version;
  7890. };
  7891. static int test_wolfSSL_CTX_add_session_ext(
  7892. struct test_add_session_ext_params* param)
  7893. {
  7894. EXPECT_DECLS;
  7895. /* Test the default 33 sessions */
  7896. int j;
  7897. /* Clear cache before starting */
  7898. wolfSSL_CTX_flush_sessions(NULL, -1);
  7899. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7900. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7901. return BAD_MUTEX_E;
  7902. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7903. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  7904. for (j = 0; j < 5; j++) {
  7905. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  7906. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  7907. test_ssl_cbf client_cb;
  7908. test_ssl_cbf server_cb;
  7909. (void)dtls;
  7910. /* Test five cache configurations */
  7911. twcase_client_first_session_ptr = NULL;
  7912. twcase_server_first_session_ptr = NULL;
  7913. twcase_server_current_ctx_ptr = NULL;
  7914. twcase_new_session_called = 0;
  7915. twcase_remove_session_called = 0;
  7916. twcase_get_session_called = 0;
  7917. /* connection 1 - first connection */
  7918. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  7919. XMEMSET(&client_cb, 0, sizeof(client_cb));
  7920. XMEMSET(&server_cb, 0, sizeof(server_cb));
  7921. client_cb.method = param->client_meth;
  7922. server_cb.method = param->server_meth;
  7923. if (dtls)
  7924. client_cb.doUdp = server_cb.doUdp = 1;
  7925. /* Setup internal and external cache */
  7926. switch (j) {
  7927. case 0:
  7928. /* SSL_OP_NO_TICKET stateful ticket case */
  7929. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7930. break;
  7931. case 1:
  7932. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7933. break;
  7934. case 2:
  7935. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7936. break;
  7937. case 3:
  7938. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7939. break;
  7940. case 4:
  7941. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7942. break;
  7943. }
  7944. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7945. /* Add session to internal cache and save SSL session for testing */
  7946. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7947. /* Save client SSL session for testing */
  7948. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7949. server_cb.ticNoInit = 1; /* Use default builtin */
  7950. /* Don't free/release ctx */
  7951. server_cb.ctx = twcase_server_current_ctx_ptr;
  7952. server_cb.isSharedCtx = 1;
  7953. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7954. &server_cb, NULL), TEST_SUCCESS);
  7955. ExpectIntEQ(twcase_get_session_called, 0);
  7956. if (EXPECT_FAIL()) {
  7957. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7958. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7959. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7960. break;
  7961. }
  7962. switch (j) {
  7963. case 0:
  7964. case 1:
  7965. case 2:
  7966. /* cache cannot be searched with out a connection */
  7967. /* Add a new session */
  7968. ExpectIntEQ(twcase_new_session_called, 1);
  7969. /* In twcase_server_sess_ctx_pre_shutdown
  7970. * wolfSSL_CTX_add_session which evicts the existing session
  7971. * in cache and adds it back in */
  7972. ExpectIntLE(twcase_remove_session_called, 1);
  7973. break;
  7974. case 3:
  7975. case 4:
  7976. /* no external cache */
  7977. ExpectIntEQ(twcase_new_session_called, 0);
  7978. ExpectIntEQ(twcase_remove_session_called, 0);
  7979. break;
  7980. }
  7981. /* connection 2 - session resume */
  7982. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  7983. twcase_new_session_called = 0;
  7984. twcase_remove_session_called = 0;
  7985. twcase_get_session_called = 0;
  7986. server_cb.on_result = 0;
  7987. client_cb.on_result = 0;
  7988. server_cb.ticNoInit = 1; /* Use default builtin */
  7989. server_cb.ctx = twcase_server_current_ctx_ptr;
  7990. /* try session resumption */
  7991. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  7992. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7993. &server_cb, NULL), TEST_SUCCESS);
  7994. /* Clear cache before checking */
  7995. wolfSSL_CTX_flush_sessions(NULL, -1);
  7996. switch (j) {
  7997. case 0:
  7998. if (tls13) {
  7999. /* (D)TLSv1.3 stateful case */
  8000. /* cache hit */
  8001. /* DTLS accesses cache once for stateless parsing and
  8002. * once for stateful parsing */
  8003. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8004. /* (D)TLSv1.3 creates a new ticket,
  8005. * updates both internal and external cache */
  8006. ExpectIntEQ(twcase_new_session_called, 1);
  8007. /* A new session ID is created for a new ticket */
  8008. ExpectIntEQ(twcase_remove_session_called, 2);
  8009. }
  8010. else {
  8011. /* non (D)TLSv1.3 case, no update */
  8012. /* DTLS accesses cache once for stateless parsing and
  8013. * once for stateful parsing */
  8014. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8015. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8016. #else
  8017. ExpectIntEQ(twcase_get_session_called, 1);
  8018. #endif
  8019. ExpectIntEQ(twcase_new_session_called, 0);
  8020. /* Called on session added in
  8021. * twcase_server_sess_ctx_pre_shutdown */
  8022. ExpectIntEQ(twcase_remove_session_called, 1);
  8023. }
  8024. break;
  8025. case 1:
  8026. if (tls13) {
  8027. /* (D)TLSv1.3 case */
  8028. /* cache hit */
  8029. ExpectIntEQ(twcase_get_session_called, 1);
  8030. /* (D)TLSv1.3 creates a new ticket,
  8031. * updates both internal and external cache */
  8032. ExpectIntEQ(twcase_new_session_called, 1);
  8033. /* Called on session added in
  8034. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8035. ExpectIntEQ(twcase_remove_session_called, 1);
  8036. }
  8037. else {
  8038. /* non (D)TLSv1.3 case */
  8039. /* cache hit */
  8040. /* DTLS accesses cache once for stateless parsing and
  8041. * once for stateful parsing */
  8042. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8043. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8044. #else
  8045. ExpectIntEQ(twcase_get_session_called, 1);
  8046. #endif
  8047. ExpectIntEQ(twcase_new_session_called, 0);
  8048. /* Called on session added in
  8049. * twcase_server_sess_ctx_pre_shutdown */
  8050. ExpectIntEQ(twcase_remove_session_called, 1);
  8051. }
  8052. break;
  8053. case 2:
  8054. if (tls13) {
  8055. /* (D)TLSv1.3 case */
  8056. /* cache hit */
  8057. ExpectIntEQ(twcase_get_session_called, 1);
  8058. /* (D)TLSv1.3 creates a new ticket,
  8059. * updates both internal and external cache */
  8060. ExpectIntEQ(twcase_new_session_called, 1);
  8061. /* Called on session added in
  8062. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8063. ExpectIntEQ(twcase_remove_session_called, 1);
  8064. }
  8065. else {
  8066. /* non (D)TLSv1.3 case */
  8067. /* cache hit */
  8068. /* DTLS accesses cache once for stateless parsing and
  8069. * once for stateful parsing */
  8070. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8071. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8072. #else
  8073. ExpectIntEQ(twcase_get_session_called, 1);
  8074. #endif
  8075. ExpectIntEQ(twcase_new_session_called, 0);
  8076. /* Called on session added in
  8077. * twcase_server_sess_ctx_pre_shutdown */
  8078. ExpectIntEQ(twcase_remove_session_called, 1);
  8079. }
  8080. break;
  8081. case 3:
  8082. case 4:
  8083. /* no external cache */
  8084. ExpectIntEQ(twcase_get_session_called, 0);
  8085. ExpectIntEQ(twcase_new_session_called, 0);
  8086. ExpectIntEQ(twcase_remove_session_called, 0);
  8087. break;
  8088. }
  8089. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8090. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8091. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8092. if (EXPECT_FAIL())
  8093. break;
  8094. }
  8095. twcase_get_sessionCb_cleanup();
  8096. XMEMSET(&server_sessionCache.entries, 0,
  8097. sizeof(server_sessionCache.entries));
  8098. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8099. wc_FreeMutex(&server_sessionCache.htLock);
  8100. return EXPECT_RESULT();
  8101. }
  8102. #endif
  8103. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8104. {
  8105. EXPECT_DECLS;
  8106. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8107. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8108. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8109. defined(HAVE_SESSION_TICKET) && \
  8110. !defined(TITAN_SESSION_CACHE) && \
  8111. !defined(HUGE_SESSION_CACHE) && \
  8112. !defined(BIG_SESSION_CACHE) && \
  8113. !defined(MEDIUM_SESSION_CACHE)
  8114. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8115. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8116. struct test_add_session_ext_params param[1] = {
  8117. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8118. };
  8119. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8120. #endif
  8121. #endif
  8122. return EXPECT_RESULT();
  8123. }
  8124. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8125. {
  8126. EXPECT_DECLS;
  8127. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8128. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8129. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8130. defined(HAVE_SESSION_TICKET) && \
  8131. !defined(TITAN_SESSION_CACHE) && \
  8132. !defined(HUGE_SESSION_CACHE) && \
  8133. !defined(BIG_SESSION_CACHE) && \
  8134. !defined(MEDIUM_SESSION_CACHE)
  8135. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8136. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8137. #ifdef WOLFSSL_DTLS13
  8138. struct test_add_session_ext_params param[1] = {
  8139. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8140. };
  8141. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8142. #endif
  8143. #endif
  8144. #endif
  8145. return EXPECT_RESULT();
  8146. }
  8147. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8148. {
  8149. EXPECT_DECLS;
  8150. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8151. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8152. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8153. defined(HAVE_SESSION_TICKET) && \
  8154. !defined(TITAN_SESSION_CACHE) && \
  8155. !defined(HUGE_SESSION_CACHE) && \
  8156. !defined(BIG_SESSION_CACHE) && \
  8157. !defined(MEDIUM_SESSION_CACHE)
  8158. #ifndef WOLFSSL_NO_TLS12
  8159. struct test_add_session_ext_params param[1] = {
  8160. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8161. };
  8162. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8163. #endif
  8164. #endif
  8165. return EXPECT_RESULT();
  8166. }
  8167. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8168. {
  8169. EXPECT_DECLS;
  8170. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8171. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8172. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8173. defined(HAVE_SESSION_TICKET) && \
  8174. !defined(TITAN_SESSION_CACHE) && \
  8175. !defined(HUGE_SESSION_CACHE) && \
  8176. !defined(BIG_SESSION_CACHE) && \
  8177. !defined(MEDIUM_SESSION_CACHE)
  8178. #ifndef WOLFSSL_NO_TLS12
  8179. #ifdef WOLFSSL_DTLS
  8180. struct test_add_session_ext_params param[1] = {
  8181. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8182. };
  8183. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8184. #endif
  8185. #endif
  8186. #endif
  8187. return EXPECT_RESULT();
  8188. }
  8189. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8190. {
  8191. EXPECT_DECLS;
  8192. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8193. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8194. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8195. defined(HAVE_SESSION_TICKET) && \
  8196. !defined(TITAN_SESSION_CACHE) && \
  8197. !defined(HUGE_SESSION_CACHE) && \
  8198. !defined(BIG_SESSION_CACHE) && \
  8199. !defined(MEDIUM_SESSION_CACHE)
  8200. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8201. !defined(NO_DES3))
  8202. struct test_add_session_ext_params param[1] = {
  8203. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8204. };
  8205. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8206. #endif
  8207. #endif
  8208. return EXPECT_RESULT();
  8209. }
  8210. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8211. {
  8212. EXPECT_DECLS;
  8213. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8214. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8215. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8216. defined(HAVE_SESSION_TICKET) && \
  8217. !defined(TITAN_SESSION_CACHE) && \
  8218. !defined(HUGE_SESSION_CACHE) && \
  8219. !defined(BIG_SESSION_CACHE) && \
  8220. !defined(MEDIUM_SESSION_CACHE)
  8221. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8222. !defined(NO_DES3))
  8223. #ifdef WOLFSSL_DTLS
  8224. struct test_add_session_ext_params param[1] = {
  8225. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8226. };
  8227. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8228. #endif
  8229. #endif
  8230. #endif
  8231. return EXPECT_RESULT();
  8232. }
  8233. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8234. /* canned export of a session using older version 3 */
  8235. static unsigned char version_3[] = {
  8236. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8237. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8238. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8239. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8240. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8241. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8242. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8243. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8244. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8245. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8246. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8247. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8248. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8249. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8250. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8251. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8252. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8253. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8254. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8255. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8256. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8257. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8258. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8259. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8260. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8261. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8262. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8263. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8264. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8265. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8266. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8267. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8268. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8269. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8270. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8271. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8272. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8273. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8274. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8275. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8276. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8277. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8278. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8279. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8280. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8281. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8282. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8283. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8284. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8285. 0x2E, 0x31, 0xED, 0x4F
  8286. };
  8287. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8288. static int test_wolfSSL_dtls_export(void)
  8289. {
  8290. EXPECT_DECLS;
  8291. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8292. tcp_ready ready;
  8293. func_args client_args;
  8294. func_args server_args;
  8295. THREAD_TYPE serverThread;
  8296. callback_functions server_cbf;
  8297. callback_functions client_cbf;
  8298. #ifdef WOLFSSL_TIRTOS
  8299. fdOpenSession(Task_self());
  8300. #endif
  8301. InitTcpReady(&ready);
  8302. #if defined(USE_WINDOWS_API)
  8303. /* use RNG to get random port if using windows */
  8304. ready.port = GetRandomPort();
  8305. #endif
  8306. /* set using dtls */
  8307. XMEMSET(&client_args, 0, sizeof(func_args));
  8308. XMEMSET(&server_args, 0, sizeof(func_args));
  8309. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8310. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8311. server_cbf.method = wolfDTLSv1_2_server_method;
  8312. client_cbf.method = wolfDTLSv1_2_client_method;
  8313. server_args.callbacks = &server_cbf;
  8314. client_args.callbacks = &client_cbf;
  8315. server_args.signal = &ready;
  8316. client_args.signal = &ready;
  8317. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8318. wait_tcp_ready(&server_args);
  8319. run_wolfssl_client(&client_args);
  8320. join_thread(serverThread);
  8321. ExpectTrue(client_args.return_code);
  8322. ExpectTrue(server_args.return_code);
  8323. FreeTcpReady(&ready);
  8324. #ifdef WOLFSSL_TIRTOS
  8325. fdOpenSession(Task_self());
  8326. #endif
  8327. if (EXPECT_SUCCESS()) {
  8328. SOCKET_T sockfd = 0;
  8329. WOLFSSL_CTX* ctx = NULL;
  8330. WOLFSSL* ssl = NULL;
  8331. char msg[64] = "hello wolfssl!";
  8332. char reply[1024];
  8333. int msgSz = (int)XSTRLEN(msg);
  8334. byte *session, *window;
  8335. unsigned int sessionSz = 0;
  8336. unsigned int windowSz = 0;
  8337. #ifndef TEST_IPV6
  8338. struct sockaddr_in peerAddr;
  8339. #else
  8340. struct sockaddr_in6 peerAddr;
  8341. #endif /* TEST_IPV6 */
  8342. int i;
  8343. /* Set ctx to DTLS 1.2 */
  8344. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8345. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8346. /* test importing version 3 */
  8347. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8348. /* test importing bad length and bad version */
  8349. version_3[2] += 1;
  8350. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8351. version_3[2] -= 1; version_3[1] = 0XA0;
  8352. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8353. wolfSSL_free(ssl);
  8354. wolfSSL_CTX_free(ctx);
  8355. /* check storing client state after connection and storing window only */
  8356. #ifdef WOLFSSL_TIRTOS
  8357. fdOpenSession(Task_self());
  8358. #endif
  8359. InitTcpReady(&ready);
  8360. #if defined(USE_WINDOWS_API)
  8361. /* use RNG to get random port if using windows */
  8362. ready.port = GetRandomPort();
  8363. #endif
  8364. /* set using dtls */
  8365. XMEMSET(&server_args, 0, sizeof(func_args));
  8366. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8367. server_cbf.method = wolfDTLSv1_2_server_method;
  8368. server_cbf.doUdp = 1;
  8369. server_args.callbacks = &server_cbf;
  8370. server_args.argc = 3; /* set loop_count to 3 */
  8371. server_args.signal = &ready;
  8372. start_thread(test_server_nofail, &server_args, &serverThread);
  8373. wait_tcp_ready(&server_args);
  8374. /* create and connect with client */
  8375. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8376. ExpectIntEQ(WOLFSSL_SUCCESS,
  8377. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8378. ExpectIntEQ(WOLFSSL_SUCCESS,
  8379. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8380. ExpectIntEQ(WOLFSSL_SUCCESS,
  8381. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8382. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8383. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8384. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8385. /* store server information connected too */
  8386. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8387. #ifndef TEST_IPV6
  8388. peerAddr.sin_family = AF_INET;
  8389. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8390. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8391. #else
  8392. peerAddr.sin6_family = AF_INET6;
  8393. ExpectIntEQ(
  8394. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8395. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8396. #endif
  8397. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8398. WOLFSSL_SUCCESS);
  8399. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8400. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8401. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8402. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8403. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8404. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8405. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8406. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8407. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8408. wolfSSL_free(ssl);
  8409. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8410. /* restore state */
  8411. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8412. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8413. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8414. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8415. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8416. WOLFSSL_SUCCESS);
  8417. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8418. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8419. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8420. wolfSSL_free(ssl);
  8421. }
  8422. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8423. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8424. wolfSSL_CTX_free(ctx);
  8425. fprintf(stderr, "done and waiting for server\n");
  8426. join_thread(serverThread);
  8427. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8428. FreeTcpReady(&ready);
  8429. #ifdef WOLFSSL_TIRTOS
  8430. fdOpenSession(Task_self());
  8431. #endif
  8432. }
  8433. #endif
  8434. return EXPECT_RESULT();
  8435. }
  8436. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8437. #ifdef WOLFSSL_TLS13
  8438. static const byte canned_client_tls13_session[] = {
  8439. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8440. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8441. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8442. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8443. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8444. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8445. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8446. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8447. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8448. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8449. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8450. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8451. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8452. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8453. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8454. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8455. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8456. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8457. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8458. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8459. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8460. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8461. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8462. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8463. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8464. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8465. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8466. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8467. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8468. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8469. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8470. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8471. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8472. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8473. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8474. 0x00, 0x03
  8475. };
  8476. static const byte canned_server_tls13_session[] = {
  8477. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8478. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8479. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8480. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8481. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8482. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8483. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8484. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8485. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8486. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8487. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8488. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8489. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8490. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8491. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8492. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8493. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8494. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8495. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8496. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8497. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8498. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8499. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8500. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8501. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8502. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8503. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8504. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8505. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8506. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8507. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8508. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8509. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8510. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8511. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8512. 0x00, 0x04
  8513. };
  8514. #endif /* WOLFSSL_TLS13 */
  8515. static const byte canned_client_session[] = {
  8516. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8517. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8518. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8519. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8520. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8521. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8522. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8523. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8524. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8525. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8526. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8527. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8528. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8529. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8530. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8531. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8532. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8533. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8534. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8535. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8536. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8537. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8538. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8539. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8540. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8541. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8542. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8543. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8544. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8545. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8546. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8547. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8548. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8549. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8550. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8551. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8552. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8553. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8554. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8555. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8556. 0x00, 0x03
  8557. };
  8558. static const byte canned_server_session[] = {
  8559. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8560. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8561. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8562. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8563. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8564. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8565. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8566. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8567. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8568. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8569. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8570. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8571. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8572. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8573. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8574. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8575. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8576. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8577. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8578. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8579. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8580. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8581. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8582. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8583. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8584. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8585. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8586. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8587. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8588. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8589. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8590. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8591. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8592. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8593. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8594. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8595. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8596. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8597. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8598. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8599. 0x00, 0x04
  8600. };
  8601. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8602. {
  8603. SOCKET_T sockfd = 0;
  8604. SOCKET_T clientfd = 0;
  8605. word16 port;
  8606. callback_functions* cbf;
  8607. WOLFSSL_CTX* ctx = 0;
  8608. WOLFSSL* ssl = 0;
  8609. char msg[] = "I hear you fa shizzle!";
  8610. char input[1024];
  8611. int idx;
  8612. #ifdef WOLFSSL_TIRTOS
  8613. fdOpenSession(Task_self());
  8614. #endif
  8615. ((func_args*)args)->return_code = TEST_FAIL;
  8616. cbf = ((func_args*)args)->callbacks;
  8617. #if defined(USE_WINDOWS_API)
  8618. port = ((func_args*)args)->signal->port;
  8619. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8620. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8621. /* Let tcp_listen assign port */
  8622. port = 0;
  8623. #else
  8624. /* Use default port */
  8625. port = wolfSSLPort;
  8626. #endif
  8627. /* do it here to detect failure */
  8628. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8629. CloseSocket(sockfd);
  8630. {
  8631. WOLFSSL_METHOD* method = NULL;
  8632. if (cbf != NULL && cbf->method != NULL) {
  8633. method = cbf->method();
  8634. }
  8635. else {
  8636. method = wolfTLSv1_2_server_method();
  8637. }
  8638. ctx = wolfSSL_CTX_new(method);
  8639. }
  8640. if (ctx == NULL) {
  8641. goto done;
  8642. }
  8643. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8644. /* call ctx setup callback */
  8645. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8646. cbf->ctx_ready(ctx);
  8647. }
  8648. ssl = wolfSSL_new(ctx);
  8649. if (ssl == NULL) {
  8650. goto done;
  8651. }
  8652. wolfSSL_set_fd(ssl, clientfd);
  8653. /* call ssl setup callback */
  8654. if (cbf != NULL && cbf->ssl_ready != NULL) {
  8655. cbf->ssl_ready(ssl);
  8656. }
  8657. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  8658. if (idx > 0) {
  8659. input[idx] = '\0';
  8660. fprintf(stderr, "Client message export/import: %s\n", input);
  8661. }
  8662. else {
  8663. fprintf(stderr, "ret = %d error = %d\n", idx,
  8664. wolfSSL_get_error(ssl, idx));
  8665. goto done;
  8666. }
  8667. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  8668. /*err_sys("SSL_write failed");*/
  8669. WOLFSSL_RETURN_FROM_THREAD(0);
  8670. }
  8671. #ifdef WOLFSSL_TIRTOS
  8672. Task_yield();
  8673. #endif
  8674. ((func_args*)args)->return_code = TEST_SUCCESS;
  8675. done:
  8676. wolfSSL_shutdown(ssl);
  8677. wolfSSL_free(ssl);
  8678. wolfSSL_CTX_free(ctx);
  8679. CloseSocket(clientfd);
  8680. #ifdef WOLFSSL_TIRTOS
  8681. fdCloseSession(Task_self());
  8682. #endif
  8683. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8684. && defined(HAVE_THREAD_LS)
  8685. wc_ecc_fp_free(); /* free per thread cache */
  8686. #endif
  8687. #if defined(HAVE_SESSION_TICKET) && \
  8688. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  8689. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  8690. OpenSSLTicketCleanup();
  8691. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  8692. TicketCleanup();
  8693. #endif
  8694. #endif
  8695. WOLFSSL_RETURN_FROM_THREAD(0);
  8696. }
  8697. static void load_tls12_canned_server(WOLFSSL* ssl)
  8698. {
  8699. int clientfd = wolfSSL_get_fd(ssl);
  8700. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  8701. sizeof(canned_server_session)), sizeof(canned_server_session));
  8702. wolfSSL_set_fd(ssl, clientfd);
  8703. }
  8704. #ifdef WOLFSSL_TLS13
  8705. static void load_tls13_canned_server(WOLFSSL* ssl)
  8706. {
  8707. int clientfd = wolfSSL_get_fd(ssl);
  8708. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  8709. sizeof(canned_server_tls13_session)),
  8710. sizeof(canned_server_tls13_session));
  8711. wolfSSL_set_fd(ssl, clientfd);
  8712. }
  8713. #endif
  8714. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  8715. static int test_wolfSSL_tls_export_run(int v)
  8716. {
  8717. EXPECT_DECLS;
  8718. SOCKET_T sockfd = 0;
  8719. WOLFSSL_CTX* ctx = 0;
  8720. WOLFSSL* ssl = 0;
  8721. char msg[64] = "hello wolfssl!";
  8722. char reply[1024];
  8723. word32 replySz;
  8724. int msgSz = (int)XSTRLEN(msg);
  8725. const byte* clientSession = NULL;
  8726. int clientSessionSz = 0;
  8727. tcp_ready ready;
  8728. func_args server_args;
  8729. THREAD_TYPE serverThread;
  8730. callback_functions server_cbf;
  8731. #ifdef WOLFSSL_TIRTOS
  8732. fdOpenSession(Task_self());
  8733. #endif
  8734. InitTcpReady(&ready);
  8735. #if defined(USE_WINDOWS_API)
  8736. /* use RNG to get random port if using windows */
  8737. ready.port = GetRandomPort();
  8738. #endif
  8739. XMEMSET(&server_args, 0, sizeof(func_args));
  8740. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8741. switch (v) {
  8742. case WOLFSSL_TLSV1_2:
  8743. server_cbf.method = wolfTLSv1_2_server_method;
  8744. server_cbf.ssl_ready = load_tls12_canned_server;
  8745. /* setup the client side */
  8746. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8747. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8748. clientSession = canned_client_session;
  8749. clientSessionSz = sizeof(canned_client_session);
  8750. break;
  8751. #ifdef WOLFSSL_TLS13
  8752. case WOLFSSL_TLSV1_3:
  8753. server_cbf.method = wolfTLSv1_3_server_method;
  8754. server_cbf.ssl_ready = load_tls13_canned_server;
  8755. /* setup the client side */
  8756. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8757. clientSession = canned_client_tls13_session;
  8758. clientSessionSz = sizeof(canned_client_tls13_session);
  8759. break;
  8760. #endif
  8761. }
  8762. server_args.callbacks = &server_cbf;
  8763. server_args.signal = &ready;
  8764. start_thread(tls_export_server, &server_args, &serverThread);
  8765. wait_tcp_ready(&server_args);
  8766. #ifdef WOLFSSL_TIRTOS
  8767. fdOpenSession(Task_self());
  8768. #endif
  8769. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8770. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  8771. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  8772. clientSessionSz);
  8773. replySz = sizeof(reply);
  8774. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  8775. #if !defined(NO_PSK) && defined(HAVE_ANON)
  8776. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  8777. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  8778. #endif
  8779. wolfSSL_set_fd(ssl, sockfd);
  8780. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8781. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  8782. wolfSSL_free(ssl);
  8783. wolfSSL_CTX_free(ctx);
  8784. CloseSocket(sockfd);
  8785. #ifdef WOLFSSL_TIRTOS
  8786. fdCloseSession(Task_self());
  8787. #endif
  8788. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8789. && defined(HAVE_THREAD_LS)
  8790. wc_ecc_fp_free(); /* free per thread cache */
  8791. #endif
  8792. join_thread(serverThread);
  8793. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8794. FreeTcpReady(&ready);
  8795. #ifdef WOLFSSL_TIRTOS
  8796. fdOpenSession(Task_self());
  8797. #endif
  8798. return EXPECT_RESULT();
  8799. }
  8800. #endif
  8801. static int test_wolfSSL_tls_export(void)
  8802. {
  8803. int res = TEST_SKIPPED;
  8804. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8805. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  8806. #ifdef WOLFSSL_TLS13
  8807. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  8808. #endif
  8809. res = TEST_RES_CHECK(1);
  8810. #endif
  8811. return res;
  8812. }
  8813. /*----------------------------------------------------------------------------*
  8814. | TLS extensions tests
  8815. *----------------------------------------------------------------------------*/
  8816. #ifdef ENABLE_TLS_CALLBACK_TEST
  8817. /* Connection test runner - generic */
  8818. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  8819. callback_functions* server_callbacks)
  8820. {
  8821. tcp_ready ready;
  8822. func_args client_args;
  8823. func_args server_args;
  8824. THREAD_TYPE serverThread;
  8825. XMEMSET(&client_args, 0, sizeof(func_args));
  8826. XMEMSET(&server_args, 0, sizeof(func_args));
  8827. StartTCP();
  8828. client_args.callbacks = client_callbacks;
  8829. server_args.callbacks = server_callbacks;
  8830. #ifdef WOLFSSL_TIRTOS
  8831. fdOpenSession(Task_self());
  8832. #endif
  8833. /* RUN Server side */
  8834. InitTcpReady(&ready);
  8835. #if defined(USE_WINDOWS_API)
  8836. /* use RNG to get random port if using windows */
  8837. ready.port = GetRandomPort();
  8838. #endif
  8839. server_args.signal = &ready;
  8840. client_args.signal = &ready;
  8841. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8842. wait_tcp_ready(&server_args);
  8843. /* RUN Client side */
  8844. run_wolfssl_client(&client_args);
  8845. join_thread(serverThread);
  8846. FreeTcpReady(&ready);
  8847. #ifdef WOLFSSL_TIRTOS
  8848. fdCloseSession(Task_self());
  8849. #endif
  8850. client_callbacks->return_code = client_args.return_code;
  8851. server_callbacks->return_code = server_args.return_code;
  8852. }
  8853. #endif /* ENABLE_TLS_CALLBACK_TEST */
  8854. #ifdef HAVE_SNI
  8855. static int test_wolfSSL_UseSNI_params(void)
  8856. {
  8857. EXPECT_DECLS;
  8858. #if !defined(NO_WOLFSSL_CLIENT)
  8859. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8860. WOLFSSL *ssl = wolfSSL_new(ctx);
  8861. ExpectNotNull(ctx);
  8862. ExpectNotNull(ssl);
  8863. /* invalid [ctx|ssl] */
  8864. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  8865. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  8866. /* invalid type */
  8867. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  8868. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  8869. /* invalid data */
  8870. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  8871. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  8872. /* success case */
  8873. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  8874. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  8875. wolfSSL_free(ssl);
  8876. wolfSSL_CTX_free(ctx);
  8877. #endif /* !NO_WOLFSSL_CLIENT */
  8878. return EXPECT_RESULT();
  8879. }
  8880. /* BEGIN of connection tests callbacks */
  8881. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8882. {
  8883. AssertIntEQ(WOLFSSL_SUCCESS,
  8884. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8885. }
  8886. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8887. {
  8888. AssertIntEQ(WOLFSSL_SUCCESS,
  8889. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8890. }
  8891. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8892. {
  8893. AssertIntEQ(WOLFSSL_SUCCESS,
  8894. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8895. }
  8896. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8897. {
  8898. use_SNI_at_ssl(ssl);
  8899. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8900. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8901. }
  8902. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8903. {
  8904. use_SNI_at_ssl(ssl);
  8905. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8906. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8907. }
  8908. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8909. {
  8910. use_SNI_at_ctx(ctx);
  8911. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8912. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8913. }
  8914. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8915. {
  8916. use_SNI_at_ssl(ssl);
  8917. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8918. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8919. }
  8920. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8921. {
  8922. use_SNI_at_ctx(ctx);
  8923. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8924. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8925. }
  8926. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8927. {
  8928. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8929. }
  8930. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8931. {
  8932. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8933. }
  8934. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8935. {
  8936. byte type = WOLFSSL_SNI_HOST_NAME;
  8937. void* request = (void*) &type; /* to be overwritten */
  8938. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8939. AssertNotNull(request);
  8940. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8941. AssertNull(request);
  8942. }
  8943. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8944. {
  8945. byte type = WOLFSSL_SNI_HOST_NAME;
  8946. void* request = NULL;
  8947. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8948. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8949. AssertNotNull(request);
  8950. AssertStrEQ("www.wolfssl.com", (char*)request);
  8951. }
  8952. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8953. {
  8954. byte type = WOLFSSL_SNI_HOST_NAME;
  8955. void* request = NULL;
  8956. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8957. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8958. AssertNotNull(request);
  8959. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8960. }
  8961. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8962. {
  8963. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8964. }
  8965. /* END of connection tests callbacks */
  8966. static int test_wolfSSL_UseSNI_connection(void)
  8967. {
  8968. int res = TEST_SKIPPED;
  8969. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8970. callback_functions client_cb;
  8971. callback_functions server_cb;
  8972. size_t i;
  8973. #ifdef WOLFSSL_STATIC_MEMORY
  8974. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  8975. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  8976. #endif
  8977. struct {
  8978. method_provider client_meth;
  8979. method_provider server_meth;
  8980. #ifdef WOLFSSL_STATIC_MEMORY
  8981. wolfSSL_method_func client_meth_ex;
  8982. wolfSSL_method_func server_meth_ex;
  8983. #endif
  8984. } methods[] = {
  8985. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  8986. {wolfSSLv23_client_method, wolfSSLv23_server_method
  8987. #ifdef WOLFSSL_STATIC_MEMORY
  8988. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  8989. #endif
  8990. },
  8991. #endif
  8992. #ifndef WOLFSSL_NO_TLS12
  8993. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  8994. #ifdef WOLFSSL_STATIC_MEMORY
  8995. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  8996. #endif
  8997. },
  8998. #endif
  8999. #ifdef WOLFSSL_TLS13
  9000. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9001. #ifdef WOLFSSL_STATIC_MEMORY
  9002. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9003. #endif
  9004. },
  9005. #endif
  9006. };
  9007. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9008. for (i = 0; i < methodsSz; i++) {
  9009. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9010. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9011. client_cb.method = methods[i].client_meth;
  9012. server_cb.method = methods[i].server_meth;
  9013. client_cb.devId = testDevId;
  9014. server_cb.devId = testDevId;
  9015. #ifdef WOLFSSL_STATIC_MEMORY
  9016. client_cb.method_ex = methods[i].client_meth_ex;
  9017. server_cb.method_ex = methods[i].server_meth_ex;
  9018. client_cb.mem = cliMem;
  9019. client_cb.memSz = (word32)sizeof(cliMem);
  9020. server_cb.mem = svrMem;
  9021. server_cb.memSz = (word32)sizeof(svrMem);;
  9022. #endif
  9023. /* success case at ctx */
  9024. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9025. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9026. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9027. test_wolfSSL_client_server(&client_cb, &server_cb);
  9028. /* success case at ssl */
  9029. fprintf(stderr, "\tsuccess case at ssl\n");
  9030. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9031. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9032. test_wolfSSL_client_server(&client_cb, &server_cb);
  9033. /* default mismatch behavior */
  9034. fprintf(stderr, "\tdefault mismatch behavior\n");
  9035. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9036. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9037. test_wolfSSL_client_server(&client_cb, &server_cb);
  9038. /* continue on mismatch */
  9039. fprintf(stderr, "\tcontinue on mismatch\n");
  9040. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9041. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9042. test_wolfSSL_client_server(&client_cb, &server_cb);
  9043. /* fake answer on mismatch */
  9044. fprintf(stderr, "\tfake answer on mismatch\n");
  9045. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9046. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9047. test_wolfSSL_client_server(&client_cb, &server_cb);
  9048. /* sni abort - success */
  9049. fprintf(stderr, "\tsni abort - success\n");
  9050. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9051. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9052. test_wolfSSL_client_server(&client_cb, &server_cb);
  9053. /* sni abort - abort when absent (ctx) */
  9054. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9055. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9056. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9057. test_wolfSSL_client_server(&client_cb, &server_cb);
  9058. /* sni abort - abort when absent (ssl) */
  9059. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9060. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9061. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9062. test_wolfSSL_client_server(&client_cb, &server_cb);
  9063. /* sni abort - success when overwritten */
  9064. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9065. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9066. 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;
  9067. test_wolfSSL_client_server(&client_cb, &server_cb);
  9068. /* sni abort - success when allowing mismatches */
  9069. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9070. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9071. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9072. test_wolfSSL_client_server(&client_cb, &server_cb);
  9073. }
  9074. res = TEST_RES_CHECK(1);
  9075. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9076. return res;
  9077. }
  9078. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9079. {
  9080. EXPECT_DECLS;
  9081. byte buff[] = { /* www.paypal.com */
  9082. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9083. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9084. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9085. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9086. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9087. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9088. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9089. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9090. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9091. };
  9092. byte buff2[] = { /* api.textmate.org */
  9093. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9094. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9095. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9096. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9097. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9098. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9099. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9100. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9101. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9102. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9103. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9104. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9105. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9106. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9107. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9108. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9109. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9110. };
  9111. byte buff3[] = { /* no sni extension */
  9112. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9113. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9114. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9115. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9116. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9117. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9118. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9119. };
  9120. byte buff4[] = { /* last extension has zero size */
  9121. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9122. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9123. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9124. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9125. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9126. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9127. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9128. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9129. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9130. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9131. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9132. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9133. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9134. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9135. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9136. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9137. 0x12, 0x00, 0x00
  9138. };
  9139. byte buff5[] = { /* SSL v2.0 client hello */
  9140. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9141. /* dummy bytes below, just to pass size check */
  9142. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9143. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9144. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9145. };
  9146. byte result[32] = {0};
  9147. word32 length = 32;
  9148. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9149. 0, result, &length));
  9150. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9151. 0, result, &length));
  9152. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9153. 1, result, &length));
  9154. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9155. 0, result, &length));
  9156. buff[0] = 0x16;
  9157. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9158. 0, result, &length));
  9159. buff[1] = 0x03;
  9160. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9161. sizeof(buff), 0, result, &length));
  9162. buff[2] = 0x03;
  9163. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9164. sizeof(buff), 0, result, &length));
  9165. buff[4] = 0x64;
  9166. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9167. 0, result, &length));
  9168. if (EXPECT_SUCCESS())
  9169. result[length] = 0;
  9170. ExpectStrEQ("www.paypal.com", (const char*) result);
  9171. length = 32;
  9172. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9173. 0, result, &length));
  9174. if (EXPECT_SUCCESS())
  9175. result[length] = 0;
  9176. ExpectStrEQ("api.textmate.org", (const char*) result);
  9177. /* SSL v2.0 tests */
  9178. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9179. sizeof(buff5), 0, result, &length));
  9180. buff5[2] = 0x02;
  9181. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9182. sizeof(buff5), 0, result, &length));
  9183. buff5[2] = 0x01; buff5[6] = 0x08;
  9184. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9185. sizeof(buff5), 0, result, &length));
  9186. buff5[6] = 0x09; buff5[8] = 0x01;
  9187. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9188. sizeof(buff5), 0, result, &length));
  9189. return EXPECT_RESULT();
  9190. }
  9191. #endif /* HAVE_SNI */
  9192. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9193. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9194. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9195. /* Dummy peer functions to satisfy the exporter/importer */
  9196. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9197. int* ipSz, unsigned short* port, int* fam)
  9198. {
  9199. (void)ssl;
  9200. ip[0] = -1;
  9201. *ipSz = 1;
  9202. *port = 1;
  9203. *fam = 2;
  9204. return 1;
  9205. }
  9206. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9207. int ipSz, unsigned short port, int fam)
  9208. {
  9209. (void)ssl;
  9210. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9211. return 0;
  9212. return 1;
  9213. }
  9214. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9215. WOLFSSL **ssl)
  9216. {
  9217. EXPECT_DECLS;
  9218. unsigned char* sessionBuf = NULL;
  9219. unsigned int sessionSz = 0;
  9220. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9221. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9222. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9223. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9224. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9225. ExpectNotNull(sessionBuf =
  9226. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9227. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9228. wolfSSL_free(*ssl);
  9229. *ssl = NULL;
  9230. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9231. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9232. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9233. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9234. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9235. return EXPECT_RESULT();
  9236. }
  9237. #endif
  9238. static int test_wolfSSL_dtls_export_peers(void)
  9239. {
  9240. EXPECT_DECLS;
  9241. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9242. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9243. test_ssl_cbf client_cbf;
  9244. test_ssl_cbf server_cbf;
  9245. size_t i, j;
  9246. struct test_params {
  9247. method_provider client_meth;
  9248. method_provider server_meth;
  9249. const char* dtls_version;
  9250. } params[] = {
  9251. #ifndef NO_OLD_TLS
  9252. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9253. #endif
  9254. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9255. /* TODO DTLS 1.3 exporting not supported
  9256. #ifdef WOLFSSL_DTLS13
  9257. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9258. #endif
  9259. */
  9260. };
  9261. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9262. for (j = 0; j <= 0b11; j++) {
  9263. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9264. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9265. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9266. client_cbf.method = params[i].client_meth;
  9267. server_cbf.method = params[i].server_meth;
  9268. if (j & 0b01) {
  9269. client_cbf.on_handshake =
  9270. test_wolfSSL_dtls_export_peers_on_handshake;
  9271. printf(" With client export;");
  9272. }
  9273. if (j & 0b10) {
  9274. server_cbf.on_handshake =
  9275. test_wolfSSL_dtls_export_peers_on_handshake;
  9276. printf(" With server export;");
  9277. }
  9278. printf("\n");
  9279. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9280. &server_cbf, NULL), TEST_SUCCESS);
  9281. if (!EXPECT_SUCCESS())
  9282. break;
  9283. }
  9284. }
  9285. #endif
  9286. return EXPECT_RESULT();
  9287. }
  9288. static int test_wolfSSL_UseTrustedCA(void)
  9289. {
  9290. EXPECT_DECLS;
  9291. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9292. && !defined(NO_RSA)
  9293. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9294. WOLFSSL_CTX *ctx = NULL;
  9295. WOLFSSL *ssl = NULL;
  9296. byte id[20];
  9297. #ifndef NO_WOLFSSL_SERVER
  9298. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9299. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9300. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9301. #else
  9302. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9303. #endif
  9304. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9305. XMEMSET(id, 0, sizeof(id));
  9306. /* error cases */
  9307. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9308. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9309. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9310. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9311. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9312. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9313. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9314. #ifdef NO_SHA
  9315. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9316. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9317. #endif
  9318. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9319. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9320. /* success cases */
  9321. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9322. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9323. #ifndef NO_SHA
  9324. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9325. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9326. #endif
  9327. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9328. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9329. wolfSSL_free(ssl);
  9330. wolfSSL_CTX_free(ctx);
  9331. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9332. #endif /* HAVE_TRUSTED_CA */
  9333. return EXPECT_RESULT();
  9334. }
  9335. static int test_wolfSSL_UseMaxFragment(void)
  9336. {
  9337. EXPECT_DECLS;
  9338. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9339. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9340. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9341. #ifndef NO_WOLFSSL_SERVER
  9342. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9343. #else
  9344. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9345. #endif
  9346. WOLFSSL *ssl = NULL;
  9347. #ifdef OPENSSL_EXTRA
  9348. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  9349. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  9350. #else
  9351. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9352. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9353. #endif
  9354. #ifndef NO_WOLFSSL_SERVER
  9355. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9356. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9357. #endif
  9358. ExpectNotNull(ctx);
  9359. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9360. #ifdef OPENSSL_EXTRA
  9361. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9362. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9363. #else
  9364. UseMaxFragment = wolfSSL_UseMaxFragment;
  9365. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9366. #endif
  9367. /* error cases */
  9368. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9369. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9370. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9371. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9372. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9373. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9374. /* success case */
  9375. #ifdef OPENSSL_EXTRA
  9376. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9377. #else
  9378. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9379. #endif
  9380. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9381. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9382. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9383. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9384. #ifdef OPENSSL_EXTRA
  9385. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9386. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9387. #else
  9388. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9389. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9390. #endif
  9391. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9392. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9393. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9394. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9395. #ifdef OPENSSL_EXTRA
  9396. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9397. #else
  9398. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9399. #endif
  9400. wolfSSL_free(ssl);
  9401. wolfSSL_CTX_free(ctx);
  9402. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9403. #endif
  9404. return EXPECT_RESULT();
  9405. }
  9406. static int test_wolfSSL_UseTruncatedHMAC(void)
  9407. {
  9408. EXPECT_DECLS;
  9409. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9410. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9411. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9412. #ifndef NO_WOLFSSL_SERVER
  9413. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9414. #else
  9415. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9416. #endif
  9417. WOLFSSL *ssl = NULL;
  9418. ExpectNotNull(ctx);
  9419. #ifndef NO_WOLFSSL_SERVER
  9420. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9421. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9422. #endif
  9423. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9424. /* error cases */
  9425. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9426. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9427. /* success case */
  9428. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9429. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9430. wolfSSL_free(ssl);
  9431. wolfSSL_CTX_free(ctx);
  9432. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9433. #endif
  9434. return EXPECT_RESULT();
  9435. }
  9436. static int test_wolfSSL_UseSupportedCurve(void)
  9437. {
  9438. EXPECT_DECLS;
  9439. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9440. !defined(NO_TLS)
  9441. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9442. WOLFSSL *ssl = wolfSSL_new(ctx);
  9443. ExpectNotNull(ctx);
  9444. ExpectNotNull(ssl);
  9445. /* error cases */
  9446. ExpectIntNE(WOLFSSL_SUCCESS,
  9447. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9448. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9449. ExpectIntNE(WOLFSSL_SUCCESS,
  9450. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9451. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9452. /* success case */
  9453. ExpectIntEQ(WOLFSSL_SUCCESS,
  9454. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9455. ExpectIntEQ(WOLFSSL_SUCCESS,
  9456. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9457. wolfSSL_free(ssl);
  9458. wolfSSL_CTX_free(ctx);
  9459. #endif
  9460. return EXPECT_RESULT();
  9461. }
  9462. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9463. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9464. {
  9465. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9466. }
  9467. static void use_ALPN_all(WOLFSSL* ssl)
  9468. {
  9469. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9470. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9471. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9472. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9473. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9474. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9475. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9476. }
  9477. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9478. {
  9479. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9480. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9481. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9482. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9483. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9484. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9485. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9486. }
  9487. static void use_ALPN_one(WOLFSSL* ssl)
  9488. {
  9489. /* spdy/2 */
  9490. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9491. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9492. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9493. }
  9494. static void use_ALPN_unknown(WOLFSSL* ssl)
  9495. {
  9496. /* http/2.0 */
  9497. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9498. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9499. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9500. }
  9501. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9502. {
  9503. /* http/2.0 */
  9504. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9505. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9506. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9507. }
  9508. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9509. {
  9510. /* spdy/3 */
  9511. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9512. char *proto = NULL;
  9513. word16 protoSz = 0;
  9514. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9515. /* check value */
  9516. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9517. if (proto) {
  9518. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9519. }
  9520. }
  9521. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9522. {
  9523. char *proto = NULL;
  9524. word16 protoSz = 0;
  9525. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9526. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9527. /* check value */
  9528. AssertIntEQ(1, (0 == protoSz));
  9529. AssertIntEQ(1, (NULL == proto));
  9530. }
  9531. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9532. {
  9533. /* http/1.1 */
  9534. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9535. char *proto;
  9536. word16 protoSz = 0;
  9537. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9538. /* check value */
  9539. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9540. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9541. }
  9542. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9543. {
  9544. /* spdy/2 */
  9545. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9546. char *proto;
  9547. word16 protoSz = 0;
  9548. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9549. /* check value */
  9550. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9551. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9552. }
  9553. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9554. {
  9555. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9556. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9557. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9558. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9559. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9560. char *clist = NULL;
  9561. word16 clistSz = 0;
  9562. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9563. &clistSz));
  9564. /* check value */
  9565. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9566. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9567. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9568. }
  9569. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  9570. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  9571. /* ALPN select callback, success with spdy/2 */
  9572. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  9573. unsigned char *outlen, const unsigned char *in,
  9574. unsigned int inlen, void *arg)
  9575. {
  9576. /* spdy/2 */
  9577. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9578. (void)ssl;
  9579. (void)arg;
  9580. /* adding +1 since LEN byte comes first */
  9581. if (inlen < sizeof(proto) + 1) {
  9582. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9583. }
  9584. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  9585. *out = in + 1;
  9586. *outlen = (unsigned char)sizeof(proto);
  9587. return SSL_TLSEXT_ERR_OK;
  9588. }
  9589. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9590. }
  9591. /* ALPN select callback, force failure */
  9592. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  9593. unsigned char *outlen, const unsigned char *in,
  9594. unsigned int inlen, void *arg)
  9595. {
  9596. (void)ssl;
  9597. (void)out;
  9598. (void)outlen;
  9599. (void)in;
  9600. (void)inlen;
  9601. (void)arg;
  9602. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9603. }
  9604. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  9605. {
  9606. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  9607. }
  9608. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  9609. {
  9610. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  9611. }
  9612. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  9613. static int test_wolfSSL_UseALPN_connection(void)
  9614. {
  9615. int res = TEST_SKIPPED;
  9616. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9617. callback_functions client_cb;
  9618. callback_functions server_cb;
  9619. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9620. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9621. client_cb.method = wolfSSLv23_client_method;
  9622. server_cb.method = wolfSSLv23_server_method;
  9623. client_cb.devId = testDevId;
  9624. server_cb.devId = testDevId;
  9625. /* success case same list */
  9626. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9627. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9628. test_wolfSSL_client_server(&client_cb, &server_cb);
  9629. /* success case only one for server */
  9630. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9631. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9632. test_wolfSSL_client_server(&client_cb, &server_cb);
  9633. /* success case only one for client */
  9634. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9635. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9636. test_wolfSSL_client_server(&client_cb, &server_cb);
  9637. /* success case none for client */
  9638. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9639. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9640. test_wolfSSL_client_server(&client_cb, &server_cb);
  9641. /* success case mismatch behavior but option 'continue' set */
  9642. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9643. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9644. test_wolfSSL_client_server(&client_cb, &server_cb);
  9645. /* success case read protocol send by client */
  9646. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9647. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9648. test_wolfSSL_client_server(&client_cb, &server_cb);
  9649. /* mismatch behavior with same list
  9650. * the first and only this one must be taken */
  9651. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9652. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  9653. test_wolfSSL_client_server(&client_cb, &server_cb);
  9654. /* default mismatch behavior */
  9655. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9656. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9657. test_wolfSSL_client_server(&client_cb, &server_cb);
  9658. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  9659. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  9660. /* WOLFSSL-level ALPN select callback tests */
  9661. /* Callback: success (one protocol, spdy/2) */
  9662. client_cb.ctx_ready = NULL;
  9663. client_cb.ssl_ready = use_ALPN_one;
  9664. client_cb.on_result = verify_ALPN_matching_spdy2;
  9665. server_cb.ctx_ready = NULL;
  9666. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  9667. server_cb.on_result = verify_ALPN_matching_spdy2;
  9668. test_wolfSSL_client_server(&client_cb, &server_cb);
  9669. /* Callback: failure (one client protocol, spdy/2) */
  9670. client_cb.ctx_ready = NULL;
  9671. client_cb.ssl_ready = use_ALPN_one;
  9672. client_cb.on_result = NULL;
  9673. server_cb.ctx_ready = NULL;
  9674. server_cb.ssl_ready = use_ALPN_failure_callback;
  9675. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9676. test_wolfSSL_client_server(&client_cb, &server_cb);
  9677. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  9678. res = TEST_RES_CHECK(1);
  9679. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9680. return res;
  9681. }
  9682. static int test_wolfSSL_UseALPN_params(void)
  9683. {
  9684. EXPECT_DECLS;
  9685. #ifndef NO_WOLFSSL_CLIENT
  9686. /* "http/1.1" */
  9687. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9688. /* "spdy/1" */
  9689. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  9690. /* "spdy/2" */
  9691. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9692. /* "spdy/3" */
  9693. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9694. char buff[256];
  9695. word32 idx;
  9696. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9697. WOLFSSL *ssl = wolfSSL_new(ctx);
  9698. ExpectNotNull(ctx);
  9699. ExpectNotNull(ssl);
  9700. /* error cases */
  9701. ExpectIntNE(WOLFSSL_SUCCESS,
  9702. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  9703. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9704. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  9705. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9706. /* success case */
  9707. /* http1 only */
  9708. ExpectIntEQ(WOLFSSL_SUCCESS,
  9709. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  9710. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9711. /* http1, spdy1 */
  9712. XMEMCPY(buff, http1, sizeof(http1));
  9713. idx = sizeof(http1);
  9714. buff[idx++] = ',';
  9715. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9716. idx += sizeof(spdy1);
  9717. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9718. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9719. /* http1, spdy2, spdy1 */
  9720. XMEMCPY(buff, http1, sizeof(http1));
  9721. idx = sizeof(http1);
  9722. buff[idx++] = ',';
  9723. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9724. idx += sizeof(spdy2);
  9725. buff[idx++] = ',';
  9726. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9727. idx += sizeof(spdy1);
  9728. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9729. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9730. /* spdy3, http1, spdy2, spdy1 */
  9731. XMEMCPY(buff, spdy3, sizeof(spdy3));
  9732. idx = sizeof(spdy3);
  9733. buff[idx++] = ',';
  9734. XMEMCPY(buff+idx, http1, sizeof(http1));
  9735. idx += sizeof(http1);
  9736. buff[idx++] = ',';
  9737. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9738. idx += sizeof(spdy2);
  9739. buff[idx++] = ',';
  9740. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9741. idx += sizeof(spdy1);
  9742. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9743. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9744. wolfSSL_free(ssl);
  9745. wolfSSL_CTX_free(ctx);
  9746. #endif
  9747. return EXPECT_RESULT();
  9748. }
  9749. #endif /* HAVE_ALPN */
  9750. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  9751. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  9752. {
  9753. unsigned char p[] = {
  9754. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9755. 6, 's', 'p', 'd', 'y', '/', '2',
  9756. 6, 's', 'p', 'd', 'y', '/', '1',
  9757. };
  9758. unsigned char p_len = sizeof(p);
  9759. int ret;
  9760. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  9761. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9762. AssertIntEQ(ret, 0);
  9763. #else
  9764. AssertIntEQ(ret, SSL_SUCCESS);
  9765. #endif
  9766. }
  9767. static void set_alpn_protos(SSL* ssl)
  9768. {
  9769. unsigned char p[] = {
  9770. 6, 's', 'p', 'd', 'y', '/', '3',
  9771. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9772. 6, 's', 'p', 'd', 'y', '/', '2',
  9773. 6, 's', 'p', 'd', 'y', '/', '1',
  9774. };
  9775. unsigned char p_len = sizeof(p);
  9776. int ret;
  9777. ret = SSL_set_alpn_protos(ssl, p, p_len);
  9778. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9779. AssertIntEQ(ret, 0);
  9780. #else
  9781. AssertIntEQ(ret, SSL_SUCCESS);
  9782. #endif
  9783. }
  9784. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  9785. {
  9786. /* "spdy/3" */
  9787. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9788. const unsigned char *proto;
  9789. unsigned int protoSz = 0;
  9790. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9791. /* check value */
  9792. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9793. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9794. }
  9795. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  9796. {
  9797. /* "http/1.1" */
  9798. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9799. const unsigned char *proto;
  9800. unsigned int protoSz = 0;
  9801. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9802. /* check value */
  9803. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9804. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9805. }
  9806. static int test_wolfSSL_set_alpn_protos(void)
  9807. {
  9808. int res = TEST_SKIPPED;
  9809. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9810. callback_functions client_cb;
  9811. callback_functions server_cb;
  9812. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9813. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9814. client_cb.method = wolfSSLv23_client_method;
  9815. server_cb.method = wolfSSLv23_server_method;
  9816. client_cb.devId = testDevId;
  9817. server_cb.devId = testDevId;
  9818. /* use CTX_alpn_protos */
  9819. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9820. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  9821. test_wolfSSL_client_server(&client_cb, &server_cb);
  9822. /* use set_alpn_protos */
  9823. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  9824. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  9825. test_wolfSSL_client_server(&client_cb, &server_cb);
  9826. res = TEST_SUCCESS;
  9827. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9828. return res;
  9829. }
  9830. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  9831. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  9832. {
  9833. EXPECT_DECLS;
  9834. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  9835. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9836. WOLFSSL *ssl = wolfSSL_new(ctx);
  9837. ExpectNotNull(ctx);
  9838. ExpectNotNull(ssl);
  9839. /* error cases */
  9840. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  9841. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  9842. /* success cases */
  9843. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  9844. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  9845. wolfSSL_free(ssl);
  9846. wolfSSL_CTX_free(ctx);
  9847. #endif
  9848. return EXPECT_RESULT();
  9849. }
  9850. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  9851. {
  9852. EXPECT_DECLS;
  9853. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  9854. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9855. WOLFSSL *ssl = wolfSSL_new(ctx);
  9856. ExpectNotNull(ctx);
  9857. ExpectNotNull(ssl);
  9858. /* error cases */
  9859. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  9860. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  9861. /* success cases */
  9862. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  9863. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  9864. wolfSSL_free(ssl);
  9865. wolfSSL_CTX_free(ctx);
  9866. #endif
  9867. return EXPECT_RESULT();
  9868. }
  9869. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  9870. static int test_wolfSSL_SCR_Reconnect(void)
  9871. {
  9872. EXPECT_DECLS;
  9873. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  9874. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  9875. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  9876. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9877. struct test_memio_ctx test_ctx;
  9878. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  9879. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  9880. byte data;
  9881. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9882. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  9883. test_ctx.s_ciphers =
  9884. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  9885. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9886. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9887. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  9888. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  9889. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  9890. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  9891. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9892. /* WOLFSSL_FATAL_ERROR since it will block */
  9893. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  9894. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9895. WOLFSSL_ERROR_WANT_READ);
  9896. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  9897. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9898. WOLFSSL_ERROR_WANT_READ);
  9899. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9900. wolfSSL_free(ssl_c);
  9901. ssl_c = NULL;
  9902. wolfSSL_free(ssl_s);
  9903. ssl_s = NULL;
  9904. wolfSSL_CTX_free(ctx_c);
  9905. ctx_c = NULL;
  9906. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  9907. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9908. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9909. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9910. wolfSSL_free(ssl_s);
  9911. wolfSSL_free(ssl_c);
  9912. wolfSSL_CTX_free(ctx_s);
  9913. wolfSSL_CTX_free(ctx_c);
  9914. #endif
  9915. return EXPECT_RESULT();
  9916. }
  9917. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  9918. (!defined(NO_RSA) || defined(HAVE_ECC))
  9919. /* Called when writing. */
  9920. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9921. {
  9922. (void)ssl;
  9923. (void)buf;
  9924. (void)sz;
  9925. (void)ctx;
  9926. /* Force error return from wolfSSL_accept_TLSv13(). */
  9927. return WANT_WRITE;
  9928. }
  9929. /* Called when reading. */
  9930. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9931. {
  9932. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  9933. int len = (int)msg->length;
  9934. (void)ssl;
  9935. (void)sz;
  9936. /* Pass back as much of message as will fit in buffer. */
  9937. if (len > sz)
  9938. len = sz;
  9939. XMEMCPY(buf, msg->buffer, len);
  9940. /* Move over returned data. */
  9941. msg->buffer += len;
  9942. msg->length -= len;
  9943. /* Amount actually copied. */
  9944. return len;
  9945. }
  9946. #endif
  9947. /* Test the detection of duplicate known TLS extensions.
  9948. * Specifically in a ClientHello.
  9949. */
  9950. static int test_tls_ext_duplicate(void)
  9951. {
  9952. EXPECT_DECLS;
  9953. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  9954. !defined(NO_FILESYSTEM)
  9955. const unsigned char clientHelloDupTlsExt[] = {
  9956. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  9957. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  9958. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  9959. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  9960. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  9961. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  9962. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  9963. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  9964. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9965. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  9966. 0x00, 0x9e, 0x01, 0x00,
  9967. /* Extensions - duplicate signature algorithms. */
  9968. 0x00, 0x19, 0x00, 0x0d,
  9969. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  9970. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  9971. /* Supported Versions extension for TLS 1.3. */
  9972. 0x00, 0x2b,
  9973. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  9974. };
  9975. WOLFSSL_BUFFER_INFO msg;
  9976. const char* testCertFile;
  9977. const char* testKeyFile;
  9978. WOLFSSL_CTX *ctx = NULL;
  9979. WOLFSSL *ssl = NULL;
  9980. #ifndef NO_RSA
  9981. testCertFile = svrCertFile;
  9982. testKeyFile = svrKeyFile;
  9983. #elif defined(HAVE_ECC)
  9984. testCertFile = eccCertFile;
  9985. testKeyFile = eccKeyFile;
  9986. #endif
  9987. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9988. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  9989. WOLFSSL_FILETYPE_PEM));
  9990. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  9991. WOLFSSL_FILETYPE_PEM));
  9992. /* Read from 'msg'. */
  9993. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  9994. /* No where to send to - dummy sender. */
  9995. wolfSSL_SetIOSend(ctx, DummySend);
  9996. ssl = wolfSSL_new(ctx);
  9997. ExpectNotNull(ssl);
  9998. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  9999. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10000. wolfSSL_SetIOReadCtx(ssl, &msg);
  10001. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10002. /* can return duplicate ext error or socket error if the peer closed down
  10003. * while sending alert */
  10004. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10005. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10006. }
  10007. wolfSSL_free(ssl);
  10008. wolfSSL_CTX_free(ctx);
  10009. #endif
  10010. return EXPECT_RESULT();
  10011. }
  10012. /*----------------------------------------------------------------------------*
  10013. | X509 Tests
  10014. *----------------------------------------------------------------------------*/
  10015. static int test_wolfSSL_X509_NAME_get_entry(void)
  10016. {
  10017. EXPECT_DECLS;
  10018. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10019. #if defined(OPENSSL_ALL) || \
  10020. (defined(OPENSSL_EXTRA) && \
  10021. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10022. /* use openssl like name to test mapping */
  10023. X509_NAME_ENTRY* ne = NULL;
  10024. X509_NAME* name = NULL;
  10025. X509* x509 = NULL;
  10026. #ifndef NO_FILESYSTEM
  10027. ASN1_STRING* asn = NULL;
  10028. char* subCN = NULL;
  10029. #endif
  10030. int idx = 0;
  10031. ASN1_OBJECT *object = NULL;
  10032. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10033. defined(WOLFSSL_NGINX)
  10034. #ifndef NO_BIO
  10035. BIO* bio = NULL;
  10036. #endif
  10037. #endif
  10038. #ifndef NO_FILESYSTEM
  10039. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10040. WOLFSSL_FILETYPE_PEM));
  10041. ExpectNotNull(name = X509_get_subject_name(x509));
  10042. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10043. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10044. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10045. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10046. wolfSSL_FreeX509(x509);
  10047. x509 = NULL;
  10048. #endif
  10049. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10050. WOLFSSL_FILETYPE_PEM));
  10051. ExpectNotNull(name = X509_get_subject_name(x509));
  10052. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10053. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10054. defined(WOLFSSL_NGINX)
  10055. #ifndef NO_BIO
  10056. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10057. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10058. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10059. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10060. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10061. BIO_free(bio);
  10062. #endif
  10063. #endif
  10064. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10065. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10066. wolfSSL_FreeX509(x509);
  10067. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10068. #endif /* !NO_CERTS && !NO_RSA */
  10069. return EXPECT_RESULT();
  10070. }
  10071. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10072. static int test_wolfSSL_PKCS12(void)
  10073. {
  10074. EXPECT_DECLS;
  10075. /* .p12 file is encrypted with DES3 */
  10076. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10077. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10078. * Password Key
  10079. */
  10080. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10081. !defined(NO_STDIO_FILESYSTEM) && \
  10082. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10083. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10084. byte buf[6000];
  10085. char file[] = "./certs/test-servercert.p12";
  10086. char order[] = "./certs/ecc-rsa-server.p12";
  10087. #ifdef WC_RC2
  10088. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10089. #endif
  10090. char pass[] = "a password";
  10091. const char goodPsw[] = "wolfSSL test";
  10092. const char badPsw[] = "bad";
  10093. #ifdef HAVE_ECC
  10094. WOLFSSL_X509_NAME *subject = NULL;
  10095. WOLFSSL_X509 *x509 = NULL;
  10096. #endif
  10097. XFILE f = XBADFILE;
  10098. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10099. WOLFSSL_BIO *bio = NULL;
  10100. WOLFSSL_EVP_PKEY *pkey = NULL;
  10101. WC_PKCS12 *pkcs12 = NULL;
  10102. WC_PKCS12 *pkcs12_2 = NULL;
  10103. WOLFSSL_X509 *cert = NULL;
  10104. WOLFSSL_X509 *tmp = NULL;
  10105. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10106. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10107. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10108. WOLFSSL_CTX *ctx = NULL;
  10109. WOLFSSL *ssl = NULL;
  10110. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10111. #endif
  10112. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10113. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10114. if (f != XBADFILE) {
  10115. XFCLOSE(f);
  10116. f = XBADFILE;
  10117. }
  10118. goodPswLen = (int)XSTRLEN(goodPsw);
  10119. badPswLen = (int)XSTRLEN(badPsw);
  10120. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10121. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10122. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10123. ExpectNotNull(pkcs12);
  10124. BIO_free(bio);
  10125. bio = NULL;
  10126. /* check verify MAC directly */
  10127. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10128. /* check verify MAC fail case directly */
  10129. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10130. /* check verify MAC fail case */
  10131. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10132. ExpectNull(pkey);
  10133. ExpectNull(cert);
  10134. /* check parse with no extra certs kept */
  10135. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10136. 1);
  10137. ExpectNotNull(pkey);
  10138. ExpectNotNull(cert);
  10139. wolfSSL_EVP_PKEY_free(pkey);
  10140. pkey = NULL;
  10141. wolfSSL_X509_free(cert);
  10142. cert = NULL;
  10143. /* check parse with extra certs kept */
  10144. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10145. 1);
  10146. ExpectNotNull(pkey);
  10147. ExpectNotNull(cert);
  10148. ExpectNotNull(ca);
  10149. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10150. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10151. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10152. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10153. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10154. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10155. #else
  10156. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10157. #endif
  10158. /* Copy stack structure */
  10159. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10160. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10161. /* CTX now owns the tmp_ca stack structure */
  10162. tmp_ca = NULL;
  10163. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10164. ExpectNotNull(tmp_ca);
  10165. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10166. /* Check that the main cert is also set */
  10167. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10168. ExpectNotNull(ssl = SSL_new(ctx));
  10169. ExpectNotNull(SSL_get_certificate(ssl));
  10170. SSL_free(ssl);
  10171. SSL_CTX_free(ctx);
  10172. ctx = NULL;
  10173. #endif
  10174. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10175. /* should be 2 other certs on stack */
  10176. ExpectNotNull(tmp = sk_X509_pop(ca));
  10177. X509_free(tmp);
  10178. ExpectNotNull(tmp = sk_X509_pop(ca));
  10179. X509_free(tmp);
  10180. ExpectNull(sk_X509_pop(ca));
  10181. EVP_PKEY_free(pkey);
  10182. pkey = NULL;
  10183. X509_free(cert);
  10184. cert = NULL;
  10185. sk_X509_pop_free(ca, X509_free);
  10186. ca = NULL;
  10187. /* check PKCS12_create */
  10188. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10189. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10190. SSL_SUCCESS);
  10191. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10192. -1, -1, 100, -1, 0)));
  10193. EVP_PKEY_free(pkey);
  10194. pkey = NULL;
  10195. X509_free(cert);
  10196. cert = NULL;
  10197. sk_X509_pop_free(ca, NULL);
  10198. ca = NULL;
  10199. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10200. SSL_SUCCESS);
  10201. PKCS12_free(pkcs12_2);
  10202. pkcs12_2 = NULL;
  10203. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10204. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10205. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10206. 2000, 1, 0)));
  10207. EVP_PKEY_free(pkey);
  10208. pkey = NULL;
  10209. X509_free(cert);
  10210. cert = NULL;
  10211. sk_X509_pop_free(ca, NULL);
  10212. ca = NULL;
  10213. /* convert to DER then back and parse */
  10214. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10215. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10216. PKCS12_free(pkcs12_2);
  10217. pkcs12_2 = NULL;
  10218. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10219. BIO_free(bio);
  10220. bio = NULL;
  10221. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10222. SSL_SUCCESS);
  10223. /* should be 2 other certs on stack */
  10224. ExpectNotNull(tmp = sk_X509_pop(ca));
  10225. X509_free(tmp);
  10226. ExpectNotNull(tmp = sk_X509_pop(ca));
  10227. X509_free(tmp);
  10228. ExpectNull(sk_X509_pop(ca));
  10229. #ifndef NO_RC4
  10230. PKCS12_free(pkcs12_2);
  10231. pkcs12_2 = NULL;
  10232. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10233. NID_pbe_WithSHA1And128BitRC4,
  10234. NID_pbe_WithSHA1And128BitRC4,
  10235. 2000, 1, 0)));
  10236. EVP_PKEY_free(pkey);
  10237. pkey = NULL;
  10238. X509_free(cert);
  10239. cert = NULL;
  10240. sk_X509_pop_free(ca, NULL);
  10241. ca = NULL;
  10242. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10243. SSL_SUCCESS);
  10244. #endif /* NO_RC4 */
  10245. EVP_PKEY_free(pkey);
  10246. pkey = NULL;
  10247. X509_free(cert);
  10248. cert = NULL;
  10249. PKCS12_free(pkcs12);
  10250. pkcs12 = NULL;
  10251. PKCS12_free(pkcs12_2);
  10252. pkcs12_2 = NULL;
  10253. sk_X509_pop_free(ca, NULL);
  10254. ca = NULL;
  10255. #ifdef HAVE_ECC
  10256. /* test order of parsing */
  10257. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10258. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10259. if (f != XBADFILE) {
  10260. XFCLOSE(f);
  10261. f = XBADFILE;
  10262. }
  10263. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10264. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10265. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10266. WOLFSSL_SUCCESS);
  10267. /* check use of pkey after parse */
  10268. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10269. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10270. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10271. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10272. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10273. #else
  10274. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10275. #endif
  10276. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10277. SSL_CTX_free(ctx);
  10278. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10279. #endif
  10280. ExpectNotNull(pkey);
  10281. ExpectNotNull(cert);
  10282. ExpectNotNull(ca);
  10283. /* compare subject lines of certificates */
  10284. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10285. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10286. SSL_FILETYPE_PEM));
  10287. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10288. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10289. X509_free(x509);
  10290. x509 = NULL;
  10291. /* test expected fail case */
  10292. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10293. SSL_FILETYPE_PEM));
  10294. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10295. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10296. X509_free(x509);
  10297. x509 = NULL;
  10298. X509_free(cert);
  10299. cert = NULL;
  10300. /* get subject line from ca stack */
  10301. ExpectNotNull(cert = sk_X509_pop(ca));
  10302. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10303. /* compare subject from certificate in ca to expected */
  10304. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10305. SSL_FILETYPE_PEM));
  10306. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10307. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10308. EVP_PKEY_free(pkey);
  10309. pkey = NULL;
  10310. X509_free(x509);
  10311. x509 = NULL;
  10312. X509_free(cert);
  10313. cert = NULL;
  10314. BIO_free(bio);
  10315. bio = NULL;
  10316. PKCS12_free(pkcs12);
  10317. pkcs12 = NULL;
  10318. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10319. ca = NULL;
  10320. /* test order of parsing */
  10321. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10322. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10323. if (f != XBADFILE) {
  10324. XFCLOSE(f);
  10325. f = XBADFILE;
  10326. }
  10327. /* check verify MAC fail case */
  10328. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10329. ExpectNull(pkey);
  10330. ExpectNull(cert);
  10331. /* check parse with no extra certs kept */
  10332. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10333. 1);
  10334. ExpectNotNull(pkey);
  10335. ExpectNotNull(cert);
  10336. wolfSSL_EVP_PKEY_free(pkey);
  10337. pkey = NULL;
  10338. wolfSSL_X509_free(cert);
  10339. cert = NULL;
  10340. /* check parse with extra certs kept */
  10341. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10342. 1);
  10343. ExpectNotNull(pkey);
  10344. ExpectNotNull(cert);
  10345. ExpectNotNull(ca);
  10346. wolfSSL_EVP_PKEY_free(pkey);
  10347. pkey = NULL;
  10348. wolfSSL_X509_free(cert);
  10349. cert = NULL;
  10350. sk_X509_pop_free(ca, NULL);
  10351. ca = NULL;
  10352. PKCS12_free(pkcs12);
  10353. pkcs12 = NULL;
  10354. #endif /* HAVE_ECC */
  10355. #ifdef WC_RC2
  10356. /* test PKCS#12 with RC2 encryption */
  10357. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10358. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10359. if (f != XBADFILE) {
  10360. XFCLOSE(f);
  10361. f = XBADFILE;
  10362. }
  10363. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10364. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10365. /* check verify MAC fail case */
  10366. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10367. ExpectNull(pkey);
  10368. ExpectNull(cert);
  10369. /* check parse with not extra certs kept */
  10370. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10371. WOLFSSL_SUCCESS);
  10372. ExpectNotNull(pkey);
  10373. ExpectNotNull(cert);
  10374. wolfSSL_EVP_PKEY_free(pkey);
  10375. pkey = NULL;
  10376. wolfSSL_X509_free(cert);
  10377. cert = NULL;
  10378. /* check parse with extra certs kept */
  10379. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10380. WOLFSSL_SUCCESS);
  10381. ExpectNotNull(pkey);
  10382. ExpectNotNull(cert);
  10383. ExpectNotNull(ca);
  10384. wolfSSL_EVP_PKEY_free(pkey);
  10385. wolfSSL_X509_free(cert);
  10386. sk_X509_pop_free(ca, NULL);
  10387. BIO_free(bio);
  10388. bio = NULL;
  10389. PKCS12_free(pkcs12);
  10390. pkcs12 = NULL;
  10391. #endif /* WC_RC2 */
  10392. /* Test i2d_PKCS12_bio */
  10393. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10394. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10395. if (f != XBADFILE)
  10396. XFCLOSE(f);
  10397. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10398. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10399. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10400. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10401. PKCS12_free(pkcs12);
  10402. BIO_free(bio);
  10403. (void)order;
  10404. #endif /* OPENSSL_EXTRA */
  10405. #endif /* HAVE_FIPS */
  10406. return EXPECT_RESULT();
  10407. }
  10408. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10409. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10410. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10411. #define TEST_PKCS8_ENC
  10412. #endif
  10413. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10414. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10415. /* used to keep track if FailTestCallback was called */
  10416. static int failTestCallbackCalled = 0;
  10417. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10418. {
  10419. (void)passwd;
  10420. (void)sz;
  10421. (void)rw;
  10422. (void)userdata;
  10423. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10424. failTestCallbackCalled = 1;
  10425. return -1;
  10426. }
  10427. #endif
  10428. static int test_wolfSSL_no_password_cb(void)
  10429. {
  10430. EXPECT_DECLS;
  10431. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10432. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10433. WOLFSSL_CTX* ctx = NULL;
  10434. byte buff[FOURK_BUF];
  10435. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10436. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10437. XFILE f = XBADFILE;
  10438. int bytes = 0;
  10439. #ifndef NO_WOLFSSL_CLIENT
  10440. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10441. #else
  10442. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10443. #endif
  10444. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10445. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10446. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10447. if (f != XBADFILE) {
  10448. XFCLOSE(f);
  10449. f = XBADFILE;
  10450. }
  10451. ExpectIntLE(bytes, sizeof(buff));
  10452. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10453. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10454. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10455. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10456. if (f != XBADFILE)
  10457. XFCLOSE(f);
  10458. ExpectIntLE(bytes, sizeof(buff));
  10459. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10460. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10461. wolfSSL_CTX_free(ctx);
  10462. /* Password callback should not be called by default */
  10463. ExpectIntEQ(failTestCallbackCalled, 0);
  10464. #endif
  10465. return EXPECT_RESULT();
  10466. }
  10467. #ifdef TEST_PKCS8_ENC
  10468. /* for PKCS8 test case */
  10469. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10470. {
  10471. int flag = 0;
  10472. (void)rw;
  10473. if (userdata != NULL) {
  10474. flag = *((int*)userdata); /* user set data */
  10475. }
  10476. switch (flag) {
  10477. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10478. * can be anything the user wishes to be passed to the callback
  10479. * associated with the WOLFSSL_CTX */
  10480. XSTRNCPY(passwd, "yassl123", sz);
  10481. return 8;
  10482. default:
  10483. return BAD_FUNC_ARG;
  10484. }
  10485. }
  10486. #endif /* TEST_PKCS8_ENC */
  10487. /* Testing functions dealing with PKCS8 */
  10488. static int test_wolfSSL_PKCS8(void)
  10489. {
  10490. EXPECT_DECLS;
  10491. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10492. !defined(WOLFCRYPT_ONLY)
  10493. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10494. byte buff[FOURK_BUF];
  10495. byte der[FOURK_BUF];
  10496. #ifndef NO_RSA
  10497. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10498. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10499. #endif
  10500. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10501. #ifdef HAVE_ECC
  10502. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10503. #endif
  10504. XFILE f = XBADFILE;
  10505. int bytes = 0;
  10506. WOLFSSL_CTX* ctx = NULL;
  10507. #if defined(HAVE_ECC) && !defined(NO_CODING)
  10508. int ret;
  10509. ecc_key key;
  10510. word32 x = 0;
  10511. #endif
  10512. #ifdef TEST_PKCS8_ENC
  10513. #if !defined(NO_RSA) && !defined(NO_SHA)
  10514. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10515. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10516. #endif
  10517. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10518. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10519. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10520. #endif
  10521. int flag;
  10522. #endif
  10523. (void)der;
  10524. #ifndef NO_WOLFSSL_CLIENT
  10525. #ifndef WOLFSSL_NO_TLS12
  10526. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10527. #else
  10528. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10529. #endif
  10530. #else
  10531. #ifndef WOLFSSL_NO_TLS12
  10532. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10533. #else
  10534. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10535. #endif
  10536. #endif
  10537. #ifdef TEST_PKCS8_ENC
  10538. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10539. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10540. flag = 1; /* used by password callback as return code */
  10541. #if !defined(NO_RSA) && !defined(NO_SHA)
  10542. /* test loading PEM PKCS8 encrypted file */
  10543. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10544. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10545. if (f != XBADFILE) {
  10546. XFCLOSE(f);
  10547. f = XBADFILE;
  10548. }
  10549. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10550. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10551. /* this next case should fail because of password callback return code */
  10552. flag = 0; /* used by password callback as return code */
  10553. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10554. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10555. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10556. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10557. "yassl123"), 0);
  10558. /* test that error value is returned with a bad password */
  10559. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10560. "bad"), 0);
  10561. /* test loading PEM PKCS8 encrypted file */
  10562. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10563. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10564. if (f != XBADFILE) {
  10565. XFCLOSE(f);
  10566. f = XBADFILE;
  10567. }
  10568. flag = 1; /* used by password callback as return code */
  10569. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10570. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10571. /* this next case should fail because of password callback return code */
  10572. flag = 0; /* used by password callback as return code */
  10573. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10574. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10575. #endif /* !NO_RSA && !NO_SHA */
  10576. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10577. /* test loading PEM PKCS8 encrypted ECC Key file */
  10578. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  10579. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10580. if (f != XBADFILE) {
  10581. XFCLOSE(f);
  10582. f = XBADFILE;
  10583. }
  10584. flag = 1; /* used by password callback as return code */
  10585. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10586. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10587. /* this next case should fail because of password callback return code */
  10588. flag = 0; /* used by password callback as return code */
  10589. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10590. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10591. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10592. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10593. "yassl123"), 0);
  10594. /* test that error value is returned with a bad password */
  10595. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10596. "bad"), 0);
  10597. /* test loading DER PKCS8 encrypted ECC Key file */
  10598. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  10599. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10600. if (f != XBADFILE) {
  10601. XFCLOSE(f);
  10602. f = XBADFILE;
  10603. }
  10604. flag = 1; /* used by password callback as return code */
  10605. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10606. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10607. /* this next case should fail because of password callback return code */
  10608. flag = 0; /* used by password callback as return code */
  10609. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10610. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10611. /* leave flag as "okay" */
  10612. flag = 1;
  10613. #endif /* HAVE_ECC && !NO_SHA */
  10614. #endif /* TEST_PKCS8_ENC */
  10615. #ifndef NO_RSA
  10616. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  10617. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  10618. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10619. if (f != XBADFILE) {
  10620. XFCLOSE(f);
  10621. f = XBADFILE;
  10622. }
  10623. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10624. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10625. /* test loading PEM PKCS8 private key file (not encrypted) */
  10626. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  10627. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10628. if (f != XBADFILE) {
  10629. XFCLOSE(f);
  10630. f = XBADFILE;
  10631. }
  10632. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10633. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10634. #endif /* !NO_RSA */
  10635. /* Test PKCS8 PEM ECC key no crypt */
  10636. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10637. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10638. if (f != XBADFILE) {
  10639. XFCLOSE(f);
  10640. f = XBADFILE;
  10641. }
  10642. #ifdef HAVE_ECC
  10643. /* Test PKCS8 PEM ECC key no crypt */
  10644. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10645. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10646. #ifndef NO_CODING
  10647. /* decrypt PKCS8 PEM to key in DER format */
  10648. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  10649. (word32)sizeof(der), NULL)), 0);
  10650. ret = wc_ecc_init(&key);
  10651. if (ret == 0) {
  10652. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  10653. wc_ecc_free(&key);
  10654. }
  10655. ExpectIntEQ(ret, 0);
  10656. #endif
  10657. /* Test PKCS8 DER ECC key no crypt */
  10658. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10659. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10660. if (f != XBADFILE)
  10661. XFCLOSE(f);
  10662. /* Test using a PKCS8 ECC PEM */
  10663. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10664. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10665. #else
  10666. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  10667. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  10668. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  10669. #endif /* HAVE_ECC */
  10670. wolfSSL_CTX_free(ctx);
  10671. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10672. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  10673. return EXPECT_RESULT();
  10674. }
  10675. static int test_wolfSSL_PKCS8_ED25519(void)
  10676. {
  10677. EXPECT_DECLS;
  10678. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10679. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  10680. defined(HAVE_ED25519_KEY_IMPORT)
  10681. const byte encPrivKey[] = \
  10682. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10683. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  10684. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  10685. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  10686. "2L9W4v7swXkV+X+a1ww=\n"
  10687. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10688. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10689. byte der[FOURK_BUF];
  10690. WOLFSSL_CTX* ctx = NULL;
  10691. int bytes;
  10692. XMEMSET(der, 0, sizeof(der));
  10693. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10694. (word32)sizeof(der), password)), 0);
  10695. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10696. #ifndef NO_WOLFSSL_SERVER
  10697. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10698. #else
  10699. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10700. #endif
  10701. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10702. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10703. wolfSSL_CTX_free(ctx);
  10704. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10705. #endif
  10706. return EXPECT_RESULT();
  10707. }
  10708. static int test_wolfSSL_PKCS8_ED448(void)
  10709. {
  10710. EXPECT_DECLS;
  10711. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10712. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  10713. defined(HAVE_ED448_KEY_IMPORT)
  10714. const byte encPrivKey[] = \
  10715. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10716. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  10717. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  10718. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  10719. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  10720. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10721. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10722. byte der[FOURK_BUF];
  10723. WOLFSSL_CTX* ctx = NULL;
  10724. int bytes;
  10725. XMEMSET(der, 0, sizeof(der));
  10726. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10727. (word32)sizeof(der), password)), 0);
  10728. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10729. #ifndef NO_WOLFSSL_SERVER
  10730. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10731. #else
  10732. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10733. #endif
  10734. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10735. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10736. wolfSSL_CTX_free(ctx);
  10737. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10738. #endif
  10739. return EXPECT_RESULT();
  10740. }
  10741. /* Testing functions dealing with PKCS5 */
  10742. static int test_wolfSSL_PKCS5(void)
  10743. {
  10744. EXPECT_DECLS;
  10745. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  10746. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  10747. const char* passwd = "myfipsPa$$W0rd";
  10748. #else
  10749. const char *passwd = "pass1234";
  10750. #endif
  10751. const unsigned char *salt = (unsigned char *)"salt1234";
  10752. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  10753. DYNAMIC_TYPE_TMP_BUFFER);
  10754. int ret = 0;
  10755. ExpectNotNull(out);
  10756. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  10757. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  10758. WOLFSSL_SUCCESS);
  10759. #ifdef WOLFSSL_SHA512
  10760. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  10761. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  10762. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  10763. #endif
  10764. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10765. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  10766. return EXPECT_RESULT();
  10767. }
  10768. /* test parsing URI from certificate */
  10769. static int test_wolfSSL_URI(void)
  10770. {
  10771. EXPECT_DECLS;
  10772. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10773. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  10774. defined(OPENSSL_EXTRA))
  10775. WOLFSSL_X509* x509 = NULL;
  10776. const char uri[] = "./certs/client-uri-cert.pem";
  10777. const char urn[] = "./certs/client-absolute-urn.pem";
  10778. const char badUri[] = "./certs/client-relative-uri.pem";
  10779. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  10780. WOLFSSL_FILETYPE_PEM));
  10781. wolfSSL_FreeX509(x509);
  10782. x509 = NULL;
  10783. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  10784. WOLFSSL_FILETYPE_PEM));
  10785. wolfSSL_FreeX509(x509);
  10786. x509 = NULL;
  10787. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  10788. && !defined(WOLFSSL_FPKI)
  10789. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10790. WOLFSSL_FILETYPE_PEM));
  10791. #else
  10792. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10793. WOLFSSL_FILETYPE_PEM));
  10794. #endif
  10795. wolfSSL_FreeX509(x509);
  10796. #endif
  10797. return EXPECT_RESULT();
  10798. }
  10799. static int test_wolfSSL_TBS(void)
  10800. {
  10801. EXPECT_DECLS;
  10802. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10803. && defined(OPENSSL_EXTRA)
  10804. WOLFSSL_X509* x509 = NULL;
  10805. const unsigned char* tbs;
  10806. int tbsSz;
  10807. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  10808. WOLFSSL_FILETYPE_PEM));
  10809. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  10810. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  10811. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  10812. ExpectIntEQ(tbsSz, 1003);
  10813. wolfSSL_FreeX509(x509);
  10814. #endif
  10815. return EXPECT_RESULT();
  10816. }
  10817. static int test_wolfSSL_X509_verify(void)
  10818. {
  10819. EXPECT_DECLS;
  10820. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  10821. defined(OPENSSL_EXTRA)
  10822. WOLFSSL_X509* ca = NULL;
  10823. WOLFSSL_X509* serv = NULL;
  10824. WOLFSSL_EVP_PKEY* pkey = NULL;
  10825. unsigned char buf[2048];
  10826. const unsigned char* pt = NULL;
  10827. int bufSz;
  10828. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  10829. WOLFSSL_FILETYPE_PEM));
  10830. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  10831. WOLFSSL_SUCCESS);
  10832. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  10833. WOLFSSL_SUCCESS);
  10834. ExpectIntEQ(bufSz, 294);
  10835. bufSz = 2048;
  10836. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  10837. WOLFSSL_SUCCESS);
  10838. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  10839. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  10840. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  10841. WOLFSSL_FILETYPE_PEM));
  10842. /* success case */
  10843. pt = buf;
  10844. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10845. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  10846. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  10847. wolfSSL_EVP_PKEY_free(pkey);
  10848. pkey = NULL;
  10849. /* fail case */
  10850. bufSz = 2048;
  10851. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  10852. WOLFSSL_SUCCESS);
  10853. pt = buf;
  10854. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10855. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  10856. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  10857. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  10858. wolfSSL_EVP_PKEY_free(pkey);
  10859. wolfSSL_FreeX509(ca);
  10860. wolfSSL_FreeX509(serv);
  10861. #endif
  10862. return EXPECT_RESULT();
  10863. }
  10864. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10865. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10866. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10867. /* create certificate with version 2 */
  10868. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  10869. {
  10870. EXPECT_DECLS;
  10871. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  10872. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  10873. unsigned char *der = NULL, *key = NULL, *pt;
  10874. char *header = NULL, *name = NULL;
  10875. int derSz;
  10876. long keySz;
  10877. XFILE fp = XBADFILE;
  10878. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  10879. time_t t;
  10880. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10881. WOLFSSL_FILETYPE_PEM));
  10882. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  10883. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  10884. WOLFSSL_SUCCESS);
  10885. if (fp != XBADFILE)
  10886. XFCLOSE(fp);
  10887. pt = key;
  10888. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  10889. (const unsigned char**)&pt, keySz));
  10890. /* create the version 2 certificate */
  10891. ExpectNotNull(x509v2 = X509_new());
  10892. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  10893. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  10894. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  10895. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  10896. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  10897. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  10898. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  10899. t = time(NULL);
  10900. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  10901. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  10902. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  10903. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  10904. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  10905. derSz = wolfSSL_i2d_X509(x509v2, &der);
  10906. ExpectIntGT(derSz, 0);
  10907. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  10908. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10909. /* TODO: Replace with API call */
  10910. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  10911. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10912. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10913. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10914. wolfSSL_X509_free(x509);
  10915. wolfSSL_X509_free(x509v2);
  10916. wolfSSL_EVP_PKEY_free(priv);
  10917. wolfSSL_EVP_PKEY_free(pub);
  10918. wolfSSL_ASN1_TIME_free(notBefore);
  10919. wolfSSL_ASN1_TIME_free(notAfter);
  10920. return EXPECT_RESULT();
  10921. }
  10922. /* override certificate version error */
  10923. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  10924. {
  10925. EXPECT_DECLS;
  10926. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10927. ExpectIntEQ(store->error, ASN_VERSION_E);
  10928. #else
  10929. ExpectIntEQ(store->error, 0);
  10930. #endif
  10931. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  10932. (void)preverify;
  10933. return EXPECT_RESULT() == TEST_SUCCESS;
  10934. }
  10935. /* set verify callback that will override bad certificate version */
  10936. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  10937. {
  10938. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  10939. return TEST_SUCCESS;
  10940. }
  10941. #endif
  10942. static int test_wolfSSL_X509_TLS_version_test_1(void)
  10943. {
  10944. EXPECT_DECLS;
  10945. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10946. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10947. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10948. test_ssl_cbf func_cb_client;
  10949. test_ssl_cbf func_cb_server;
  10950. /* test server rejects a client certificate that is not version 3 */
  10951. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10952. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10953. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10954. #ifndef WOLFSSL_NO_TLS12
  10955. func_cb_client.method = wolfTLSv1_2_client_method;
  10956. #else
  10957. func_cb_client.method = wolfTLSv1_3_client_method;
  10958. #endif
  10959. #ifndef WOLFSSL_NO_TLS12
  10960. func_cb_server.method = wolfTLSv1_2_server_method;
  10961. #else
  10962. func_cb_server.method = wolfTLSv1_3_server_method;
  10963. #endif
  10964. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10965. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10966. &func_cb_server, NULL), TEST_FAIL);
  10967. #else
  10968. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10969. &func_cb_server, NULL), TEST_SUCCESS);
  10970. #endif
  10971. #endif
  10972. return EXPECT_RESULT();
  10973. }
  10974. static int test_wolfSSL_X509_TLS_version_test_2(void)
  10975. {
  10976. EXPECT_DECLS;
  10977. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10978. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10979. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10980. test_ssl_cbf func_cb_client;
  10981. test_ssl_cbf func_cb_server;
  10982. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10983. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10984. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10985. func_cb_server.ctx_ready = &test_set_override_x509;
  10986. #ifndef WOLFSSL_NO_TLS12
  10987. func_cb_client.method = wolfTLSv1_2_client_method;
  10988. #else
  10989. func_cb_client.method = wolfTLSv1_3_client_method;
  10990. #endif
  10991. #ifndef WOLFSSL_NO_TLS12
  10992. func_cb_server.method = wolfTLSv1_2_server_method;
  10993. #else
  10994. func_cb_server.method = wolfTLSv1_3_server_method;
  10995. #endif
  10996. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10997. &func_cb_server, NULL), TEST_SUCCESS);
  10998. #endif
  10999. return EXPECT_RESULT();
  11000. }
  11001. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11002. * version allowed.
  11003. * POST: 1 on success.
  11004. */
  11005. static int test_wolfSSL_CTX_SetMinVersion(void)
  11006. {
  11007. int res = TEST_SKIPPED;
  11008. #ifndef NO_WOLFSSL_CLIENT
  11009. int failFlag = WOLFSSL_SUCCESS;
  11010. WOLFSSL_CTX* ctx;
  11011. int itr;
  11012. #ifndef NO_OLD_TLS
  11013. const int versions[] = {
  11014. #ifdef WOLFSSL_ALLOW_TLSV10
  11015. WOLFSSL_TLSV1,
  11016. #endif
  11017. WOLFSSL_TLSV1_1,
  11018. WOLFSSL_TLSV1_2 };
  11019. #elif !defined(WOLFSSL_NO_TLS12)
  11020. const int versions[] = { WOLFSSL_TLSV1_2 };
  11021. #elif defined(WOLFSSL_TLS13)
  11022. const int versions[] = { WOLFSSL_TLSV1_3 };
  11023. #else
  11024. const int versions[0];
  11025. #endif
  11026. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11027. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11028. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11029. != WOLFSSL_SUCCESS) {
  11030. failFlag = WOLFSSL_FAILURE;
  11031. }
  11032. }
  11033. wolfSSL_CTX_free(ctx);
  11034. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11035. #endif
  11036. return res;
  11037. } /* END test_wolfSSL_CTX_SetMinVersion */
  11038. /*----------------------------------------------------------------------------*
  11039. | OCSP Stapling
  11040. *----------------------------------------------------------------------------*/
  11041. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11042. * need to contact the CA, lowering the cost of cert revocation checking.
  11043. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11044. * POST: 1 returned for success.
  11045. */
  11046. static int test_wolfSSL_UseOCSPStapling(void)
  11047. {
  11048. EXPECT_DECLS;
  11049. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11050. !defined(NO_WOLFSSL_CLIENT)
  11051. WOLFSSL_CTX* ctx = NULL;
  11052. WOLFSSL* ssl = NULL;
  11053. #ifndef NO_WOLFSSL_CLIENT
  11054. #ifndef WOLFSSL_NO_TLS12
  11055. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11056. #else
  11057. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11058. #endif
  11059. #else
  11060. #ifndef WOLFSSL_NO_TLS12
  11061. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11062. #else
  11063. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11064. #endif
  11065. #endif
  11066. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11067. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11068. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11069. #ifndef NO_WOLFSSL_CLIENT
  11070. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11071. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11072. #else
  11073. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11074. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11075. #endif
  11076. wolfSSL_free(ssl);
  11077. wolfSSL_CTX_free(ctx);
  11078. #endif
  11079. return EXPECT_RESULT();
  11080. } /* END test_wolfSSL_UseOCSPStapling */
  11081. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11082. * stapling eliminates the need to contact the CA and lowers cert revocation
  11083. * check.
  11084. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11085. */
  11086. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11087. {
  11088. EXPECT_DECLS;
  11089. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11090. !defined(NO_WOLFSSL_CLIENT)
  11091. WOLFSSL_CTX* ctx = NULL;
  11092. WOLFSSL* ssl = NULL;
  11093. #ifndef NO_WOLFSSL_CLIENT
  11094. #ifndef WOLFSSL_NO_TLS12
  11095. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11096. #else
  11097. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11098. #endif
  11099. #else
  11100. #ifndef WOLFSSL_NO_TLS12
  11101. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11102. #else
  11103. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11104. #endif
  11105. #endif
  11106. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11107. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11108. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11109. #ifndef NO_WOLFSSL_CLIENT
  11110. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11111. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11112. #else
  11113. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11114. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11115. #endif
  11116. wolfSSL_free(ssl);
  11117. wolfSSL_CTX_free(ctx);
  11118. #endif
  11119. return EXPECT_RESULT();
  11120. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11121. /*----------------------------------------------------------------------------*
  11122. | Multicast Tests
  11123. *----------------------------------------------------------------------------*/
  11124. static int test_wolfSSL_mcast(void)
  11125. {
  11126. EXPECT_DECLS;
  11127. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11128. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11129. WOLFSSL_CTX* ctx = NULL;
  11130. WOLFSSL* ssl = NULL;
  11131. byte preMasterSecret[512];
  11132. byte clientRandom[32];
  11133. byte serverRandom[32];
  11134. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11135. byte buf[256];
  11136. word16 newId;
  11137. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11138. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11139. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11140. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11141. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11142. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11143. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11144. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11145. WOLFSSL_SUCCESS);
  11146. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11147. ExpectIntLE(newId, 100);
  11148. wolfSSL_free(ssl);
  11149. wolfSSL_CTX_free(ctx);
  11150. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11151. * WOLFSSL_SNIFFER) */
  11152. return EXPECT_RESULT();
  11153. }
  11154. /*----------------------------------------------------------------------------*
  11155. | Wolfcrypt
  11156. *----------------------------------------------------------------------------*/
  11157. /*
  11158. * Unit test for the wc_InitBlake2b()
  11159. */
  11160. static int test_wc_InitBlake2b(void)
  11161. {
  11162. EXPECT_DECLS;
  11163. #ifdef HAVE_BLAKE2
  11164. Blake2b blake;
  11165. /* Test good arg. */
  11166. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11167. /* Test bad arg. */
  11168. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11169. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11170. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11171. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11172. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11173. #endif
  11174. return EXPECT_RESULT();
  11175. } /* END test_wc_InitBlake2b*/
  11176. /*
  11177. * Unit test for the wc_InitBlake2b_WithKey()
  11178. */
  11179. static int test_wc_InitBlake2b_WithKey(void)
  11180. {
  11181. EXPECT_DECLS;
  11182. #ifdef HAVE_BLAKE2
  11183. Blake2b blake;
  11184. word32 digestSz = BLAKE2B_KEYBYTES;
  11185. byte key[BLAKE2B_KEYBYTES];
  11186. word32 keylen = BLAKE2B_KEYBYTES;
  11187. XMEMSET(key, 0, sizeof(key));
  11188. /* Test good arg. */
  11189. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11190. /* Test bad args. */
  11191. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11192. BAD_FUNC_ARG);
  11193. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11194. BAD_FUNC_ARG);
  11195. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11196. #endif
  11197. return EXPECT_RESULT();
  11198. } /* END wc_InitBlake2b_WithKey*/
  11199. /*
  11200. * Unit test for the wc_InitBlake2s_WithKey()
  11201. */
  11202. static int test_wc_InitBlake2s_WithKey(void)
  11203. {
  11204. EXPECT_DECLS;
  11205. #ifdef HAVE_BLAKE2S
  11206. Blake2s blake;
  11207. word32 digestSz = BLAKE2S_KEYBYTES;
  11208. byte *key = (byte*)"01234567890123456789012345678901";
  11209. word32 keylen = BLAKE2S_KEYBYTES;
  11210. /* Test good arg. */
  11211. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11212. /* Test bad args. */
  11213. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11214. BAD_FUNC_ARG);
  11215. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11216. BAD_FUNC_ARG);
  11217. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11218. #endif
  11219. return EXPECT_RESULT();
  11220. } /* END wc_InitBlake2s_WithKey*/
  11221. /*
  11222. * Unit test for the wc_InitMd5()
  11223. */
  11224. static int test_wc_InitMd5(void)
  11225. {
  11226. EXPECT_DECLS;
  11227. #ifndef NO_MD5
  11228. wc_Md5 md5;
  11229. /* Test good arg. */
  11230. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11231. /* Test bad arg. */
  11232. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11233. wc_Md5Free(&md5);
  11234. #endif
  11235. return EXPECT_RESULT();
  11236. } /* END test_wc_InitMd5 */
  11237. /*
  11238. * Testing wc_UpdateMd5()
  11239. */
  11240. static int test_wc_Md5Update(void)
  11241. {
  11242. EXPECT_DECLS;
  11243. #ifndef NO_MD5
  11244. wc_Md5 md5;
  11245. byte hash[WC_MD5_DIGEST_SIZE];
  11246. testVector a, b, c;
  11247. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11248. /* Input */
  11249. a.input = "a";
  11250. a.inLen = XSTRLEN(a.input);
  11251. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11252. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11253. /* Update input. */
  11254. a.input = "abc";
  11255. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11256. "\x72";
  11257. a.inLen = XSTRLEN(a.input);
  11258. a.outLen = XSTRLEN(a.output);
  11259. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11260. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11261. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11262. /* Pass in bad values. */
  11263. b.input = NULL;
  11264. b.inLen = 0;
  11265. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11266. c.input = NULL;
  11267. c.inLen = WC_MD5_DIGEST_SIZE;
  11268. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11269. BAD_FUNC_ARG);
  11270. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11271. BAD_FUNC_ARG);
  11272. wc_Md5Free(&md5);
  11273. #endif
  11274. return EXPECT_RESULT();
  11275. } /* END test_wc_Md5Update() */
  11276. /*
  11277. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11278. */
  11279. static int test_wc_Md5Final(void)
  11280. {
  11281. EXPECT_DECLS;
  11282. #ifndef NO_MD5
  11283. /* Instantiate */
  11284. wc_Md5 md5;
  11285. byte* hash_test[3];
  11286. byte hash1[WC_MD5_DIGEST_SIZE];
  11287. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11288. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11289. int times, i;
  11290. /* Initialize */
  11291. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11292. hash_test[0] = hash1;
  11293. hash_test[1] = hash2;
  11294. hash_test[2] = hash3;
  11295. times = sizeof(hash_test)/sizeof(byte*);
  11296. for (i = 0; i < times; i++) {
  11297. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11298. }
  11299. /* Test bad args. */
  11300. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11301. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11302. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11303. wc_Md5Free(&md5);
  11304. #endif
  11305. return EXPECT_RESULT();
  11306. }
  11307. /*
  11308. * Unit test for the wc_InitSha()
  11309. */
  11310. static int test_wc_InitSha(void)
  11311. {
  11312. EXPECT_DECLS;
  11313. #ifndef NO_SHA
  11314. wc_Sha sha;
  11315. /* Test good arg. */
  11316. ExpectIntEQ(wc_InitSha(&sha), 0);
  11317. /* Test bad arg. */
  11318. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11319. wc_ShaFree(&sha);
  11320. #endif
  11321. return EXPECT_RESULT();
  11322. } /* END test_wc_InitSha */
  11323. /*
  11324. * Tesing wc_ShaUpdate()
  11325. */
  11326. static int test_wc_ShaUpdate(void)
  11327. {
  11328. EXPECT_DECLS;
  11329. #ifndef NO_SHA
  11330. wc_Sha sha;
  11331. byte hash[WC_SHA_DIGEST_SIZE];
  11332. testVector a, b, c;
  11333. ExpectIntEQ(wc_InitSha(&sha), 0);
  11334. /* Input. */
  11335. a.input = "a";
  11336. a.inLen = XSTRLEN(a.input);
  11337. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11338. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11339. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11340. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11341. /* Update input. */
  11342. a.input = "abc";
  11343. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11344. "\x6C\x9C\xD0\xD8\x9D";
  11345. a.inLen = XSTRLEN(a.input);
  11346. a.outLen = XSTRLEN(a.output);
  11347. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11348. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11349. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11350. /* Try passing in bad values. */
  11351. b.input = NULL;
  11352. b.inLen = 0;
  11353. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11354. c.input = NULL;
  11355. c.inLen = WC_SHA_DIGEST_SIZE;
  11356. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11357. BAD_FUNC_ARG);
  11358. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11359. BAD_FUNC_ARG);
  11360. wc_ShaFree(&sha);
  11361. #endif
  11362. return EXPECT_RESULT();
  11363. } /* END test_wc_ShaUpdate() */
  11364. /*
  11365. * Unit test on wc_ShaFinal
  11366. */
  11367. static int test_wc_ShaFinal(void)
  11368. {
  11369. EXPECT_DECLS;
  11370. #ifndef NO_SHA
  11371. wc_Sha sha;
  11372. byte* hash_test[3];
  11373. byte hash1[WC_SHA_DIGEST_SIZE];
  11374. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11375. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11376. int times, i;
  11377. /* Initialize*/
  11378. ExpectIntEQ(wc_InitSha(&sha), 0);
  11379. hash_test[0] = hash1;
  11380. hash_test[1] = hash2;
  11381. hash_test[2] = hash3;
  11382. times = sizeof(hash_test)/sizeof(byte*);
  11383. for (i = 0; i < times; i++) {
  11384. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11385. }
  11386. /* Test bad args. */
  11387. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11388. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11389. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11390. wc_ShaFree(&sha);
  11391. #endif
  11392. return EXPECT_RESULT();
  11393. } /* END test_wc_ShaFinal */
  11394. /*
  11395. * Unit test for wc_InitSha256()
  11396. */
  11397. static int test_wc_InitSha256(void)
  11398. {
  11399. EXPECT_DECLS;
  11400. #ifndef NO_SHA256
  11401. wc_Sha256 sha256;
  11402. /* Test good arg. */
  11403. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11404. /* Test bad arg. */
  11405. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11406. wc_Sha256Free(&sha256);
  11407. #endif
  11408. return EXPECT_RESULT();
  11409. } /* END test_wc_InitSha256 */
  11410. /*
  11411. * Unit test for wc_Sha256Update()
  11412. */
  11413. static int test_wc_Sha256Update(void)
  11414. {
  11415. EXPECT_DECLS;
  11416. #ifndef NO_SHA256
  11417. wc_Sha256 sha256;
  11418. byte hash[WC_SHA256_DIGEST_SIZE];
  11419. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11420. testVector a, b, c;
  11421. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11422. /* Input. */
  11423. a.input = "a";
  11424. a.inLen = XSTRLEN(a.input);
  11425. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11426. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11427. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11428. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11429. /* Update input. */
  11430. a.input = "abc";
  11431. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11432. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11433. "\x15\xAD";
  11434. a.inLen = XSTRLEN(a.input);
  11435. a.outLen = XSTRLEN(a.output);
  11436. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11437. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11438. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11439. /* Unaligned check. */
  11440. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11441. 0);
  11442. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11443. /* Try passing in bad values */
  11444. b.input = NULL;
  11445. b.inLen = 0;
  11446. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11447. c.input = NULL;
  11448. c.inLen = WC_SHA256_DIGEST_SIZE;
  11449. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11450. BAD_FUNC_ARG);
  11451. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11452. BAD_FUNC_ARG);
  11453. wc_Sha256Free(&sha256);
  11454. #endif
  11455. return EXPECT_RESULT();
  11456. } /* END test_wc_Sha256Update */
  11457. /*
  11458. * Unit test function for wc_Sha256Final()
  11459. */
  11460. static int test_wc_Sha256Final(void)
  11461. {
  11462. EXPECT_DECLS;
  11463. #ifndef NO_SHA256
  11464. wc_Sha256 sha256;
  11465. byte* hash_test[3];
  11466. byte hash1[WC_SHA256_DIGEST_SIZE];
  11467. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11468. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11469. int times, i;
  11470. /* Initialize */
  11471. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11472. hash_test[0] = hash1;
  11473. hash_test[1] = hash2;
  11474. hash_test[2] = hash3;
  11475. times = sizeof(hash_test) / sizeof(byte*);
  11476. for (i = 0; i < times; i++) {
  11477. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11478. }
  11479. /* Test bad args. */
  11480. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11481. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11482. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11483. wc_Sha256Free(&sha256);
  11484. #endif
  11485. return EXPECT_RESULT();
  11486. } /* END test_wc_Sha256Final */
  11487. /*
  11488. * Unit test function for wc_Sha256FinalRaw()
  11489. */
  11490. static int test_wc_Sha256FinalRaw(void)
  11491. {
  11492. EXPECT_DECLS;
  11493. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11494. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11495. !defined(WOLFSSL_NO_HASH_RAW)
  11496. wc_Sha256 sha256;
  11497. byte* hash_test[3];
  11498. byte hash1[WC_SHA256_DIGEST_SIZE];
  11499. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11500. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11501. int times, i;
  11502. /* Initialize */
  11503. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11504. hash_test[0] = hash1;
  11505. hash_test[1] = hash2;
  11506. hash_test[2] = hash3;
  11507. times = sizeof(hash_test) / sizeof(byte*);
  11508. for (i = 0; i < times; i++) {
  11509. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11510. }
  11511. /* Test bad args. */
  11512. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11513. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11514. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11515. wc_Sha256Free(&sha256);
  11516. #endif
  11517. return EXPECT_RESULT();
  11518. } /* END test_wc_Sha256FinalRaw */
  11519. /*
  11520. * Unit test function for wc_Sha256GetFlags()
  11521. */
  11522. static int test_wc_Sha256GetFlags(void)
  11523. {
  11524. EXPECT_DECLS;
  11525. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11526. wc_Sha256 sha256;
  11527. word32 flags = 0;
  11528. /* Initialize */
  11529. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11530. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11531. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11532. wc_Sha256Free(&sha256);
  11533. #endif
  11534. return EXPECT_RESULT();
  11535. } /* END test_wc_Sha256GetFlags */
  11536. /*
  11537. * Unit test function for wc_Sha256Free()
  11538. */
  11539. static int test_wc_Sha256Free(void)
  11540. {
  11541. EXPECT_DECLS;
  11542. #ifndef NO_SHA256
  11543. wc_Sha256Free(NULL);
  11544. /* Set result to SUCCESS. */
  11545. ExpectTrue(1);
  11546. #endif
  11547. return EXPECT_RESULT();
  11548. } /* END test_wc_Sha256Free */
  11549. /*
  11550. * Unit test function for wc_Sha256GetHash()
  11551. */
  11552. static int test_wc_Sha256GetHash(void)
  11553. {
  11554. EXPECT_DECLS;
  11555. #ifndef NO_SHA256
  11556. wc_Sha256 sha256;
  11557. byte hash1[WC_SHA256_DIGEST_SIZE];
  11558. /* Initialize */
  11559. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11560. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11561. /* test bad arguments*/
  11562. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11563. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11564. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11565. wc_Sha256Free(&sha256);
  11566. #endif
  11567. return EXPECT_RESULT();
  11568. } /* END test_wc_Sha256GetHash */
  11569. /*
  11570. * Unit test function for wc_Sha256Copy()
  11571. */
  11572. static int test_wc_Sha256Copy(void)
  11573. {
  11574. EXPECT_DECLS;
  11575. #ifndef NO_SHA256
  11576. wc_Sha256 sha256;
  11577. wc_Sha256 temp;
  11578. XMEMSET(&sha256, 0, sizeof(sha256));
  11579. XMEMSET(&temp, 0, sizeof(temp));
  11580. /* Initialize */
  11581. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11582. ExpectIntEQ(wc_InitSha256(&temp), 0);
  11583. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  11584. /* test bad arguments*/
  11585. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  11586. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  11587. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  11588. wc_Sha256Free(&sha256);
  11589. wc_Sha256Free(&temp);
  11590. #endif
  11591. return EXPECT_RESULT();
  11592. } /* END test_wc_Sha256Copy */
  11593. /*
  11594. * Testing wc_InitSha512()
  11595. */
  11596. static int test_wc_InitSha512(void)
  11597. {
  11598. EXPECT_DECLS;
  11599. #ifdef WOLFSSL_SHA512
  11600. wc_Sha512 sha512;
  11601. /* Test good arg. */
  11602. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11603. /* Test bad arg. */
  11604. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  11605. wc_Sha512Free(&sha512);
  11606. #endif
  11607. return EXPECT_RESULT();
  11608. } /* END test_wc_InitSha512 */
  11609. /*
  11610. * wc_Sha512Update() test.
  11611. */
  11612. static int test_wc_Sha512Update(void)
  11613. {
  11614. EXPECT_DECLS;
  11615. #ifdef WOLFSSL_SHA512
  11616. wc_Sha512 sha512;
  11617. byte hash[WC_SHA512_DIGEST_SIZE];
  11618. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  11619. testVector a, b, c;
  11620. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11621. /* Input. */
  11622. a.input = "a";
  11623. a.inLen = XSTRLEN(a.input);
  11624. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  11625. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  11626. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  11627. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11628. /* Update input. */
  11629. a.input = "abc";
  11630. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11631. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11632. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11633. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11634. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11635. a.inLen = XSTRLEN(a.input);
  11636. a.outLen = XSTRLEN(a.output);
  11637. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  11638. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11639. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  11640. /* Unaligned check. */
  11641. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  11642. 0);
  11643. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  11644. /* Try passing in bad values */
  11645. b.input = NULL;
  11646. b.inLen = 0;
  11647. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  11648. c.input = NULL;
  11649. c.inLen = WC_SHA512_DIGEST_SIZE;
  11650. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11651. BAD_FUNC_ARG);
  11652. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  11653. BAD_FUNC_ARG);
  11654. wc_Sha512Free(&sha512);
  11655. #endif
  11656. return EXPECT_RESULT();
  11657. } /* END test_wc_Sha512Update */
  11658. #ifdef WOLFSSL_SHA512
  11659. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11660. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11661. /* Performs test for
  11662. * - wc_Sha512Final/wc_Sha512FinalRaw
  11663. * - wc_Sha512_224Final/wc_Sha512_224Final
  11664. * - wc_Sha512_256Final/wc_Sha512_256Final
  11665. * parameter:
  11666. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11667. * WC_HASH_TYPE_SHA512_256
  11668. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11669. *return 0 on success
  11670. */
  11671. static int test_Sha512_Family_Final(int type, int isRaw)
  11672. {
  11673. EXPECT_DECLS;
  11674. wc_Sha512 sha512;
  11675. byte* hash_test[3];
  11676. byte hash1[WC_SHA512_DIGEST_SIZE];
  11677. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11678. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11679. int times, i;
  11680. int(*initFp)(wc_Sha512*);
  11681. int(*finalFp)(wc_Sha512*, byte*);
  11682. void(*freeFp)(wc_Sha512*);
  11683. if (type == WC_HASH_TYPE_SHA512) {
  11684. initFp = wc_InitSha512;
  11685. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11686. !defined(WOLFSSL_NO_HASH_RAW)
  11687. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11688. #else
  11689. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11690. #endif
  11691. freeFp = wc_Sha512Free;
  11692. }
  11693. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11694. #if !defined(WOLFSSL_NOSHA512_224)
  11695. else if (type == WC_HASH_TYPE_SHA512_224) {
  11696. initFp = wc_InitSha512_224;
  11697. #if !defined(WOLFSSL_NO_HASH_RAW)
  11698. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11699. #else
  11700. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11701. #endif
  11702. freeFp = wc_Sha512_224Free;
  11703. }
  11704. #endif
  11705. #if !defined(WOLFSSL_NOSHA512_256)
  11706. else if (type == WC_HASH_TYPE_SHA512_256) {
  11707. initFp = wc_InitSha512_256;
  11708. #if !defined(WOLFSSL_NO_HASH_RAW)
  11709. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11710. #else
  11711. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11712. #endif
  11713. freeFp = wc_Sha512_256Free;
  11714. }
  11715. #endif
  11716. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11717. else
  11718. return TEST_FAIL;
  11719. /* Initialize */
  11720. ExpectIntEQ(initFp(&sha512), 0);
  11721. hash_test[0] = hash1;
  11722. hash_test[1] = hash2;
  11723. hash_test[2] = hash3;
  11724. times = sizeof(hash_test) / sizeof(byte *);
  11725. /* Good test args. */
  11726. for (i = 0; i < times; i++) {
  11727. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  11728. }
  11729. /* Test bad args. */
  11730. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  11731. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  11732. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  11733. freeFp(&sha512);
  11734. return EXPECT_RESULT();
  11735. }
  11736. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11737. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11738. #endif /* WOLFSSL_SHA512 */
  11739. /*
  11740. * Unit test function for wc_Sha512Final()
  11741. */
  11742. static int test_wc_Sha512Final(void)
  11743. {
  11744. EXPECT_DECLS;
  11745. #ifdef WOLFSSL_SHA512
  11746. wc_Sha512 sha512;
  11747. byte* hash_test[3];
  11748. byte hash1[WC_SHA512_DIGEST_SIZE];
  11749. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11750. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11751. int times, i;
  11752. /* Initialize */
  11753. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11754. hash_test[0] = hash1;
  11755. hash_test[1] = hash2;
  11756. hash_test[2] = hash3;
  11757. times = sizeof(hash_test) / sizeof(byte *);
  11758. for (i = 0; i < times; i++) {
  11759. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  11760. }
  11761. /* Test bad args. */
  11762. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  11763. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  11764. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  11765. wc_Sha512Free(&sha512);
  11766. #endif
  11767. return EXPECT_RESULT();
  11768. } /* END test_wc_Sha512Final */
  11769. /*
  11770. * Unit test function for wc_Sha512GetFlags()
  11771. */
  11772. static int test_wc_Sha512GetFlags(void)
  11773. {
  11774. EXPECT_DECLS;
  11775. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11776. wc_Sha512 sha512;
  11777. word32 flags = 0;
  11778. /* Initialize */
  11779. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11780. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  11781. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  11782. wc_Sha512Free(&sha512);
  11783. #endif
  11784. return EXPECT_RESULT();
  11785. } /* END test_wc_Sha512GetFlags */
  11786. /*
  11787. * Unit test function for wc_Sha512FinalRaw()
  11788. */
  11789. static int test_wc_Sha512FinalRaw(void)
  11790. {
  11791. EXPECT_DECLS;
  11792. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11793. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11794. !defined(WOLFSSL_NO_HASH_RAW)
  11795. wc_Sha512 sha512;
  11796. byte* hash_test[3];
  11797. byte hash1[WC_SHA512_DIGEST_SIZE];
  11798. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11799. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11800. int times, i;
  11801. /* Initialize */
  11802. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11803. hash_test[0] = hash1;
  11804. hash_test[1] = hash2;
  11805. hash_test[2] = hash3;
  11806. times = sizeof(hash_test) / sizeof(byte*);
  11807. /* Good test args. */
  11808. for (i = 0; i < times; i++) {
  11809. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  11810. }
  11811. /* Test bad args. */
  11812. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11813. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11814. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  11815. wc_Sha512Free(&sha512);
  11816. #endif
  11817. return EXPECT_RESULT();
  11818. } /* END test_wc_Sha512FinalRaw */
  11819. /*
  11820. * Unit test function for wc_Sha512Free()
  11821. */
  11822. static int test_wc_Sha512Free(void)
  11823. {
  11824. EXPECT_DECLS;
  11825. #ifdef WOLFSSL_SHA512
  11826. wc_Sha512Free(NULL);
  11827. /* Set result to SUCCESS. */
  11828. ExpectTrue(1);
  11829. #endif
  11830. return EXPECT_RESULT();
  11831. } /* END test_wc_Sha512Free */
  11832. #ifdef WOLFSSL_SHA512
  11833. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11834. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11835. static int test_Sha512_Family_GetHash(int type )
  11836. {
  11837. EXPECT_DECLS;
  11838. int(*initFp)(wc_Sha512*);
  11839. int(*ghashFp)(wc_Sha512*, byte*);
  11840. wc_Sha512 sha512;
  11841. byte hash1[WC_SHA512_DIGEST_SIZE];
  11842. if (type == WC_HASH_TYPE_SHA512) {
  11843. initFp = wc_InitSha512;
  11844. ghashFp = wc_Sha512GetHash;
  11845. }
  11846. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11847. #if !defined(WOLFSSL_NOSHA512_224)
  11848. else if (type == WC_HASH_TYPE_SHA512_224) {
  11849. initFp = wc_InitSha512_224;
  11850. ghashFp = wc_Sha512_224GetHash;
  11851. }
  11852. #endif
  11853. #if !defined(WOLFSSL_NOSHA512_256)
  11854. else if (type == WC_HASH_TYPE_SHA512_256) {
  11855. initFp = wc_InitSha512_256;
  11856. ghashFp = wc_Sha512_256GetHash;
  11857. }
  11858. #endif
  11859. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11860. else {
  11861. initFp = NULL;
  11862. ghashFp = NULL;
  11863. }
  11864. if (initFp == NULL || ghashFp == NULL)
  11865. return TEST_FAIL;
  11866. ExpectIntEQ(initFp(&sha512), 0);
  11867. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  11868. /* test bad arguments*/
  11869. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  11870. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  11871. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  11872. wc_Sha512Free(&sha512);
  11873. return EXPECT_RESULT();
  11874. }
  11875. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11876. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11877. #endif /* WOLFSSL_SHA512 */
  11878. /*
  11879. * Unit test function for wc_Sha512GetHash()
  11880. */
  11881. static int test_wc_Sha512GetHash(void)
  11882. {
  11883. EXPECT_DECLS;
  11884. #ifdef WOLFSSL_SHA512
  11885. wc_Sha512 sha512;
  11886. byte hash1[WC_SHA512_DIGEST_SIZE];
  11887. /* Initialize */
  11888. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11889. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  11890. /* test bad arguments*/
  11891. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  11892. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  11893. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  11894. wc_Sha512Free(&sha512);
  11895. #endif
  11896. return EXPECT_RESULT();
  11897. } /* END test_wc_Sha512GetHash */
  11898. /*
  11899. * Unit test function for wc_Sha512Copy()
  11900. */
  11901. static int test_wc_Sha512Copy(void)
  11902. {
  11903. EXPECT_DECLS;
  11904. #ifdef WOLFSSL_SHA512
  11905. wc_Sha512 sha512;
  11906. wc_Sha512 temp;
  11907. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11908. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11909. /* Initialize */
  11910. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11911. ExpectIntEQ(wc_InitSha512(&temp), 0);
  11912. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  11913. /* test bad arguments*/
  11914. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  11915. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  11916. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  11917. wc_Sha512Free(&sha512);
  11918. wc_Sha512Free(&temp);
  11919. #endif
  11920. return EXPECT_RESULT();
  11921. } /* END test_wc_Sha512Copy */
  11922. static int test_wc_InitSha512_224(void)
  11923. {
  11924. EXPECT_DECLS;
  11925. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11926. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11927. wc_Sha512 sha512;
  11928. /* Test good arg. */
  11929. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11930. /* Test bad arg. */
  11931. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  11932. wc_Sha512_224Free(&sha512);
  11933. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11934. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11935. return EXPECT_RESULT();
  11936. }
  11937. static int test_wc_Sha512_224Update(void)
  11938. {
  11939. EXPECT_DECLS;
  11940. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11941. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11942. wc_Sha512 sha512;
  11943. byte hash[WC_SHA512_DIGEST_SIZE];
  11944. testVector a, c;
  11945. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11946. /* Input. */
  11947. a.input = "a";
  11948. a.inLen = XSTRLEN(a.input);
  11949. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  11950. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  11951. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11952. 0);
  11953. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11954. /* Update input. */
  11955. a.input = "abc";
  11956. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11957. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11958. a.inLen = XSTRLEN(a.input);
  11959. a.outLen = XSTRLEN(a.output);
  11960. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11961. 0);
  11962. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11963. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  11964. c.input = NULL;
  11965. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11966. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11967. BAD_FUNC_ARG);
  11968. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11969. BAD_FUNC_ARG);
  11970. wc_Sha512_224Free(&sha512);
  11971. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11972. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11973. return EXPECT_RESULT();
  11974. }
  11975. static int test_wc_Sha512_224Final(void)
  11976. {
  11977. EXPECT_DECLS;
  11978. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11979. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11980. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  11981. TEST_SUCCESS);
  11982. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11983. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11984. return EXPECT_RESULT();
  11985. }
  11986. static int test_wc_Sha512_224GetFlags(void)
  11987. {
  11988. EXPECT_DECLS;
  11989. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11990. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11991. wc_Sha512 sha512;
  11992. wc_Sha512 copy;
  11993. word32 flags = 0;
  11994. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11995. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11996. /* Initialize */
  11997. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11998. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  11999. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12000. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12001. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12002. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12003. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12004. wc_Sha512_224Free(&copy);
  12005. wc_Sha512_224Free(&sha512);
  12006. #endif
  12007. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12008. return EXPECT_RESULT();
  12009. }
  12010. static int test_wc_Sha512_224FinalRaw(void)
  12011. {
  12012. EXPECT_DECLS;
  12013. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12014. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12015. !defined(WOLFSSL_NO_HASH_RAW)
  12016. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12017. TEST_SUCCESS);
  12018. #endif
  12019. return EXPECT_RESULT();
  12020. }
  12021. static int test_wc_Sha512_224Free(void)
  12022. {
  12023. EXPECT_DECLS;
  12024. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12025. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12026. wc_Sha512_224Free(NULL);
  12027. /* Set result to SUCCESS. */
  12028. ExpectTrue(1);
  12029. #endif
  12030. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12031. return EXPECT_RESULT();
  12032. }
  12033. static int test_wc_Sha512_224GetHash(void)
  12034. {
  12035. EXPECT_DECLS;
  12036. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12037. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12038. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12039. TEST_SUCCESS);
  12040. #endif
  12041. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12042. return EXPECT_RESULT();
  12043. }
  12044. static int test_wc_Sha512_224Copy(void)
  12045. {
  12046. EXPECT_DECLS;
  12047. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12048. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12049. wc_Sha512 sha512;
  12050. wc_Sha512 temp;
  12051. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12052. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12053. /* Initialize */
  12054. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12055. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12056. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12057. /* test bad arguments*/
  12058. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12059. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12060. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12061. wc_Sha512_224Free(&sha512);
  12062. wc_Sha512_224Free(&temp);
  12063. #endif
  12064. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12065. return EXPECT_RESULT();
  12066. }
  12067. static int test_wc_InitSha512_256(void)
  12068. {
  12069. EXPECT_DECLS;
  12070. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12071. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12072. wc_Sha512 sha512;
  12073. /* Test good arg. */
  12074. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12075. /* Test bad arg. */
  12076. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12077. wc_Sha512_256Free(&sha512);
  12078. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12079. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12080. return EXPECT_RESULT();
  12081. }
  12082. static int test_wc_Sha512_256Update(void)
  12083. {
  12084. EXPECT_DECLS;
  12085. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12086. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12087. wc_Sha512 sha512;
  12088. byte hash[WC_SHA512_DIGEST_SIZE];
  12089. testVector a, c;
  12090. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12091. /* Input. */
  12092. a.input = "a";
  12093. a.inLen = XSTRLEN(a.input);
  12094. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12095. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12096. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12097. 0);
  12098. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12099. /* Update input. */
  12100. a.input = "abc";
  12101. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12102. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12103. "\x07\xe7\xaf\x23";
  12104. a.inLen = XSTRLEN(a.input);
  12105. a.outLen = XSTRLEN(a.output);
  12106. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12107. 0);
  12108. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12109. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12110. c.input = NULL;
  12111. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12112. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12113. BAD_FUNC_ARG);
  12114. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12115. BAD_FUNC_ARG);
  12116. wc_Sha512_256Free(&sha512);
  12117. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12118. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12119. return EXPECT_RESULT();
  12120. }
  12121. static int test_wc_Sha512_256Final(void)
  12122. {
  12123. EXPECT_DECLS;
  12124. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12125. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12126. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12127. TEST_SUCCESS);
  12128. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12129. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12130. return EXPECT_RESULT();
  12131. }
  12132. static int test_wc_Sha512_256GetFlags(void)
  12133. {
  12134. EXPECT_DECLS;
  12135. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12136. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12137. wc_Sha512 sha512, copy;
  12138. word32 flags = 0;
  12139. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12140. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12141. /* Initialize */
  12142. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12143. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12144. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12145. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12146. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12147. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12148. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12149. wc_Sha512_256Free(&sha512);
  12150. #endif
  12151. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12152. return EXPECT_RESULT();
  12153. }
  12154. static int test_wc_Sha512_256FinalRaw(void)
  12155. {
  12156. EXPECT_DECLS;
  12157. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12158. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12159. !defined(WOLFSSL_NO_HASH_RAW)
  12160. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12161. TEST_SUCCESS);
  12162. #endif
  12163. return EXPECT_RESULT();
  12164. }
  12165. static int test_wc_Sha512_256Free(void)
  12166. {
  12167. EXPECT_DECLS;
  12168. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12169. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12170. wc_Sha512_256Free(NULL);
  12171. /* Set result to SUCCESS. */
  12172. ExpectTrue(1);
  12173. #endif
  12174. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12175. return EXPECT_RESULT();
  12176. }
  12177. static int test_wc_Sha512_256GetHash(void)
  12178. {
  12179. EXPECT_DECLS;
  12180. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12181. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12182. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12183. TEST_SUCCESS);
  12184. #endif
  12185. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12186. return EXPECT_RESULT();
  12187. }
  12188. static int test_wc_Sha512_256Copy(void)
  12189. {
  12190. EXPECT_DECLS;
  12191. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12192. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12193. wc_Sha512 sha512;
  12194. wc_Sha512 temp;
  12195. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12196. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12197. /* Initialize */
  12198. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12199. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12200. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12201. /* test bad arguments*/
  12202. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12203. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12204. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12205. wc_Sha512_256Free(&sha512);
  12206. wc_Sha512_256Free(&temp);
  12207. #endif
  12208. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12209. return EXPECT_RESULT();
  12210. }
  12211. /*
  12212. * Testing wc_InitSha384()
  12213. */
  12214. static int test_wc_InitSha384(void)
  12215. {
  12216. EXPECT_DECLS;
  12217. #ifdef WOLFSSL_SHA384
  12218. wc_Sha384 sha384;
  12219. /* Test good arg. */
  12220. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12221. /* Test bad arg. */
  12222. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12223. wc_Sha384Free(&sha384);
  12224. #endif
  12225. return EXPECT_RESULT();
  12226. } /* END test_wc_InitSha384 */
  12227. /*
  12228. * test wc_Sha384Update()
  12229. */
  12230. static int test_wc_Sha384Update(void)
  12231. {
  12232. EXPECT_DECLS;
  12233. #ifdef WOLFSSL_SHA384
  12234. wc_Sha384 sha384;
  12235. byte hash[WC_SHA384_DIGEST_SIZE];
  12236. testVector a, b, c;
  12237. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12238. /* Input */
  12239. a.input = "a";
  12240. a.inLen = XSTRLEN(a.input);
  12241. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12242. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12243. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12244. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12245. /* Update input. */
  12246. a.input = "abc";
  12247. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12248. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12249. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12250. "\xc8\x25\xa7";
  12251. a.inLen = XSTRLEN(a.input);
  12252. a.outLen = XSTRLEN(a.output);
  12253. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12254. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12255. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12256. /* Pass in bad values. */
  12257. b.input = NULL;
  12258. b.inLen = 0;
  12259. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12260. c.input = NULL;
  12261. c.inLen = WC_SHA384_DIGEST_SIZE;
  12262. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12263. BAD_FUNC_ARG);
  12264. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12265. BAD_FUNC_ARG);
  12266. wc_Sha384Free(&sha384);
  12267. #endif
  12268. return EXPECT_RESULT();
  12269. } /* END test_wc_Sha384Update */
  12270. /*
  12271. * Unit test function for wc_Sha384Final();
  12272. */
  12273. static int test_wc_Sha384Final(void)
  12274. {
  12275. EXPECT_DECLS;
  12276. #ifdef WOLFSSL_SHA384
  12277. wc_Sha384 sha384;
  12278. byte* hash_test[3];
  12279. byte hash1[WC_SHA384_DIGEST_SIZE];
  12280. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12281. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12282. int times, i;
  12283. /* Initialize */
  12284. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12285. hash_test[0] = hash1;
  12286. hash_test[1] = hash2;
  12287. hash_test[2] = hash3;
  12288. times = sizeof(hash_test) / sizeof(byte*);
  12289. /* Good test args. */
  12290. for (i = 0; i < times; i++) {
  12291. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12292. }
  12293. /* Test bad args. */
  12294. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12295. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12296. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12297. wc_Sha384Free(&sha384);
  12298. #endif
  12299. return EXPECT_RESULT();
  12300. } /* END test_wc_Sha384Final */
  12301. /*
  12302. * Unit test function for wc_Sha384GetFlags()
  12303. */
  12304. static int test_wc_Sha384GetFlags(void)
  12305. {
  12306. EXPECT_DECLS;
  12307. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12308. wc_Sha384 sha384;
  12309. word32 flags = 0;
  12310. /* Initialize */
  12311. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12312. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12313. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12314. wc_Sha384Free(&sha384);
  12315. #endif
  12316. return EXPECT_RESULT();
  12317. } /* END test_wc_Sha384GetFlags */
  12318. /*
  12319. * Unit test function for wc_Sha384FinalRaw()
  12320. */
  12321. static int test_wc_Sha384FinalRaw(void)
  12322. {
  12323. EXPECT_DECLS;
  12324. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12325. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12326. !defined(WOLFSSL_NO_HASH_RAW)
  12327. wc_Sha384 sha384;
  12328. byte* hash_test[3];
  12329. byte hash1[WC_SHA384_DIGEST_SIZE];
  12330. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12331. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12332. int times, i;
  12333. /* Initialize */
  12334. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12335. hash_test[0] = hash1;
  12336. hash_test[1] = hash2;
  12337. hash_test[2] = hash3;
  12338. times = sizeof(hash_test) / sizeof(byte*);
  12339. /* Good test args. */
  12340. for (i = 0; i < times; i++) {
  12341. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12342. }
  12343. /* Test bad args. */
  12344. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12345. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12346. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12347. wc_Sha384Free(&sha384);
  12348. #endif
  12349. return EXPECT_RESULT();
  12350. } /* END test_wc_Sha384FinalRaw */
  12351. /*
  12352. * Unit test function for wc_Sha384Free()
  12353. */
  12354. static int test_wc_Sha384Free(void)
  12355. {
  12356. EXPECT_DECLS;
  12357. #ifdef WOLFSSL_SHA384
  12358. wc_Sha384Free(NULL);
  12359. /* Set result to SUCCESS. */
  12360. ExpectTrue(1);
  12361. #endif
  12362. return EXPECT_RESULT();
  12363. } /* END test_wc_Sha384Free */
  12364. /*
  12365. * Unit test function for wc_Sha384GetHash()
  12366. */
  12367. static int test_wc_Sha384GetHash(void)
  12368. {
  12369. EXPECT_DECLS;
  12370. #ifdef WOLFSSL_SHA384
  12371. wc_Sha384 sha384;
  12372. byte hash1[WC_SHA384_DIGEST_SIZE];
  12373. /* Initialize */
  12374. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12375. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12376. /* test bad arguments*/
  12377. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12378. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12379. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12380. wc_Sha384Free(&sha384);
  12381. #endif
  12382. return EXPECT_RESULT();
  12383. } /* END test_wc_Sha384GetHash */
  12384. /*
  12385. * Unit test function for wc_Sha384Copy()
  12386. */
  12387. static int test_wc_Sha384Copy(void)
  12388. {
  12389. EXPECT_DECLS;
  12390. #ifdef WOLFSSL_SHA384
  12391. wc_Sha384 sha384;
  12392. wc_Sha384 temp;
  12393. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12394. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12395. /* Initialize */
  12396. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12397. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12398. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12399. /* test bad arguments*/
  12400. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12401. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12402. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12403. wc_Sha384Free(&sha384);
  12404. wc_Sha384Free(&temp);
  12405. #endif
  12406. return EXPECT_RESULT();
  12407. } /* END test_wc_Sha384Copy */
  12408. /*
  12409. * Testing wc_InitSha224();
  12410. */
  12411. static int test_wc_InitSha224(void)
  12412. {
  12413. EXPECT_DECLS;
  12414. #ifdef WOLFSSL_SHA224
  12415. wc_Sha224 sha224;
  12416. /* Test good arg. */
  12417. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12418. /* Test bad arg. */
  12419. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12420. wc_Sha224Free(&sha224);
  12421. #endif
  12422. return EXPECT_RESULT();
  12423. } /* END test_wc_InitSha224 */
  12424. /*
  12425. * Unit test on wc_Sha224Update
  12426. */
  12427. static int test_wc_Sha224Update(void)
  12428. {
  12429. EXPECT_DECLS;
  12430. #ifdef WOLFSSL_SHA224
  12431. wc_Sha224 sha224;
  12432. byte hash[WC_SHA224_DIGEST_SIZE];
  12433. testVector a, b, c;
  12434. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12435. /* Input. */
  12436. a.input = "a";
  12437. a.inLen = XSTRLEN(a.input);
  12438. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12439. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12440. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12441. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12442. /* Update input. */
  12443. a.input = "abc";
  12444. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12445. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12446. a.inLen = XSTRLEN(a.input);
  12447. a.outLen = XSTRLEN(a.output);
  12448. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12449. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12450. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12451. /* Pass in bad values. */
  12452. b.input = NULL;
  12453. b.inLen = 0;
  12454. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12455. c.input = NULL;
  12456. c.inLen = WC_SHA224_DIGEST_SIZE;
  12457. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12458. BAD_FUNC_ARG);
  12459. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12460. BAD_FUNC_ARG);
  12461. wc_Sha224Free(&sha224);
  12462. #endif
  12463. return EXPECT_RESULT();
  12464. } /* END test_wc_Sha224Update */
  12465. /*
  12466. * Unit test for wc_Sha224Final();
  12467. */
  12468. static int test_wc_Sha224Final(void)
  12469. {
  12470. EXPECT_DECLS;
  12471. #ifdef WOLFSSL_SHA224
  12472. wc_Sha224 sha224;
  12473. byte* hash_test[3];
  12474. byte hash1[WC_SHA224_DIGEST_SIZE];
  12475. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12476. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12477. int times, i;
  12478. /* Initialize */
  12479. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12480. hash_test[0] = hash1;
  12481. hash_test[1] = hash2;
  12482. hash_test[2] = hash3;
  12483. times = sizeof(hash_test) / sizeof(byte*);
  12484. /* Good test args. */
  12485. /* Testing oversized buffers. */
  12486. for (i = 0; i < times; i++) {
  12487. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12488. }
  12489. /* Test bad args. */
  12490. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12491. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12492. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12493. wc_Sha224Free(&sha224);
  12494. #endif
  12495. return EXPECT_RESULT();
  12496. } /* END test_wc_Sha224Final */
  12497. /*
  12498. * Unit test function for wc_Sha224SetFlags()
  12499. */
  12500. static int test_wc_Sha224SetFlags(void)
  12501. {
  12502. EXPECT_DECLS;
  12503. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12504. wc_Sha224 sha224;
  12505. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12506. /* Initialize */
  12507. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12508. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12509. flags = 0;
  12510. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12511. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12512. wc_Sha224Free(&sha224);
  12513. #endif
  12514. return EXPECT_RESULT();
  12515. } /* END test_wc_Sha224SetFlags */
  12516. /*
  12517. * Unit test function for wc_Sha224GetFlags()
  12518. */
  12519. static int test_wc_Sha224GetFlags(void)
  12520. {
  12521. EXPECT_DECLS;
  12522. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12523. wc_Sha224 sha224;
  12524. word32 flags = 0;
  12525. /* Initialize */
  12526. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12527. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12528. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12529. wc_Sha224Free(&sha224);
  12530. #endif
  12531. return EXPECT_RESULT();
  12532. } /* END test_wc_Sha224GetFlags */
  12533. /*
  12534. * Unit test function for wc_Sha224Free()
  12535. */
  12536. static int test_wc_Sha224Free(void)
  12537. {
  12538. EXPECT_DECLS;
  12539. #ifdef WOLFSSL_SHA224
  12540. wc_Sha224Free(NULL);
  12541. /* Set result to SUCCESS. */
  12542. ExpectTrue(1);
  12543. #endif
  12544. return EXPECT_RESULT();
  12545. } /* END test_wc_Sha224Free */
  12546. /*
  12547. * Unit test function for wc_Sha224GetHash()
  12548. */
  12549. static int test_wc_Sha224GetHash(void)
  12550. {
  12551. EXPECT_DECLS;
  12552. #ifdef WOLFSSL_SHA224
  12553. wc_Sha224 sha224;
  12554. byte hash1[WC_SHA224_DIGEST_SIZE];
  12555. /* Initialize */
  12556. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12557. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12558. /* test bad arguments*/
  12559. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12560. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12561. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12562. wc_Sha224Free(&sha224);
  12563. #endif
  12564. return EXPECT_RESULT();
  12565. } /* END test_wc_Sha224GetHash */
  12566. /*
  12567. * Unit test function for wc_Sha224Copy()
  12568. */
  12569. static int test_wc_Sha224Copy(void)
  12570. {
  12571. EXPECT_DECLS;
  12572. #ifdef WOLFSSL_SHA224
  12573. wc_Sha224 sha224;
  12574. wc_Sha224 temp;
  12575. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  12576. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  12577. /* Initialize */
  12578. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12579. ExpectIntEQ(wc_InitSha224(&temp), 0);
  12580. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  12581. /* test bad arguments*/
  12582. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  12583. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  12584. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  12585. wc_Sha224Free(&sha224);
  12586. wc_Sha224Free(&temp);
  12587. #endif
  12588. return EXPECT_RESULT();
  12589. } /* END test_wc_Sha224Copy */
  12590. /*
  12591. * Testing wc_InitRipeMd()
  12592. */
  12593. static int test_wc_InitRipeMd(void)
  12594. {
  12595. EXPECT_DECLS;
  12596. #ifdef WOLFSSL_RIPEMD
  12597. RipeMd ripemd;
  12598. /* Test good arg. */
  12599. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12600. /* Test bad arg. */
  12601. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  12602. #endif
  12603. return EXPECT_RESULT();
  12604. } /* END test_wc_InitRipeMd */
  12605. /*
  12606. * Testing wc_RipeMdUpdate()
  12607. */
  12608. static int test_wc_RipeMdUpdate(void)
  12609. {
  12610. EXPECT_DECLS;
  12611. #ifdef WOLFSSL_RIPEMD
  12612. RipeMd ripemd;
  12613. byte hash[RIPEMD_DIGEST_SIZE];
  12614. testVector a, b, c;
  12615. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12616. /* Input */
  12617. a.input = "a";
  12618. a.inLen = XSTRLEN(a.input);
  12619. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12620. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12621. /* Update input. */
  12622. a.input = "abc";
  12623. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12624. "\xb0\x87\xf1\x5a\x0b\xfc";
  12625. a.inLen = XSTRLEN(a.input);
  12626. a.outLen = XSTRLEN(a.output);
  12627. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12628. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12629. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  12630. /* Pass in bad values. */
  12631. b.input = NULL;
  12632. b.inLen = 0;
  12633. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  12634. c.input = NULL;
  12635. c.inLen = RIPEMD_DIGEST_SIZE;
  12636. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  12637. BAD_FUNC_ARG);
  12638. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12639. BAD_FUNC_ARG);
  12640. #endif
  12641. return EXPECT_RESULT();
  12642. } /* END test_wc_RipeMdUdpate */
  12643. /*
  12644. * Unit test function for wc_RipeMdFinal()
  12645. */
  12646. static int test_wc_RipeMdFinal(void)
  12647. {
  12648. EXPECT_DECLS;
  12649. #ifdef WOLFSSL_RIPEMD
  12650. RipeMd ripemd;
  12651. byte* hash_test[3];
  12652. byte hash1[RIPEMD_DIGEST_SIZE];
  12653. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12654. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12655. int times, i;
  12656. /* Initialize */
  12657. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12658. hash_test[0] = hash1;
  12659. hash_test[1] = hash2;
  12660. hash_test[2] = hash3;
  12661. times = sizeof(hash_test) / sizeof(byte*);
  12662. /* Testing oversized buffers. */
  12663. for (i = 0; i < times; i++) {
  12664. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  12665. }
  12666. /* Test bad args. */
  12667. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  12668. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  12669. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  12670. #endif
  12671. return EXPECT_RESULT();
  12672. } /* END test_wc_RipeMdFinal */
  12673. /*
  12674. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12675. * wc_InitSha3_512
  12676. */
  12677. static int test_wc_InitSha3(void)
  12678. {
  12679. EXPECT_DECLS;
  12680. #if defined(WOLFSSL_SHA3)
  12681. wc_Sha3 sha3;
  12682. (void)sha3;
  12683. #if !defined(WOLFSSL_NOSHA3_224)
  12684. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12685. /* Test bad args. */
  12686. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12687. wc_Sha3_224_Free(&sha3);
  12688. #endif /* NOSHA3_224 */
  12689. #if !defined(WOLFSSL_NOSHA3_256)
  12690. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12691. /* Test bad args. */
  12692. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12693. wc_Sha3_256_Free(&sha3);
  12694. #endif /* NOSHA3_256 */
  12695. #if !defined(WOLFSSL_NOSHA3_384)
  12696. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12697. /* Test bad args. */
  12698. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12699. wc_Sha3_384_Free(&sha3);
  12700. #endif /* NOSHA3_384 */
  12701. #if !defined(WOLFSSL_NOSHA3_512)
  12702. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12703. /* Test bad args. */
  12704. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12705. wc_Sha3_512_Free(&sha3);
  12706. #endif /* NOSHA3_512 */
  12707. #endif
  12708. return EXPECT_RESULT();
  12709. } /* END test_wc_InitSha3 */
  12710. /*
  12711. * Testing wc_Sha3_Update()
  12712. */
  12713. static int testing_wc_Sha3_Update(void)
  12714. {
  12715. EXPECT_DECLS;
  12716. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12717. !defined(WOLFSSL_AFALG_XILINX)
  12718. wc_Sha3 sha3;
  12719. byte msg[] = "Everybody's working for the weekend.";
  12720. byte msg2[] = "Everybody gets Friday off.";
  12721. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12722. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12723. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12724. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12725. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12726. word32 msglen = sizeof(msg) - 1;
  12727. word32 msg2len = sizeof(msg2);
  12728. word32 msgCmplen = sizeof(msgCmp);
  12729. #if !defined(WOLFSSL_NOSHA3_224)
  12730. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12731. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  12732. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12733. ExpectTrue(sha3.i == msglen);
  12734. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12735. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12736. /* Pass bad args. */
  12737. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12738. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12739. wc_Sha3_224_Free(&sha3);
  12740. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12741. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  12742. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12743. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12744. wc_Sha3_224_Free(&sha3);
  12745. #endif /* SHA3_224 */
  12746. #if !defined(WOLFSSL_NOSHA3_256)
  12747. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12748. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  12749. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12750. ExpectTrue(sha3.i == msglen);
  12751. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12752. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12753. /* Pass bad args. */
  12754. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12755. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12756. wc_Sha3_256_Free(&sha3);
  12757. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12758. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  12759. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12760. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12761. wc_Sha3_256_Free(&sha3);
  12762. #endif /* SHA3_256 */
  12763. #if !defined(WOLFSSL_NOSHA3_384)
  12764. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12765. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  12766. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12767. ExpectTrue(sha3.i == msglen);
  12768. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12769. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12770. /* Pass bad args. */
  12771. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12772. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12773. wc_Sha3_384_Free(&sha3);
  12774. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12775. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  12776. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12777. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12778. wc_Sha3_384_Free(&sha3);
  12779. #endif /* SHA3_384 */
  12780. #if !defined(WOLFSSL_NOSHA3_512)
  12781. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12782. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  12783. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12784. ExpectTrue(sha3.i == msglen);
  12785. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12786. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12787. /* Pass bad args. */
  12788. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12789. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12790. wc_Sha3_512_Free(&sha3);
  12791. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12792. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  12793. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12794. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12795. wc_Sha3_512_Free(&sha3);
  12796. #endif /* SHA3_512 */
  12797. #endif /* WOLFSSL_SHA3 */
  12798. return EXPECT_RESULT();
  12799. } /* END testing_wc_Sha3_Update */
  12800. /*
  12801. * Testing wc_Sha3_224_Final()
  12802. */
  12803. static int test_wc_Sha3_224_Final(void)
  12804. {
  12805. EXPECT_DECLS;
  12806. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12807. wc_Sha3 sha3;
  12808. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12809. "nopnopq";
  12810. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12811. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12812. "\x64\xea\xd0\xfc\xce\x33";
  12813. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12814. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12815. /* Init stack variables. */
  12816. XMEMSET(hash, 0, sizeof(hash));
  12817. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12818. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12819. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12820. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  12821. /* Test bad args. */
  12822. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  12823. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  12824. wc_Sha3_224_Free(&sha3);
  12825. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12826. /* Init stack variables. */
  12827. XMEMSET(hash, 0, sizeof(hash));
  12828. XMEMSET(hashRet, 0, sizeof(hashRet));
  12829. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12830. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  12831. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12832. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  12833. /* Test bad args. */
  12834. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12835. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12836. wc_Sha3_224_Free(&sha3);
  12837. #endif
  12838. return EXPECT_RESULT();
  12839. } /* END test_wc_Sha3_224_Final */
  12840. /*
  12841. * Testing wc_Sha3_256_Final()
  12842. */
  12843. static int test_wc_Sha3_256_Final(void)
  12844. {
  12845. EXPECT_DECLS;
  12846. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12847. wc_Sha3 sha3;
  12848. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12849. "nopnopq";
  12850. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12851. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12852. "\xdd\x97\x49\x6d\x33\x76";
  12853. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12854. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12855. /* Init stack variables. */
  12856. XMEMSET(hash, 0, sizeof(hash));
  12857. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12858. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12859. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12860. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  12861. /* Test bad args. */
  12862. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  12863. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  12864. wc_Sha3_256_Free(&sha3);
  12865. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12866. /* Init stack variables. */
  12867. XMEMSET(hash, 0, sizeof(hash));
  12868. XMEMSET(hashRet, 0, sizeof(hashRet));
  12869. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12870. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  12871. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12872. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  12873. /* Test bad args. */
  12874. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12875. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12876. wc_Sha3_256_Free(&sha3);
  12877. #endif
  12878. return EXPECT_RESULT();
  12879. } /* END test_wc_Sha3_256_Final */
  12880. /*
  12881. * Testing wc_Sha3_384_Final()
  12882. */
  12883. static int test_wc_Sha3_384_Final(void)
  12884. {
  12885. EXPECT_DECLS;
  12886. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12887. wc_Sha3 sha3;
  12888. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12889. "nopnopq";
  12890. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12891. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12892. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12893. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12894. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12895. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12896. /* Init stack variables. */
  12897. XMEMSET(hash, 0, sizeof(hash));
  12898. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12899. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12900. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12901. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  12902. /* Test bad args. */
  12903. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  12904. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  12905. wc_Sha3_384_Free(&sha3);
  12906. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12907. /* Init stack variables. */
  12908. XMEMSET(hash, 0, sizeof(hash));
  12909. XMEMSET(hashRet, 0, sizeof(hashRet));
  12910. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12911. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  12912. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12913. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  12914. /* Test bad args. */
  12915. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12916. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12917. wc_Sha3_384_Free(&sha3);
  12918. #endif
  12919. return EXPECT_RESULT();
  12920. } /* END test_wc_Sha3_384_Final */
  12921. /*
  12922. * Testing wc_Sha3_512_Final()
  12923. */
  12924. static int test_wc_Sha3_512_Final(void)
  12925. {
  12926. EXPECT_DECLS;
  12927. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12928. !defined(WOLFSSL_NOSHA3_384)
  12929. wc_Sha3 sha3;
  12930. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12931. "nopnopq";
  12932. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12933. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12934. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12935. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12936. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12937. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12938. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12939. /* Init stack variables. */
  12940. XMEMSET(hash, 0, sizeof(hash));
  12941. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12942. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12943. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12944. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  12945. /* Test bad args. */
  12946. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  12947. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  12948. wc_Sha3_512_Free(&sha3);
  12949. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12950. /* Init stack variables. */
  12951. XMEMSET(hash, 0, sizeof(hash));
  12952. XMEMSET(hashRet, 0, sizeof(hashRet));
  12953. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12954. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  12955. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12956. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  12957. /* Test bad args. */
  12958. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12959. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12960. wc_Sha3_512_Free(&sha3);
  12961. #endif
  12962. return EXPECT_RESULT();
  12963. } /* END test_wc_Sha3_512_Final */
  12964. /*
  12965. * Testing wc_Sha3_224_Copy()
  12966. */
  12967. static int test_wc_Sha3_224_Copy(void)
  12968. {
  12969. EXPECT_DECLS;
  12970. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12971. wc_Sha3 sha3, sha3Cpy;
  12972. const char* msg = TEST_STRING;
  12973. word32 msglen = (word32)TEST_STRING_SZ;
  12974. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12975. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12976. XMEMSET(hash, 0, sizeof(hash));
  12977. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12978. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12979. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12980. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12981. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12982. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  12983. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  12984. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12985. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  12986. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12987. /* Test bad args. */
  12988. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12989. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12990. wc_Sha3_224_Free(&sha3);
  12991. wc_Sha3_224_Free(&sha3Cpy);
  12992. #endif
  12993. return EXPECT_RESULT();
  12994. } /* END test_wc_Sha3_224_Copy */
  12995. /*
  12996. * Testing wc_Sha3_256_Copy()
  12997. */
  12998. static int test_wc_Sha3_256_Copy(void)
  12999. {
  13000. EXPECT_DECLS;
  13001. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13002. wc_Sha3 sha3, sha3Cpy;
  13003. const char* msg = TEST_STRING;
  13004. word32 msglen = (word32)TEST_STRING_SZ;
  13005. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13006. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13007. XMEMSET(hash, 0, sizeof(hash));
  13008. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13009. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13010. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13011. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13012. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13013. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13014. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13015. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13016. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13017. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13018. /* Test bad args. */
  13019. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13020. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13021. wc_Sha3_256_Free(&sha3);
  13022. wc_Sha3_256_Free(&sha3Cpy);
  13023. #endif
  13024. return EXPECT_RESULT();
  13025. } /* END test_wc_Sha3_256_Copy */
  13026. /*
  13027. * Testing wc_Sha3_384_Copy()
  13028. */
  13029. static int test_wc_Sha3_384_Copy(void)
  13030. {
  13031. EXPECT_DECLS;
  13032. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13033. wc_Sha3 sha3, sha3Cpy;
  13034. const char* msg = TEST_STRING;
  13035. word32 msglen = (word32)TEST_STRING_SZ;
  13036. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13037. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13038. XMEMSET(hash, 0, sizeof(hash));
  13039. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13040. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13041. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13042. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13043. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13044. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13045. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13046. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13047. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13048. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13049. /* Test bad args. */
  13050. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13051. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13052. wc_Sha3_384_Free(&sha3);
  13053. wc_Sha3_384_Free(&sha3Cpy);
  13054. #endif
  13055. return EXPECT_RESULT();
  13056. } /* END test_wc_Sha3_384_Copy */
  13057. /*
  13058. * Testing wc_Sha3_512_Copy()
  13059. */
  13060. static int test_wc_Sha3_512_Copy(void)
  13061. {
  13062. EXPECT_DECLS;
  13063. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13064. wc_Sha3 sha3, sha3Cpy;
  13065. const char* msg = TEST_STRING;
  13066. word32 msglen = (word32)TEST_STRING_SZ;
  13067. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13068. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13069. XMEMSET(hash, 0, sizeof(hash));
  13070. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13071. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13072. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13073. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13074. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13075. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13076. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13077. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13078. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13079. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13080. /* Test bad args. */
  13081. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13082. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13083. wc_Sha3_512_Free(&sha3);
  13084. wc_Sha3_512_Free(&sha3Cpy);
  13085. #endif
  13086. return EXPECT_RESULT();
  13087. } /* END test_wc_Sha3_512_Copy */
  13088. /*
  13089. * Unit test function for wc_Sha3_GetFlags()
  13090. */
  13091. static int test_wc_Sha3_GetFlags(void)
  13092. {
  13093. EXPECT_DECLS;
  13094. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13095. wc_Sha3 sha3;
  13096. word32 flags = 0;
  13097. /* Initialize */
  13098. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13099. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13100. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13101. wc_Sha3_224_Free(&sha3);
  13102. #endif
  13103. return EXPECT_RESULT();
  13104. } /* END test_wc_Sha3_GetFlags */
  13105. static int test_wc_InitShake256(void)
  13106. {
  13107. EXPECT_DECLS;
  13108. #ifdef WOLFSSL_SHAKE256
  13109. wc_Shake shake;
  13110. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13111. /* Test bad args. */
  13112. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13113. wc_Shake256_Free(&shake);
  13114. #endif
  13115. return EXPECT_RESULT();
  13116. }
  13117. static int testing_wc_Shake256_Update(void)
  13118. {
  13119. EXPECT_DECLS;
  13120. #ifdef WOLFSSL_SHAKE256
  13121. wc_Shake shake;
  13122. byte msg[] = "Everybody's working for the weekend.";
  13123. byte msg2[] = "Everybody gets Friday off.";
  13124. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13125. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13126. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13127. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13128. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13129. word32 msglen = sizeof(msg) - 1;
  13130. word32 msg2len = sizeof(msg2);
  13131. word32 msgCmplen = sizeof(msgCmp);
  13132. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13133. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13134. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13135. ExpectTrue(shake.i == msglen);
  13136. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13137. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13138. /* Pass bad args. */
  13139. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13140. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13141. wc_Shake256_Free(&shake);
  13142. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13143. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13144. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13145. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13146. wc_Shake256_Free(&shake);
  13147. #endif /* WOLFSSL_SHAKE256 */
  13148. return EXPECT_RESULT();
  13149. }
  13150. static int test_wc_Shake256_Final(void)
  13151. {
  13152. EXPECT_DECLS;
  13153. #ifdef WOLFSSL_SHAKE256
  13154. wc_Shake shake;
  13155. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13156. "nopnopq";
  13157. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13158. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13159. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13160. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13161. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13162. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13163. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13164. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13165. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13166. byte hash[114];
  13167. /* Init stack variables. */
  13168. XMEMSET(hash, 0, sizeof(hash));
  13169. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13170. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13171. 0);
  13172. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13173. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13174. /* Test bad args. */
  13175. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13176. BAD_FUNC_ARG);
  13177. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13178. BAD_FUNC_ARG);
  13179. wc_Shake256_Free(&shake);
  13180. #endif
  13181. return EXPECT_RESULT();
  13182. }
  13183. /*
  13184. * Testing wc_Shake256_Copy()
  13185. */
  13186. static int test_wc_Shake256_Copy(void)
  13187. {
  13188. EXPECT_DECLS;
  13189. #ifdef WOLFSSL_SHAKE256
  13190. wc_Shake shake, shakeCpy;
  13191. const char* msg = TEST_STRING;
  13192. word32 msglen = (word32)TEST_STRING_SZ;
  13193. byte hash[144];
  13194. byte hashCpy[144];
  13195. word32 hashLen = sizeof(hash);
  13196. word32 hashLenCpy = sizeof(hashCpy);
  13197. XMEMSET(hash, 0, sizeof(hash));
  13198. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13199. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13200. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13201. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13202. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13203. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13204. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13205. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13206. /* Test bad args. */
  13207. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13208. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13209. wc_Shake256_Free(&shake);
  13210. wc_Shake256_Free(&shakeCpy);
  13211. #endif
  13212. return EXPECT_RESULT();
  13213. } /* END test_wc_Shake256_Copy */
  13214. /*
  13215. * Unit test function for wc_Shake256Hash()
  13216. */
  13217. static int test_wc_Shake256Hash(void)
  13218. {
  13219. EXPECT_DECLS;
  13220. #ifdef WOLFSSL_SHAKE256
  13221. const byte data[] = { /* Hello World */
  13222. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13223. 0x72,0x6c,0x64
  13224. };
  13225. word32 len = sizeof(data);
  13226. byte hash[144];
  13227. word32 hashLen = sizeof(hash);
  13228. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13229. #endif
  13230. return EXPECT_RESULT();
  13231. } /* END test_wc_Shake256Hash */
  13232. /*
  13233. * Testing wc_InitSm3(), wc_Sm3Free()
  13234. */
  13235. static int test_wc_InitSm3Free(void)
  13236. {
  13237. EXPECT_DECLS;
  13238. #ifdef WOLFSSL_SM3
  13239. wc_Sm3 sm3;
  13240. /* Invalid Parameters */
  13241. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13242. /* Valid Parameters */
  13243. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13244. wc_Sm3Free(NULL);
  13245. wc_Sm3Free(&sm3);
  13246. #endif
  13247. return EXPECT_RESULT();
  13248. } /* END test_wc_InitSm3 */
  13249. /*
  13250. * Testing wc_Sm3Update(), wc_Sm3Final()
  13251. */
  13252. static int test_wc_Sm3UpdateFinal(void)
  13253. {
  13254. EXPECT_DECLS;
  13255. #ifdef WOLFSSL_SM3
  13256. wc_Sm3 sm3;
  13257. byte data[WC_SM3_BLOCK_SIZE * 4];
  13258. byte hash[WC_SM3_DIGEST_SIZE];
  13259. byte calcHash[WC_SM3_DIGEST_SIZE];
  13260. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13261. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13262. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13263. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13264. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13265. };
  13266. word32 chunk;
  13267. word32 i;
  13268. XMEMSET(data, 0, sizeof(data));
  13269. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13270. /* Invalid Parameters */
  13271. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13272. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13273. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13274. /* Valid Parameters */
  13275. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13276. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13277. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13278. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13279. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13280. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13281. /* Ensure too many bytes for lengths. */
  13282. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13283. /* Invalid Parameters */
  13284. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13285. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13286. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13287. /* Valid Parameters */
  13288. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13289. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13290. /* Chunk tests. */
  13291. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13292. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13293. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13294. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13295. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13296. }
  13297. if (i < (word32)sizeof(data)) {
  13298. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13299. 0);
  13300. }
  13301. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13302. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13303. }
  13304. /* Not testing when the low 32-bit length overflows. */
  13305. wc_Sm3Free(&sm3);
  13306. #endif
  13307. return EXPECT_RESULT();
  13308. } /* END test_wc_Sm3Update */
  13309. /*
  13310. * Testing wc_Sm3GetHash()
  13311. */
  13312. static int test_wc_Sm3GetHash(void)
  13313. {
  13314. EXPECT_DECLS;
  13315. #ifdef WOLFSSL_SM3
  13316. wc_Sm3 sm3;
  13317. byte hash[WC_SM3_DIGEST_SIZE];
  13318. byte calcHash[WC_SM3_DIGEST_SIZE];
  13319. byte data[WC_SM3_BLOCK_SIZE];
  13320. XMEMSET(data, 0, sizeof(data));
  13321. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13322. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13323. /* Invalid Parameters */
  13324. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13325. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13326. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13327. /* Valid Parameters */
  13328. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13329. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13330. /* With update. */
  13331. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13332. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13333. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13334. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13335. wc_Sm3Free(&sm3);
  13336. #endif
  13337. return EXPECT_RESULT();
  13338. } /* END test_wc_Sm3Update */
  13339. /*
  13340. * Testing wc_Sm3Copy()
  13341. */
  13342. static int test_wc_Sm3Copy(void)
  13343. {
  13344. EXPECT_DECLS;
  13345. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13346. wc_Sm3 sm3;
  13347. wc_Sm3 sm3Copy;
  13348. byte hash[WC_SM3_DIGEST_SIZE];
  13349. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13350. byte data[WC_SM3_BLOCK_SIZE + 1];
  13351. int i;
  13352. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13353. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13354. /* Invalid Parameters */
  13355. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13356. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13357. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13358. /* Valid Parameters */
  13359. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13360. /* Ensure all parts of data updated during hashing are copied. */
  13361. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13362. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13363. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13364. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13365. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13366. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13367. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13368. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13369. }
  13370. wc_Sm3Free(&sm3Copy);
  13371. wc_Sm3Free(&sm3);
  13372. #endif
  13373. return EXPECT_RESULT();
  13374. } /* END test_wc_Sm3Copy */
  13375. /*
  13376. * Testing wc_Sm3FinalRaw()
  13377. */
  13378. static int test_wc_Sm3FinalRaw(void)
  13379. {
  13380. EXPECT_DECLS;
  13381. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13382. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13383. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13384. !defined(WOLFSSL_NO_HASH_RAW)
  13385. wc_Sm3 sm3;
  13386. byte hash1[WC_SM3_DIGEST_SIZE];
  13387. byte hash2[WC_SM3_DIGEST_SIZE];
  13388. byte hash3[WC_SM3_DIGEST_SIZE];
  13389. byte* hash_test[3] = { hash1, hash2, hash3 };
  13390. int times;
  13391. int i;
  13392. XMEMSET(&sm3, 0, sizeof(sm3));
  13393. /* Initialize */
  13394. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13395. /* Invalid Parameters */
  13396. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13397. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13398. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13399. times = sizeof(hash_test) / sizeof(byte*);
  13400. for (i = 0; i < times; i++) {
  13401. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13402. }
  13403. wc_Sm3Free(&sm3);
  13404. #endif
  13405. return EXPECT_RESULT();
  13406. } /* END test_wc_Sm3FinalRaw */
  13407. /*
  13408. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13409. */
  13410. static int test_wc_Sm3GetSetFlags(void)
  13411. {
  13412. EXPECT_DECLS;
  13413. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13414. wc_Sm3 sm3;
  13415. wc_Sm3 sm3Copy;
  13416. word32 flags = 0;
  13417. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13418. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13419. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13420. ExpectIntEQ(flags, 0);
  13421. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13422. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13423. ExpectIntEQ(flags, 0);
  13424. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13425. ExpectIntEQ(flags, 0);
  13426. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13427. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13428. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13429. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13430. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13431. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13432. wc_Sm3Free(&sm3Copy);
  13433. wc_Sm3Free(&sm3);
  13434. #endif
  13435. return EXPECT_RESULT();
  13436. } /* END test_wc_Sm3Update */
  13437. /*
  13438. * Testing wc_Sm3Hash()
  13439. */
  13440. static int test_wc_Sm3Hash(void)
  13441. {
  13442. EXPECT_DECLS;
  13443. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13444. byte data[WC_SM3_BLOCK_SIZE];
  13445. byte hash[WC_SM3_DIGEST_SIZE];
  13446. /* Invalid parameters. */
  13447. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13448. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13449. /* Valid parameters. */
  13450. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13451. #endif
  13452. return EXPECT_RESULT();
  13453. } /* END test_wc_Sm3Hash */
  13454. /*
  13455. * Test function for wc_HmacSetKey
  13456. */
  13457. static int test_wc_Md5HmacSetKey(void)
  13458. {
  13459. EXPECT_DECLS;
  13460. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13461. Hmac hmac;
  13462. int ret, times, itr;
  13463. const char* keys[]=
  13464. {
  13465. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13466. #ifndef HAVE_FIPS
  13467. "Jefe", /* smaller than minimum FIPS key size */
  13468. #endif
  13469. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13470. };
  13471. times = sizeof(keys) / sizeof(char*);
  13472. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13473. for (itr = 0; itr < times; itr++) {
  13474. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13475. (word32)XSTRLEN(keys[itr]));
  13476. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13477. wc_HmacFree(&hmac);
  13478. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13479. #else
  13480. ExpectIntEQ(ret, 0);
  13481. #endif
  13482. }
  13483. /* Bad args. */
  13484. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13485. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13486. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13487. BAD_FUNC_ARG);
  13488. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13489. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13490. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13491. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13492. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13493. #elif defined(HAVE_FIPS)
  13494. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13495. #else
  13496. ExpectIntEQ(ret, 0);
  13497. #endif
  13498. wc_HmacFree(&hmac);
  13499. #endif
  13500. return EXPECT_RESULT();
  13501. } /* END test_wc_Md5HmacSetKey */
  13502. /*
  13503. * testing wc_HmacSetKey() on wc_Sha hash.
  13504. */
  13505. static int test_wc_ShaHmacSetKey(void)
  13506. {
  13507. EXPECT_DECLS;
  13508. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13509. Hmac hmac;
  13510. int ret, times, itr;
  13511. const char* keys[]=
  13512. {
  13513. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13514. "\x0b\x0b\x0b",
  13515. #ifndef HAVE_FIPS
  13516. "Jefe", /* smaller than minimum FIPS key size */
  13517. #endif
  13518. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13519. "\xAA\xAA\xAA"
  13520. };
  13521. times = sizeof(keys) / sizeof(char*);
  13522. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13523. for (itr = 0; itr < times; itr++) {
  13524. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13525. (word32)XSTRLEN(keys[itr])), 0);
  13526. }
  13527. /* Bad args. */
  13528. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13529. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13530. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13531. BAD_FUNC_ARG);
  13532. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13533. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13534. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13535. #ifdef HAVE_FIPS
  13536. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13537. #else
  13538. ExpectIntEQ(ret, 0);
  13539. #endif
  13540. wc_HmacFree(&hmac);
  13541. #endif
  13542. return EXPECT_RESULT();
  13543. } /* END test_wc_ShaHmacSetKey() */
  13544. /*
  13545. * testing wc_HmacSetKey() on Sha224 hash.
  13546. */
  13547. static int test_wc_Sha224HmacSetKey(void)
  13548. {
  13549. EXPECT_DECLS;
  13550. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13551. Hmac hmac;
  13552. int ret, times, itr;
  13553. const char* keys[]=
  13554. {
  13555. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13556. "\x0b\x0b\x0b",
  13557. #ifndef HAVE_FIPS
  13558. "Jefe", /* smaller than minimum FIPS key size */
  13559. #endif
  13560. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13561. "\xAA\xAA\xAA"
  13562. };
  13563. times = sizeof(keys) / sizeof(char*);
  13564. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13565. for (itr = 0; itr < times; itr++) {
  13566. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13567. (word32)XSTRLEN(keys[itr])), 0);
  13568. }
  13569. /* Bad args. */
  13570. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13571. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13572. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  13573. BAD_FUNC_ARG);
  13574. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13575. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13576. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13577. #ifdef HAVE_FIPS
  13578. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13579. #else
  13580. ExpectIntEQ(ret, 0);
  13581. #endif
  13582. wc_HmacFree(&hmac);
  13583. #endif
  13584. return EXPECT_RESULT();
  13585. } /* END test_wc_Sha224HmacSetKey() */
  13586. /*
  13587. * testing wc_HmacSetKey() on Sha256 hash
  13588. */
  13589. static int test_wc_Sha256HmacSetKey(void)
  13590. {
  13591. EXPECT_DECLS;
  13592. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13593. Hmac hmac;
  13594. int ret, times, itr;
  13595. const char* keys[]=
  13596. {
  13597. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13598. "\x0b\x0b\x0b",
  13599. #ifndef HAVE_FIPS
  13600. "Jefe", /* smaller than minimum FIPS key size */
  13601. #endif
  13602. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13603. "\xAA\xAA\xAA"
  13604. };
  13605. times = sizeof(keys) / sizeof(char*);
  13606. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13607. for (itr = 0; itr < times; itr++) {
  13608. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13609. (word32)XSTRLEN(keys[itr])), 0);
  13610. }
  13611. /* Bad args. */
  13612. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13613. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13614. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  13615. BAD_FUNC_ARG);
  13616. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13617. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13618. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13619. #ifdef HAVE_FIPS
  13620. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13621. #else
  13622. ExpectIntEQ(ret, 0);
  13623. #endif
  13624. wc_HmacFree(&hmac);
  13625. #endif
  13626. return EXPECT_RESULT();
  13627. } /* END test_wc_Sha256HmacSetKey() */
  13628. /*
  13629. * testing wc_HmacSetKey on Sha384 hash.
  13630. */
  13631. static int test_wc_Sha384HmacSetKey(void)
  13632. {
  13633. EXPECT_DECLS;
  13634. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13635. Hmac hmac;
  13636. int ret, times, itr;
  13637. const char* keys[]=
  13638. {
  13639. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13640. "\x0b\x0b\x0b",
  13641. #ifndef HAVE_FIPS
  13642. "Jefe", /* smaller than minimum FIPS key size */
  13643. #endif
  13644. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13645. "\xAA\xAA\xAA"
  13646. };
  13647. times = sizeof(keys) / sizeof(char*);
  13648. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13649. for (itr = 0; itr < times; itr++) {
  13650. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13651. (word32)XSTRLEN(keys[itr])), 0);
  13652. }
  13653. /* Bad args. */
  13654. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13655. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13656. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  13657. BAD_FUNC_ARG);
  13658. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13659. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13660. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13661. #ifdef HAVE_FIPS
  13662. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13663. #else
  13664. ExpectIntEQ(ret, 0);
  13665. #endif
  13666. wc_HmacFree(&hmac);
  13667. #endif
  13668. return EXPECT_RESULT();
  13669. } /* END test_wc_Sha384HmacSetKey() */
  13670. /*
  13671. * testing wc_HmacUpdate on wc_Md5 hash.
  13672. */
  13673. static int test_wc_Md5HmacUpdate(void)
  13674. {
  13675. EXPECT_DECLS;
  13676. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13677. Hmac hmac;
  13678. testVector a, b;
  13679. #ifdef HAVE_FIPS
  13680. const char* keys =
  13681. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13682. #else
  13683. const char* keys = "Jefe";
  13684. #endif
  13685. a.input = "what do ya want for nothing?";
  13686. a.inLen = XSTRLEN(a.input);
  13687. b.input = "Hi There";
  13688. b.inLen = XSTRLEN(b.input);
  13689. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13690. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  13691. (word32)XSTRLEN(keys)), 0);
  13692. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13693. /* Update Hmac. */
  13694. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13695. /* Test bad args. */
  13696. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13697. BAD_FUNC_ARG);
  13698. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13699. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13700. wc_HmacFree(&hmac);
  13701. #endif
  13702. return EXPECT_RESULT();
  13703. } /* END test_wc_Md5HmacUpdate */
  13704. /*
  13705. * testing wc_HmacUpdate on SHA hash.
  13706. */
  13707. static int test_wc_ShaHmacUpdate(void)
  13708. {
  13709. EXPECT_DECLS;
  13710. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13711. Hmac hmac;
  13712. testVector a, b;
  13713. #ifdef HAVE_FIPS
  13714. const char* keys =
  13715. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13716. #else
  13717. const char* keys = "Jefe";
  13718. #endif
  13719. a.input = "what do ya want for nothing?";
  13720. a.inLen = XSTRLEN(a.input);
  13721. b.input = "Hi There";
  13722. b.inLen = XSTRLEN(b.input);
  13723. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13724. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  13725. (word32)XSTRLEN(keys)), 0);
  13726. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13727. /* Update Hmac. */
  13728. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13729. /* Test bad args. */
  13730. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13731. BAD_FUNC_ARG);
  13732. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13733. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13734. wc_HmacFree(&hmac);
  13735. #endif
  13736. return EXPECT_RESULT();
  13737. } /* END test_wc_ShaHmacUpdate */
  13738. /*
  13739. * testing wc_HmacUpdate on SHA224 hash.
  13740. */
  13741. static int test_wc_Sha224HmacUpdate(void)
  13742. {
  13743. EXPECT_DECLS;
  13744. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13745. Hmac hmac;
  13746. testVector a, b;
  13747. #ifdef HAVE_FIPS
  13748. const char* keys =
  13749. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13750. #else
  13751. const char* keys = "Jefe";
  13752. #endif
  13753. a.input = "what do ya want for nothing?";
  13754. a.inLen = XSTRLEN(a.input);
  13755. b.input = "Hi There";
  13756. b.inLen = XSTRLEN(b.input);
  13757. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13758. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  13759. (word32)XSTRLEN(keys)), 0);
  13760. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13761. /* Update Hmac. */
  13762. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13763. /* Test bad args. */
  13764. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13765. BAD_FUNC_ARG);
  13766. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13767. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13768. wc_HmacFree(&hmac);
  13769. #endif
  13770. return EXPECT_RESULT();
  13771. } /* END test_wc_Sha224HmacUpdate */
  13772. /*
  13773. * testing wc_HmacUpdate on SHA256 hash.
  13774. */
  13775. static int test_wc_Sha256HmacUpdate(void)
  13776. {
  13777. EXPECT_DECLS;
  13778. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13779. Hmac hmac;
  13780. testVector a, b;
  13781. #ifdef HAVE_FIPS
  13782. const char* keys =
  13783. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13784. #else
  13785. const char* keys = "Jefe";
  13786. #endif
  13787. a.input = "what do ya want for nothing?";
  13788. a.inLen = XSTRLEN(a.input);
  13789. b.input = "Hi There";
  13790. b.inLen = XSTRLEN(b.input);
  13791. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13792. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  13793. (word32)XSTRLEN(keys)), 0);
  13794. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13795. /* Update Hmac. */
  13796. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13797. /* Test bad args. */
  13798. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13799. BAD_FUNC_ARG);
  13800. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13801. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13802. wc_HmacFree(&hmac);
  13803. #endif
  13804. return EXPECT_RESULT();
  13805. } /* END test_wc_Sha256HmacUpdate */
  13806. /*
  13807. * testing wc_HmacUpdate on SHA384 hash.
  13808. */
  13809. static int test_wc_Sha384HmacUpdate(void)
  13810. {
  13811. EXPECT_DECLS;
  13812. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13813. Hmac hmac;
  13814. testVector a, b;
  13815. #ifdef HAVE_FIPS
  13816. const char* keys =
  13817. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13818. #else
  13819. const char* keys = "Jefe";
  13820. #endif
  13821. a.input = "what do ya want for nothing?";
  13822. a.inLen = XSTRLEN(a.input);
  13823. b.input = "Hi There";
  13824. b.inLen = XSTRLEN(b.input);
  13825. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13826. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  13827. (word32)XSTRLEN(keys)), 0);
  13828. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13829. /* Update Hmac. */
  13830. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13831. /* Test bad args. */
  13832. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13833. BAD_FUNC_ARG);
  13834. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13835. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13836. wc_HmacFree(&hmac);
  13837. #endif
  13838. return EXPECT_RESULT();
  13839. } /* END test_wc_Sha384HmacUpdate */
  13840. /*
  13841. * Testing wc_HmacFinal() with MD5
  13842. */
  13843. static int test_wc_Md5HmacFinal(void)
  13844. {
  13845. EXPECT_DECLS;
  13846. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13847. Hmac hmac;
  13848. byte hash[WC_MD5_DIGEST_SIZE];
  13849. testVector a;
  13850. const char* key;
  13851. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13852. a.input = "Hi There";
  13853. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13854. "\x9d";
  13855. a.inLen = XSTRLEN(a.input);
  13856. a.outLen = XSTRLEN(a.output);
  13857. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13858. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  13859. 0);
  13860. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13861. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13862. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  13863. /* Try bad parameters. */
  13864. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13865. #ifndef HAVE_FIPS
  13866. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13867. #endif
  13868. wc_HmacFree(&hmac);
  13869. #endif
  13870. return EXPECT_RESULT();
  13871. } /* END test_wc_Md5HmacFinal */
  13872. /*
  13873. * Testing wc_HmacFinal() with SHA
  13874. */
  13875. static int test_wc_ShaHmacFinal(void)
  13876. {
  13877. EXPECT_DECLS;
  13878. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13879. Hmac hmac;
  13880. byte hash[WC_SHA_DIGEST_SIZE];
  13881. testVector a;
  13882. const char* key;
  13883. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13884. "\x0b\x0b\x0b";
  13885. a.input = "Hi There";
  13886. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13887. "\x8e\xf1\x46\xbe\x00";
  13888. a.inLen = XSTRLEN(a.input);
  13889. a.outLen = XSTRLEN(a.output);
  13890. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13891. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  13892. 0);
  13893. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13894. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13895. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  13896. /* Try bad parameters. */
  13897. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13898. #ifndef HAVE_FIPS
  13899. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13900. #endif
  13901. wc_HmacFree(&hmac);
  13902. #endif
  13903. return EXPECT_RESULT();
  13904. } /* END test_wc_ShaHmacFinal */
  13905. /*
  13906. * Testing wc_HmacFinal() with SHA224
  13907. */
  13908. static int test_wc_Sha224HmacFinal(void)
  13909. {
  13910. EXPECT_DECLS;
  13911. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13912. Hmac hmac;
  13913. byte hash[WC_SHA224_DIGEST_SIZE];
  13914. testVector a;
  13915. const char* key;
  13916. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13917. "\x0b\x0b\x0b";
  13918. a.input = "Hi There";
  13919. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13920. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13921. a.inLen = XSTRLEN(a.input);
  13922. a.outLen = XSTRLEN(a.output);
  13923. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13924. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  13925. (word32)XSTRLEN(key)), 0);
  13926. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13927. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13928. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13929. /* Try bad parameters. */
  13930. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13931. #ifndef HAVE_FIPS
  13932. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13933. #endif
  13934. wc_HmacFree(&hmac);
  13935. #endif
  13936. return EXPECT_RESULT();
  13937. } /* END test_wc_Sha224HmacFinal */
  13938. /*
  13939. * Testing wc_HmacFinal() with SHA256
  13940. */
  13941. static int test_wc_Sha256HmacFinal(void)
  13942. {
  13943. EXPECT_DECLS;
  13944. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13945. Hmac hmac;
  13946. byte hash[WC_SHA256_DIGEST_SIZE];
  13947. testVector a;
  13948. const char* key;
  13949. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13950. "\x0b\x0b\x0b";
  13951. a.input = "Hi There";
  13952. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13953. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13954. "\xcf\xf7";
  13955. a.inLen = XSTRLEN(a.input);
  13956. a.outLen = XSTRLEN(a.output);
  13957. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13958. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  13959. (word32)XSTRLEN(key)), 0);
  13960. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13961. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13962. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  13963. /* Try bad parameters. */
  13964. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13965. #ifndef HAVE_FIPS
  13966. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13967. #endif
  13968. wc_HmacFree(&hmac);
  13969. #endif
  13970. return EXPECT_RESULT();
  13971. } /* END test_wc_Sha256HmacFinal */
  13972. /*
  13973. * Testing wc_HmacFinal() with SHA384
  13974. */
  13975. static int test_wc_Sha384HmacFinal(void)
  13976. {
  13977. EXPECT_DECLS;
  13978. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13979. Hmac hmac;
  13980. byte hash[WC_SHA384_DIGEST_SIZE];
  13981. testVector a;
  13982. const char* key;
  13983. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13984. "\x0b\x0b\x0b";
  13985. a.input = "Hi There";
  13986. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13987. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13988. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13989. "\xfa\x9c\xb6";
  13990. a.inLen = XSTRLEN(a.input);
  13991. a.outLen = XSTRLEN(a.output);
  13992. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13993. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  13994. (word32)XSTRLEN(key)), 0);
  13995. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13996. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13997. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  13998. /* Try bad parameters. */
  13999. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14000. #ifndef HAVE_FIPS
  14001. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14002. #endif
  14003. wc_HmacFree(&hmac);
  14004. #endif
  14005. return EXPECT_RESULT();
  14006. } /* END test_wc_Sha384HmacFinal */
  14007. /*
  14008. * Testing wc_InitCmac()
  14009. */
  14010. static int test_wc_InitCmac(void)
  14011. {
  14012. EXPECT_DECLS;
  14013. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14014. Cmac cmac1;
  14015. Cmac cmac2;
  14016. Cmac cmac3;
  14017. /* AES 128 key. */
  14018. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14019. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14020. /* AES 192 key. */
  14021. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14022. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14023. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14024. /* AES 256 key. */
  14025. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14026. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14027. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14028. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14029. word32 key1Sz = (word32)sizeof(key1) - 1;
  14030. word32 key2Sz = (word32)sizeof(key2) - 1;
  14031. word32 key3Sz = (word32)sizeof(key3) - 1;
  14032. int type = WC_CMAC_AES;
  14033. (void)key1;
  14034. (void)key1Sz;
  14035. (void)key2;
  14036. (void)key2Sz;
  14037. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14038. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14039. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14040. #ifdef WOLFSSL_AES_128
  14041. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14042. #endif
  14043. #ifdef WOLFSSL_AES_192
  14044. wc_AesFree(&cmac1.aes);
  14045. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14046. #endif
  14047. #ifdef WOLFSSL_AES_256
  14048. wc_AesFree(&cmac2.aes);
  14049. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14050. #endif
  14051. wc_AesFree(&cmac3.aes);
  14052. /* Test bad args. */
  14053. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14054. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14055. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14056. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14057. #endif
  14058. return EXPECT_RESULT();
  14059. } /* END test_wc_InitCmac */
  14060. /*
  14061. * Testing wc_CmacUpdate()
  14062. */
  14063. static int test_wc_CmacUpdate(void)
  14064. {
  14065. EXPECT_DECLS;
  14066. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14067. Cmac cmac;
  14068. byte key[] = {
  14069. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14070. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14071. };
  14072. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14073. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14074. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14075. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14076. "\xf4";
  14077. word32 inSz = (word32)sizeof(in) - 1;
  14078. word32 keySz = (word32)sizeof(key);
  14079. int type = WC_CMAC_AES;
  14080. XMEMSET(&cmac, 0, sizeof(Cmac));
  14081. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14082. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14083. /* Test bad args. */
  14084. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14085. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14086. wc_AesFree(&cmac.aes);
  14087. #endif
  14088. return EXPECT_RESULT();
  14089. } /* END test_wc_CmacUpdate */
  14090. /*
  14091. * Testing wc_CmacFinal()
  14092. */
  14093. static int test_wc_CmacFinal(void)
  14094. {
  14095. EXPECT_DECLS;
  14096. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14097. Cmac cmac;
  14098. byte key[] = {
  14099. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14100. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14101. };
  14102. byte msg[] = {
  14103. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14104. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14105. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14106. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14107. 0xf4
  14108. };
  14109. /* Test vectors from CMACGenAES128.rsp from
  14110. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14111. * Per RFC4493 truncation of lsb is possible.
  14112. */
  14113. byte expMac[] = {
  14114. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14115. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14116. };
  14117. byte mac[AES_BLOCK_SIZE];
  14118. word32 msgSz = (word32)sizeof(msg);
  14119. word32 keySz = (word32)sizeof(key);
  14120. word32 macSz = sizeof(mac);
  14121. word32 badMacSz = 17;
  14122. int expMacSz = sizeof(expMac);
  14123. int type = WC_CMAC_AES;
  14124. XMEMSET(&cmac, 0, sizeof(Cmac));
  14125. XMEMSET(mac, 0, macSz);
  14126. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14127. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14128. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14129. /* Pass in bad args. */
  14130. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14131. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14132. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14133. /* For the last call, use the API with implicit wc_CmacFree(). */
  14134. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14135. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14136. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14137. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14138. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14139. /* Pass in bad args. */
  14140. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14141. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14142. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14143. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14144. #endif
  14145. return EXPECT_RESULT();
  14146. } /* END test_wc_CmacFinal */
  14147. /*
  14148. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14149. */
  14150. static int test_wc_AesCmacGenerate(void)
  14151. {
  14152. EXPECT_DECLS;
  14153. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14154. byte key[] = {
  14155. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14156. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14157. };
  14158. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14159. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14160. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14161. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14162. byte mac[AES_BLOCK_SIZE];
  14163. word32 keySz = sizeof(key);
  14164. word32 macSz = sizeof(mac);
  14165. word32 msgSz = sizeof(msg) - 1;
  14166. word32 expMacSz = sizeof(expMac) - 1;
  14167. XMEMSET(mac, 0, macSz);
  14168. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14169. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14170. /* Pass in bad args. */
  14171. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14172. BAD_FUNC_ARG);
  14173. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14174. BAD_FUNC_ARG);
  14175. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14176. BAD_FUNC_ARG);
  14177. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14178. BAD_FUNC_ARG);
  14179. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14180. /* Test bad args. */
  14181. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14182. BAD_FUNC_ARG);
  14183. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14184. BAD_FUNC_ARG);
  14185. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14186. BAD_FUNC_ARG);
  14187. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14188. BAD_FUNC_ARG);
  14189. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14190. BAD_FUNC_ARG);
  14191. #endif
  14192. return EXPECT_RESULT();
  14193. } /* END test_wc_AesCmacGenerate */
  14194. /*
  14195. * Testing streaming AES-GCM API.
  14196. */
  14197. static int test_wc_AesGcmStream(void)
  14198. {
  14199. EXPECT_DECLS;
  14200. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14201. defined(WOLFSSL_AESGCM_STREAM)
  14202. int i;
  14203. WC_RNG rng[1];
  14204. Aes aesEnc[1];
  14205. Aes aesDec[1];
  14206. byte tag[AES_BLOCK_SIZE];
  14207. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14208. byte out[AES_BLOCK_SIZE * 3 + 2];
  14209. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14210. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14211. byte key[AES_128_KEY_SIZE] = { 0, };
  14212. byte iv[AES_IV_SIZE] = { 1, };
  14213. byte ivOut[AES_IV_SIZE];
  14214. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14215. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14216. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14217. };
  14218. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14219. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14220. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14221. };
  14222. static const byte expTag[AES_BLOCK_SIZE] = {
  14223. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14224. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14225. };
  14226. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14227. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14228. XMEMSET(&aesDec, 0, sizeof(Aes));
  14229. /* Create a random for generating IV/nonce. */
  14230. ExpectIntEQ(wc_InitRng(rng), 0);
  14231. /* Initialize data structures. */
  14232. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14233. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14234. /* BadParameters to streaming init. */
  14235. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14236. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14237. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14238. BAD_FUNC_ARG);
  14239. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14240. BAD_FUNC_ARG);
  14241. /* Bad parameters to encrypt update. */
  14242. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14243. BAD_FUNC_ARG);
  14244. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14245. BAD_FUNC_ARG);
  14246. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14247. BAD_FUNC_ARG);
  14248. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14249. BAD_FUNC_ARG);
  14250. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14251. BAD_FUNC_ARG);
  14252. /* Bad parameters to decrypt update. */
  14253. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14254. BAD_FUNC_ARG);
  14255. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14256. BAD_FUNC_ARG);
  14257. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14258. BAD_FUNC_ARG);
  14259. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14260. BAD_FUNC_ARG);
  14261. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14262. BAD_FUNC_ARG);
  14263. /* Bad parameters to encrypt final. */
  14264. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14265. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14266. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14267. BAD_FUNC_ARG);
  14268. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14269. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14270. BAD_FUNC_ARG);
  14271. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14272. BAD_FUNC_ARG);
  14273. /* Bad parameters to decrypt final. */
  14274. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14275. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14276. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14277. BAD_FUNC_ARG);
  14278. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14279. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14280. BAD_FUNC_ARG);
  14281. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14282. BAD_FUNC_ARG);
  14283. /* Check calling final before setting key fails. */
  14284. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14285. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14286. /* Check calling update before setting key else fails. */
  14287. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14288. MISSING_KEY);
  14289. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14290. MISSING_KEY);
  14291. /* Set key but not IV. */
  14292. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14293. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14294. /* Check calling final before setting IV fails. */
  14295. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14296. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14297. /* Check calling update before setting IV else fails. */
  14298. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14299. MISSING_IV);
  14300. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14301. MISSING_IV);
  14302. /* Set IV using fixed part IV and external IV APIs. */
  14303. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14304. rng), 0);
  14305. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14306. GCM_NONCE_MID_SZ), 0);
  14307. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14308. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14309. /* Encrypt and decrypt data. */
  14310. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14311. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14312. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14313. /* Finalize and check tag matches. */
  14314. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14315. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14316. /* Set key and IV through streaming init API. */
  14317. wc_AesFree(aesEnc);
  14318. wc_AesFree(aesDec);
  14319. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14320. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14321. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14322. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14323. /* Encrypt/decrypt one block and AAD of one block. */
  14324. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14325. AES_BLOCK_SIZE), 0);
  14326. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14327. AES_BLOCK_SIZE), 0);
  14328. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14329. /* Finalize and check tag matches. */
  14330. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14331. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14332. /* Set key and IV through streaming init API. */
  14333. wc_AesFree(aesEnc);
  14334. wc_AesFree(aesDec);
  14335. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14336. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14337. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14338. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14339. /* No data to encrypt/decrypt one byte of AAD. */
  14340. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14341. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14342. /* Finalize and check tag matches. */
  14343. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14344. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14345. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14346. /* Set key and IV through streaming init API. */
  14347. wc_AesFree(aesEnc);
  14348. wc_AesFree(aesDec);
  14349. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14350. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14351. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14352. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14353. /* Encrypt/decrypt one byte and no AAD. */
  14354. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14355. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14356. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14357. /* Finalize and check tag matches. */
  14358. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14359. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14360. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14361. /* Set key and IV through streaming init API. */
  14362. wc_AesFree(aesEnc);
  14363. wc_AesFree(aesDec);
  14364. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14365. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14366. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14367. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14368. /* Encryption AES is one byte at a time */
  14369. for (i = 0; i < (int)sizeof(aad); i++) {
  14370. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14371. 0);
  14372. }
  14373. for (i = 0; i < (int)sizeof(in); i++) {
  14374. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14375. 0);
  14376. }
  14377. /* Decryption AES is two bytes at a time */
  14378. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14379. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14380. 0);
  14381. }
  14382. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14383. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14384. 0), 0);
  14385. }
  14386. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14387. /* Finalize and check tag matches. */
  14388. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14389. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14390. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14391. /* Check streaming encryption can be decrypted with one shot. */
  14392. wc_AesFree(aesDec);
  14393. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14394. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14395. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14396. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14397. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14398. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14399. wc_AesFree(aesEnc);
  14400. wc_AesFree(aesDec);
  14401. wc_FreeRng(rng);
  14402. #endif
  14403. return EXPECT_RESULT();
  14404. } /* END test_wc_AesGcmStream */
  14405. /*
  14406. * Testing streaming SM4 API.
  14407. */
  14408. static int test_wc_Sm4(void)
  14409. {
  14410. int res = TEST_SKIPPED;
  14411. #ifdef WOLFSSL_SM4
  14412. EXPECT_DECLS;
  14413. wc_Sm4 sm4;
  14414. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14415. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14416. unsigned char key[SM4_KEY_SIZE];
  14417. #endif
  14418. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14419. unsigned char iv[SM4_IV_SIZE];
  14420. #endif
  14421. /* Invalid parameters - wc_Sm4Init */
  14422. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14423. /* Valid cases - wc_Sm4Init */
  14424. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14425. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14426. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14427. XMEMSET(key, 0, sizeof(key));
  14428. /* Invalid parameters - wc_Sm4SetKey. */
  14429. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14430. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14431. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14432. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14433. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14434. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14435. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14436. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14437. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14438. /* Valid cases - wc_Sm4SetKey. */
  14439. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14440. #endif
  14441. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14442. XMEMSET(iv, 0, sizeof(iv));
  14443. /* Invalid parameters - wc_Sm4SetIV. */
  14444. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14445. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14446. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14447. /* Valid cases - wc_Sm4SetIV. */
  14448. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14449. #endif
  14450. /* Valid cases - wc_Sm4Free */
  14451. wc_Sm4Free(NULL);
  14452. wc_Sm4Free(&sm4);
  14453. res = EXPECT_RESULT();
  14454. #endif
  14455. return res;
  14456. } /* END test_wc_Sm4 */
  14457. /*
  14458. * Testing block based SM4-ECB API.
  14459. */
  14460. static int test_wc_Sm4Ecb(void)
  14461. {
  14462. int res = TEST_SKIPPED;
  14463. #ifdef WOLFSSL_SM4_ECB
  14464. EXPECT_DECLS;
  14465. wc_Sm4 sm4;
  14466. unsigned char key[SM4_KEY_SIZE];
  14467. unsigned char in[SM4_BLOCK_SIZE * 2];
  14468. unsigned char out[SM4_BLOCK_SIZE * 2];
  14469. unsigned char out2[SM4_BLOCK_SIZE];
  14470. XMEMSET(key, 0, sizeof(key));
  14471. XMEMSET(in, 0, sizeof(in));
  14472. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14473. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14474. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14475. /* Tested in test_wc_Sm4. */
  14476. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14477. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14478. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14479. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14480. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14481. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14482. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14483. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14484. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14485. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14486. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14487. /* Valid cases - wc_Sm4EcbEncrypt. */
  14488. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14489. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14490. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14491. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14492. /* In and out are same pointer. */
  14493. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14494. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14495. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14496. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14497. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14498. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14499. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14500. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14501. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14502. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14503. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14504. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14505. /* Valid cases - wc_Sm4EcbDecrypt. */
  14506. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14507. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14508. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14509. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14510. /* In and out are same pointer. */
  14511. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14512. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14513. wc_Sm4Free(&sm4);
  14514. res = EXPECT_RESULT();
  14515. #endif
  14516. return res;
  14517. } /* END test_wc_Sm4Ecb */
  14518. /*
  14519. * Testing block based SM4-CBC API.
  14520. */
  14521. static int test_wc_Sm4Cbc(void)
  14522. {
  14523. int res = TEST_SKIPPED;
  14524. #ifdef WOLFSSL_SM4_CBC
  14525. EXPECT_DECLS;
  14526. wc_Sm4 sm4;
  14527. unsigned char key[SM4_KEY_SIZE];
  14528. unsigned char iv[SM4_IV_SIZE];
  14529. unsigned char in[SM4_BLOCK_SIZE * 2];
  14530. unsigned char out[SM4_BLOCK_SIZE * 2];
  14531. unsigned char out2[SM4_BLOCK_SIZE];
  14532. XMEMSET(key, 0, sizeof(key));
  14533. XMEMSET(iv, 0, sizeof(iv));
  14534. XMEMSET(in, 0, sizeof(in));
  14535. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14536. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14537. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14538. /* Tested in test_wc_Sm4. */
  14539. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14540. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14541. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14542. /* Tested in test_wc_Sm4. */
  14543. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14544. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  14545. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14546. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14547. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14548. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14549. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14550. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14551. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14552. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14553. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14554. /* Valid cases - wc_Sm4CbcEncrypt. */
  14555. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  14556. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14557. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14558. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14559. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14560. /* In and out are same pointer. */
  14561. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14562. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14563. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14564. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  14565. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14566. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14567. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14568. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14569. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14570. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14571. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14572. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14573. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14574. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14575. /* Valid cases - wc_Sm4CbcDecrypt. */
  14576. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  14577. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14578. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14579. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14580. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14581. /* In and out are same pointer. */
  14582. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14583. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14584. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14585. wc_Sm4Free(&sm4);
  14586. res = EXPECT_RESULT();
  14587. #endif
  14588. return res;
  14589. } /* END test_wc_Sm4Cbc */
  14590. /*
  14591. * Testing streaming SM4-CTR API.
  14592. */
  14593. static int test_wc_Sm4Ctr(void)
  14594. {
  14595. int res = TEST_SKIPPED;
  14596. #ifdef WOLFSSL_SM4_CTR
  14597. EXPECT_DECLS;
  14598. wc_Sm4 sm4;
  14599. unsigned char key[SM4_KEY_SIZE];
  14600. unsigned char iv[SM4_IV_SIZE];
  14601. unsigned char in[SM4_BLOCK_SIZE * 4];
  14602. unsigned char out[SM4_BLOCK_SIZE * 4];
  14603. unsigned char out2[SM4_BLOCK_SIZE * 4];
  14604. word32 chunk;
  14605. word32 i;
  14606. XMEMSET(key, 0, sizeof(key));
  14607. XMEMSET(iv, 0, sizeof(iv));
  14608. XMEMSET(in, 0, sizeof(in));
  14609. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14610. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14611. /* Tested in test_wc_Sm4. */
  14612. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14613. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  14614. /* Tested in test_wc_Sm4. */
  14615. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14616. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  14617. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14618. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  14619. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  14620. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  14621. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14622. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14623. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14624. /* Valid cases - wc_Sm4CtrEncrypt. */
  14625. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  14626. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  14627. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14628. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  14629. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  14630. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14631. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14632. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  14633. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14634. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14635. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14636. /* In and out are same pointer. Also check encrypt of cipher text produces
  14637. * plaintext.
  14638. */
  14639. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14640. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  14641. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14642. /* Chunking tests. */
  14643. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14644. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  14645. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14646. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14647. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  14648. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  14649. }
  14650. if (i < (word32)sizeof(in)) {
  14651. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  14652. (word32)sizeof(in) - i), 0);
  14653. }
  14654. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  14655. }
  14656. for (i = 0; i < (word32)sizeof(iv); i++) {
  14657. iv[i] = 0xff;
  14658. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14659. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14660. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14661. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  14662. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  14663. }
  14664. wc_Sm4Free(&sm4);
  14665. res = EXPECT_RESULT();
  14666. #endif
  14667. return res;
  14668. } /* END test_wc_Sm4Ctr */
  14669. /*
  14670. * Testing stream SM4-GCM API.
  14671. */
  14672. static int test_wc_Sm4Gcm(void)
  14673. {
  14674. int res = TEST_SKIPPED;
  14675. #ifdef WOLFSSL_SM4_GCM
  14676. EXPECT_DECLS;
  14677. wc_Sm4 sm4;
  14678. unsigned char key[SM4_KEY_SIZE];
  14679. unsigned char nonce[GCM_NONCE_MAX_SZ];
  14680. unsigned char in[SM4_BLOCK_SIZE * 2];
  14681. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14682. unsigned char out[SM4_BLOCK_SIZE * 2];
  14683. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14684. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14685. unsigned char tag[SM4_BLOCK_SIZE];
  14686. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14687. word32 i;
  14688. XMEMSET(key, 0, sizeof(key));
  14689. XMEMSET(nonce, 0, sizeof(nonce));
  14690. XMEMSET(in, 0, sizeof(in));
  14691. XMEMSET(in2, 0, sizeof(in2));
  14692. XMEMSET(aad, 0, sizeof(aad));
  14693. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14694. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14695. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14696. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14697. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14698. /* Invalid parameters - wc_Sm4GcmSetKey. */
  14699. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14700. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14701. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  14702. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14703. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14704. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14705. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14706. /* Valid parameters - wc_Sm4GcmSetKey. */
  14707. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14708. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  14709. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14710. 0), BAD_FUNC_ARG);
  14711. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14712. 0), BAD_FUNC_ARG);
  14713. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14714. 0), BAD_FUNC_ARG);
  14715. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14716. 0), BAD_FUNC_ARG);
  14717. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14718. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14719. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14720. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14721. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14722. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14723. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14724. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14725. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14726. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14727. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14728. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14729. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14730. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14731. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14732. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14733. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14734. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14735. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14736. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14737. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  14738. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14739. 0), BAD_FUNC_ARG);
  14740. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14741. 0), BAD_FUNC_ARG);
  14742. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14743. 0), BAD_FUNC_ARG);
  14744. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14745. 0), BAD_FUNC_ARG);
  14746. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14747. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14748. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14749. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14750. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14751. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14752. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14753. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14754. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14755. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14756. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14757. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14758. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14759. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14760. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14761. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14762. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14763. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14764. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14765. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14766. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14767. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14768. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14769. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14770. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14771. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14772. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14773. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14774. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14775. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14776. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14777. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14778. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14779. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14780. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14781. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14782. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14783. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14784. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14785. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14786. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14787. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14788. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14789. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14790. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14791. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14792. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14793. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14794. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14795. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14796. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14797. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14798. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14799. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14800. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14801. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14802. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14803. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14804. SM4_GCM_AUTH_E);
  14805. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14806. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  14807. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14808. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14809. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14810. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14811. }
  14812. /* Check different in/out sizes. */
  14813. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  14814. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14815. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  14816. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14817. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  14818. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14819. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14820. XMEMCPY(out2, out, i - 1);
  14821. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  14822. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14823. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14824. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  14825. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14826. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14827. }
  14828. /* Force the counter to roll over in first byte. */
  14829. {
  14830. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14831. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14832. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14833. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14834. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14835. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14836. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14837. }
  14838. wc_Sm4Free(&sm4);
  14839. res = EXPECT_RESULT();
  14840. #endif
  14841. return res;
  14842. } /* END test_wc_Sm4Gcm */
  14843. /*
  14844. * Testing stream SM4-CCM API.
  14845. */
  14846. static int test_wc_Sm4Ccm(void)
  14847. {
  14848. int res = TEST_SKIPPED;
  14849. #ifdef WOLFSSL_SM4_CCM
  14850. EXPECT_DECLS;
  14851. wc_Sm4 sm4;
  14852. unsigned char key[SM4_KEY_SIZE];
  14853. unsigned char nonce[CCM_NONCE_MAX_SZ];
  14854. unsigned char in[SM4_BLOCK_SIZE * 2];
  14855. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14856. unsigned char out[SM4_BLOCK_SIZE * 2];
  14857. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14858. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14859. unsigned char tag[SM4_BLOCK_SIZE];
  14860. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14861. word32 i;
  14862. XMEMSET(key, 0, sizeof(key));
  14863. XMEMSET(nonce, 0, sizeof(nonce));
  14864. XMEMSET(in, 0, sizeof(in));
  14865. XMEMSET(in2, 0, sizeof(in2));
  14866. XMEMSET(aad, 0, sizeof(aad));
  14867. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14868. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14869. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14870. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14871. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14872. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14873. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  14874. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14875. 0), BAD_FUNC_ARG);
  14876. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14877. 0), BAD_FUNC_ARG);
  14878. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14879. 0), BAD_FUNC_ARG);
  14880. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14881. 0), BAD_FUNC_ARG);
  14882. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14883. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14884. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14885. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14886. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14887. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14888. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14889. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14890. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14891. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14892. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14893. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14894. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14895. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14896. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14897. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14898. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14899. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14900. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14901. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14902. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  14903. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14904. 0), BAD_FUNC_ARG);
  14905. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14906. 0), BAD_FUNC_ARG);
  14907. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14908. 0), BAD_FUNC_ARG);
  14909. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14910. 0), BAD_FUNC_ARG);
  14911. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14912. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14913. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14914. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14915. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14916. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14917. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14918. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14919. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14920. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14921. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14922. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14923. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14924. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14925. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14926. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14927. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14928. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14929. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14930. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14931. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14932. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14933. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14934. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14935. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14936. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14937. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14938. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14939. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14940. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14941. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14942. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14943. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14944. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14945. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14946. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14947. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14948. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14949. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14950. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14951. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14952. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14953. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14954. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14955. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14956. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14957. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14958. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14959. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  14960. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14961. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14962. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14963. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14964. }
  14965. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14966. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14967. SM4_CCM_AUTH_E);
  14968. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14969. for (i = 0; i < 4; i++) {
  14970. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14971. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14972. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14973. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14974. }
  14975. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  14976. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14977. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14978. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14979. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14980. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14981. }
  14982. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  14983. * Even values in range 4..SM4_BLOCK_SIZE.
  14984. */
  14985. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14986. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14987. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14988. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14989. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14990. }
  14991. /* Check different in/out sizes. */
  14992. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  14993. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14994. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  14995. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14996. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  14997. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14998. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14999. XMEMCPY(out2, out, i - 1);
  15000. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15001. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15002. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15003. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15004. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15005. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15006. }
  15007. /* Force the counter to roll over in first byte. */
  15008. {
  15009. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15010. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15011. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15012. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15013. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15014. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15015. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15016. }
  15017. wc_Sm4Free(&sm4);
  15018. res = EXPECT_RESULT();
  15019. #endif
  15020. return res;
  15021. } /* END test_wc_Sm4Ccm */
  15022. /*
  15023. * unit test for wc_Des3_SetIV()
  15024. */
  15025. static int test_wc_Des3_SetIV(void)
  15026. {
  15027. EXPECT_DECLS;
  15028. #ifndef NO_DES3
  15029. Des3 des;
  15030. const byte key[] = {
  15031. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15032. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15033. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15034. };
  15035. const byte iv[] = {
  15036. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15037. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15038. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15039. };
  15040. XMEMSET(&des, 0, sizeof(Des3));
  15041. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15042. /* DES_ENCRYPTION or DES_DECRYPTION */
  15043. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15044. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15045. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15046. /* Test explicitly wc_Des3_SetIV() */
  15047. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15048. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15049. #endif
  15050. wc_Des3Free(&des);
  15051. #endif
  15052. return EXPECT_RESULT();
  15053. } /* END test_wc_Des3_SetIV */
  15054. /*
  15055. * unit test for wc_Des3_SetKey()
  15056. */
  15057. static int test_wc_Des3_SetKey(void)
  15058. {
  15059. EXPECT_DECLS;
  15060. #ifndef NO_DES3
  15061. Des3 des;
  15062. const byte key[] = {
  15063. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15064. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15065. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15066. };
  15067. const byte iv[] = {
  15068. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15069. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15070. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15071. };
  15072. XMEMSET(&des, 0, sizeof(Des3));
  15073. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15074. /* DES_ENCRYPTION or DES_DECRYPTION */
  15075. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15076. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15077. /* Test bad args. */
  15078. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15079. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15080. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15081. /* Default case. Should return 0. */
  15082. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15083. wc_Des3Free(&des);
  15084. #endif
  15085. return EXPECT_RESULT();
  15086. } /* END test_wc_Des3_SetKey */
  15087. /*
  15088. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15089. */
  15090. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15091. {
  15092. EXPECT_DECLS;
  15093. #ifndef NO_DES3
  15094. Des3 des;
  15095. byte cipher[24];
  15096. byte plain[24];
  15097. const byte key[] = {
  15098. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15099. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15100. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15101. };
  15102. const byte iv[] = {
  15103. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15104. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15105. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15106. };
  15107. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15108. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15109. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15110. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15111. };
  15112. XMEMSET(&des, 0, sizeof(Des3));
  15113. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15114. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15115. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15116. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15117. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15118. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15119. /* Pass in bad args. */
  15120. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15121. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15122. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15123. BAD_FUNC_ARG);
  15124. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15125. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15126. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15127. wc_Des3Free(&des);
  15128. #endif
  15129. return EXPECT_RESULT();
  15130. } /* END wc_Des3_CbcEncrypt */
  15131. /*
  15132. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15133. */
  15134. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15135. {
  15136. EXPECT_DECLS;
  15137. #ifndef NO_DES3
  15138. word32 vectorSz, cipherSz;
  15139. byte cipher[24];
  15140. byte plain[24];
  15141. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15142. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15143. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15144. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15145. };
  15146. byte key[] = {
  15147. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15148. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15149. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15150. };
  15151. byte iv[] = {
  15152. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15153. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15154. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15155. };
  15156. vectorSz = sizeof(byte) * 24;
  15157. cipherSz = sizeof(byte) * 24;
  15158. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15159. 0);
  15160. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15161. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15162. /* pass in bad args. */
  15163. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15164. BAD_FUNC_ARG);
  15165. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15166. BAD_FUNC_ARG);
  15167. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15168. BAD_FUNC_ARG);
  15169. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15170. 0);
  15171. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15172. BAD_FUNC_ARG);
  15173. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15174. BAD_FUNC_ARG);
  15175. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15176. BAD_FUNC_ARG);
  15177. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15178. 0);
  15179. #endif
  15180. return EXPECT_RESULT();
  15181. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15182. /*
  15183. * Unit test for wc_Des3_EcbEncrypt
  15184. */
  15185. static int test_wc_Des3_EcbEncrypt(void)
  15186. {
  15187. EXPECT_DECLS;
  15188. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15189. Des3 des;
  15190. byte cipher[24];
  15191. word32 cipherSz = sizeof(cipher);
  15192. const byte key[] = {
  15193. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15194. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15195. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15196. };
  15197. const byte iv[] = {
  15198. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15199. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15200. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15201. };
  15202. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15203. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15204. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15205. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15206. };
  15207. XMEMSET(&des, 0, sizeof(Des3));
  15208. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15209. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15210. /* Bad Cases */
  15211. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15212. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15213. BAD_FUNC_ARG);
  15214. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15215. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15216. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15217. /* Good Cases */
  15218. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15219. wc_Des3Free(&des);
  15220. #endif
  15221. return EXPECT_RESULT();
  15222. } /* END test_wc_Des3_EcbEncrypt */
  15223. /*
  15224. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15225. */
  15226. static int test_wc_Chacha_SetKey(void)
  15227. {
  15228. EXPECT_DECLS;
  15229. #ifdef HAVE_CHACHA
  15230. ChaCha ctx;
  15231. const byte key[] = {
  15232. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15233. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15234. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15235. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15236. };
  15237. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15238. byte cipher[128];
  15239. XMEMSET(cipher, 0, sizeof(cipher));
  15240. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15241. /* Test bad args. */
  15242. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15243. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15244. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15245. /* Test bad args. */
  15246. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15247. #endif
  15248. return EXPECT_RESULT();
  15249. } /* END test_wc_Chacha_SetKey */
  15250. /*
  15251. * unit test for wc_Poly1305SetKey()
  15252. */
  15253. static int test_wc_Poly1305SetKey(void)
  15254. {
  15255. EXPECT_DECLS;
  15256. #ifdef HAVE_POLY1305
  15257. Poly1305 ctx;
  15258. const byte key[] =
  15259. {
  15260. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15261. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15262. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15263. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15264. };
  15265. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15266. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15267. /* Test bad args. */
  15268. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15269. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15270. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15271. #endif
  15272. return EXPECT_RESULT();
  15273. } /* END test_wc_Poly1305_SetKey() */
  15274. /*
  15275. * Testing wc_Chacha_Process()
  15276. */
  15277. static int test_wc_Chacha_Process(void)
  15278. {
  15279. EXPECT_DECLS;
  15280. #ifdef HAVE_CHACHA
  15281. ChaCha enc, dec;
  15282. byte cipher[128];
  15283. byte plain[128];
  15284. const byte key[] =
  15285. {
  15286. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15287. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15288. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15289. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15290. };
  15291. const char* input = "Everybody gets Friday off.";
  15292. word32 keySz = sizeof(key)/sizeof(byte);
  15293. unsigned long int inlen = XSTRLEN(input);
  15294. /* Initialize stack variables. */
  15295. XMEMSET(cipher, 0, 128);
  15296. XMEMSET(plain, 0, 128);
  15297. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15298. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15299. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15300. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15301. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15302. 0);
  15303. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15304. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15305. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15306. /* test checking and using leftovers, currently just in C code */
  15307. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15308. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15309. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15310. (word32)inlen - 2), 0);
  15311. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15312. (byte*)input + (inlen - 2), 2), 0);
  15313. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15314. (word32)inlen - 2), 0);
  15315. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15316. (byte*)input + (inlen - 2), 2), 0);
  15317. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15318. /* check edge cases with counter increment */
  15319. {
  15320. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15321. const byte expected[] = {
  15322. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15323. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15324. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15325. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15326. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15327. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15328. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15329. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15330. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15331. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15332. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15333. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15334. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15335. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15336. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15337. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15338. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15339. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15340. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15341. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15342. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15343. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15344. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15345. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15346. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15347. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15348. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15349. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15350. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15351. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15352. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15353. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15354. };
  15355. const byte iv2[] = {
  15356. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15357. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15358. };
  15359. byte input2[256];
  15360. int i;
  15361. for (i = 0; i < 256; i++)
  15362. input2[i] = i;
  15363. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15364. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15365. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15366. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15367. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15368. /* partial */
  15369. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15370. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15371. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15372. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15373. }
  15374. #endif
  15375. /* Test bad args. */
  15376. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15377. BAD_FUNC_ARG);
  15378. #endif
  15379. return EXPECT_RESULT();
  15380. } /* END test_wc_Chacha_Process */
  15381. /*
  15382. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15383. */
  15384. static int test_wc_ChaCha20Poly1305_aead(void)
  15385. {
  15386. EXPECT_DECLS;
  15387. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15388. const byte key[] = {
  15389. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15390. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15391. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15392. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15393. };
  15394. const byte plaintext[] = {
  15395. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15396. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15397. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15398. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15399. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15400. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15401. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15402. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15403. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15404. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15405. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15406. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15407. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15408. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15409. 0x74, 0x2e
  15410. };
  15411. const byte iv[] = {
  15412. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15413. 0x44, 0x45, 0x46, 0x47
  15414. };
  15415. const byte aad[] = { /* additional data */
  15416. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15417. 0xc4, 0xc5, 0xc6, 0xc7
  15418. };
  15419. const byte cipher[] = { /* expected output from operation */
  15420. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15421. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15422. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15423. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15424. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15425. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15426. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15427. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15428. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15429. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15430. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15431. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15432. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15433. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15434. 0x61, 0x16
  15435. };
  15436. const byte authTag[] = { /* expected output from operation */
  15437. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15438. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15439. };
  15440. byte generatedCiphertext[272];
  15441. byte generatedPlaintext[272];
  15442. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15443. /* Initialize stack variables. */
  15444. XMEMSET(generatedCiphertext, 0, 272);
  15445. XMEMSET(generatedPlaintext, 0, 272);
  15446. /* Test Encrypt */
  15447. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15448. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15449. 0);
  15450. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15451. sizeof(cipher)/sizeof(byte)), 0);
  15452. /* Test bad args. */
  15453. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15454. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15455. BAD_FUNC_ARG);
  15456. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15457. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15458. BAD_FUNC_ARG);
  15459. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15460. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15461. BAD_FUNC_ARG);
  15462. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15463. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15464. BAD_FUNC_ARG);
  15465. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15466. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15467. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15468. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15469. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15470. sizeof(cipher), authTag, generatedPlaintext), 0);
  15471. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15472. sizeof(plaintext)/sizeof(byte)), 0);
  15473. /* Test bad args. */
  15474. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15475. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15476. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15477. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15478. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15479. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15480. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15481. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15482. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15483. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15484. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15485. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15486. #endif
  15487. return EXPECT_RESULT();
  15488. } /* END test_wc_ChaCha20Poly1305_aead */
  15489. /*
  15490. * Testing function for wc_Rc2SetKey().
  15491. */
  15492. static int test_wc_Rc2SetKey(void)
  15493. {
  15494. EXPECT_DECLS;
  15495. #ifdef WC_RC2
  15496. Rc2 rc2;
  15497. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15498. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15499. /* valid key and IV */
  15500. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15501. iv, 40), 0);
  15502. /* valid key, no IV */
  15503. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15504. NULL, 40), 0);
  15505. /* bad arguments */
  15506. /* null Rc2 struct */
  15507. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15508. iv, 40), BAD_FUNC_ARG);
  15509. /* null key */
  15510. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15511. iv, 40), BAD_FUNC_ARG);
  15512. /* key size == 0 */
  15513. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15514. /* key size > 128 */
  15515. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15516. /* effective bits == 0 */
  15517. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15518. iv, 0), WC_KEY_SIZE_E);
  15519. /* effective bits > 1024 */
  15520. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15521. iv, 1025), WC_KEY_SIZE_E);
  15522. #endif
  15523. return EXPECT_RESULT();
  15524. } /* END test_wc_Rc2SetKey */
  15525. /*
  15526. * Testing function for wc_Rc2SetIV().
  15527. */
  15528. static int test_wc_Rc2SetIV(void)
  15529. {
  15530. EXPECT_DECLS;
  15531. #ifdef WC_RC2
  15532. Rc2 rc2;
  15533. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15534. /* valid IV */
  15535. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15536. /* valid NULL IV */
  15537. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15538. /* bad arguments */
  15539. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15540. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15541. #endif
  15542. return EXPECT_RESULT();
  15543. } /* END test_wc_Rc2SetIV */
  15544. /*
  15545. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  15546. */
  15547. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15548. {
  15549. EXPECT_DECLS;
  15550. #ifdef WC_RC2
  15551. Rc2 rc2;
  15552. int effectiveKeyBits = 63;
  15553. byte cipher[RC2_BLOCK_SIZE];
  15554. byte plain[RC2_BLOCK_SIZE];
  15555. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15556. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15557. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15558. XMEMSET(cipher, 0, sizeof(cipher));
  15559. XMEMSET(plain, 0, sizeof(plain));
  15560. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15561. NULL, effectiveKeyBits), 0);
  15562. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  15563. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  15564. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  15565. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  15566. /* Rc2EcbEncrypt bad arguments */
  15567. /* null Rc2 struct */
  15568. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  15569. BAD_FUNC_ARG);
  15570. /* null out buffer */
  15571. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  15572. BAD_FUNC_ARG);
  15573. /* null input buffer */
  15574. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  15575. BAD_FUNC_ARG);
  15576. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15577. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  15578. /* Rc2EcbDecrypt bad arguments */
  15579. /* null Rc2 struct */
  15580. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  15581. BAD_FUNC_ARG);
  15582. /* null out buffer */
  15583. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  15584. BAD_FUNC_ARG);
  15585. /* null input buffer */
  15586. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  15587. BAD_FUNC_ARG);
  15588. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15589. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  15590. #endif
  15591. return EXPECT_RESULT();
  15592. } /* END test_wc_Rc2EcbEncryptDecrypt */
  15593. /*
  15594. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  15595. */
  15596. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15597. {
  15598. EXPECT_DECLS;
  15599. #ifdef WC_RC2
  15600. Rc2 rc2;
  15601. int effectiveKeyBits = 63;
  15602. byte cipher[RC2_BLOCK_SIZE*2];
  15603. byte plain[RC2_BLOCK_SIZE*2];
  15604. /* vector taken from test.c */
  15605. byte key[] = {
  15606. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15607. };
  15608. byte iv[] = {
  15609. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15610. };
  15611. byte input[] = {
  15612. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15613. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15614. };
  15615. byte output[] = {
  15616. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15617. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15618. };
  15619. XMEMSET(cipher, 0, sizeof(cipher));
  15620. XMEMSET(plain, 0, sizeof(plain));
  15621. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15622. iv, effectiveKeyBits), 0);
  15623. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  15624. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  15625. /* reset IV for decrypt */
  15626. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15627. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  15628. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  15629. /* Rc2CbcEncrypt bad arguments */
  15630. /* null Rc2 struct */
  15631. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  15632. BAD_FUNC_ARG);
  15633. /* null out buffer */
  15634. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  15635. BAD_FUNC_ARG);
  15636. /* null input buffer */
  15637. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  15638. BAD_FUNC_ARG);
  15639. /* Rc2CbcDecrypt bad arguments */
  15640. /* in size is 0 */
  15641. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  15642. /* null Rc2 struct */
  15643. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  15644. BAD_FUNC_ARG);
  15645. /* null out buffer */
  15646. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  15647. BAD_FUNC_ARG);
  15648. /* null input buffer */
  15649. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  15650. BAD_FUNC_ARG);
  15651. #endif
  15652. return EXPECT_RESULT();
  15653. } /* END test_wc_Rc2CbcEncryptDecrypt */
  15654. /*
  15655. * Testing function for wc_AesSetIV
  15656. */
  15657. static int test_wc_AesSetIV(void)
  15658. {
  15659. int res = TEST_SKIPPED;
  15660. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15661. Aes aes;
  15662. int ret = 0;
  15663. byte key16[] =
  15664. {
  15665. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15666. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15667. };
  15668. byte iv1[] = "1234567890abcdef";
  15669. byte iv2[] = "0987654321fedcba";
  15670. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15671. if (ret != 0)
  15672. return ret;
  15673. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15674. iv1, AES_ENCRYPTION);
  15675. if (ret == 0) {
  15676. ret = wc_AesSetIV(&aes, iv2);
  15677. }
  15678. /* Test bad args. */
  15679. if (ret == 0) {
  15680. ret = wc_AesSetIV(NULL, iv1);
  15681. if (ret == BAD_FUNC_ARG) {
  15682. /* NULL iv should return 0. */
  15683. ret = wc_AesSetIV(&aes, NULL);
  15684. }
  15685. else {
  15686. ret = WOLFSSL_FATAL_ERROR;
  15687. }
  15688. }
  15689. wc_AesFree(&aes);
  15690. res = TEST_RES_CHECK(ret == 0);
  15691. #endif
  15692. return res;
  15693. } /* test_wc_AesSetIV */
  15694. /*
  15695. * Testing function for wc_AesSetKey().
  15696. */
  15697. static int test_wc_AesSetKey(void)
  15698. {
  15699. EXPECT_DECLS;
  15700. #ifndef NO_AES
  15701. Aes aes;
  15702. byte key16[] = {
  15703. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15704. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15705. };
  15706. #ifdef WOLFSSL_AES_192
  15707. byte key24[] = {
  15708. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15709. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15710. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15711. };
  15712. #endif
  15713. #ifdef WOLFSSL_AES_256
  15714. byte key32[] = {
  15715. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15716. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15717. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15718. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15719. };
  15720. #endif
  15721. byte badKey16[] = {
  15722. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15723. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15724. };
  15725. byte iv[] = "1234567890abcdef";
  15726. XMEMSET(&aes, 0, sizeof(Aes));
  15727. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15728. #ifdef WOLFSSL_AES_128
  15729. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  15730. iv, AES_ENCRYPTION), 0);
  15731. #endif
  15732. #ifdef WOLFSSL_AES_192
  15733. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  15734. iv, AES_ENCRYPTION), 0);
  15735. #endif
  15736. #ifdef WOLFSSL_AES_256
  15737. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  15738. iv, AES_ENCRYPTION), 0);
  15739. #endif
  15740. /* Pass in bad args. */
  15741. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  15742. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  15743. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  15744. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  15745. BAD_FUNC_ARG);
  15746. wc_AesFree(&aes);
  15747. #endif
  15748. return EXPECT_RESULT();
  15749. } /* END test_wc_AesSetKey */
  15750. /*
  15751. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15752. * and wc_AesCbcDecryptWithKey()
  15753. */
  15754. static int test_wc_AesCbcEncryptDecrypt(void)
  15755. {
  15756. EXPECT_DECLS;
  15757. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15758. defined(WOLFSSL_AES_256)
  15759. Aes aes;
  15760. byte key32[] = {
  15761. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15762. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15763. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15764. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15765. };
  15766. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  15767. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  15768. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  15769. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  15770. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  15771. };
  15772. byte iv[] = "1234567890abcdef";
  15773. byte enc[sizeof(vector)];
  15774. byte dec[sizeof(vector)];
  15775. byte dec2[sizeof(vector)];
  15776. /* Init stack variables. */
  15777. XMEMSET(&aes, 0, sizeof(Aes));
  15778. XMEMSET(enc, 0, sizeof(enc));
  15779. XMEMSET(dec, 0, sizeof(vector));
  15780. XMEMSET(dec2, 0, sizeof(vector));
  15781. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15782. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15783. AES_ENCRYPTION), 0);
  15784. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  15785. /* Re init for decrypt and set flag. */
  15786. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15787. AES_DECRYPTION), 0);
  15788. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  15789. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15790. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  15791. sizeof(key32)/sizeof(byte), iv), 0);
  15792. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  15793. /* Pass in bad args */
  15794. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  15795. BAD_FUNC_ARG);
  15796. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  15797. BAD_FUNC_ARG);
  15798. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  15799. BAD_FUNC_ARG);
  15800. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15801. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  15802. BAD_LENGTH_E);
  15803. #endif
  15804. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15805. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15806. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15807. "mode (v2), skip test");
  15808. #else
  15809. /* Test passing in size of 0 */
  15810. XMEMSET(enc, 0, sizeof(enc));
  15811. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  15812. /* Check enc was not modified */
  15813. {
  15814. int i;
  15815. for (i = 0; i < (int)sizeof(enc); i++)
  15816. ExpectIntEQ(enc[i], 0);
  15817. }
  15818. #endif
  15819. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  15820. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  15821. BAD_FUNC_ARG);
  15822. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  15823. BAD_FUNC_ARG);
  15824. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15825. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15826. BAD_LENGTH_E);
  15827. #else
  15828. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15829. BAD_FUNC_ARG);
  15830. #endif
  15831. /* Test passing in size of 0 */
  15832. XMEMSET(dec, 0, sizeof(dec));
  15833. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  15834. /* Check dec was not modified */
  15835. {
  15836. int i;
  15837. for (i = 0; i < (int)sizeof(dec); i++)
  15838. ExpectIntEQ(dec[i], 0);
  15839. }
  15840. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15841. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15842. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15843. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15844. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15845. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15846. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15847. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  15848. wc_AesFree(&aes);
  15849. #endif
  15850. return EXPECT_RESULT();
  15851. } /* END test_wc_AesCbcEncryptDecrypt */
  15852. /*
  15853. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15854. */
  15855. static int test_wc_AesCtrEncryptDecrypt(void)
  15856. {
  15857. EXPECT_DECLS;
  15858. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15859. Aes aesEnc;
  15860. Aes aesDec;
  15861. byte key32[] = {
  15862. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15863. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15864. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15865. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15866. };
  15867. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15868. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15869. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15870. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15871. };
  15872. byte iv[] = "1234567890abcdef";
  15873. byte enc[AES_BLOCK_SIZE * 2];
  15874. byte dec[AES_BLOCK_SIZE * 2];
  15875. /* Init stack variables. */
  15876. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15877. XMEMSET(&aesDec, 0, sizeof(Aes));
  15878. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15879. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15880. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15881. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15882. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  15883. AES_ENCRYPTION), 0);
  15884. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15885. sizeof(vector)/sizeof(byte)), 0);
  15886. /* Decrypt with wc_AesCtrEncrypt() */
  15887. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  15888. AES_ENCRYPTION), 0);
  15889. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  15890. 0);
  15891. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15892. /* Test bad args. */
  15893. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  15894. BAD_FUNC_ARG);
  15895. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  15896. BAD_FUNC_ARG);
  15897. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  15898. BAD_FUNC_ARG);
  15899. wc_AesFree(&aesEnc);
  15900. wc_AesFree(&aesDec);
  15901. #endif
  15902. return EXPECT_RESULT();
  15903. } /* END test_wc_AesCtrEncryptDecrypt */
  15904. /*
  15905. * test function for wc_AesGcmSetKey()
  15906. */
  15907. static int test_wc_AesGcmSetKey(void)
  15908. {
  15909. EXPECT_DECLS;
  15910. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15911. Aes aes;
  15912. #ifdef WOLFSSL_AES_128
  15913. byte key16[] = {
  15914. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15915. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15916. };
  15917. #endif
  15918. #ifdef WOLFSSL_AES_192
  15919. byte key24[] = {
  15920. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15921. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15922. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15923. };
  15924. #endif
  15925. #ifdef WOLFSSL_AES_256
  15926. byte key32[] = {
  15927. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15928. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15929. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15930. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15931. };
  15932. #endif
  15933. byte badKey16[] = {
  15934. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15935. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15936. };
  15937. byte badKey24[] = {
  15938. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15939. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15940. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15941. };
  15942. byte badKey32[] = {
  15943. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15944. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15945. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15946. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15947. };
  15948. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15949. #ifdef WOLFSSL_AES_128
  15950. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  15951. #endif
  15952. #ifdef WOLFSSL_AES_192
  15953. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  15954. #endif
  15955. #ifdef WOLFSSL_AES_256
  15956. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15957. #endif
  15958. /* Pass in bad args. */
  15959. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  15960. BAD_FUNC_ARG);
  15961. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  15962. BAD_FUNC_ARG);
  15963. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  15964. BAD_FUNC_ARG);
  15965. wc_AesFree(&aes);
  15966. #endif
  15967. return EXPECT_RESULT();
  15968. } /* END test_wc_AesGcmSetKey */
  15969. /*
  15970. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15971. */
  15972. static int test_wc_AesGcmEncryptDecrypt(void)
  15973. {
  15974. EXPECT_DECLS;
  15975. /* WOLFSSL_AFALG requires 12 byte IV */
  15976. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15977. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15978. Aes aes;
  15979. byte key32[] = {
  15980. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15981. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15982. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15983. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15984. };
  15985. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15986. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15987. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15988. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15989. };
  15990. const byte a[] = {
  15991. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15992. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15993. 0xab, 0xad, 0xda, 0xd2
  15994. };
  15995. byte iv[] = "1234567890a";
  15996. byte longIV[] = "1234567890abcdefghij";
  15997. byte enc[sizeof(vector)];
  15998. byte resultT[AES_BLOCK_SIZE];
  15999. byte dec[sizeof(vector)];
  16000. /* Init stack variables. */
  16001. XMEMSET(&aes, 0, sizeof(Aes));
  16002. XMEMSET(enc, 0, sizeof(vector));
  16003. XMEMSET(dec, 0, sizeof(vector));
  16004. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16005. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16006. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16007. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16008. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16009. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16010. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16011. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16012. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16013. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16014. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16015. BAD_FUNC_ARG);
  16016. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16017. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16018. BAD_FUNC_ARG);
  16019. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16020. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16021. BAD_FUNC_ARG);
  16022. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16023. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16024. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16025. /* FIPS does not check the lower bound of ivSz */
  16026. #else
  16027. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16028. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16029. #endif
  16030. /* This case is now considered good. Long IVs are now allowed.
  16031. * Except for the original FIPS release, it still has an upper
  16032. * bound on the IV length. */
  16033. #if (!defined(HAVE_FIPS) || \
  16034. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16035. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16036. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16037. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16038. 0);
  16039. #else
  16040. (void)longIV;
  16041. #endif /* Old FIPS */
  16042. /* END wc_AesGcmEncrypt */
  16043. #ifdef HAVE_AES_DECRYPT
  16044. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16045. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16046. BAD_FUNC_ARG);
  16047. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16048. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16049. BAD_FUNC_ARG);
  16050. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16051. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16052. BAD_FUNC_ARG);
  16053. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16054. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16055. BAD_FUNC_ARG);
  16056. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16057. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16058. BAD_FUNC_ARG);
  16059. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16060. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16061. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16062. AES_GCM_AUTH_E);
  16063. #else
  16064. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16065. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16066. BAD_FUNC_ARG);
  16067. #endif
  16068. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16069. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16070. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16071. /* FIPS does not check the lower bound of ivSz */
  16072. #else
  16073. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16074. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16075. #endif
  16076. #endif /* HAVE_AES_DECRYPT */
  16077. wc_AesFree(&aes);
  16078. #endif
  16079. return EXPECT_RESULT();
  16080. } /* END test_wc_AesGcmEncryptDecrypt */
  16081. /*
  16082. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16083. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16084. */
  16085. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16086. {
  16087. EXPECT_DECLS;
  16088. #if (!defined(HAVE_FIPS) || \
  16089. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16090. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16091. const byte key[] = {
  16092. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16093. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16094. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16095. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16096. };
  16097. const byte in[] = {
  16098. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16099. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16100. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16101. };
  16102. const byte aad[] = {
  16103. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16104. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16105. 0xab, 0xad, 0xda, 0xd2
  16106. };
  16107. Aes aesEnc;
  16108. Aes aesDec;
  16109. byte iv[] = "1234567890abcdefghij";
  16110. byte out[sizeof(in)];
  16111. byte plain[sizeof(in)];
  16112. byte tag[AES_BLOCK_SIZE];
  16113. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16114. XMEMSET(&aesDec, 0, sizeof(Aes));
  16115. XMEMSET(out, 0, sizeof(out));
  16116. XMEMSET(plain, 0, sizeof(plain));
  16117. XMEMSET(tag, 0, sizeof(tag));
  16118. /* Perform one-shot encryption using long IV */
  16119. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16120. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16121. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16122. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16123. /* Perform streaming decryption using long IV */
  16124. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16125. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16126. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16127. sizeof(aad)), 0);
  16128. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16129. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16130. /* Free resources */
  16131. wc_AesFree(&aesEnc);
  16132. wc_AesFree(&aesDec);
  16133. #endif
  16134. return EXPECT_RESULT();
  16135. } /* END wc_AesGcmMixedEncDecLongIV */
  16136. /*
  16137. * unit test for wc_GmacSetKey()
  16138. */
  16139. static int test_wc_GmacSetKey(void)
  16140. {
  16141. EXPECT_DECLS;
  16142. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16143. Gmac gmac;
  16144. byte key16[] = {
  16145. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16146. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16147. };
  16148. #ifdef WOLFSSL_AES_192
  16149. byte key24[] = {
  16150. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16151. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16152. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16153. };
  16154. #endif
  16155. #ifdef WOLFSSL_AES_256
  16156. byte key32[] = {
  16157. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16158. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16159. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16160. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16161. };
  16162. #endif
  16163. byte badKey16[] = {
  16164. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16165. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16166. };
  16167. byte badKey24[] = {
  16168. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16169. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16170. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16171. };
  16172. byte badKey32[] = {
  16173. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16174. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16175. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16176. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16177. };
  16178. XMEMSET(&gmac, 0, sizeof(Gmac));
  16179. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16180. #ifdef WOLFSSL_AES_128
  16181. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16182. #endif
  16183. #ifdef WOLFSSL_AES_192
  16184. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16185. #endif
  16186. #ifdef WOLFSSL_AES_256
  16187. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16188. #endif
  16189. /* Pass in bad args. */
  16190. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16191. BAD_FUNC_ARG);
  16192. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16193. BAD_FUNC_ARG);
  16194. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16195. BAD_FUNC_ARG);
  16196. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16197. BAD_FUNC_ARG);
  16198. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16199. BAD_FUNC_ARG);
  16200. wc_AesFree(&gmac.aes);
  16201. #endif
  16202. return EXPECT_RESULT();
  16203. } /* END test_wc_GmacSetKey */
  16204. /*
  16205. * unit test for wc_GmacUpdate
  16206. */
  16207. static int test_wc_GmacUpdate(void)
  16208. {
  16209. EXPECT_DECLS;
  16210. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16211. Gmac gmac;
  16212. #ifdef WOLFSSL_AES_128
  16213. const byte key16[] = {
  16214. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16215. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16216. };
  16217. #endif
  16218. #ifdef WOLFSSL_AES_192
  16219. byte key24[] = {
  16220. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16221. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16222. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16223. };
  16224. #endif
  16225. #ifdef WOLFSSL_AES_256
  16226. byte key32[] = {
  16227. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16228. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16229. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16230. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16231. };
  16232. #endif
  16233. #ifdef WOLFSSL_AES_128
  16234. const byte authIn[] = {
  16235. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16236. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16237. };
  16238. #endif
  16239. #ifdef WOLFSSL_AES_192
  16240. const byte authIn2[] = {
  16241. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16242. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16243. };
  16244. #endif
  16245. const byte authIn3[] = {
  16246. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16247. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16248. };
  16249. #ifdef WOLFSSL_AES_128
  16250. const byte tag1[] = { /* Known. */
  16251. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16252. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16253. };
  16254. #endif
  16255. #ifdef WOLFSSL_AES_192
  16256. const byte tag2[] = { /* Known */
  16257. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16258. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16259. };
  16260. #endif
  16261. const byte tag3[] = { /* Known */
  16262. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16263. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16264. };
  16265. #ifdef WOLFSSL_AES_128
  16266. const byte iv[] = {
  16267. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16268. 0xe2, 0x8c, 0x8f, 0x16
  16269. };
  16270. #endif
  16271. #ifdef WOLFSSL_AES_192
  16272. const byte iv2[] = {
  16273. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16274. 0x7e, 0x1a, 0x6f, 0xbc
  16275. };
  16276. #endif
  16277. const byte iv3[] = {
  16278. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16279. 0xc3, 0xfb, 0x6c, 0x8a
  16280. };
  16281. byte tagOut[16];
  16282. byte tagOut2[24];
  16283. byte tagOut3[32];
  16284. /* Init stack variables. */
  16285. XMEMSET(&gmac, 0, sizeof(Gmac));
  16286. XMEMSET(tagOut, 0, sizeof(tagOut));
  16287. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16288. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16289. #ifdef WOLFSSL_AES_128
  16290. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16291. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16292. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16293. tagOut, sizeof(tag1)), 0);
  16294. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16295. wc_AesFree(&gmac.aes);
  16296. #endif
  16297. #ifdef WOLFSSL_AES_192
  16298. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16299. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16300. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16301. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16302. tagOut2, sizeof(tag2)), 0);
  16303. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16304. wc_AesFree(&gmac.aes);
  16305. #endif
  16306. #ifdef WOLFSSL_AES_256
  16307. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16308. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16309. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16310. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16311. tagOut3, sizeof(tag3)), 0);
  16312. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16313. wc_AesFree(&gmac.aes);
  16314. #endif
  16315. /* Pass bad args. */
  16316. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16317. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16318. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16319. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16320. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16321. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16322. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16323. wc_AesFree(&gmac.aes);
  16324. #endif
  16325. return EXPECT_RESULT();
  16326. } /* END test_wc_GmacUpdate */
  16327. /*
  16328. * testing wc_CamelliaSetKey
  16329. */
  16330. static int test_wc_CamelliaSetKey(void)
  16331. {
  16332. EXPECT_DECLS;
  16333. #ifdef HAVE_CAMELLIA
  16334. Camellia camellia;
  16335. /*128-bit key*/
  16336. static const byte key16[] = {
  16337. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16338. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16339. };
  16340. /* 192-bit key */
  16341. static const byte key24[] = {
  16342. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16343. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16344. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16345. };
  16346. /* 256-bit key */
  16347. static const byte key32[] = {
  16348. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16349. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16350. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16351. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16352. };
  16353. static const byte iv[] = {
  16354. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16355. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16356. };
  16357. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16358. 0);
  16359. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16360. NULL), 0);
  16361. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16362. 0);
  16363. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16364. NULL), 0);
  16365. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16366. 0);
  16367. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16368. NULL), 0);
  16369. /* Bad args. */
  16370. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16371. BAD_FUNC_ARG);
  16372. #endif
  16373. return EXPECT_RESULT();
  16374. } /* END test_wc_CammeliaSetKey */
  16375. /*
  16376. * Testing wc_CamelliaSetIV()
  16377. */
  16378. static int test_wc_CamelliaSetIV(void)
  16379. {
  16380. EXPECT_DECLS;
  16381. #ifdef HAVE_CAMELLIA
  16382. Camellia camellia;
  16383. static const byte iv[] = {
  16384. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16385. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16386. };
  16387. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16388. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16389. /* Bad args. */
  16390. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16391. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16392. #endif
  16393. return EXPECT_RESULT();
  16394. } /* END test_wc_CamelliaSetIV*/
  16395. /*
  16396. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16397. */
  16398. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16399. {
  16400. EXPECT_DECLS;
  16401. #ifdef HAVE_CAMELLIA
  16402. Camellia camellia;
  16403. static const byte key24[] = {
  16404. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16405. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16406. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16407. };
  16408. static const byte iv[] = {
  16409. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16410. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16411. };
  16412. static const byte plainT[] = {
  16413. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16414. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16415. };
  16416. byte enc[sizeof(plainT)];
  16417. byte dec[sizeof(enc)];
  16418. /* Init stack variables.*/
  16419. XMEMSET(enc, 0, 16);
  16420. XMEMSET(enc, 0, 16);
  16421. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16422. 0);
  16423. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16424. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16425. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16426. /* Pass bad args. */
  16427. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16428. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16429. BAD_FUNC_ARG);
  16430. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16431. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16432. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16433. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16434. #endif
  16435. return EXPECT_RESULT();
  16436. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16437. /*
  16438. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16439. */
  16440. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16441. {
  16442. EXPECT_DECLS;
  16443. #ifdef HAVE_CAMELLIA
  16444. Camellia camellia;
  16445. static const byte key24[] = {
  16446. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16447. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16448. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16449. };
  16450. static const byte plainT[] = {
  16451. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16452. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16453. };
  16454. byte enc[CAMELLIA_BLOCK_SIZE];
  16455. byte dec[CAMELLIA_BLOCK_SIZE];
  16456. /* Init stack variables. */
  16457. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16458. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16459. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16460. NULL), 0);
  16461. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16462. CAMELLIA_BLOCK_SIZE), 0);
  16463. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16464. NULL), 0);
  16465. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16466. 0);
  16467. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16468. /* Pass in bad args. */
  16469. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16470. BAD_FUNC_ARG);
  16471. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16472. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16473. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16474. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16475. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16476. BAD_FUNC_ARG);
  16477. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16478. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16479. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16480. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16481. #endif
  16482. return EXPECT_RESULT();
  16483. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16484. /*
  16485. * Testing wc_Arc4SetKey()
  16486. */
  16487. static int test_wc_Arc4SetKey(void)
  16488. {
  16489. EXPECT_DECLS;
  16490. #ifndef NO_RC4
  16491. Arc4 arc;
  16492. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16493. int keyLen = 8;
  16494. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  16495. /* Test bad args. */
  16496. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  16497. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  16498. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16499. #endif
  16500. return EXPECT_RESULT();
  16501. } /* END test_wc_Arc4SetKey */
  16502. /*
  16503. * Testing wc_Arc4Process for ENC/DEC.
  16504. */
  16505. static int test_wc_Arc4Process(void)
  16506. {
  16507. EXPECT_DECLS;
  16508. #ifndef NO_RC4
  16509. Arc4 enc;
  16510. Arc4 dec;
  16511. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16512. int keyLen = 8;
  16513. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16514. byte cipher[8];
  16515. byte plain[8];
  16516. /* Init stack variables */
  16517. XMEMSET(&enc, 0, sizeof(Arc4));
  16518. XMEMSET(&dec, 0, sizeof(Arc4));
  16519. XMEMSET(cipher, 0, sizeof(cipher));
  16520. XMEMSET(plain, 0, sizeof(plain));
  16521. /* Use for async. */
  16522. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16523. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16524. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  16525. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  16526. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  16527. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  16528. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16529. /* Bad args. */
  16530. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  16531. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  16532. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  16533. wc_Arc4Free(&enc);
  16534. wc_Arc4Free(&dec);
  16535. #endif
  16536. return EXPECT_RESULT();
  16537. } /* END test_wc_Arc4Process */
  16538. /*
  16539. * Testing wc_Init RsaKey()
  16540. */
  16541. static int test_wc_InitRsaKey(void)
  16542. {
  16543. EXPECT_DECLS;
  16544. #ifndef NO_RSA
  16545. RsaKey key;
  16546. XMEMSET(&key, 0, sizeof(RsaKey));
  16547. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16548. /* Test bad args. */
  16549. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  16550. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16551. #endif
  16552. return EXPECT_RESULT();
  16553. } /* END test_wc_InitRsaKey */
  16554. /*
  16555. * Testing wc_RsaPrivateKeyDecode()
  16556. */
  16557. static int test_wc_RsaPrivateKeyDecode(void)
  16558. {
  16559. EXPECT_DECLS;
  16560. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16561. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16562. RsaKey key;
  16563. byte* tmp = NULL;
  16564. word32 idx = 0;
  16565. int bytes = 0;
  16566. XMEMSET(&key, 0, sizeof(RsaKey));
  16567. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  16568. DYNAMIC_TYPE_TMP_BUFFER));
  16569. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16570. if (tmp != NULL) {
  16571. #ifdef USE_CERT_BUFFERS_1024
  16572. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16573. bytes = sizeof_client_key_der_1024;
  16574. #else
  16575. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16576. bytes = sizeof_client_key_der_2048;
  16577. #endif /* Use cert buffers. */
  16578. }
  16579. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  16580. /* Test bad args. */
  16581. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16582. BAD_FUNC_ARG);
  16583. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16584. BAD_FUNC_ARG);
  16585. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16586. BAD_FUNC_ARG);
  16587. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16588. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16589. #endif
  16590. return EXPECT_RESULT();
  16591. } /* END test_wc_RsaPrivateKeyDecode */
  16592. /*
  16593. * Testing wc_RsaPublicKeyDecode()
  16594. */
  16595. static int test_wc_RsaPublicKeyDecode(void)
  16596. {
  16597. EXPECT_DECLS;
  16598. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16599. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16600. RsaKey keyPub;
  16601. byte* tmp = NULL;
  16602. word32 idx = 0;
  16603. int bytes = 0;
  16604. word32 keySz = 0;
  16605. word32 tstKeySz = 0;
  16606. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16607. XFILE f = XBADFILE;
  16608. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16609. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16610. byte buf[4096];
  16611. #endif
  16612. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  16613. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16614. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  16615. if (tmp != NULL) {
  16616. #ifdef USE_CERT_BUFFERS_1024
  16617. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16618. bytes = sizeof_client_keypub_der_1024;
  16619. keySz = 1024;
  16620. #else
  16621. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16622. bytes = sizeof_client_keypub_der_2048;
  16623. keySz = 2048;
  16624. #endif
  16625. }
  16626. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  16627. /* Pass in bad args. */
  16628. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16629. BAD_FUNC_ARG);
  16630. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16631. BAD_FUNC_ARG);
  16632. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16633. BAD_FUNC_ARG);
  16634. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  16635. /* Test for getting modulus key size */
  16636. idx = 0;
  16637. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16638. &tstKeySz, NULL, NULL), 0);
  16639. ExpectIntEQ(tstKeySz, keySz/8);
  16640. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16641. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  16642. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16643. if (f != XBADFILE) {
  16644. XFCLOSE(f);
  16645. f = XBADFILE;
  16646. }
  16647. idx = 0;
  16648. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16649. NULL), 0);
  16650. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  16651. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16652. if (f != XBADFILE)
  16653. XFCLOSE(f);
  16654. idx = 0;
  16655. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16656. NULL), 0);
  16657. #endif
  16658. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16659. #endif
  16660. return EXPECT_RESULT();
  16661. } /* END test_wc_RsaPublicKeyDecode */
  16662. /*
  16663. * Testing wc_RsaPublicKeyDecodeRaw()
  16664. */
  16665. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16666. {
  16667. EXPECT_DECLS;
  16668. #if !defined(NO_RSA)
  16669. RsaKey key;
  16670. const byte n = 0x23;
  16671. const byte e = 0x03;
  16672. int nSz = sizeof(n);
  16673. int eSz = sizeof(e);
  16674. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16675. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  16676. /* Pass in bad args. */
  16677. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16678. BAD_FUNC_ARG);
  16679. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16680. BAD_FUNC_ARG);
  16681. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16682. BAD_FUNC_ARG);
  16683. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16684. #endif
  16685. return EXPECT_RESULT();
  16686. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16687. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16688. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16689. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16690. * trying until it gets a probable prime. */
  16691. #ifdef HAVE_FIPS
  16692. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16693. {
  16694. int ret;
  16695. for (;;) {
  16696. ret = wc_MakeRsaKey(key, size, e, rng);
  16697. if (ret != PRIME_GEN_E) break;
  16698. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16699. "trying again.\n");
  16700. }
  16701. return ret;
  16702. }
  16703. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16704. #else
  16705. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16706. #endif
  16707. #endif
  16708. /*
  16709. * Testing wc_MakeRsaKey()
  16710. */
  16711. static int test_wc_MakeRsaKey(void)
  16712. {
  16713. EXPECT_DECLS;
  16714. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16715. RsaKey genKey;
  16716. WC_RNG rng;
  16717. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16718. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16719. int bits = 1024;
  16720. #else
  16721. int bits = 2048;
  16722. #endif
  16723. XMEMSET(&genKey, 0, sizeof(RsaKey));
  16724. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16725. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16726. ExpectIntEQ(wc_InitRng(&rng), 0);
  16727. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16728. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16729. /* Test bad args. */
  16730. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  16731. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16732. BAD_FUNC_ARG);
  16733. /* e < 3 */
  16734. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  16735. /* e & 1 == 0 */
  16736. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  16737. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16738. #endif
  16739. return EXPECT_RESULT();
  16740. } /* END test_wc_MakeRsaKey */
  16741. /*
  16742. * Test the bounds checking on the cipher text versus the key modulus.
  16743. * 1. Make a new RSA key.
  16744. * 2. Set c to 1.
  16745. * 3. Decrypt c into k. (error)
  16746. * 4. Copy the key modulus to c and sub 1 from the copy.
  16747. * 5. Decrypt c into k. (error)
  16748. * Valid bounds test cases are covered by all the other RSA tests.
  16749. */
  16750. static int test_RsaDecryptBoundsCheck(void)
  16751. {
  16752. EXPECT_DECLS;
  16753. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16754. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16755. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16756. WC_RNG rng;
  16757. RsaKey key;
  16758. byte flatC[256];
  16759. word32 flatCSz;
  16760. byte out[256];
  16761. word32 outSz = sizeof(out);
  16762. XMEMSET(&key, 0, sizeof(RsaKey));
  16763. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16764. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16765. ExpectIntEQ(wc_InitRng(&rng), 0);
  16766. if (EXPECT_SUCCESS()) {
  16767. const byte* derKey;
  16768. word32 derKeySz;
  16769. word32 idx = 0;
  16770. #ifdef USE_CERT_BUFFERS_1024
  16771. derKey = server_key_der_1024;
  16772. derKeySz = (word32)sizeof_server_key_der_1024;
  16773. flatCSz = 128;
  16774. #else
  16775. derKey = server_key_der_2048;
  16776. derKeySz = (word32)sizeof_server_key_der_2048;
  16777. flatCSz = 256;
  16778. #endif
  16779. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  16780. }
  16781. if (EXPECT_SUCCESS()) {
  16782. XMEMSET(flatC, 0, flatCSz);
  16783. flatC[flatCSz-1] = 1;
  16784. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16785. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  16786. if (EXPECT_SUCCESS()) {
  16787. mp_int c;
  16788. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  16789. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  16790. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  16791. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16792. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  16793. mp_clear(&c);
  16794. }
  16795. }
  16796. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16797. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16798. #endif
  16799. return EXPECT_RESULT();
  16800. } /* END test_wc_RsaDecryptBoundsCheck */
  16801. /*
  16802. * Testing wc_SetKeyUsage()
  16803. */
  16804. static int test_wc_SetKeyUsage(void)
  16805. {
  16806. EXPECT_DECLS;
  16807. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16808. Cert myCert;
  16809. ExpectIntEQ(wc_InitCert(&myCert), 0);
  16810. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  16811. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  16812. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  16813. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  16814. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  16815. /* Test bad args. */
  16816. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  16817. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  16818. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  16819. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  16820. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  16821. KEYUSAGE_E);
  16822. #endif
  16823. return EXPECT_RESULT();
  16824. } /* END test_wc_SetKeyUsage */
  16825. /*
  16826. * Testing wc_CheckProbablePrime()
  16827. */
  16828. static int test_wc_CheckProbablePrime(void)
  16829. {
  16830. EXPECT_DECLS;
  16831. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16832. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16833. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16834. RsaKey key;
  16835. WC_RNG rng;
  16836. byte e[3];
  16837. word32 eSz = (word32)sizeof(e);
  16838. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16839. word32 nSz = (word32)sizeof(n);
  16840. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16841. word32 dSz = (word32)sizeof(d);
  16842. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16843. word32 pSz = (word32)sizeof(p);
  16844. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16845. word32 qSz = (word32)sizeof(q);
  16846. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16847. int* isPrime;
  16848. int test[5];
  16849. isPrime = test;
  16850. XMEMSET(&key, 0, sizeof(RsaKey));
  16851. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16852. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16853. ExpectIntEQ(wc_InitRng(&rng), 0);
  16854. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16855. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  16856. WC_RSA_EXPONENT, &rng), 0);
  16857. PRIVATE_KEY_UNLOCK();
  16858. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  16859. &qSz), 0);
  16860. PRIVATE_KEY_LOCK();
  16861. /* Bad cases */
  16862. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  16863. BAD_FUNC_ARG);
  16864. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  16865. BAD_FUNC_ARG);
  16866. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  16867. BAD_FUNC_ARG);
  16868. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  16869. BAD_FUNC_ARG);
  16870. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  16871. BAD_FUNC_ARG);
  16872. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  16873. BAD_FUNC_ARG);
  16874. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  16875. BAD_FUNC_ARG);
  16876. /* Good case */
  16877. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  16878. 0);
  16879. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16880. wc_FreeRng(&rng);
  16881. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16882. #endif
  16883. return EXPECT_RESULT();
  16884. } /* END test_wc_CheckProbablePrime */
  16885. /*
  16886. * Testing wc_RsaPSS_Verify()
  16887. */
  16888. static int test_wc_RsaPSS_Verify(void)
  16889. {
  16890. EXPECT_DECLS;
  16891. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16892. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16893. RsaKey key;
  16894. WC_RNG rng;
  16895. int sz = 256;
  16896. const char* szMessage = "This is the string to be signed";
  16897. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16898. unsigned char pDecrypted[2048/8];
  16899. byte* pt = pDecrypted;
  16900. word32 outLen = sizeof(pDecrypted);
  16901. XMEMSET(&key, 0, sizeof(RsaKey));
  16902. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16903. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16904. ExpectIntEQ(wc_InitRng(&rng), 0);
  16905. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16906. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16907. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  16908. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  16909. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16910. /* Bad cases */
  16911. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16912. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16913. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16914. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16915. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16916. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16917. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16918. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16919. /* Good case */
  16920. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16921. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16922. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16923. wc_FreeRng(&rng);
  16924. #endif
  16925. return EXPECT_RESULT();
  16926. } /* END test_wc_RsaPSS_Verify */
  16927. /*
  16928. * Testing wc_RsaPSS_VerifyCheck()
  16929. */
  16930. static int test_wc_RsaPSS_VerifyCheck(void)
  16931. {
  16932. EXPECT_DECLS;
  16933. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16934. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16935. RsaKey key;
  16936. WC_RNG rng;
  16937. int sz = 256; /* 2048/8 */
  16938. byte digest[32];
  16939. word32 digestSz = sizeof(digest);
  16940. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16941. word32 pSignatureSz = sizeof(pSignature);
  16942. unsigned char pDecrypted[2048/8];
  16943. byte* pt = pDecrypted;
  16944. word32 outLen = sizeof(pDecrypted);
  16945. XMEMSET(&key, 0, sizeof(RsaKey));
  16946. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16947. XMEMSET(digest, 0, sizeof(digest));
  16948. XMEMSET(pSignature, 0, sizeof(pSignature));
  16949. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16950. ExpectIntEQ(wc_InitRng(&rng), 0);
  16951. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16952. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16953. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16954. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16955. 0);
  16956. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16957. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16958. /* Bad cases */
  16959. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  16960. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16961. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  16962. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16963. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  16964. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16965. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  16966. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16967. /* Good case */
  16968. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  16969. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16970. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16971. wc_FreeRng(&rng);
  16972. #endif
  16973. return EXPECT_RESULT();
  16974. } /* END test_wc_RsaPSS_VerifyCheck */
  16975. /*
  16976. * Testing wc_RsaPSS_VerifyCheckInline()
  16977. */
  16978. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16979. {
  16980. EXPECT_DECLS;
  16981. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16982. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16983. RsaKey key;
  16984. WC_RNG rng;
  16985. int sz = 256;
  16986. byte digest[32];
  16987. word32 digestSz = sizeof(digest);
  16988. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16989. unsigned char pDecrypted[2048/8];
  16990. byte* pt = pDecrypted;
  16991. XMEMSET(&key, 0, sizeof(RsaKey));
  16992. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16993. XMEMSET(digest, 0, sizeof(digest));
  16994. XMEMSET(pSignature, 0, sizeof(pSignature));
  16995. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16996. ExpectIntEQ(wc_InitRng(&rng), 0);
  16997. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16998. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16999. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17000. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17001. 0);
  17002. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17003. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17004. /* Bad Cases */
  17005. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  17006. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17007. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17008. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17009. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17010. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17011. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17012. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17013. /* Good case */
  17014. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17015. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17016. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17017. wc_FreeRng(&rng);
  17018. #endif
  17019. return EXPECT_RESULT();
  17020. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17021. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17022. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17023. {
  17024. (void)flag;
  17025. (void)type;
  17026. (void)file;
  17027. (void)line;
  17028. }
  17029. #endif
  17030. /*
  17031. * Testing wc_LockMutex_ex
  17032. */
  17033. static int test_wc_LockMutex_ex(void)
  17034. {
  17035. EXPECT_DECLS;
  17036. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17037. int flag = CRYPTO_LOCK;
  17038. int type = 0;
  17039. const char* file = "./test-LockMutex_ex.txt";
  17040. int line = 0;
  17041. /* without SetMutexCb */
  17042. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17043. /* with SetMutexCb */
  17044. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17045. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17046. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17047. #endif
  17048. return EXPECT_RESULT();
  17049. } /* End test_wc_LockMutex_ex*/
  17050. /*
  17051. * Testing wc_SetMutexCb
  17052. */
  17053. static int test_wc_SetMutexCb(void)
  17054. {
  17055. EXPECT_DECLS;
  17056. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17057. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17058. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17059. #endif
  17060. return EXPECT_RESULT();
  17061. } /* End test_wc_SetMutexCb*/
  17062. /*
  17063. * Testing wc_RsaKeyToDer()
  17064. */
  17065. static int test_wc_RsaKeyToDer(void)
  17066. {
  17067. EXPECT_DECLS;
  17068. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17069. RsaKey genKey;
  17070. WC_RNG rng;
  17071. byte* der = NULL;
  17072. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17073. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17074. int bits = 1024;
  17075. word32 derSz = 611;
  17076. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17077. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17078. #else
  17079. int bits = 2048;
  17080. word32 derSz = 1196;
  17081. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17082. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17083. #endif
  17084. XMEMSET(&rng, 0, sizeof(rng));
  17085. XMEMSET(&genKey, 0, sizeof(genKey));
  17086. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17087. /* Init structures. */
  17088. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17089. ExpectIntEQ(wc_InitRng(&rng), 0);
  17090. /* Make key. */
  17091. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17092. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17093. /* Pass good/bad args. */
  17094. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17095. /* Get just the output length */
  17096. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17097. /* Try Public Key. */
  17098. genKey.type = 0;
  17099. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17100. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17101. /* Put back to Private Key */
  17102. genKey.type = 1;
  17103. #endif
  17104. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17105. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17106. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17107. #endif
  17108. return EXPECT_RESULT();
  17109. } /* END test_wc_RsaKeyToDer */
  17110. /*
  17111. * Testing wc_RsaKeyToPublicDer()
  17112. */
  17113. static int test_wc_RsaKeyToPublicDer(void)
  17114. {
  17115. EXPECT_DECLS;
  17116. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17117. RsaKey key;
  17118. WC_RNG rng;
  17119. byte* der = NULL;
  17120. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17121. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17122. int bits = 1024;
  17123. word32 derLen = 162;
  17124. #else
  17125. int bits = 2048;
  17126. word32 derLen = 294;
  17127. #endif
  17128. int ret;
  17129. XMEMSET(&rng, 0, sizeof(rng));
  17130. XMEMSET(&key, 0, sizeof(key));
  17131. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17132. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17133. ExpectIntEQ(wc_InitRng(&rng), 0);
  17134. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17135. /* test getting size only */
  17136. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17137. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17138. /* test getting size only */
  17139. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17140. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17141. /* Pass in bad args. */
  17142. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17143. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17144. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17145. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17146. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17147. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17148. #endif
  17149. return EXPECT_RESULT();
  17150. } /* END test_wc_RsaKeyToPublicDer */
  17151. /*
  17152. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17153. */
  17154. static int test_wc_RsaPublicEncryptDecrypt(void)
  17155. {
  17156. EXPECT_DECLS;
  17157. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17158. RsaKey key;
  17159. WC_RNG rng;
  17160. const char inStr[] = TEST_STRING;
  17161. const word32 plainLen = (word32)TEST_STRING_SZ;
  17162. const word32 inLen = (word32)TEST_STRING_SZ;
  17163. int bits = TEST_RSA_BITS;
  17164. const word32 cipherLen = TEST_RSA_BYTES;
  17165. word32 cipherLenResult = cipherLen;
  17166. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17167. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17168. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17169. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17170. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17171. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17172. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17173. ExpectNotNull(in);
  17174. ExpectNotNull(plain);
  17175. ExpectNotNull(cipher);
  17176. #endif
  17177. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17178. /* Initialize stack structures. */
  17179. XMEMSET(&key, 0, sizeof(RsaKey));
  17180. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17181. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17182. ExpectIntEQ(wc_InitRng(&rng), 0);
  17183. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17184. /* Encrypt. */
  17185. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  17186. cipherLen, &key, &rng), 0);
  17187. /* Pass bad args - tested in another testing function.*/
  17188. /* Decrypt */
  17189. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17190. /* Bind rng */
  17191. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17192. #endif
  17193. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17194. &key), 0);
  17195. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17196. /* Pass bad args - tested in another testing function.*/
  17197. WC_FREE_VAR(in, NULL);
  17198. WC_FREE_VAR(plain, NULL);
  17199. WC_FREE_VAR(cipher, NULL);
  17200. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17201. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17202. #endif
  17203. return EXPECT_RESULT();
  17204. } /* END test_wc_RsaPublicEncryptDecrypt */
  17205. /*
  17206. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17207. */
  17208. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17209. {
  17210. EXPECT_DECLS;
  17211. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17212. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17213. RsaKey key;
  17214. WC_RNG rng;
  17215. const char inStr[] = TEST_STRING;
  17216. const word32 inLen = (word32)TEST_STRING_SZ;
  17217. const word32 plainSz = (word32)TEST_STRING_SZ;
  17218. byte* res = NULL;
  17219. int idx = 0;
  17220. int bits = TEST_RSA_BITS;
  17221. const word32 cipherSz = TEST_RSA_BYTES;
  17222. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17223. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17224. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17225. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17226. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17227. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17228. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17229. ExpectNotNull(in);
  17230. ExpectNotNull(plain);
  17231. ExpectNotNull(cipher);
  17232. #endif
  17233. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17234. /* Initialize stack structures. */
  17235. XMEMSET(&key, 0, sizeof(RsaKey));
  17236. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17237. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17238. ExpectIntEQ(wc_InitRng(&rng), 0);
  17239. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17240. /* Encrypt */
  17241. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17242. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17243. /* Pass bad args - tested in another testing function.*/
  17244. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17245. /* Decrypt */
  17246. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17247. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17248. #endif
  17249. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17250. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17251. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17252. /* Pass bad args - tested in another testing function.*/
  17253. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17254. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17255. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17256. #endif
  17257. WC_FREE_VAR(in, NULL);
  17258. WC_FREE_VAR(plain, NULL);
  17259. WC_FREE_VAR(cipher, NULL);
  17260. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17261. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17262. #endif
  17263. return EXPECT_RESULT();
  17264. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17265. /*
  17266. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17267. */
  17268. static int test_wc_RsaSSL_SignVerify(void)
  17269. {
  17270. EXPECT_DECLS;
  17271. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17272. RsaKey key;
  17273. WC_RNG rng;
  17274. const char inStr[] = TEST_STRING;
  17275. const word32 plainSz = (word32)TEST_STRING_SZ;
  17276. const word32 inLen = (word32)TEST_STRING_SZ;
  17277. word32 idx = 0;
  17278. int bits = TEST_RSA_BITS;
  17279. const word32 outSz = TEST_RSA_BYTES;
  17280. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17281. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17282. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17283. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17284. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17285. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17286. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17287. ExpectNotNull(in);
  17288. ExpectNotNull(out);
  17289. ExpectNotNull(plain);
  17290. #endif
  17291. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17292. XMEMSET(&key, 0, sizeof(RsaKey));
  17293. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17294. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17295. ExpectIntEQ(wc_InitRng(&rng), 0);
  17296. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17297. /* Sign. */
  17298. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17299. idx = (int)outSz;
  17300. /* Test bad args. */
  17301. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17302. BAD_FUNC_ARG);
  17303. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17304. BAD_FUNC_ARG);
  17305. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17306. BAD_FUNC_ARG);
  17307. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17308. BAD_FUNC_ARG);
  17309. /* Verify. */
  17310. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17311. /* Pass bad args. */
  17312. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17313. BAD_FUNC_ARG);
  17314. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17315. BAD_FUNC_ARG);
  17316. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17317. BAD_FUNC_ARG);
  17318. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17319. BAD_FUNC_ARG);
  17320. WC_FREE_VAR(in, NULL);
  17321. WC_FREE_VAR(out, NULL);
  17322. WC_FREE_VAR(plain, NULL);
  17323. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17324. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17325. #endif
  17326. return EXPECT_RESULT();
  17327. } /* END test_wc_RsaSSL_SignVerify */
  17328. /*
  17329. * Testing wc_RsaEncryptSize()
  17330. */
  17331. static int test_wc_RsaEncryptSize(void)
  17332. {
  17333. EXPECT_DECLS;
  17334. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17335. RsaKey key;
  17336. WC_RNG rng;
  17337. XMEMSET(&key, 0, sizeof(RsaKey));
  17338. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17339. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17340. ExpectIntEQ(wc_InitRng(&rng), 0);
  17341. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17342. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17343. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17344. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17345. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17346. #endif
  17347. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17348. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17349. /* Pass in bad arg. */
  17350. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17351. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17352. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17353. #endif
  17354. return EXPECT_RESULT();
  17355. } /* END test_wc_RsaEncryptSize*/
  17356. /*
  17357. * Testing wc_RsaFlattenPublicKey()
  17358. */
  17359. static int test_wc_RsaFlattenPublicKey(void)
  17360. {
  17361. EXPECT_DECLS;
  17362. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17363. RsaKey key;
  17364. WC_RNG rng;
  17365. byte e[256];
  17366. byte n[256];
  17367. word32 eSz = sizeof(e);
  17368. word32 nSz = sizeof(n);
  17369. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17370. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17371. int bits = 1024;
  17372. #else
  17373. int bits = 2048;
  17374. #endif
  17375. XMEMSET(&key, 0, sizeof(RsaKey));
  17376. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17377. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17378. ExpectIntEQ(wc_InitRng(&rng), 0);
  17379. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17380. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17381. /* Pass bad args. */
  17382. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17383. BAD_FUNC_ARG);
  17384. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17385. BAD_FUNC_ARG);
  17386. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17387. BAD_FUNC_ARG);
  17388. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17389. BAD_FUNC_ARG);
  17390. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17391. BAD_FUNC_ARG);
  17392. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17393. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17394. #endif
  17395. return EXPECT_RESULT();
  17396. } /* END test_wc_RsaFlattenPublicKey */
  17397. /*
  17398. * unit test for wc_AesCcmSetKey
  17399. */
  17400. static int test_wc_AesCcmSetKey(void)
  17401. {
  17402. EXPECT_DECLS;
  17403. #ifdef HAVE_AESCCM
  17404. Aes aes;
  17405. const byte key16[] = {
  17406. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17407. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17408. };
  17409. const byte key24[] = {
  17410. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17411. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17412. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17413. };
  17414. const byte key32[] = {
  17415. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17416. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17417. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17418. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17419. };
  17420. XMEMSET(&aes, 0, sizeof(Aes));
  17421. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17422. #ifdef WOLFSSL_AES_128
  17423. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17424. #endif
  17425. #ifdef WOLFSSL_AES_192
  17426. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17427. #endif
  17428. #ifdef WOLFSSL_AES_256
  17429. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17430. #endif
  17431. /* Test bad args. */
  17432. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17433. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17434. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17435. wc_AesFree(&aes);
  17436. #endif
  17437. return EXPECT_RESULT();
  17438. } /* END test_wc_AesCcmSetKey */
  17439. /*
  17440. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17441. */
  17442. static int test_wc_AesCcmEncryptDecrypt(void)
  17443. {
  17444. EXPECT_DECLS;
  17445. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17446. Aes aes;
  17447. const byte key16[] = {
  17448. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17449. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17450. };
  17451. /* plaintext */
  17452. const byte plainT[] = {
  17453. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17454. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17455. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17456. };
  17457. /* nonce */
  17458. const byte iv[] = {
  17459. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17460. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17461. };
  17462. const byte c[] = { /* cipher text. */
  17463. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17464. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17465. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17466. };
  17467. const byte t[] = { /* Auth tag */
  17468. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17469. };
  17470. const byte authIn[] = {
  17471. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17472. };
  17473. byte cipherOut[sizeof(plainT)];
  17474. byte authTag[sizeof(t)];
  17475. #ifdef HAVE_AES_DECRYPT
  17476. byte plainOut[sizeof(cipherOut)];
  17477. #endif
  17478. XMEMSET(&aes, 0, sizeof(Aes));
  17479. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17480. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17481. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17482. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17483. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17484. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17485. #ifdef HAVE_AES_DECRYPT
  17486. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17487. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17488. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17489. #endif
  17490. /* Pass in bad args. Encrypt*/
  17491. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17492. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17493. BAD_FUNC_ARG);
  17494. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17495. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17496. BAD_FUNC_ARG);
  17497. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17498. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17499. BAD_FUNC_ARG);
  17500. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17501. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17502. BAD_FUNC_ARG);
  17503. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17504. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17505. BAD_FUNC_ARG);
  17506. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17507. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17508. BAD_FUNC_ARG);
  17509. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17510. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17511. BAD_FUNC_ARG);
  17512. #ifdef HAVE_AES_DECRYPT
  17513. /* Pass in bad args. Decrypt*/
  17514. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17515. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17516. BAD_FUNC_ARG);
  17517. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17518. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17519. BAD_FUNC_ARG);
  17520. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17521. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17522. BAD_FUNC_ARG);
  17523. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17524. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17525. BAD_FUNC_ARG);
  17526. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17527. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17528. BAD_FUNC_ARG);
  17529. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17530. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17531. BAD_FUNC_ARG);
  17532. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17533. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17534. BAD_FUNC_ARG);
  17535. #endif
  17536. wc_AesFree(&aes);
  17537. #endif /* HAVE_AESCCM */
  17538. return EXPECT_RESULT();
  17539. } /* END test_wc_AesCcmEncryptDecrypt */
  17540. #if defined(WOLFSSL_AES_EAX) && \
  17541. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17542. /*
  17543. * Testing test_wc_AesEaxVectors()
  17544. */
  17545. static int test_wc_AesEaxVectors(void)
  17546. {
  17547. EXPECT_DECLS;
  17548. typedef struct {
  17549. byte key[AES_256_KEY_SIZE];
  17550. int key_length;
  17551. byte iv[AES_BLOCK_SIZE];
  17552. int iv_length;
  17553. byte aad[AES_BLOCK_SIZE * 2];
  17554. int aad_length;
  17555. byte msg[AES_BLOCK_SIZE * 5];
  17556. int msg_length;
  17557. byte ct[AES_BLOCK_SIZE * 5];
  17558. int ct_length;
  17559. byte tag[AES_BLOCK_SIZE];
  17560. int tag_length;
  17561. int valid;
  17562. } AadVector;
  17563. /* Test vectors obtained from Google wycheproof project
  17564. * https://github.com/google/wycheproof
  17565. * from testvectors/aes_eax_test.json
  17566. */
  17567. const AadVector vectors[] = {
  17568. {
  17569. /* key, key length */
  17570. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  17571. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  17572. /* iv, iv length */
  17573. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  17574. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  17575. /* aad, aad length */
  17576. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  17577. /* msg, msg length */
  17578. {0x00}, 0,
  17579. /* ct, ct length */
  17580. {0x00}, 0,
  17581. /* tag, tag length */
  17582. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  17583. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  17584. /* valid */
  17585. 1,
  17586. },
  17587. {
  17588. /* key, key length */
  17589. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  17590. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  17591. /* iv, iv length */
  17592. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  17593. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  17594. /* aad, aad length */
  17595. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  17596. /* msg, msg length */
  17597. {0xf7, 0xfb}, 2,
  17598. /* ct, ct length */
  17599. {0x19, 0xdd}, 2,
  17600. /* tag, tag length */
  17601. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  17602. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  17603. /* valid */
  17604. 1,
  17605. },
  17606. {
  17607. /* key, key length */
  17608. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  17609. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  17610. /* iv, iv length */
  17611. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  17612. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  17613. /* aad, aad length */
  17614. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  17615. /* msg, msg length */
  17616. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  17617. /* ct, ct length */
  17618. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  17619. /* tag, tag length */
  17620. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  17621. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  17622. /* valid */
  17623. 1,
  17624. },
  17625. {
  17626. /* key, key length */
  17627. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  17628. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  17629. /* iv, iv length */
  17630. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  17631. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  17632. /* aad, aad length */
  17633. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  17634. /* msg, msg length */
  17635. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  17636. /* ct, ct length */
  17637. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  17638. /* tag, tag length */
  17639. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  17640. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  17641. /* valid */
  17642. 1,
  17643. },
  17644. {
  17645. /* key, key length */
  17646. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  17647. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  17648. /* iv, iv length */
  17649. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  17650. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  17651. /* aad, aad length */
  17652. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  17653. /* msg, msg length */
  17654. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  17655. /* ct, ct length */
  17656. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  17657. /* tag, tag length */
  17658. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  17659. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  17660. /* valid */
  17661. 1,
  17662. },
  17663. {
  17664. /* key, key length */
  17665. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  17666. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  17667. /* iv, iv length */
  17668. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  17669. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  17670. /* aad, aad length */
  17671. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  17672. /* msg, msg length */
  17673. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  17674. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  17675. /* ct, ct length */
  17676. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  17677. 0x0e, 0x72, 0x84, 0x14}, 12,
  17678. /* tag, tag length */
  17679. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  17680. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  17681. /* valid */
  17682. 1,
  17683. },
  17684. {
  17685. /* key, key length */
  17686. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  17687. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  17688. /* iv, iv length */
  17689. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  17690. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  17691. /* aad, aad length */
  17692. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  17693. /* msg, msg length */
  17694. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  17695. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  17696. 0x36}, 17,
  17697. /* ct, ct length */
  17698. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  17699. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  17700. 0x30}, 17,
  17701. /* tag, tag length */
  17702. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  17703. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  17704. /* valid */
  17705. 1,
  17706. },
  17707. {
  17708. /* key, key length */
  17709. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  17710. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  17711. /* iv, iv length */
  17712. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  17713. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  17714. /* aad, aad length */
  17715. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  17716. /* msg, msg length */
  17717. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  17718. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  17719. 0x2d, 0x56}, 18,
  17720. /* ct, ct length */
  17721. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  17722. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  17723. 0x8c, 0xc3}, 18,
  17724. /* tag, tag length */
  17725. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  17726. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  17727. /* valid */
  17728. 1,
  17729. },
  17730. {
  17731. /* key, key length */
  17732. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  17733. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  17734. /* iv, iv length */
  17735. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  17736. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  17737. /* aad, aad length */
  17738. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  17739. /* msg, msg length */
  17740. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  17741. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  17742. 0xef, 0x11}, 18,
  17743. /* ct, ct length */
  17744. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  17745. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  17746. 0xb1, 0xe8}, 18,
  17747. /* tag, tag length */
  17748. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  17749. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  17750. /* valid */
  17751. 1,
  17752. },
  17753. {
  17754. /* key, key length */
  17755. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  17756. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  17757. /* iv, iv length */
  17758. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  17759. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  17760. /* aad, aad length */
  17761. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  17762. /* msg, msg length */
  17763. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  17764. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  17765. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  17766. /* ct, ct length */
  17767. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  17768. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  17769. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  17770. /* tag, tag length */
  17771. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  17772. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  17773. /* valid */
  17774. 1,
  17775. },
  17776. {
  17777. /* key, key length */
  17778. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17779. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17780. /* iv, iv length */
  17781. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  17782. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  17783. /* aad, aad length */
  17784. {0x00}, 0,
  17785. /* msg, msg length */
  17786. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17787. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17788. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17789. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17790. /* ct, ct length */
  17791. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  17792. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  17793. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  17794. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  17795. /* tag, tag length */
  17796. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  17797. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  17798. /* valid */
  17799. 1,
  17800. },
  17801. {
  17802. /* key, key length */
  17803. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17804. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17805. /* iv, iv length */
  17806. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  17807. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  17808. /* aad, aad length */
  17809. {0x00}, 0,
  17810. /* msg, msg length */
  17811. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17812. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17813. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17814. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17815. /* ct, ct length */
  17816. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  17817. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  17818. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  17819. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  17820. /* tag, tag length */
  17821. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  17822. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  17823. /* valid */
  17824. 1,
  17825. },
  17826. {
  17827. /* key, key length */
  17828. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17829. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17830. /* iv, iv length */
  17831. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  17832. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  17833. /* aad, aad length */
  17834. {0x00}, 0,
  17835. /* msg, msg length */
  17836. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17837. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17838. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17839. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17840. /* ct, ct length */
  17841. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  17842. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  17843. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  17844. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  17845. /* tag, tag length */
  17846. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  17847. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  17848. /* valid */
  17849. 1,
  17850. },
  17851. {
  17852. /* key, key length */
  17853. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17854. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17855. /* iv, iv length */
  17856. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  17857. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  17858. /* aad, aad length */
  17859. {0x00}, 0,
  17860. /* msg, msg length */
  17861. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17862. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17863. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17864. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17865. /* ct, ct length */
  17866. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  17867. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  17868. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  17869. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  17870. /* tag, tag length */
  17871. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  17872. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  17873. /* valid */
  17874. 1,
  17875. },
  17876. {
  17877. /* key, key length */
  17878. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17879. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17880. /* iv, iv length */
  17881. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  17882. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  17883. /* aad, aad length */
  17884. {0x00}, 0,
  17885. /* msg, msg length */
  17886. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17887. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17888. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17889. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17890. /* ct, ct length */
  17891. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  17892. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  17893. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  17894. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  17895. /* tag, tag length */
  17896. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  17897. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  17898. /* valid */
  17899. 1,
  17900. },
  17901. {
  17902. /* key, key length */
  17903. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17904. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17905. /* iv, iv length */
  17906. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  17907. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  17908. /* aad, aad length */
  17909. {0x00}, 0,
  17910. /* msg, msg length */
  17911. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17912. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17913. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17914. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17915. /* ct, ct length */
  17916. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  17917. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  17918. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  17919. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  17920. /* tag, tag length */
  17921. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  17922. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  17923. /* valid */
  17924. 1,
  17925. },
  17926. {
  17927. /* key, key length */
  17928. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17929. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17930. /* iv, iv length */
  17931. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  17932. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  17933. /* aad, aad length */
  17934. {0x00}, 0,
  17935. /* msg, msg length */
  17936. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17937. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17938. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17939. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17940. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17941. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17942. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17943. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  17944. /* ct, ct length */
  17945. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  17946. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  17947. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  17948. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  17949. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  17950. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  17951. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  17952. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  17953. /* tag, tag length */
  17954. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  17955. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  17956. /* valid */
  17957. 1,
  17958. },
  17959. {
  17960. /* key, key length */
  17961. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17962. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17963. /* iv, iv length */
  17964. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  17965. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  17966. /* aad, aad length */
  17967. {0x00}, 0,
  17968. /* msg, msg length */
  17969. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17970. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17971. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17972. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17973. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17974. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17975. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17976. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17977. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  17978. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  17979. /* ct, ct length */
  17980. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  17981. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  17982. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  17983. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  17984. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  17985. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  17986. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  17987. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  17988. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  17989. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  17990. /* tag, tag length */
  17991. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  17992. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  17993. /* valid */
  17994. 1,
  17995. },
  17996. {
  17997. /* key, key length */
  17998. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  17999. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18000. /* iv, iv length */
  18001. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18002. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18003. /* aad, aad length */
  18004. {0x00}, 0,
  18005. /* msg, msg length */
  18006. {0x00}, 0,
  18007. /* ct, ct length */
  18008. {0x00}, 0,
  18009. /* tag, tag length */
  18010. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18011. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18012. /* valid */
  18013. 1,
  18014. },
  18015. {
  18016. /* key, key length */
  18017. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18018. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18019. /* iv, iv length */
  18020. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18021. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18022. /* aad, aad length */
  18023. {0x00}, 0,
  18024. /* msg, msg length */
  18025. {0x01}, 1,
  18026. /* ct, ct length */
  18027. {0x1d}, 1,
  18028. /* tag, tag length */
  18029. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18030. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18031. /* valid */
  18032. 1,
  18033. },
  18034. {
  18035. /* key, key length */
  18036. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18037. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18038. /* iv, iv length */
  18039. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18040. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18041. /* aad, aad length */
  18042. {0x00}, 0,
  18043. /* msg, msg length */
  18044. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18045. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18046. /* ct, ct length */
  18047. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18048. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18049. /* tag, tag length */
  18050. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18051. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18052. /* valid */
  18053. 1,
  18054. },
  18055. {
  18056. /* key, key length */
  18057. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18058. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18059. /* iv, iv length */
  18060. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18061. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18062. /* aad, aad length */
  18063. {0x00}, 0,
  18064. /* msg, msg length */
  18065. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18066. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18067. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18068. /* ct, ct length */
  18069. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18070. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18071. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18072. /* tag, tag length */
  18073. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18074. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18075. /* valid */
  18076. 1,
  18077. },
  18078. {
  18079. /* key, key length */
  18080. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18081. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18082. /* iv, iv length */
  18083. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18084. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18085. /* aad, aad length */
  18086. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18087. /* msg, msg length */
  18088. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18089. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18090. /* ct, ct length */
  18091. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18092. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18093. /* tag, tag length */
  18094. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18095. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18096. /* valid */
  18097. 1,
  18098. },
  18099. {
  18100. /* key, key length */
  18101. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18102. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18103. /* iv, iv length */
  18104. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18105. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18106. /* aad, aad length */
  18107. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18108. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18109. /* msg, msg length */
  18110. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18111. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18112. /* ct, ct length */
  18113. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18114. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18115. /* tag, tag length */
  18116. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18117. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18118. /* valid */
  18119. 1,
  18120. },
  18121. {
  18122. /* key, key length */
  18123. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18124. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18125. /* iv, iv length */
  18126. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18127. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18128. /* aad, aad length */
  18129. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18130. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18131. /* msg, msg length */
  18132. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18133. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18134. /* ct, ct length */
  18135. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18136. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18137. /* tag, tag length */
  18138. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18139. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18140. /* valid */
  18141. 1,
  18142. },
  18143. {
  18144. /* key, key length */
  18145. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18146. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18147. /* iv, iv length */
  18148. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18149. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18150. /* aad, aad length */
  18151. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18152. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18153. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18154. /* msg, msg length */
  18155. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18156. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18157. /* ct, ct length */
  18158. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18159. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18160. /* tag, tag length */
  18161. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18162. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18163. /* valid */
  18164. 1,
  18165. },
  18166. {
  18167. /* key, key length */
  18168. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18169. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18170. /* iv, iv length */
  18171. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18172. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18173. /* aad, aad length */
  18174. {0x00}, 0,
  18175. /* msg, msg length */
  18176. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18177. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18178. /* ct, ct length */
  18179. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18180. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18181. /* tag, tag length */
  18182. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18183. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18184. /* valid */
  18185. 0,
  18186. },
  18187. {
  18188. /* key, key length */
  18189. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18190. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18191. /* iv, iv length */
  18192. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18193. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18194. /* aad, aad length */
  18195. {0x00}, 0,
  18196. /* msg, msg length */
  18197. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18198. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18199. /* ct, ct length */
  18200. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18201. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18202. /* tag, tag length */
  18203. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18204. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18205. /* valid */
  18206. 0,
  18207. },
  18208. {
  18209. /* key, key length */
  18210. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18211. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18212. /* iv, iv length */
  18213. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18214. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18215. /* aad, aad length */
  18216. {0x00}, 0,
  18217. /* msg, msg length */
  18218. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18219. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18220. /* ct, ct length */
  18221. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18222. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18223. /* tag, tag length */
  18224. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18225. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18226. /* valid */
  18227. 0,
  18228. },
  18229. {
  18230. /* key, key length */
  18231. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18232. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18233. /* iv, iv length */
  18234. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18235. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18236. /* aad, aad length */
  18237. {0x00}, 0,
  18238. /* msg, msg length */
  18239. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18240. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18241. /* ct, ct length */
  18242. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18243. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18244. /* tag, tag length */
  18245. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18246. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18247. /* valid */
  18248. 0,
  18249. },
  18250. {
  18251. /* key, key length */
  18252. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18253. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18254. /* iv, iv length */
  18255. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18256. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18257. /* aad, aad length */
  18258. {0x00}, 0,
  18259. /* msg, msg length */
  18260. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18261. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18262. /* ct, ct length */
  18263. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18264. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18265. /* tag, tag length */
  18266. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18267. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18268. /* valid */
  18269. 0,
  18270. },
  18271. {
  18272. /* key, key length */
  18273. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18274. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18275. /* iv, iv length */
  18276. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18277. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18278. /* aad, aad length */
  18279. {0x00}, 0,
  18280. /* msg, msg length */
  18281. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18282. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18283. /* ct, ct length */
  18284. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18285. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18286. /* tag, tag length */
  18287. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18288. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18289. /* valid */
  18290. 0,
  18291. },
  18292. {
  18293. /* key, key length */
  18294. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18295. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18296. /* iv, iv length */
  18297. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18298. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18299. /* aad, aad length */
  18300. {0x00}, 0,
  18301. /* msg, msg length */
  18302. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18303. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18304. /* ct, ct length */
  18305. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18306. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18307. /* tag, tag length */
  18308. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18309. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18310. /* valid */
  18311. 0,
  18312. },
  18313. {
  18314. /* key, key length */
  18315. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18316. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18317. /* iv, iv length */
  18318. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18319. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18320. /* aad, aad length */
  18321. {0x00}, 0,
  18322. /* msg, msg length */
  18323. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18324. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18325. /* ct, ct length */
  18326. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18327. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18328. /* tag, tag length */
  18329. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18330. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18331. /* valid */
  18332. 0,
  18333. },
  18334. {
  18335. /* key, key length */
  18336. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18337. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18338. /* iv, iv length */
  18339. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18340. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18341. /* aad, aad length */
  18342. {0x00}, 0,
  18343. /* msg, msg length */
  18344. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18345. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18346. /* ct, ct length */
  18347. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18348. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18349. /* tag, tag length */
  18350. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18351. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18352. /* valid */
  18353. 0,
  18354. },
  18355. {
  18356. /* key, key length */
  18357. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18358. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18359. /* iv, iv length */
  18360. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18361. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18362. /* aad, aad length */
  18363. {0x00}, 0,
  18364. /* msg, msg length */
  18365. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18366. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18367. /* ct, ct length */
  18368. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18369. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18370. /* tag, tag length */
  18371. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18372. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18373. /* valid */
  18374. 0,
  18375. },
  18376. {
  18377. /* key, key length */
  18378. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18379. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18380. /* iv, iv length */
  18381. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18382. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18383. /* aad, aad length */
  18384. {0x00}, 0,
  18385. /* msg, msg length */
  18386. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18387. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18388. /* ct, ct length */
  18389. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18390. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18391. /* tag, tag length */
  18392. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18393. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18394. /* valid */
  18395. 0,
  18396. },
  18397. {
  18398. /* key, key length */
  18399. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18400. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18401. /* iv, iv length */
  18402. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18403. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18404. /* aad, aad length */
  18405. {0x00}, 0,
  18406. /* msg, msg length */
  18407. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18408. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18409. /* ct, ct length */
  18410. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18411. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18412. /* tag, tag length */
  18413. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18414. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18415. /* valid */
  18416. 0,
  18417. },
  18418. {
  18419. /* key, key length */
  18420. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18421. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18422. /* iv, iv length */
  18423. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18424. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18425. /* aad, aad length */
  18426. {0x00}, 0,
  18427. /* msg, msg length */
  18428. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18429. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18430. /* ct, ct length */
  18431. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18432. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18433. /* tag, tag length */
  18434. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18435. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18436. /* valid */
  18437. 0,
  18438. },
  18439. {
  18440. /* key, key length */
  18441. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18442. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18443. /* iv, iv length */
  18444. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18445. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18446. /* aad, aad length */
  18447. {0x00}, 0,
  18448. /* msg, msg length */
  18449. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18450. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18451. /* ct, ct length */
  18452. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18453. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18454. /* tag, tag length */
  18455. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18456. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18457. /* valid */
  18458. 0,
  18459. },
  18460. {
  18461. /* key, key length */
  18462. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18463. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18464. /* iv, iv length */
  18465. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18466. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18467. /* aad, aad length */
  18468. {0x00}, 0,
  18469. /* msg, msg length */
  18470. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18471. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18472. /* ct, ct length */
  18473. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18474. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18475. /* tag, tag length */
  18476. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18477. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18478. /* valid */
  18479. 0,
  18480. },
  18481. {
  18482. /* key, key length */
  18483. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18484. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18485. /* iv, iv length */
  18486. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18487. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18488. /* aad, aad length */
  18489. {0x00}, 0,
  18490. /* msg, msg length */
  18491. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18492. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18493. /* ct, ct length */
  18494. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18495. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18496. /* tag, tag length */
  18497. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18498. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18499. /* valid */
  18500. 0,
  18501. },
  18502. {
  18503. /* key, key length */
  18504. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18505. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18506. /* iv, iv length */
  18507. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18508. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18509. /* aad, aad length */
  18510. {0x00}, 0,
  18511. /* msg, msg length */
  18512. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18513. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18514. /* ct, ct length */
  18515. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18516. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18517. /* tag, tag length */
  18518. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18519. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18520. /* valid */
  18521. 0,
  18522. },
  18523. {
  18524. /* key, key length */
  18525. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18526. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18527. /* iv, iv length */
  18528. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18529. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18530. /* aad, aad length */
  18531. {0x00}, 0,
  18532. /* msg, msg length */
  18533. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18534. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18535. /* ct, ct length */
  18536. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18537. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18538. /* tag, tag length */
  18539. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18540. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18541. /* valid */
  18542. 0,
  18543. },
  18544. {
  18545. /* key, key length */
  18546. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18547. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18548. /* iv, iv length */
  18549. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18550. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18551. /* aad, aad length */
  18552. {0x00}, 0,
  18553. /* msg, msg length */
  18554. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18555. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18556. /* ct, ct length */
  18557. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18558. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18559. /* tag, tag length */
  18560. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18561. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18562. /* valid */
  18563. 0,
  18564. },
  18565. {
  18566. /* key, key length */
  18567. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18568. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18569. /* iv, iv length */
  18570. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18571. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18572. /* aad, aad length */
  18573. {0x00}, 0,
  18574. /* msg, msg length */
  18575. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18576. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18577. /* ct, ct length */
  18578. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18579. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18580. /* tag, tag length */
  18581. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18582. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18583. /* valid */
  18584. 0,
  18585. },
  18586. {
  18587. /* key, key length */
  18588. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18589. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18590. /* iv, iv length */
  18591. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18592. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18593. /* aad, aad length */
  18594. {0x00}, 0,
  18595. /* msg, msg length */
  18596. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18597. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18598. /* ct, ct length */
  18599. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18600. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18601. /* tag, tag length */
  18602. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  18603. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18604. /* valid */
  18605. 0,
  18606. },
  18607. {
  18608. /* key, key length */
  18609. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18610. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18611. /* iv, iv length */
  18612. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18613. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18614. /* aad, aad length */
  18615. {0x00}, 0,
  18616. /* msg, msg length */
  18617. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18618. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18619. /* ct, ct length */
  18620. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18621. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18622. /* tag, tag length */
  18623. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18624. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18625. /* valid */
  18626. 0,
  18627. },
  18628. {
  18629. /* key, key length */
  18630. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18631. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18632. /* iv, iv length */
  18633. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18634. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18635. /* aad, aad length */
  18636. {0x00}, 0,
  18637. /* msg, msg length */
  18638. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18639. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18640. /* ct, ct length */
  18641. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18642. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18643. /* tag, tag length */
  18644. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  18645. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  18646. /* valid */
  18647. 0,
  18648. },
  18649. {
  18650. /* key, key length */
  18651. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18652. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18653. /* iv, iv length */
  18654. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18655. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18656. /* aad, aad length */
  18657. {0x00}, 0,
  18658. /* msg, msg length */
  18659. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18660. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18661. /* ct, ct length */
  18662. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18663. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18664. /* tag, tag length */
  18665. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18666. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  18667. /* valid */
  18668. 0,
  18669. },
  18670. {
  18671. /* key, key length */
  18672. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18673. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18674. /* iv, iv length */
  18675. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18676. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18677. /* aad, aad length */
  18678. {0x00}, 0,
  18679. /* msg, msg length */
  18680. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18681. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18682. /* ct, ct length */
  18683. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18684. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18685. /* tag, tag length */
  18686. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  18687. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  18688. /* valid */
  18689. 0,
  18690. },
  18691. {
  18692. /* key, key length */
  18693. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18694. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18695. /* iv, iv length */
  18696. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18697. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18698. /* aad, aad length */
  18699. {0x00}, 0,
  18700. /* msg, msg length */
  18701. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18702. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18703. /* ct, ct length */
  18704. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18705. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18706. /* tag, tag length */
  18707. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  18708. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  18709. /* valid */
  18710. 0,
  18711. },
  18712. {
  18713. /* key, key length */
  18714. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18715. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18716. /* iv, iv length */
  18717. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18718. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18719. /* aad, aad length */
  18720. {0x00}, 0,
  18721. /* msg, msg length */
  18722. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18723. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18724. /* ct, ct length */
  18725. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18726. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18727. /* tag, tag length */
  18728. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  18729. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  18730. /* valid */
  18731. 0,
  18732. },
  18733. };
  18734. byte ciphertext[sizeof(vectors[0].ct)];
  18735. byte authtag[sizeof(vectors[0].tag)];
  18736. int i;
  18737. int len;
  18738. int ret;
  18739. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  18740. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18741. len = sizeof(authtag);
  18742. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  18743. ciphertext,
  18744. vectors[i].msg, vectors[i].msg_length,
  18745. vectors[i].iv, vectors[i].iv_length,
  18746. authtag, len,
  18747. vectors[i].aad, vectors[i].aad_length),
  18748. 0);
  18749. /* check ciphertext matches vector */
  18750. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  18751. 0);
  18752. /* check that computed tag matches vector only for vectors marked as valid */
  18753. ret = XMEMCMP(authtag, vectors[i].tag, len);
  18754. if (vectors[i].valid) {
  18755. ExpectIntEQ(ret, 0);
  18756. }
  18757. else {
  18758. ExpectIntNE(ret, 0);
  18759. }
  18760. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18761. /* Decrypt, checking that the computed auth tags match */
  18762. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  18763. ciphertext,
  18764. vectors[i].ct, vectors[i].ct_length,
  18765. vectors[i].iv, vectors[i].iv_length,
  18766. authtag, len,
  18767. vectors[i].aad, vectors[i].aad_length),
  18768. 0);
  18769. /* check decrypted ciphertext matches vector plaintext */
  18770. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  18771. 0);
  18772. }
  18773. return EXPECT_RESULT();
  18774. } /* END test_wc_AesEaxVectors */
  18775. /*
  18776. * Testing test_wc_AesEaxEncryptAuth()
  18777. */
  18778. static int test_wc_AesEaxEncryptAuth(void)
  18779. {
  18780. EXPECT_DECLS;
  18781. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18782. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18783. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18784. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18785. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18786. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18787. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18788. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18789. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18790. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18791. byte ciphertext[sizeof(msg)];
  18792. byte authtag[AES_BLOCK_SIZE];
  18793. int i;
  18794. int len;
  18795. len = sizeof(authtag);
  18796. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18797. ciphertext,
  18798. msg, sizeof(msg),
  18799. iv, sizeof(iv),
  18800. authtag, len,
  18801. aad, sizeof(aad)),
  18802. 0);
  18803. /* Test null checking */
  18804. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  18805. ciphertext,
  18806. msg, sizeof(msg),
  18807. iv, sizeof(iv),
  18808. authtag, len,
  18809. aad, sizeof(aad)),
  18810. BAD_FUNC_ARG);
  18811. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18812. NULL,
  18813. msg, sizeof(msg),
  18814. iv, sizeof(iv),
  18815. authtag, len,
  18816. aad, sizeof(aad)),
  18817. BAD_FUNC_ARG);
  18818. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18819. ciphertext,
  18820. NULL, sizeof(msg),
  18821. iv, sizeof(iv),
  18822. authtag, len,
  18823. aad, sizeof(aad)),
  18824. BAD_FUNC_ARG);
  18825. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18826. ciphertext,
  18827. msg, sizeof(msg),
  18828. NULL, sizeof(iv),
  18829. authtag, len,
  18830. aad, sizeof(aad)),
  18831. BAD_FUNC_ARG);
  18832. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18833. ciphertext,
  18834. msg, sizeof(msg),
  18835. iv, sizeof(iv),
  18836. NULL, len,
  18837. aad, sizeof(aad)),
  18838. BAD_FUNC_ARG);
  18839. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18840. ciphertext,
  18841. msg, sizeof(msg),
  18842. iv, sizeof(iv),
  18843. authtag, len,
  18844. NULL, sizeof(aad)),
  18845. BAD_FUNC_ARG);
  18846. /* Test bad key lengths */
  18847. for (i = 0; i <= 32; i++) {
  18848. int exp_ret;
  18849. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18850. || i == AES_256_KEY_SIZE) {
  18851. exp_ret = 0;
  18852. }
  18853. else {
  18854. exp_ret = BAD_FUNC_ARG;
  18855. }
  18856. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  18857. ciphertext,
  18858. msg, sizeof(msg),
  18859. iv, sizeof(iv),
  18860. authtag, len,
  18861. aad, sizeof(aad)),
  18862. exp_ret);
  18863. }
  18864. /* Test auth tag size out of range */
  18865. len = AES_BLOCK_SIZE + 1;
  18866. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18867. ciphertext,
  18868. msg, sizeof(msg),
  18869. iv, sizeof(iv),
  18870. authtag, len,
  18871. aad, sizeof(aad)),
  18872. BAD_FUNC_ARG);
  18873. return EXPECT_RESULT();
  18874. } /* END test_wc_AesEaxEncryptAuth() */
  18875. /*
  18876. * Testing test_wc_AesEaxDecryptAuth()
  18877. */
  18878. static int test_wc_AesEaxDecryptAuth(void)
  18879. {
  18880. EXPECT_DECLS;
  18881. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18882. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18883. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18884. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18885. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18886. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18887. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18888. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18889. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18890. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18891. /* Garbage tag that should always fail for above aad */
  18892. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  18893. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  18894. byte plaintext[sizeof(ct)];
  18895. int i;
  18896. int len;
  18897. len = sizeof(tag);
  18898. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18899. plaintext,
  18900. ct, sizeof(ct),
  18901. iv, sizeof(iv),
  18902. tag, len,
  18903. aad, sizeof(aad)),
  18904. AES_EAX_AUTH_E);
  18905. /* Test null checking */
  18906. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  18907. plaintext,
  18908. ct, sizeof(ct),
  18909. iv, sizeof(iv),
  18910. tag, len,
  18911. aad, sizeof(aad)),
  18912. BAD_FUNC_ARG);
  18913. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18914. NULL,
  18915. ct, sizeof(ct),
  18916. iv, sizeof(iv),
  18917. tag, len,
  18918. aad, sizeof(aad)),
  18919. BAD_FUNC_ARG);
  18920. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18921. plaintext,
  18922. NULL, sizeof(ct),
  18923. iv, sizeof(iv),
  18924. tag, len,
  18925. aad, sizeof(aad)),
  18926. BAD_FUNC_ARG);
  18927. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18928. plaintext,
  18929. ct, sizeof(ct),
  18930. NULL, sizeof(iv),
  18931. tag, len,
  18932. aad, sizeof(aad)),
  18933. BAD_FUNC_ARG);
  18934. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18935. plaintext,
  18936. ct, sizeof(ct),
  18937. iv, sizeof(iv),
  18938. NULL, len,
  18939. aad, sizeof(aad)),
  18940. BAD_FUNC_ARG);
  18941. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18942. plaintext,
  18943. ct, sizeof(ct),
  18944. iv, sizeof(iv),
  18945. tag, len,
  18946. NULL, sizeof(aad)),
  18947. BAD_FUNC_ARG);
  18948. /* Test bad key lengths */
  18949. for (i = 0; i <= 32; i++) {
  18950. int exp_ret;
  18951. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18952. || i == AES_256_KEY_SIZE) {
  18953. exp_ret = AES_EAX_AUTH_E;
  18954. }
  18955. else {
  18956. exp_ret = BAD_FUNC_ARG;
  18957. }
  18958. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  18959. plaintext,
  18960. ct, sizeof(ct),
  18961. iv, sizeof(iv),
  18962. tag, len,
  18963. aad, sizeof(aad)),
  18964. exp_ret);
  18965. }
  18966. /* Test auth tag size out of range */
  18967. len = AES_BLOCK_SIZE + 1;
  18968. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18969. plaintext,
  18970. ct, sizeof(ct),
  18971. iv, sizeof(iv),
  18972. tag, len,
  18973. aad, sizeof(aad)),
  18974. BAD_FUNC_ARG);
  18975. return EXPECT_RESULT();
  18976. } /* END test_wc_AesEaxDecryptAuth() */
  18977. #endif /* WOLFSSL_AES_EAX &&
  18978. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  18979. */
  18980. /*
  18981. * Testing wc_InitDsaKey()
  18982. */
  18983. static int test_wc_InitDsaKey(void)
  18984. {
  18985. EXPECT_DECLS;
  18986. #ifndef NO_DSA
  18987. DsaKey key;
  18988. XMEMSET(&key, 0, sizeof(DsaKey));
  18989. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18990. /* Pass in bad args. */
  18991. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  18992. wc_FreeDsaKey(&key);
  18993. #endif
  18994. return EXPECT_RESULT();
  18995. } /* END test_wc_InitDsaKey */
  18996. /*
  18997. * Testing wc_DsaSign() and wc_DsaVerify()
  18998. */
  18999. static int test_wc_DsaSignVerify(void)
  19000. {
  19001. EXPECT_DECLS;
  19002. #if !defined(NO_DSA)
  19003. DsaKey key;
  19004. WC_RNG rng;
  19005. wc_Sha sha;
  19006. byte signature[DSA_SIG_SIZE];
  19007. byte hash[WC_SHA_DIGEST_SIZE];
  19008. word32 idx = 0;
  19009. word32 bytes;
  19010. int answer;
  19011. #ifdef USE_CERT_BUFFERS_1024
  19012. byte tmp[ONEK_BUF];
  19013. XMEMSET(tmp, 0, sizeof(tmp));
  19014. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19015. bytes = sizeof_dsa_key_der_1024;
  19016. #elif defined(USE_CERT_BUFFERS_2048)
  19017. byte tmp[TWOK_BUF];
  19018. XMEMSET(tmp, 0, sizeof(tmp));
  19019. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19020. bytes = sizeof_dsa_key_der_2048;
  19021. #else
  19022. byte tmp[TWOK_BUF];
  19023. XFILE fp = XBADFILE;
  19024. XMEMSET(tmp, 0, sizeof(tmp));
  19025. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19026. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19027. if (fp != XBADFILE)
  19028. XFCLOSE(fp);
  19029. #endif /* END USE_CERT_BUFFERS_1024 */
  19030. ExpectIntEQ(wc_InitSha(&sha), 0);
  19031. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19032. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19033. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19034. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19035. ExpectIntEQ(wc_InitRng(&rng), 0);
  19036. /* Sign. */
  19037. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19038. /* Test bad args. */
  19039. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19040. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19041. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19042. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19043. /* Verify. */
  19044. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19045. ExpectIntEQ(answer, 1);
  19046. /* Pass in bad args. */
  19047. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19048. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19049. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19050. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19051. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19052. /* hard set q to 0 and test fail case */
  19053. mp_free(&key.q);
  19054. mp_init(&key.q);
  19055. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19056. mp_set(&key.q, 1);
  19057. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19058. #endif
  19059. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19060. wc_FreeDsaKey(&key);
  19061. wc_ShaFree(&sha);
  19062. #endif
  19063. return EXPECT_RESULT();
  19064. } /* END test_wc_DsaSign */
  19065. /*
  19066. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19067. */
  19068. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19069. {
  19070. EXPECT_DECLS;
  19071. #if !defined(NO_DSA)
  19072. DsaKey key;
  19073. word32 bytes = 0;
  19074. word32 idx = 0;
  19075. int ret = 0;
  19076. #ifdef USE_CERT_BUFFERS_1024
  19077. byte tmp[ONEK_BUF];
  19078. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19079. bytes = sizeof_dsa_key_der_1024;
  19080. #elif defined(USE_CERT_BUFFERS_2048)
  19081. byte tmp[TWOK_BUF];
  19082. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19083. bytes = sizeof_dsa_key_der_2048;
  19084. #else
  19085. byte tmp[TWOK_BUF];
  19086. XFILE fp = XBADFILE;
  19087. XMEMSET(tmp, 0, sizeof(tmp));
  19088. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19089. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19090. if (fp != XBADFILE)
  19091. XFCLOSE(fp);
  19092. #endif /* END USE_CERT_BUFFERS_1024 */
  19093. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19094. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19095. /* Test bad args. */
  19096. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19097. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19098. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19099. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19100. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19101. wc_FreeDsaKey(&key);
  19102. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19103. idx = 0; /* Reset */
  19104. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19105. /* Test bad args. */
  19106. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19107. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19108. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19109. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19110. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19111. wc_FreeDsaKey(&key);
  19112. #endif /* !NO_DSA */
  19113. return EXPECT_RESULT();
  19114. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19115. /*
  19116. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19117. */
  19118. static int test_wc_MakeDsaKey(void)
  19119. {
  19120. EXPECT_DECLS;
  19121. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19122. DsaKey genKey;
  19123. WC_RNG rng;
  19124. XMEMSET(&genKey, 0, sizeof(genKey));
  19125. XMEMSET(&rng, 0, sizeof(rng));
  19126. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19127. ExpectIntEQ(wc_InitRng(&rng), 0);
  19128. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19129. /* Test bad args. */
  19130. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19131. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19132. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19133. BAD_FUNC_ARG);
  19134. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19135. /* Test bad args. */
  19136. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19137. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19138. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19139. wc_FreeDsaKey(&genKey);
  19140. #endif
  19141. return EXPECT_RESULT();
  19142. } /* END test_wc_MakeDsaKey */
  19143. /*
  19144. * Testing wc_DsaKeyToDer()
  19145. */
  19146. static int test_wc_DsaKeyToDer(void)
  19147. {
  19148. EXPECT_DECLS;
  19149. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19150. DsaKey key;
  19151. word32 bytes;
  19152. word32 idx = 0;
  19153. #ifdef USE_CERT_BUFFERS_1024
  19154. byte tmp[ONEK_BUF];
  19155. byte der[ONEK_BUF];
  19156. XMEMSET(tmp, 0, sizeof(tmp));
  19157. XMEMSET(der, 0, sizeof(der));
  19158. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19159. bytes = sizeof_dsa_key_der_1024;
  19160. #elif defined(USE_CERT_BUFFERS_2048)
  19161. byte tmp[TWOK_BUF];
  19162. byte der[TWOK_BUF];
  19163. XMEMSET(tmp, 0, sizeof(tmp));
  19164. XMEMSET(der, 0, sizeof(der));
  19165. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19166. bytes = sizeof_dsa_key_der_2048;
  19167. #else
  19168. byte tmp[TWOK_BUF];
  19169. byte der[TWOK_BUF];
  19170. XFILE fp = XBADFILE;
  19171. XMEMSET(tmp, 0, sizeof(tmp));
  19172. XMEMSET(der, 0, sizeof(der));
  19173. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19174. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19175. if (fp != XBADFILE)
  19176. XFCLOSE(fp);
  19177. #endif /* END USE_CERT_BUFFERS_1024 */
  19178. XMEMSET(&key, 0, sizeof(DsaKey));
  19179. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19180. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19181. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19182. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19183. /* Test bad args. */
  19184. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19185. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19186. wc_FreeDsaKey(&key);
  19187. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19188. return EXPECT_RESULT();
  19189. } /* END test_wc_DsaKeyToDer */
  19190. /*
  19191. * Testing wc_DsaKeyToPublicDer()
  19192. * (indirectly testing setDsaPublicKey())
  19193. */
  19194. static int test_wc_DsaKeyToPublicDer(void)
  19195. {
  19196. EXPECT_DECLS;
  19197. #ifndef HAVE_SELFTEST
  19198. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19199. DsaKey key;
  19200. WC_RNG rng;
  19201. byte* der = NULL;
  19202. word32 sz = 0;
  19203. word32 idx = 0;
  19204. XMEMSET(&key, 0, sizeof(DsaKey));
  19205. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19206. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19207. DYNAMIC_TYPE_TMP_BUFFER));
  19208. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19209. ExpectIntEQ(wc_InitRng(&rng), 0);
  19210. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19211. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19212. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19213. wc_FreeDsaKey(&key);
  19214. idx = 0;
  19215. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19216. /* Test without the SubjectPublicKeyInfo header */
  19217. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19218. wc_FreeDsaKey(&key);
  19219. idx = 0;
  19220. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19221. /* Test bad args. */
  19222. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19223. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19224. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19225. wc_FreeDsaKey(&key);
  19226. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19227. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19228. #endif /* !HAVE_SELFTEST */
  19229. return EXPECT_RESULT();
  19230. } /* END test_wc_DsaKeyToPublicDer */
  19231. /*
  19232. * Testing wc_DsaImportParamsRaw()
  19233. */
  19234. static int test_wc_DsaImportParamsRaw(void)
  19235. {
  19236. EXPECT_DECLS;
  19237. #if !defined(NO_DSA)
  19238. DsaKey key;
  19239. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19240. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19241. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19242. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19243. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19244. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19245. "80a0093113a8bd73";
  19246. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19247. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19248. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19249. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19250. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19251. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19252. "76341a7e7d9";
  19253. /* invalid p and q parameters */
  19254. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19255. const char* invalidQ = "96c5390a";
  19256. XMEMSET(&key, 0, sizeof(DsaKey));
  19257. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19258. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19259. /* test bad args */
  19260. /* null key struct */
  19261. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19262. /* null param pointers */
  19263. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19264. /* illegal p length */
  19265. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19266. /* illegal q length */
  19267. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19268. wc_FreeDsaKey(&key);
  19269. #endif
  19270. return EXPECT_RESULT();
  19271. } /* END test_wc_DsaImportParamsRaw */
  19272. /*
  19273. * Testing wc_DsaImportParamsRawCheck()
  19274. */
  19275. static int test_wc_DsaImportParamsRawCheck(void)
  19276. {
  19277. EXPECT_DECLS;
  19278. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19279. DsaKey key;
  19280. int trusted = 0;
  19281. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19282. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19283. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19284. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19285. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19286. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19287. "80a0093113a8bd73";
  19288. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19289. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19290. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19291. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19292. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19293. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19294. "76341a7e7d9";
  19295. /* invalid p and q parameters */
  19296. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19297. const char* invalidQ = "96c5390a";
  19298. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19299. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19300. /* test bad args */
  19301. /* null key struct */
  19302. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19303. BAD_FUNC_ARG);
  19304. /* null param pointers */
  19305. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19306. NULL), BAD_FUNC_ARG);
  19307. /* illegal p length */
  19308. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19309. BAD_FUNC_ARG);
  19310. /* illegal q length */
  19311. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19312. BAD_FUNC_ARG);
  19313. wc_FreeDsaKey(&key);
  19314. #endif
  19315. return EXPECT_RESULT();
  19316. } /* END test_wc_DsaImportParamsRawCheck */
  19317. /*
  19318. * Testing wc_DsaExportParamsRaw()
  19319. */
  19320. static int test_wc_DsaExportParamsRaw(void)
  19321. {
  19322. EXPECT_DECLS;
  19323. #if !defined(NO_DSA)
  19324. DsaKey key;
  19325. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19326. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19327. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19328. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19329. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19330. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19331. "80a0093113a8bd73";
  19332. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19333. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19334. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19335. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19336. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19337. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19338. "76341a7e7d9";
  19339. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19340. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19341. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19342. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19343. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19344. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19345. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19346. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19347. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19348. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19349. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19350. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19351. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19352. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19353. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19354. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19355. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19356. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19357. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19358. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19359. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19360. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19361. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19362. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19363. byte pOut[MAX_DSA_PARAM_SIZE];
  19364. byte qOut[MAX_DSA_PARAM_SIZE];
  19365. byte gOut[MAX_DSA_PARAM_SIZE];
  19366. word32 pOutSz;
  19367. word32 qOutSz;
  19368. word32 gOutSz;
  19369. XMEMSET(&key, 0, sizeof(DsaKey));
  19370. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19371. /* first test using imported raw parameters, for expected */
  19372. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19373. pOutSz = sizeof(pOut);
  19374. qOutSz = sizeof(qOut);
  19375. gOutSz = sizeof(gOut);
  19376. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19377. &gOutSz), 0);
  19378. /* validate exported parameters are correct */
  19379. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19380. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19381. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19382. /* test bad args */
  19383. /* null key struct */
  19384. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19385. &gOutSz), BAD_FUNC_ARG);
  19386. /* null output pointers */
  19387. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19388. &gOutSz), LENGTH_ONLY_E);
  19389. /* null output size pointers */
  19390. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19391. NULL), BAD_FUNC_ARG);
  19392. /* p output buffer size too small */
  19393. pOutSz = 1;
  19394. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19395. &gOutSz), BUFFER_E);
  19396. pOutSz = sizeof(pOut);
  19397. /* q output buffer size too small */
  19398. qOutSz = 1;
  19399. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19400. &gOutSz), BUFFER_E);
  19401. qOutSz = sizeof(qOut);
  19402. /* g output buffer size too small */
  19403. gOutSz = 1;
  19404. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19405. &gOutSz), BUFFER_E);
  19406. wc_FreeDsaKey(&key);
  19407. #endif
  19408. return EXPECT_RESULT();
  19409. } /* END test_wc_DsaExportParamsRaw */
  19410. /*
  19411. * Testing wc_DsaExportKeyRaw()
  19412. */
  19413. static int test_wc_DsaExportKeyRaw(void)
  19414. {
  19415. EXPECT_DECLS;
  19416. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19417. DsaKey key;
  19418. WC_RNG rng;
  19419. byte xOut[MAX_DSA_PARAM_SIZE];
  19420. byte yOut[MAX_DSA_PARAM_SIZE];
  19421. word32 xOutSz, yOutSz;
  19422. XMEMSET(&key, 0, sizeof(key));
  19423. XMEMSET(&rng, 0, sizeof(rng));
  19424. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19425. ExpectIntEQ(wc_InitRng(&rng), 0);
  19426. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19427. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19428. /* try successful export */
  19429. xOutSz = sizeof(xOut);
  19430. yOutSz = sizeof(yOut);
  19431. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19432. /* test bad args */
  19433. /* null key struct */
  19434. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19435. BAD_FUNC_ARG);
  19436. /* null output pointers */
  19437. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19438. LENGTH_ONLY_E);
  19439. /* null output size pointers */
  19440. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19441. BAD_FUNC_ARG);
  19442. /* x output buffer size too small */
  19443. xOutSz = 1;
  19444. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19445. BUFFER_E);
  19446. xOutSz = sizeof(xOut);
  19447. /* y output buffer size too small */
  19448. yOutSz = 1;
  19449. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19450. BUFFER_E);
  19451. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19452. wc_FreeDsaKey(&key);
  19453. #endif
  19454. return EXPECT_RESULT();
  19455. } /* END test_wc_DsaExportParamsRaw */
  19456. /*
  19457. * Testing wc_ed25519_make_key().
  19458. */
  19459. static int test_wc_ed25519_make_key(void)
  19460. {
  19461. EXPECT_DECLS;
  19462. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19463. ed25519_key key;
  19464. WC_RNG rng;
  19465. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19466. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19467. XMEMSET(&key, 0, sizeof(ed25519_key));
  19468. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19469. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19470. ExpectIntEQ(wc_InitRng(&rng), 0);
  19471. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19472. ECC_PRIV_KEY_E);
  19473. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19474. ECC_PRIV_KEY_E);
  19475. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19476. /* Test bad args. */
  19477. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19478. BAD_FUNC_ARG);
  19479. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19480. BAD_FUNC_ARG);
  19481. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19482. BAD_FUNC_ARG);
  19483. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19484. BAD_FUNC_ARG);
  19485. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19486. wc_ed25519_free(&key);
  19487. #endif
  19488. return EXPECT_RESULT();
  19489. } /* END test_wc_ed25519_make_key */
  19490. /*
  19491. * Testing wc_ed25519_init()
  19492. */
  19493. static int test_wc_ed25519_init(void)
  19494. {
  19495. EXPECT_DECLS;
  19496. #if defined(HAVE_ED25519)
  19497. ed25519_key key;
  19498. XMEMSET(&key, 0, sizeof(ed25519_key));
  19499. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19500. /* Test bad args. */
  19501. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19502. wc_ed25519_free(&key);
  19503. #endif
  19504. return EXPECT_RESULT();
  19505. } /* END test_wc_ed25519_init */
  19506. /*
  19507. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19508. */
  19509. static int test_wc_ed25519_sign_msg(void)
  19510. {
  19511. EXPECT_DECLS;
  19512. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19513. WC_RNG rng;
  19514. ed25519_key key;
  19515. byte msg[] = "Everybody gets Friday off.\n";
  19516. byte sig[ED25519_SIG_SIZE+1];
  19517. word32 msglen = sizeof(msg);
  19518. word32 siglen = ED25519_SIG_SIZE;
  19519. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19520. #ifdef HAVE_ED25519_VERIFY
  19521. int verify_ok = 0; /*1 = Verify success.*/
  19522. #endif
  19523. /* Initialize stack variables. */
  19524. XMEMSET(&key, 0, sizeof(ed25519_key));
  19525. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19526. XMEMSET(sig, 0, sizeof(sig));
  19527. /* Initialize key. */
  19528. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19529. ExpectIntEQ(wc_InitRng(&rng), 0);
  19530. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19531. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19532. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19533. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19534. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19535. /* Test bad args. */
  19536. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19537. BAD_FUNC_ARG);
  19538. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19539. BAD_FUNC_ARG);
  19540. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19541. BAD_FUNC_ARG);
  19542. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19543. BAD_FUNC_ARG);
  19544. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19545. BUFFER_E);
  19546. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19547. badSigLen -= 1;
  19548. #ifdef HAVE_ED25519_VERIFY
  19549. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19550. &key), 0);
  19551. ExpectIntEQ(verify_ok, 1);
  19552. /* Test bad args. */
  19553. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19554. &verify_ok, &key), BAD_FUNC_ARG);
  19555. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19556. &verify_ok, &key), BAD_FUNC_ARG);
  19557. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19558. &key), BAD_FUNC_ARG);
  19559. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19560. &key), BAD_FUNC_ARG);
  19561. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19562. BAD_FUNC_ARG);
  19563. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19564. NULL), BAD_FUNC_ARG);
  19565. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19566. &key), BAD_FUNC_ARG);
  19567. #endif /* Verify. */
  19568. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19569. wc_ed25519_free(&key);
  19570. #endif
  19571. return EXPECT_RESULT();
  19572. } /* END test_wc_ed25519_sign_msg */
  19573. /*
  19574. * Testing wc_ed25519_import_public()
  19575. */
  19576. static int test_wc_ed25519_import_public(void)
  19577. {
  19578. EXPECT_DECLS;
  19579. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19580. ed25519_key pubKey;
  19581. WC_RNG rng;
  19582. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19583. word32 inlen = sizeof(in);
  19584. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  19585. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19586. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  19587. ExpectIntEQ(wc_InitRng(&rng), 0);
  19588. #ifdef HAVE_ED25519_MAKE_KEY
  19589. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  19590. #endif
  19591. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  19592. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19593. /* Test bad args. */
  19594. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19595. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19596. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19597. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19598. wc_ed25519_free(&pubKey);
  19599. #endif
  19600. return EXPECT_RESULT();
  19601. } /* END wc_ed25519_import_public */
  19602. /*
  19603. * Testing wc_ed25519_import_private_key()
  19604. */
  19605. static int test_wc_ed25519_import_private_key(void)
  19606. {
  19607. EXPECT_DECLS;
  19608. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19609. ed25519_key key;
  19610. WC_RNG rng;
  19611. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19612. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19613. word32 privKeySz = sizeof(privKey);
  19614. word32 pubKeySz = sizeof(pubKey);
  19615. #ifdef HAVE_ED25519_KEY_EXPORT
  19616. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19617. word32 bothKeysSz = sizeof(bothKeys);
  19618. #endif
  19619. XMEMSET(&key, 0, sizeof(ed25519_key));
  19620. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19621. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19622. ExpectIntEQ(wc_InitRng(&rng), 0);
  19623. #ifdef HAVE_ED25519_MAKE_KEY
  19624. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19625. #endif
  19626. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19627. pubKeySz, &key, 1), 0);
  19628. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19629. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19630. #ifdef HAVE_ED25519_KEY_EXPORT
  19631. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  19632. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19633. &key, 1), 0);
  19634. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19635. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19636. #endif
  19637. /* Test bad args. */
  19638. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19639. &key), BAD_FUNC_ARG);
  19640. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19641. pubKeySz, &key), BAD_FUNC_ARG);
  19642. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19643. pubKeySz, NULL), BAD_FUNC_ARG);
  19644. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19645. pubKeySz, &key), BAD_FUNC_ARG);
  19646. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19647. pubKeySz - 1, &key), BAD_FUNC_ARG);
  19648. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  19649. &key), BAD_FUNC_ARG);
  19650. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19651. wc_ed25519_free(&key);
  19652. #endif
  19653. return EXPECT_RESULT();
  19654. } /* END test_wc_ed25519_import_private_key */
  19655. /*
  19656. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19657. */
  19658. static int test_wc_ed25519_export(void)
  19659. {
  19660. EXPECT_DECLS;
  19661. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19662. ed25519_key key;
  19663. WC_RNG rng;
  19664. byte priv[ED25519_PRV_KEY_SIZE];
  19665. byte pub[ED25519_PUB_KEY_SIZE];
  19666. word32 privSz = sizeof(priv);
  19667. word32 pubSz = sizeof(pub);
  19668. #ifndef HAVE_ED25519_MAKE_KEY
  19669. const byte privKey[] = {
  19670. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19671. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19672. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19673. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19674. };
  19675. const byte pubKey[] = {
  19676. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19677. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19678. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19679. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19680. };
  19681. #endif
  19682. XMEMSET(&key, 0, sizeof(ed25519_key));
  19683. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19684. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19685. ExpectIntEQ(wc_InitRng(&rng), 0);
  19686. #ifdef HAVE_ED25519_MAKE_KEY
  19687. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19688. #else
  19689. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19690. pubKey, sizeof(pubKey), &key, 1), 0);
  19691. #endif
  19692. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  19693. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  19694. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  19695. /* Test bad args. */
  19696. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  19697. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  19698. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  19699. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  19700. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  19701. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  19702. /* Test bad args. */
  19703. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  19704. BAD_FUNC_ARG);
  19705. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  19706. BAD_FUNC_ARG);
  19707. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  19708. BAD_FUNC_ARG);
  19709. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19710. wc_ed25519_free(&key);
  19711. #endif
  19712. return EXPECT_RESULT();
  19713. } /* END test_wc_ed25519_export */
  19714. /*
  19715. * Testing wc_ed25519_size()
  19716. */
  19717. static int test_wc_ed25519_size(void)
  19718. {
  19719. EXPECT_DECLS;
  19720. #if defined(HAVE_ED25519)
  19721. ed25519_key key;
  19722. WC_RNG rng;
  19723. #ifndef HAVE_ED25519_MAKE_KEY
  19724. const byte privKey[] = {
  19725. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19726. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19727. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19728. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19729. };
  19730. const byte pubKey[] = {
  19731. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19732. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19733. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19734. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19735. };
  19736. #endif
  19737. XMEMSET(&key, 0, sizeof(ed25519_key));
  19738. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19739. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19740. ExpectIntEQ(wc_InitRng(&rng), 0);
  19741. #ifdef HAVE_ED25519_MAKE_KEY
  19742. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19743. #else
  19744. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19745. pubKey, sizeof(pubKey), &key, 1), 0);
  19746. #endif
  19747. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  19748. /* Test bad args. */
  19749. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  19750. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  19751. /* Test bad args. */
  19752. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  19753. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  19754. /* Test bad args. */
  19755. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  19756. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  19757. /* Test bad args. */
  19758. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  19759. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19760. wc_ed25519_free(&key);
  19761. #endif
  19762. return EXPECT_RESULT();
  19763. } /* END test_wc_ed25519_size */
  19764. /*
  19765. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19766. */
  19767. static int test_wc_ed25519_exportKey(void)
  19768. {
  19769. EXPECT_DECLS;
  19770. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19771. WC_RNG rng;
  19772. ed25519_key key;
  19773. byte priv[ED25519_PRV_KEY_SIZE];
  19774. byte pub[ED25519_PUB_KEY_SIZE];
  19775. byte privOnly[ED25519_PRV_KEY_SIZE];
  19776. word32 privSz = sizeof(priv);
  19777. word32 pubSz = sizeof(pub);
  19778. word32 privOnlySz = sizeof(privOnly);
  19779. #ifndef HAVE_ED25519_MAKE_KEY
  19780. const byte privKey[] = {
  19781. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19782. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19783. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19784. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19785. };
  19786. const byte pubKey[] = {
  19787. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19788. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19789. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19790. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19791. };
  19792. #endif
  19793. XMEMSET(&key, 0, sizeof(ed25519_key));
  19794. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19795. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19796. ExpectIntEQ(wc_InitRng(&rng), 0);
  19797. #ifdef HAVE_ED25519_MAKE_KEY
  19798. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19799. #else
  19800. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19801. pubKey, sizeof(pubKey), &key, 1), 0);
  19802. #endif
  19803. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  19804. /* Test bad args. */
  19805. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  19806. BAD_FUNC_ARG);
  19807. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  19808. BAD_FUNC_ARG);
  19809. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  19810. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  19811. /* Test bad args. */
  19812. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  19813. BAD_FUNC_ARG);
  19814. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  19815. BAD_FUNC_ARG);
  19816. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  19817. BAD_FUNC_ARG);
  19818. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  19819. BAD_FUNC_ARG);
  19820. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  19821. BAD_FUNC_ARG);
  19822. /* Cross check output. */
  19823. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  19824. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19825. wc_ed25519_free(&key);
  19826. #endif
  19827. return EXPECT_RESULT();
  19828. } /* END test_wc_ed25519_exportKey */
  19829. /*
  19830. * Testing wc_Ed25519PublicKeyToDer
  19831. */
  19832. static int test_wc_Ed25519PublicKeyToDer(void)
  19833. {
  19834. EXPECT_DECLS;
  19835. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19836. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19837. ed25519_key key;
  19838. byte derBuf[1024];
  19839. XMEMSET(&key, 0, sizeof(ed25519_key));
  19840. /* Test bad args */
  19841. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  19842. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19843. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  19844. wc_ed25519_free(&key);
  19845. /* Test good args */
  19846. if (EXPECT_SUCCESS()) {
  19847. WC_RNG rng;
  19848. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19849. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19850. ExpectIntEQ(wc_InitRng(&rng), 0);
  19851. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19852. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  19853. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19854. wc_ed25519_free(&key);
  19855. }
  19856. #endif
  19857. return EXPECT_RESULT();
  19858. } /* END testing wc_Ed25519PublicKeyToDer */
  19859. /*
  19860. * Testing wc_curve25519_init and wc_curve25519_free.
  19861. */
  19862. static int test_wc_curve25519_init(void)
  19863. {
  19864. EXPECT_DECLS;
  19865. #if defined(HAVE_CURVE25519)
  19866. curve25519_key key;
  19867. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19868. /* Test bad args for wc_curve25519_init */
  19869. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  19870. /* Test good args for wc_curve_25519_free */
  19871. wc_curve25519_free(&key);
  19872. /* Test bad args for wc_curve25519 free. */
  19873. wc_curve25519_free(NULL);
  19874. #endif
  19875. return EXPECT_RESULT();
  19876. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19877. /*
  19878. * Testing test_wc_curve25519_size.
  19879. */
  19880. static int test_wc_curve25519_size(void)
  19881. {
  19882. EXPECT_DECLS;
  19883. #if defined(HAVE_CURVE25519)
  19884. curve25519_key key;
  19885. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19886. /* Test good args for wc_curve25519_size */
  19887. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19888. /* Test bad args for wc_curve25519_size */
  19889. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  19890. wc_curve25519_free(&key);
  19891. #endif
  19892. return EXPECT_RESULT();
  19893. } /* END test_wc_curve25519_size*/
  19894. /*
  19895. * Testing test_wc_curve25519_export_key_raw().
  19896. */
  19897. static int test_wc_curve25519_export_key_raw(void)
  19898. {
  19899. EXPECT_DECLS;
  19900. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19901. curve25519_key key;
  19902. WC_RNG rng;
  19903. byte privateKey[CURVE25519_KEYSIZE];
  19904. byte publicKey[CURVE25519_KEYSIZE];
  19905. word32 prvkSz;
  19906. word32 pubkSz;
  19907. byte prik[CURVE25519_KEYSIZE];
  19908. byte pubk[CURVE25519_KEYSIZE];
  19909. word32 prksz;
  19910. word32 pbksz;
  19911. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19912. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19913. ExpectIntEQ(wc_InitRng(&rng), 0);
  19914. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19915. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19916. prvkSz = CURVE25519_KEYSIZE;
  19917. pubkSz = CURVE25519_KEYSIZE;
  19918. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  19919. publicKey, &pubkSz), BAD_FUNC_ARG);
  19920. prvkSz = CURVE25519_KEYSIZE;
  19921. pubkSz = CURVE25519_KEYSIZE;
  19922. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  19923. &pubkSz), BAD_FUNC_ARG);
  19924. prvkSz = CURVE25519_KEYSIZE;
  19925. pubkSz = CURVE25519_KEYSIZE;
  19926. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  19927. publicKey, &pubkSz), BAD_FUNC_ARG);
  19928. /* prvkSz = CURVE25519_KEYSIZE; */
  19929. pubkSz = CURVE25519_KEYSIZE;
  19930. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19931. NULL, &pubkSz), BAD_FUNC_ARG);
  19932. prvkSz = CURVE25519_KEYSIZE;
  19933. pubkSz = CURVE25519_KEYSIZE;
  19934. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19935. publicKey, NULL), BAD_FUNC_ARG);
  19936. /* cross-testing */
  19937. prksz = CURVE25519_KEYSIZE;
  19938. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  19939. pbksz = CURVE25519_KEYSIZE;
  19940. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  19941. prvkSz = CURVE25519_KEYSIZE;
  19942. /* pubkSz = CURVE25519_KEYSIZE; */
  19943. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19944. publicKey, &pubkSz), 0);
  19945. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19946. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19947. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19948. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19949. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19950. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19951. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19952. wc_curve25519_free(&key);
  19953. #endif
  19954. return EXPECT_RESULT();
  19955. } /* end of test_wc_curve25519_export_key_raw */
  19956. /*
  19957. * Testing test_wc_curve25519_export_key_raw_ex().
  19958. */
  19959. static int test_wc_curve25519_export_key_raw_ex(void)
  19960. {
  19961. EXPECT_DECLS;
  19962. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19963. curve25519_key key;
  19964. WC_RNG rng;
  19965. byte privateKey[CURVE25519_KEYSIZE];
  19966. byte publicKey[CURVE25519_KEYSIZE];
  19967. word32 prvkSz;
  19968. word32 pubkSz;
  19969. byte prik[CURVE25519_KEYSIZE];
  19970. byte pubk[CURVE25519_KEYSIZE];
  19971. word32 prksz;
  19972. word32 pbksz;
  19973. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19974. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19975. ExpectIntEQ(wc_InitRng(&rng), 0);
  19976. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19977. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19978. prvkSz = CURVE25519_KEYSIZE;
  19979. pubkSz = CURVE25519_KEYSIZE;
  19980. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19981. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19982. prvkSz = CURVE25519_KEYSIZE;
  19983. pubkSz = CURVE25519_KEYSIZE;
  19984. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19985. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19986. prvkSz = CURVE25519_KEYSIZE;
  19987. pubkSz = CURVE25519_KEYSIZE;
  19988. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19989. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19990. /* prvkSz = CURVE25519_KEYSIZE; */
  19991. pubkSz = CURVE25519_KEYSIZE;
  19992. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19993. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19994. prvkSz = CURVE25519_KEYSIZE;
  19995. pubkSz = CURVE25519_KEYSIZE;
  19996. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19997. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19998. prvkSz = CURVE25519_KEYSIZE;
  19999. /* pubkSz = CURVE25519_KEYSIZE; */
  20000. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20001. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20002. prvkSz = CURVE25519_KEYSIZE;
  20003. pubkSz = CURVE25519_KEYSIZE;
  20004. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20005. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20006. prvkSz = CURVE25519_KEYSIZE;
  20007. pubkSz = CURVE25519_KEYSIZE;
  20008. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20009. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20010. /* prvkSz = CURVE25519_KEYSIZE; */
  20011. pubkSz = CURVE25519_KEYSIZE;
  20012. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20013. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20014. prvkSz = CURVE25519_KEYSIZE;
  20015. pubkSz = CURVE25519_KEYSIZE;
  20016. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20017. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20018. /* illegal value for endian */
  20019. prvkSz = CURVE25519_KEYSIZE;
  20020. /* pubkSz = CURVE25519_KEYSIZE; */
  20021. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20022. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20023. /* cross-testing */
  20024. prksz = CURVE25519_KEYSIZE;
  20025. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20026. pbksz = CURVE25519_KEYSIZE;
  20027. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20028. prvkSz = CURVE25519_KEYSIZE;
  20029. /* pubkSz = CURVE25519_KEYSIZE; */
  20030. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20031. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20032. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20033. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20034. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20035. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20036. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20037. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20038. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20039. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20040. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20041. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20042. /* try once with another endian */
  20043. prvkSz = CURVE25519_KEYSIZE;
  20044. pubkSz = CURVE25519_KEYSIZE;
  20045. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20046. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20047. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20048. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20049. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20050. wc_curve25519_free(&key);
  20051. #endif
  20052. return EXPECT_RESULT();
  20053. } /* end of test_wc_curve25519_export_key_raw_ex */
  20054. /*
  20055. * Testing wc_curve25519_make_key
  20056. */
  20057. static int test_wc_curve25519_make_key(void)
  20058. {
  20059. EXPECT_DECLS;
  20060. #if defined(HAVE_CURVE25519)
  20061. curve25519_key key;
  20062. WC_RNG rng;
  20063. int keysize;
  20064. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20065. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20066. ExpectIntEQ(wc_InitRng(&rng), 0);
  20067. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20068. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20069. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20070. /* test bad cases*/
  20071. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20072. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20073. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20074. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20075. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20076. wc_curve25519_free(&key);
  20077. #endif
  20078. return EXPECT_RESULT();
  20079. } /* END test_wc_curve25519_make_key*/
  20080. /*
  20081. * Testing wc_curve25519_shared_secret_ex
  20082. */
  20083. static int test_wc_curve25519_shared_secret_ex(void)
  20084. {
  20085. EXPECT_DECLS;
  20086. #if defined(HAVE_CURVE25519)
  20087. curve25519_key private_key;
  20088. curve25519_key public_key;
  20089. WC_RNG rng;
  20090. byte out[CURVE25519_KEYSIZE];
  20091. word32 outLen = sizeof(out);
  20092. int endian = EC25519_BIG_ENDIAN;
  20093. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20094. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20095. ExpectIntEQ(wc_InitRng(&rng), 0);
  20096. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20097. 0);
  20098. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20099. 0);
  20100. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20101. &outLen, endian), 0);
  20102. /* test bad cases*/
  20103. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20104. BAD_FUNC_ARG);
  20105. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20106. endian), BAD_FUNC_ARG);
  20107. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20108. endian), BAD_FUNC_ARG);
  20109. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20110. &outLen, endian), BAD_FUNC_ARG);
  20111. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20112. NULL, endian), BAD_FUNC_ARG);
  20113. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20114. * increasing to 0x8f checks for error being returned*/
  20115. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20116. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20117. &outLen, endian), ECC_BAD_ARG_E);
  20118. outLen = outLen - 2;
  20119. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20120. &outLen, endian), BAD_FUNC_ARG);
  20121. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20122. wc_curve25519_free(&private_key);
  20123. wc_curve25519_free(&public_key);
  20124. #endif
  20125. return EXPECT_RESULT();
  20126. } /* END test_wc_curve25519_shared_secret_ex*/
  20127. /*
  20128. * Testing wc_curve25519_make_pub
  20129. */
  20130. static int test_wc_curve25519_make_pub(void)
  20131. {
  20132. EXPECT_DECLS;
  20133. #ifdef HAVE_CURVE25519
  20134. curve25519_key key;
  20135. WC_RNG rng;
  20136. byte out[CURVE25519_KEYSIZE];
  20137. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20138. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20139. ExpectIntEQ(wc_InitRng(&rng), 0);
  20140. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20141. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20142. (int)sizeof(key.k), key.k), 0);
  20143. /* test bad cases*/
  20144. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20145. (int)sizeof out, out), ECC_BAD_ARG_E);
  20146. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20147. NULL), ECC_BAD_ARG_E);
  20148. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20149. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20150. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20151. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20152. /* verify clamping test */
  20153. key.k[0] |= ~248;
  20154. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20155. key.k), ECC_BAD_ARG_E);
  20156. key.k[0] &= 248;
  20157. /* repeat the expected-to-succeed test. */
  20158. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20159. key.k), 0);
  20160. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20161. wc_curve25519_free(&key);
  20162. #endif
  20163. return EXPECT_RESULT();
  20164. } /* END test_wc_curve25519_make_pub */
  20165. /*
  20166. * Testing test_wc_curve25519_export_public_ex
  20167. */
  20168. static int test_wc_curve25519_export_public_ex(void)
  20169. {
  20170. EXPECT_DECLS;
  20171. #if defined(HAVE_CURVE25519)
  20172. curve25519_key key;
  20173. WC_RNG rng;
  20174. byte out[CURVE25519_KEYSIZE];
  20175. word32 outLen = sizeof(out);
  20176. int endian = EC25519_BIG_ENDIAN;
  20177. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20178. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20179. ExpectIntEQ(wc_InitRng(&rng), 0);
  20180. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20181. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20182. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20183. /* test bad cases*/
  20184. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20185. BAD_FUNC_ARG);
  20186. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20187. BAD_FUNC_ARG);
  20188. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20189. BAD_FUNC_ARG);
  20190. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20191. BAD_FUNC_ARG);
  20192. outLen = outLen - 2;
  20193. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20194. ECC_BAD_ARG_E);
  20195. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20196. wc_curve25519_free(&key);
  20197. #endif
  20198. return EXPECT_RESULT();
  20199. } /* END test_wc_curve25519_export_public_ex*/
  20200. /*
  20201. * Testing test_wc_curve25519_import_private_raw_ex
  20202. */
  20203. static int test_wc_curve25519_import_private_raw_ex(void)
  20204. {
  20205. EXPECT_DECLS;
  20206. #if defined(HAVE_CURVE25519)
  20207. curve25519_key key;
  20208. WC_RNG rng;
  20209. byte priv[CURVE25519_KEYSIZE];
  20210. byte pub[CURVE25519_KEYSIZE];
  20211. word32 privSz = sizeof(priv);
  20212. word32 pubSz = sizeof(pub);
  20213. int endian = EC25519_BIG_ENDIAN;
  20214. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20215. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20216. ExpectIntEQ(wc_InitRng(&rng), 0);
  20217. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20218. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20219. endian), 0);
  20220. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20221. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20222. &key, endian), 0);
  20223. /* test bad cases*/
  20224. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20225. endian), BAD_FUNC_ARG);
  20226. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20227. &key, endian), BAD_FUNC_ARG);
  20228. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20229. &key, endian), BAD_FUNC_ARG);
  20230. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20231. NULL, endian), BAD_FUNC_ARG);
  20232. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20233. &key, endian), ECC_BAD_ARG_E);
  20234. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20235. &key, endian), ECC_BAD_ARG_E);
  20236. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20237. &key, EC25519_LITTLE_ENDIAN), 0);
  20238. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20239. wc_curve25519_free(&key);
  20240. #endif
  20241. return EXPECT_RESULT();
  20242. } /* END test_wc_curve25519_import_private_raw_ex*/
  20243. /*
  20244. * Testing test_wc_curve25519_import_private
  20245. */
  20246. static int test_wc_curve25519_import_private(void)
  20247. {
  20248. EXPECT_DECLS;
  20249. #if defined(HAVE_CURVE25519)
  20250. curve25519_key key;
  20251. WC_RNG rng;
  20252. byte priv[CURVE25519_KEYSIZE];
  20253. word32 privSz = sizeof(priv);
  20254. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20255. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20256. ExpectIntEQ(wc_InitRng(&rng), 0);
  20257. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20258. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20259. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20260. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20261. wc_curve25519_free(&key);
  20262. #endif
  20263. return EXPECT_RESULT();
  20264. } /* END test_wc_curve25519_import*/
  20265. /*
  20266. * Testing test_wc_curve25519_export_private_raw_ex
  20267. */
  20268. static int test_wc_curve25519_export_private_raw_ex(void)
  20269. {
  20270. EXPECT_DECLS;
  20271. #if defined(HAVE_CURVE25519)
  20272. curve25519_key key;
  20273. byte out[CURVE25519_KEYSIZE];
  20274. word32 outLen = sizeof(out);
  20275. int endian = EC25519_BIG_ENDIAN;
  20276. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20277. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20278. 0);
  20279. /* test bad cases*/
  20280. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20281. BAD_FUNC_ARG);
  20282. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20283. BAD_FUNC_ARG);
  20284. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20285. endian), BAD_FUNC_ARG);
  20286. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20287. BAD_FUNC_ARG);
  20288. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20289. EC25519_LITTLE_ENDIAN), 0);
  20290. outLen = outLen - 2;
  20291. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20292. ECC_BAD_ARG_E);
  20293. wc_curve25519_free(&key);
  20294. #endif
  20295. return EXPECT_RESULT();
  20296. } /* END test_wc_curve25519_export_private_raw_ex*/
  20297. /*
  20298. * Testing wc_ed448_make_key().
  20299. */
  20300. static int test_wc_ed448_make_key(void)
  20301. {
  20302. EXPECT_DECLS;
  20303. #if defined(HAVE_ED448)
  20304. ed448_key key;
  20305. WC_RNG rng;
  20306. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20307. XMEMSET(&key, 0, sizeof(ed448_key));
  20308. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20309. ExpectIntEQ(wc_ed448_init(&key), 0);
  20310. ExpectIntEQ(wc_InitRng(&rng), 0);
  20311. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20312. ECC_PRIV_KEY_E);
  20313. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20314. /* Test bad args. */
  20315. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20316. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20317. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20318. BAD_FUNC_ARG);
  20319. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20320. BAD_FUNC_ARG);
  20321. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20322. wc_ed448_free(&key);
  20323. #endif
  20324. return EXPECT_RESULT();
  20325. } /* END test_wc_ed448_make_key */
  20326. /*
  20327. * Testing wc_ed448_init()
  20328. */
  20329. static int test_wc_ed448_init(void)
  20330. {
  20331. EXPECT_DECLS;
  20332. #if defined(HAVE_ED448)
  20333. ed448_key key;
  20334. XMEMSET(&key, 0, sizeof(ed448_key));
  20335. ExpectIntEQ(wc_ed448_init(&key), 0);
  20336. /* Test bad args. */
  20337. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20338. wc_ed448_free(&key);
  20339. #endif
  20340. return EXPECT_RESULT();
  20341. } /* END test_wc_ed448_init */
  20342. /*
  20343. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20344. */
  20345. static int test_wc_ed448_sign_msg(void)
  20346. {
  20347. EXPECT_DECLS;
  20348. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20349. ed448_key key;
  20350. WC_RNG rng;
  20351. byte msg[] = "Everybody gets Friday off.\n";
  20352. byte sig[ED448_SIG_SIZE];
  20353. word32 msglen = sizeof(msg);
  20354. word32 siglen = sizeof(sig);
  20355. word32 badSigLen = sizeof(sig) - 1;
  20356. #ifdef HAVE_ED448_VERIFY
  20357. int verify_ok = 0; /*1 = Verify success.*/
  20358. #endif
  20359. /* Initialize stack variables. */
  20360. XMEMSET(&key, 0, sizeof(ed448_key));
  20361. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20362. XMEMSET(sig, 0, siglen);
  20363. /* Initialize key. */
  20364. ExpectIntEQ(wc_ed448_init(&key), 0);
  20365. ExpectIntEQ(wc_InitRng(&rng), 0);
  20366. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20367. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20368. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20369. /* Test bad args. */
  20370. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20371. BAD_FUNC_ARG);
  20372. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20373. BAD_FUNC_ARG);
  20374. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20375. BAD_FUNC_ARG);
  20376. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20377. BAD_FUNC_ARG);
  20378. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20379. BUFFER_E);
  20380. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20381. badSigLen -= 1;
  20382. #ifdef HAVE_ED448_VERIFY
  20383. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20384. NULL, 0), 0);
  20385. ExpectIntEQ(verify_ok, 1);
  20386. /* Test bad args. */
  20387. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20388. &key, NULL, 0), BAD_FUNC_ARG);
  20389. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20390. &key, NULL, 0), BAD_FUNC_ARG);
  20391. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20392. &key, NULL, 0), BAD_FUNC_ARG);
  20393. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20394. &key, NULL, 0), BAD_FUNC_ARG);
  20395. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20396. &key, NULL, 0), BAD_FUNC_ARG);
  20397. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20398. NULL, NULL, 0), BAD_FUNC_ARG);
  20399. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20400. &key, NULL, 0), BAD_FUNC_ARG);
  20401. #endif /* Verify. */
  20402. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20403. wc_ed448_free(&key);
  20404. #endif
  20405. return EXPECT_RESULT();
  20406. } /* END test_wc_ed448_sign_msg */
  20407. /*
  20408. * Testing wc_ed448_import_public()
  20409. */
  20410. static int test_wc_ed448_import_public(void)
  20411. {
  20412. EXPECT_DECLS;
  20413. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20414. ed448_key pubKey;
  20415. WC_RNG rng;
  20416. const byte in[] =
  20417. "Ed448PublicKeyUnitTest.................................\n";
  20418. word32 inlen = sizeof(in);
  20419. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20420. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20421. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20422. ExpectIntEQ(wc_InitRng(&rng), 0);
  20423. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20424. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20425. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20426. /* Test bad args. */
  20427. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20428. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20429. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20430. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20431. wc_ed448_free(&pubKey);
  20432. #endif
  20433. return EXPECT_RESULT();
  20434. } /* END wc_ed448_import_public */
  20435. /*
  20436. * Testing wc_ed448_import_private_key()
  20437. */
  20438. static int test_wc_ed448_import_private_key(void)
  20439. {
  20440. EXPECT_DECLS;
  20441. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20442. ed448_key key;
  20443. WC_RNG rng;
  20444. const byte privKey[] =
  20445. "Ed448PrivateKeyUnitTest................................\n";
  20446. const byte pubKey[] =
  20447. "Ed448PublicKeyUnitTest.................................\n";
  20448. word32 privKeySz = sizeof(privKey);
  20449. word32 pubKeySz = sizeof(pubKey);
  20450. #ifdef HAVE_ED448_KEY_EXPORT
  20451. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20452. word32 bothKeysSz = sizeof(bothKeys);
  20453. #endif
  20454. XMEMSET(&key, 0, sizeof(ed448_key));
  20455. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20456. ExpectIntEQ(wc_ed448_init(&key), 0);
  20457. ExpectIntEQ(wc_InitRng(&rng), 0);
  20458. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20459. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20460. pubKeySz, &key, 1), 0);
  20461. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20462. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20463. #ifdef HAVE_ED448_KEY_EXPORT
  20464. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20465. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20466. &key, 1), 0);
  20467. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20468. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20469. #endif
  20470. /* Test bad args. */
  20471. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20472. &key), BAD_FUNC_ARG);
  20473. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20474. &key), BAD_FUNC_ARG);
  20475. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20476. pubKeySz, NULL), BAD_FUNC_ARG);
  20477. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20478. pubKeySz, &key), BAD_FUNC_ARG);
  20479. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20480. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20481. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20482. BAD_FUNC_ARG);
  20483. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20484. wc_ed448_free(&key);
  20485. #endif
  20486. return EXPECT_RESULT();
  20487. } /* END test_wc_ed448_import_private_key */
  20488. /*
  20489. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20490. */
  20491. static int test_wc_ed448_export(void)
  20492. {
  20493. EXPECT_DECLS;
  20494. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20495. ed448_key key;
  20496. WC_RNG rng;
  20497. byte priv[ED448_PRV_KEY_SIZE];
  20498. byte pub[ED448_PUB_KEY_SIZE];
  20499. word32 privSz = sizeof(priv);
  20500. word32 pubSz = sizeof(pub);
  20501. XMEMSET(&key, 0, sizeof(ed448_key));
  20502. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20503. ExpectIntEQ(wc_ed448_init(&key), 0);
  20504. ExpectIntEQ(wc_InitRng(&rng), 0);
  20505. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20506. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20507. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20508. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20509. /* Test bad args. */
  20510. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20511. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20512. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20513. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20514. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20515. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20516. /* Test bad args. */
  20517. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20518. BAD_FUNC_ARG);
  20519. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20520. BAD_FUNC_ARG);
  20521. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20522. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20523. wc_ed448_free(&key);
  20524. #endif
  20525. return EXPECT_RESULT();
  20526. } /* END test_wc_ed448_export */
  20527. /*
  20528. * Testing wc_ed448_size()
  20529. */
  20530. static int test_wc_ed448_size(void)
  20531. {
  20532. EXPECT_DECLS;
  20533. #if defined(HAVE_ED448)
  20534. ed448_key key;
  20535. WC_RNG rng;
  20536. XMEMSET(&key, 0, sizeof(ed448_key));
  20537. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20538. ExpectIntEQ(wc_ed448_init(&key), 0);
  20539. ExpectIntEQ(wc_InitRng(&rng), 0);
  20540. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20541. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20542. /* Test bad args. */
  20543. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20544. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20545. /* Test bad args. */
  20546. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20547. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20548. /* Test bad args. */
  20549. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20550. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20551. /* Test bad args. */
  20552. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20553. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20554. wc_ed448_free(&key);
  20555. #endif
  20556. return EXPECT_RESULT();
  20557. } /* END test_wc_ed448_size */
  20558. /*
  20559. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20560. */
  20561. static int test_wc_ed448_exportKey(void)
  20562. {
  20563. EXPECT_DECLS;
  20564. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20565. ed448_key key;
  20566. WC_RNG rng;
  20567. byte priv[ED448_PRV_KEY_SIZE];
  20568. byte pub[ED448_PUB_KEY_SIZE];
  20569. byte privOnly[ED448_PRV_KEY_SIZE];
  20570. word32 privSz = sizeof(priv);
  20571. word32 pubSz = sizeof(pub);
  20572. word32 privOnlySz = sizeof(privOnly);
  20573. XMEMSET(&key, 0, sizeof(ed448_key));
  20574. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20575. ExpectIntEQ(wc_ed448_init(&key), 0);
  20576. ExpectIntEQ(wc_InitRng(&rng), 0);
  20577. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20578. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  20579. /* Test bad args. */
  20580. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  20581. BAD_FUNC_ARG);
  20582. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  20583. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20584. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20585. /* Test bad args. */
  20586. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  20587. BAD_FUNC_ARG);
  20588. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  20589. BAD_FUNC_ARG);
  20590. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  20591. BAD_FUNC_ARG);
  20592. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  20593. BAD_FUNC_ARG);
  20594. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  20595. BAD_FUNC_ARG);
  20596. /* Cross check output. */
  20597. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20598. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20599. wc_ed448_free(&key);
  20600. #endif
  20601. return EXPECT_RESULT();
  20602. } /* END test_wc_ed448_exportKey */
  20603. /*
  20604. * Testing wc_Ed448PublicKeyToDer
  20605. */
  20606. static int test_wc_Ed448PublicKeyToDer(void)
  20607. {
  20608. EXPECT_DECLS;
  20609. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20610. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20611. ed448_key key;
  20612. byte derBuf[1024];
  20613. XMEMSET(&key, 0, sizeof(ed448_key));
  20614. /* Test bad args */
  20615. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20616. ExpectIntEQ(wc_ed448_init(&key), 0);
  20617. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20618. wc_ed448_free(&key);
  20619. /* Test good args */
  20620. if (EXPECT_SUCCESS()) {
  20621. WC_RNG rng;
  20622. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20623. ExpectIntEQ(wc_ed448_init(&key), 0);
  20624. ExpectIntEQ(wc_InitRng(&rng), 0);
  20625. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20626. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20627. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20628. wc_ed448_free(&key);
  20629. }
  20630. #endif
  20631. return EXPECT_RESULT();
  20632. } /* END testing wc_Ed448PublicKeyToDer */
  20633. /*
  20634. * Testing wc_curve448_init and wc_curve448_free.
  20635. */
  20636. static int test_wc_curve448_init(void)
  20637. {
  20638. EXPECT_DECLS;
  20639. #if defined(HAVE_CURVE448)
  20640. curve448_key key;
  20641. /* Test bad args for wc_curve448_init */
  20642. ExpectIntEQ(wc_curve448_init(&key), 0);
  20643. /* Test bad args for wc_curve448_init */
  20644. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  20645. /* Test good args for wc_curve_448_free */
  20646. wc_curve448_free(&key);
  20647. /* Test bad args for wc_curve448_free */
  20648. wc_curve448_free(NULL);
  20649. #endif
  20650. return EXPECT_RESULT();
  20651. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20652. /*
  20653. * Testing wc_curve448_make_key
  20654. */
  20655. static int test_wc_curve448_make_key(void)
  20656. {
  20657. EXPECT_DECLS;
  20658. #if defined(HAVE_CURVE448)
  20659. curve448_key key;
  20660. WC_RNG rng;
  20661. int keysize;
  20662. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20663. ExpectIntEQ(wc_curve448_init(&key), 0);
  20664. ExpectIntEQ(wc_InitRng(&rng), 0);
  20665. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20666. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20667. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  20668. /* test bad cases */
  20669. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20670. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20671. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20672. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20673. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20674. wc_curve448_free(&key);
  20675. #endif
  20676. return EXPECT_RESULT();
  20677. } /* END test_wc_curve448_make_key*/
  20678. /*
  20679. * Testing test_wc_curve448_shared_secret_ex
  20680. */
  20681. static int test_wc_curve448_shared_secret_ex(void)
  20682. {
  20683. EXPECT_DECLS;
  20684. #if defined(HAVE_CURVE448)
  20685. curve448_key private_key;
  20686. curve448_key public_key;
  20687. WC_RNG rng;
  20688. byte out[CURVE448_KEY_SIZE];
  20689. word32 outLen = sizeof(out);
  20690. int endian = EC448_BIG_ENDIAN;
  20691. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20692. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  20693. ExpectIntEQ(wc_InitRng(&rng), 0);
  20694. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  20695. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  20696. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  20697. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20698. &outLen, endian), 0);
  20699. /* test bad cases */
  20700. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20701. BAD_FUNC_ARG);
  20702. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  20703. endian), BAD_FUNC_ARG);
  20704. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  20705. endian), BAD_FUNC_ARG);
  20706. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20707. &outLen, endian), BAD_FUNC_ARG);
  20708. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20709. NULL, endian), BAD_FUNC_ARG);
  20710. outLen = outLen - 2;
  20711. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20712. &outLen, endian), BAD_FUNC_ARG);
  20713. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20714. wc_curve448_free(&private_key);
  20715. wc_curve448_free(&public_key);
  20716. #endif
  20717. return EXPECT_RESULT();
  20718. } /* END test_wc_curve448_shared_secret_ex*/
  20719. /*
  20720. * Testing test_wc_curve448_export_public_ex
  20721. */
  20722. static int test_wc_curve448_export_public_ex(void)
  20723. {
  20724. EXPECT_DECLS;
  20725. #if defined(HAVE_CURVE448)
  20726. WC_RNG rng;
  20727. curve448_key key;
  20728. byte out[CURVE448_KEY_SIZE];
  20729. word32 outLen = sizeof(out);
  20730. int endian = EC448_BIG_ENDIAN;
  20731. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20732. ExpectIntEQ(wc_curve448_init(&key), 0);
  20733. ExpectIntEQ(wc_InitRng(&rng), 0);
  20734. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20735. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  20736. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  20737. /* test bad cases*/
  20738. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  20739. BAD_FUNC_ARG);
  20740. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  20741. BAD_FUNC_ARG);
  20742. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  20743. BAD_FUNC_ARG);
  20744. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  20745. BAD_FUNC_ARG);
  20746. outLen = outLen - 2;
  20747. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  20748. ECC_BAD_ARG_E);
  20749. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20750. wc_curve448_free(&key);
  20751. #endif
  20752. return EXPECT_RESULT();
  20753. } /* END test_wc_curve448_export_public_ex*/
  20754. /*
  20755. * Testing test_wc_curve448_export_private_raw_ex
  20756. */
  20757. static int test_wc_curve448_export_private_raw_ex(void)
  20758. {
  20759. EXPECT_DECLS;
  20760. #if defined(HAVE_CURVE448)
  20761. curve448_key key;
  20762. byte out[CURVE448_KEY_SIZE];
  20763. word32 outLen = sizeof(out);
  20764. int endian = EC448_BIG_ENDIAN;
  20765. ExpectIntEQ(wc_curve448_init(&key), 0);
  20766. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20767. 0);
  20768. /* test bad cases*/
  20769. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  20770. BAD_FUNC_ARG);
  20771. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  20772. BAD_FUNC_ARG);
  20773. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  20774. BAD_FUNC_ARG);
  20775. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  20776. BAD_FUNC_ARG);
  20777. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20778. EC448_LITTLE_ENDIAN), 0);
  20779. outLen = outLen - 2;
  20780. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20781. ECC_BAD_ARG_E);
  20782. wc_curve448_free(&key);
  20783. #endif
  20784. return EXPECT_RESULT();
  20785. } /* END test_wc_curve448_export_private_raw_ex*/
  20786. /*
  20787. * Testing test_wc_curve448_import_private_raw_ex
  20788. */
  20789. static int test_wc_curve448_import_private_raw_ex(void)
  20790. {
  20791. EXPECT_DECLS;
  20792. #if defined(HAVE_CURVE448)
  20793. curve448_key key;
  20794. WC_RNG rng;
  20795. byte priv[CURVE448_KEY_SIZE];
  20796. byte pub[CURVE448_KEY_SIZE];
  20797. word32 privSz = sizeof(priv);
  20798. word32 pubSz = sizeof(pub);
  20799. int endian = EC448_BIG_ENDIAN;
  20800. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20801. ExpectIntEQ(wc_curve448_init(&key), 0);
  20802. ExpectIntEQ(wc_InitRng(&rng), 0);
  20803. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20804. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20805. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20806. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20807. &key, endian), 0);
  20808. /* test bad cases */
  20809. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  20810. BAD_FUNC_ARG);
  20811. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20812. &key, endian), BAD_FUNC_ARG);
  20813. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20814. &key, endian), BAD_FUNC_ARG);
  20815. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20816. NULL, endian), BAD_FUNC_ARG);
  20817. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20818. &key, endian), ECC_BAD_ARG_E);
  20819. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20820. &key, endian), ECC_BAD_ARG_E);
  20821. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20822. &key, EC448_LITTLE_ENDIAN), 0);
  20823. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20824. wc_curve448_free(&key);
  20825. #endif
  20826. return EXPECT_RESULT();
  20827. } /* END test_wc_curve448_import_private_raw_ex*/
  20828. /*
  20829. * Testing test_curve448_export_key_raw
  20830. */
  20831. static int test_wc_curve448_export_key_raw(void)
  20832. {
  20833. EXPECT_DECLS;
  20834. #if defined(HAVE_CURVE448)
  20835. curve448_key key;
  20836. WC_RNG rng;
  20837. byte priv[CURVE448_KEY_SIZE];
  20838. byte pub[CURVE448_KEY_SIZE];
  20839. word32 privSz = sizeof(priv);
  20840. word32 pubSz = sizeof(pub);
  20841. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20842. ExpectIntEQ(wc_curve448_init(&key), 0);
  20843. ExpectIntEQ(wc_InitRng(&rng), 0);
  20844. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20845. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20846. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20847. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  20848. 0);
  20849. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20850. wc_curve448_free(&key);
  20851. #endif
  20852. return EXPECT_RESULT();
  20853. } /* END test_wc_curve448_import_private_raw_ex*/
  20854. /*
  20855. * Testing test_wc_curve448_import_private
  20856. */
  20857. static int test_wc_curve448_import_private(void)
  20858. {
  20859. EXPECT_DECLS;
  20860. #if defined(HAVE_CURVE448)
  20861. curve448_key key;
  20862. WC_RNG rng;
  20863. byte priv[CURVE448_KEY_SIZE];
  20864. word32 privSz = sizeof(priv);
  20865. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20866. ExpectIntEQ(wc_curve448_init(&key), 0);
  20867. ExpectIntEQ(wc_InitRng(&rng), 0);
  20868. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20869. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20870. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  20871. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20872. wc_curve448_free(&key);
  20873. #endif
  20874. return EXPECT_RESULT();
  20875. } /* END test_wc_curve448_import*/
  20876. /*
  20877. * Testing test_wc_curve448_size.
  20878. */
  20879. static int test_wc_curve448_size(void)
  20880. {
  20881. EXPECT_DECLS;
  20882. #if defined(HAVE_CURVE448)
  20883. curve448_key key;
  20884. ExpectIntEQ(wc_curve448_init(&key), 0);
  20885. /* Test good args for wc_curve448_size */
  20886. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20887. /* Test bad args for wc_curve448_size */
  20888. ExpectIntEQ(wc_curve448_size(NULL), 0);
  20889. wc_curve448_free(&key);
  20890. #endif
  20891. return EXPECT_RESULT();
  20892. } /* END test_wc_curve448_size*/
  20893. /*
  20894. * Testing wc_ecc_make_key.
  20895. */
  20896. static int test_wc_ecc_make_key(void)
  20897. {
  20898. EXPECT_DECLS;
  20899. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20900. ecc_key key;
  20901. WC_RNG rng;
  20902. int ret;
  20903. XMEMSET(&key, 0, sizeof(ecc_key));
  20904. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20905. ExpectIntEQ(wc_ecc_init(&key), 0);
  20906. ExpectIntEQ(wc_InitRng(&rng), 0);
  20907. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20908. #if defined(WOLFSSL_ASYNC_CRYPT)
  20909. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20910. #endif
  20911. ExpectIntEQ(ret, 0);
  20912. /* Pass in bad args. */
  20913. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  20914. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  20915. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20916. wc_ecc_free(&key);
  20917. #ifdef FP_ECC
  20918. wc_ecc_fp_free();
  20919. #endif
  20920. #endif
  20921. return EXPECT_RESULT();
  20922. } /* END test_wc_ecc_make_key */
  20923. /*
  20924. * Testing wc_ecc_init()
  20925. */
  20926. static int test_wc_ecc_init(void)
  20927. {
  20928. EXPECT_DECLS;
  20929. #ifdef HAVE_ECC
  20930. ecc_key key;
  20931. XMEMSET(&key, 0, sizeof(ecc_key));
  20932. ExpectIntEQ(wc_ecc_init(&key), 0);
  20933. /* Pass in bad args. */
  20934. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  20935. wc_ecc_free(&key);
  20936. #endif
  20937. return EXPECT_RESULT();
  20938. } /* END test_wc_ecc_init */
  20939. /*
  20940. * Testing wc_ecc_check_key()
  20941. */
  20942. static int test_wc_ecc_check_key(void)
  20943. {
  20944. EXPECT_DECLS;
  20945. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20946. ecc_key key;
  20947. WC_RNG rng;
  20948. int ret;
  20949. XMEMSET(&rng, 0, sizeof(rng));
  20950. XMEMSET(&key, 0, sizeof(key));
  20951. ExpectIntEQ(wc_ecc_init(&key), 0);
  20952. ExpectIntEQ(wc_InitRng(&rng), 0);
  20953. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20954. #if defined(WOLFSSL_ASYNC_CRYPT)
  20955. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20956. #endif
  20957. ExpectIntEQ(ret, 0);
  20958. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  20959. /* Pass in bad args. */
  20960. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  20961. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20962. wc_ecc_free(&key);
  20963. #ifdef FP_ECC
  20964. wc_ecc_fp_free();
  20965. #endif
  20966. #endif
  20967. return EXPECT_RESULT();
  20968. } /* END test_wc_ecc_check_key */
  20969. /*
  20970. * Testing wc_ecc_get_generator()
  20971. */
  20972. static int test_wc_ecc_get_generator(void)
  20973. {
  20974. EXPECT_DECLS;
  20975. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20976. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20977. ecc_point* pt = NULL;
  20978. ExpectNotNull(pt = wc_ecc_new_point());
  20979. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20980. MP_OKAY);
  20981. /* Test bad args. */
  20982. /* Returns Zero for bad arg. */
  20983. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  20984. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20985. MP_OKAY);
  20986. /* If we ever get to 1000 curves increase this number */
  20987. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  20988. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  20989. wc_ecc_del_point(pt);
  20990. #endif
  20991. return EXPECT_RESULT();
  20992. } /* END test_wc_ecc_get_generator */
  20993. /*
  20994. * Testing wc_ecc_size()
  20995. */
  20996. static int test_wc_ecc_size(void)
  20997. {
  20998. EXPECT_DECLS;
  20999. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21000. WC_RNG rng;
  21001. ecc_key key;
  21002. int ret;
  21003. XMEMSET(&key, 0, sizeof(ecc_key));
  21004. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21005. ExpectIntEQ(wc_ecc_init(&key), 0);
  21006. ExpectIntEQ(wc_InitRng(&rng), 0);
  21007. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21008. #if defined(WOLFSSL_ASYNC_CRYPT)
  21009. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21010. #endif
  21011. ExpectIntEQ(ret, 0);
  21012. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21013. /* Test bad args. */
  21014. /* Returns Zero for bad arg. */
  21015. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21016. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21017. wc_ecc_free(&key);
  21018. #endif
  21019. return EXPECT_RESULT();
  21020. } /* END test_wc_ecc_size */
  21021. static int test_wc_ecc_params(void)
  21022. {
  21023. EXPECT_DECLS;
  21024. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21025. It was added after certifications */
  21026. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21027. const ecc_set_type* ecc_set;
  21028. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21029. /* Test for SECP256R1 curve */
  21030. int curve_id = ECC_SECP256R1;
  21031. int curve_idx;
  21032. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21033. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21034. ExpectIntEQ(ecc_set->id, curve_id);
  21035. #endif
  21036. /* Test case when SECP256R1 is not enabled */
  21037. /* Test that we get curve params for index 0 */
  21038. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21039. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21040. return EXPECT_RESULT();
  21041. }
  21042. /*
  21043. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21044. */
  21045. static int test_wc_ecc_signVerify_hash(void)
  21046. {
  21047. EXPECT_DECLS;
  21048. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21049. ecc_key key;
  21050. WC_RNG rng;
  21051. int ret;
  21052. #ifdef HAVE_ECC_VERIFY
  21053. int verify = 0;
  21054. #endif
  21055. word32 siglen = ECC_BUFSIZE;
  21056. byte sig[ECC_BUFSIZE];
  21057. byte adjustedSig[ECC_BUFSIZE+1];
  21058. byte digest[] = TEST_STRING;
  21059. word32 digestlen = (word32)TEST_STRING_SZ;
  21060. /* Init stack var */
  21061. XMEMSET(&key, 0, sizeof(ecc_key));
  21062. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21063. XMEMSET(sig, 0, siglen);
  21064. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21065. /* Init structs. */
  21066. ExpectIntEQ(wc_ecc_init(&key), 0);
  21067. ExpectIntEQ(wc_InitRng(&rng), 0);
  21068. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21069. #if defined(WOLFSSL_ASYNC_CRYPT)
  21070. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21071. #endif
  21072. ExpectIntEQ(ret, 0);
  21073. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21074. 0);
  21075. /* Check bad args. */
  21076. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21077. ECC_BAD_ARG_E);
  21078. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21079. ECC_BAD_ARG_E);
  21080. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21081. ECC_BAD_ARG_E);
  21082. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21083. ECC_BAD_ARG_E);
  21084. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21085. ECC_BAD_ARG_E);
  21086. #ifdef HAVE_ECC_VERIFY
  21087. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21088. &key), 0);
  21089. ExpectIntEQ(verify, 1);
  21090. /* test check on length of signature passed in */
  21091. XMEMCPY(adjustedSig, sig, siglen);
  21092. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21093. #ifndef NO_STRICT_ECDSA_LEN
  21094. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21095. &verify, &key), 0);
  21096. #else
  21097. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21098. * is allowed */
  21099. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21100. &verify, &key), 0);
  21101. #endif
  21102. /* Test bad args. */
  21103. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21104. &key), ECC_BAD_ARG_E);
  21105. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21106. ECC_BAD_ARG_E);
  21107. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21108. ECC_BAD_ARG_E);
  21109. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21110. NULL), ECC_BAD_ARG_E);
  21111. #endif /* HAVE_ECC_VERIFY */
  21112. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21113. wc_ecc_free(&key);
  21114. #ifdef FP_ECC
  21115. wc_ecc_fp_free();
  21116. #endif
  21117. #endif
  21118. return EXPECT_RESULT();
  21119. } /* END test_wc_ecc_sign_hash */
  21120. /*
  21121. * Testing wc_ecc_shared_secret()
  21122. */
  21123. static int test_wc_ecc_shared_secret(void)
  21124. {
  21125. EXPECT_DECLS;
  21126. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21127. ecc_key key;
  21128. ecc_key pubKey;
  21129. WC_RNG rng;
  21130. #if defined(NO_ECC256)
  21131. int ret;
  21132. #endif
  21133. byte out[KEY32];
  21134. int keySz = sizeof(out);
  21135. word32 outlen = (word32)sizeof(out);
  21136. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21137. const char* qx =
  21138. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21139. const char* qy =
  21140. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21141. const char* d =
  21142. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21143. const char* curveName = "SECP256R1";
  21144. const byte expected_shared_secret[] =
  21145. {
  21146. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21147. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21148. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21149. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21150. };
  21151. #endif
  21152. PRIVATE_KEY_UNLOCK();
  21153. /* Initialize variables. */
  21154. XMEMSET(&key, 0, sizeof(ecc_key));
  21155. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21156. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21157. XMEMSET(out, 0, keySz);
  21158. ExpectIntEQ(wc_ecc_init(&key), 0);
  21159. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21160. ExpectIntEQ(wc_InitRng(&rng), 0);
  21161. #if !defined(NO_ECC256)
  21162. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21163. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21164. #else
  21165. ret = wc_ecc_make_key(&rng, keySz, &key);
  21166. #if defined(WOLFSSL_ASYNC_CRYPT)
  21167. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21168. #endif
  21169. ExpectIntEQ(ret, 0);
  21170. ret = wc_ecc_make_key(&rng, keySz, &key);
  21171. #if defined(WOLFSSL_ASYNC_CRYPT)
  21172. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21173. #endif
  21174. ExpectIntEQ(ret, 0);
  21175. #endif
  21176. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21177. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21178. !defined(HAVE_SELFTEST)
  21179. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21180. #endif
  21181. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21182. #if !defined(NO_ECC256)
  21183. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21184. #endif
  21185. /* Test bad args. */
  21186. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21187. BAD_FUNC_ARG);
  21188. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21189. BAD_FUNC_ARG);
  21190. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21191. BAD_FUNC_ARG);
  21192. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21193. BAD_FUNC_ARG);
  21194. /* Invalid length */
  21195. outlen = 1;
  21196. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21197. BUFFER_E);
  21198. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21199. wc_ecc_free(&pubKey);
  21200. wc_ecc_free(&key);
  21201. #ifdef FP_ECC
  21202. wc_ecc_fp_free();
  21203. #endif
  21204. PRIVATE_KEY_LOCK();
  21205. #endif
  21206. return EXPECT_RESULT();
  21207. } /* END tests_wc_ecc_shared_secret */
  21208. /*
  21209. * testint wc_ecc_export_x963()
  21210. */
  21211. static int test_wc_ecc_export_x963(void)
  21212. {
  21213. EXPECT_DECLS;
  21214. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21215. ecc_key key;
  21216. WC_RNG rng;
  21217. byte out[ECC_ASN963_MAX_BUF_SZ];
  21218. word32 outlen = sizeof(out);
  21219. int ret;
  21220. PRIVATE_KEY_UNLOCK();
  21221. /* Initialize variables. */
  21222. XMEMSET(&key, 0, sizeof(ecc_key));
  21223. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21224. XMEMSET(out, 0, outlen);
  21225. ExpectIntEQ(wc_ecc_init(&key), 0);
  21226. ExpectIntEQ(wc_InitRng(&rng), 0);
  21227. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21228. #if defined(WOLFSSL_ASYNC_CRYPT)
  21229. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21230. #endif
  21231. ExpectIntEQ(ret, 0);
  21232. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21233. /* Test bad args. */
  21234. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21235. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21236. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21237. key.idx = -4;
  21238. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21239. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21240. wc_ecc_free(&key);
  21241. #ifdef FP_ECC
  21242. wc_ecc_fp_free();
  21243. #endif
  21244. PRIVATE_KEY_LOCK();
  21245. #endif
  21246. return EXPECT_RESULT();
  21247. } /* END test_wc_ecc_export_x963 */
  21248. /*
  21249. * Testing wc_ecc_export_x963_ex()
  21250. * compile with --enable-compkey will use compression.
  21251. */
  21252. static int test_wc_ecc_export_x963_ex(void)
  21253. {
  21254. EXPECT_DECLS;
  21255. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21256. ecc_key key;
  21257. WC_RNG rng;
  21258. int ret;
  21259. byte out[ECC_ASN963_MAX_BUF_SZ];
  21260. word32 outlen = sizeof(out);
  21261. #ifdef HAVE_COMP_KEY
  21262. word32 badOutLen = 5;
  21263. #endif
  21264. /* Init stack variables. */
  21265. XMEMSET(&key, 0, sizeof(ecc_key));
  21266. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21267. XMEMSET(out, 0, outlen);
  21268. ExpectIntEQ(wc_ecc_init(&key), 0);
  21269. ExpectIntEQ(wc_InitRng(&rng), 0);
  21270. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21271. #if defined(WOLFSSL_ASYNC_CRYPT)
  21272. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21273. #endif
  21274. ExpectIntEQ(ret, 0);
  21275. #ifdef HAVE_COMP_KEY
  21276. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21277. #else
  21278. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21279. #endif
  21280. /* Test bad args. */
  21281. #ifdef HAVE_COMP_KEY
  21282. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21283. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21284. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21285. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21286. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21287. #else
  21288. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21289. LENGTH_ONLY_E);
  21290. #endif
  21291. key.idx = -4;
  21292. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21293. #else
  21294. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21295. ECC_BAD_ARG_E);
  21296. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21297. LENGTH_ONLY_E);
  21298. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21299. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21300. ECC_BAD_ARG_E);
  21301. key.idx = -4;
  21302. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21303. ECC_BAD_ARG_E);
  21304. #endif
  21305. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21306. wc_ecc_free(&key);
  21307. #ifdef FP_ECC
  21308. wc_ecc_fp_free();
  21309. #endif
  21310. #endif
  21311. return EXPECT_RESULT();
  21312. } /* END test_wc_ecc_export_x963_ex */
  21313. /*
  21314. * testing wc_ecc_import_x963()
  21315. */
  21316. static int test_wc_ecc_import_x963(void)
  21317. {
  21318. EXPECT_DECLS;
  21319. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21320. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21321. ecc_key pubKey;
  21322. ecc_key key;
  21323. WC_RNG rng;
  21324. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21325. word32 x963Len = (word32)sizeof(x963);
  21326. int ret;
  21327. /* Init stack variables. */
  21328. XMEMSET(&key, 0, sizeof(ecc_key));
  21329. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21330. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21331. XMEMSET(x963, 0, x963Len);
  21332. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21333. ExpectIntEQ(wc_ecc_init(&key), 0);
  21334. ExpectIntEQ(wc_InitRng(&rng), 0);
  21335. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21336. #if defined(WOLFSSL_ASYNC_CRYPT)
  21337. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21338. #endif
  21339. ExpectIntEQ(ret, 0);
  21340. PRIVATE_KEY_UNLOCK();
  21341. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21342. PRIVATE_KEY_LOCK();
  21343. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21344. /* Test bad args. */
  21345. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21346. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21347. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21348. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21349. wc_ecc_free(&key);
  21350. wc_ecc_free(&pubKey);
  21351. #ifdef FP_ECC
  21352. wc_ecc_fp_free();
  21353. #endif
  21354. #endif
  21355. return EXPECT_RESULT();
  21356. } /* END wc_ecc_import_x963 */
  21357. /*
  21358. * testing wc_ecc_import_private_key()
  21359. */
  21360. static int test_wc_ecc_import_private_key(void)
  21361. {
  21362. EXPECT_DECLS;
  21363. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21364. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21365. ecc_key key;
  21366. ecc_key keyImp;
  21367. WC_RNG rng;
  21368. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21369. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21370. word32 privKeySz = (word32)sizeof(privKey);
  21371. word32 x963KeySz = (word32)sizeof(x963Key);
  21372. int ret;
  21373. /* Init stack variables. */
  21374. XMEMSET(&key, 0, sizeof(ecc_key));
  21375. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21376. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21377. XMEMSET(privKey, 0, privKeySz);
  21378. XMEMSET(x963Key, 0, x963KeySz);
  21379. ExpectIntEQ(wc_ecc_init(&key), 0);
  21380. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21381. ExpectIntEQ(wc_InitRng(&rng), 0);
  21382. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21383. #if defined(WOLFSSL_ASYNC_CRYPT)
  21384. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21385. #endif
  21386. ExpectIntEQ(ret, 0);
  21387. PRIVATE_KEY_UNLOCK();
  21388. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21389. PRIVATE_KEY_LOCK();
  21390. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21391. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21392. x963KeySz, &keyImp), 0);
  21393. /* Pass in bad args. */
  21394. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21395. x963KeySz, NULL), BAD_FUNC_ARG);
  21396. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21397. &keyImp), BAD_FUNC_ARG);
  21398. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21399. wc_ecc_free(&keyImp);
  21400. wc_ecc_free(&key);
  21401. #ifdef FP_ECC
  21402. wc_ecc_fp_free();
  21403. #endif
  21404. #endif
  21405. return EXPECT_RESULT();
  21406. } /* END test_wc_ecc_import_private_key */
  21407. /*
  21408. * Testing wc_ecc_export_private_only()
  21409. */
  21410. static int test_wc_ecc_export_private_only(void)
  21411. {
  21412. EXPECT_DECLS;
  21413. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21414. ecc_key key;
  21415. WC_RNG rng;
  21416. byte out[ECC_PRIV_KEY_BUF];
  21417. word32 outlen = sizeof(out);
  21418. int ret;
  21419. /* Init stack variables. */
  21420. XMEMSET(&key, 0, sizeof(ecc_key));
  21421. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21422. XMEMSET(out, 0, outlen);
  21423. ExpectIntEQ(wc_ecc_init(&key), 0);
  21424. ExpectIntEQ(wc_InitRng(&rng), 0);
  21425. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21426. #if defined(WOLFSSL_ASYNC_CRYPT)
  21427. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21428. #endif
  21429. ExpectIntEQ(ret, 0);
  21430. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  21431. /* Pass in bad args. */
  21432. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21433. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21434. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21435. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21436. wc_ecc_free(&key);
  21437. #ifdef FP_ECC
  21438. wc_ecc_fp_free();
  21439. #endif
  21440. #endif
  21441. return EXPECT_RESULT();
  21442. } /* END test_wc_ecc_export_private_only */
  21443. /*
  21444. * Testing wc_ecc_rs_to_sig()
  21445. */
  21446. static int test_wc_ecc_rs_to_sig(void)
  21447. {
  21448. EXPECT_DECLS;
  21449. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21450. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21451. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21452. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21453. const char* zeroStr = "0";
  21454. byte sig[ECC_MAX_SIG_SIZE];
  21455. word32 siglen = (word32)sizeof(sig);
  21456. /* R and S max size is the order of curve. 2^192.*/
  21457. int keySz = KEY24;
  21458. byte r[KEY24];
  21459. byte s[KEY24];
  21460. word32 rlen = (word32)sizeof(r);
  21461. word32 slen = (word32)sizeof(s);
  21462. /* Init stack variables. */
  21463. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21464. XMEMSET(r, 0, keySz);
  21465. XMEMSET(s, 0, keySz);
  21466. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21467. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21468. /* Test bad args. */
  21469. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21470. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21471. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21472. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21473. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21474. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21475. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21476. ECC_BAD_ARG_E);
  21477. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21478. ECC_BAD_ARG_E);
  21479. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21480. ECC_BAD_ARG_E);
  21481. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21482. ECC_BAD_ARG_E);
  21483. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21484. ECC_BAD_ARG_E);
  21485. #endif
  21486. return EXPECT_RESULT();
  21487. } /* END test_wc_ecc_rs_to_sig */
  21488. static int test_wc_ecc_import_raw(void)
  21489. {
  21490. EXPECT_DECLS;
  21491. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21492. ecc_key key;
  21493. const char* qx =
  21494. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21495. const char* qy =
  21496. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21497. const char* d =
  21498. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21499. const char* curveName = "SECP256R1";
  21500. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21501. const char* kNullStr = "";
  21502. int ret;
  21503. #endif
  21504. XMEMSET(&key, 0, sizeof(ecc_key));
  21505. ExpectIntEQ(wc_ecc_init(&key), 0);
  21506. /* Test good import */
  21507. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21508. /* Test bad args. */
  21509. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21510. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21511. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21512. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21513. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21514. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21515. wc_ecc_free(&key);
  21516. #endif
  21517. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21518. curveName), 0);
  21519. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21520. #endif
  21521. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21522. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21523. wc_ecc_free(&key);
  21524. #endif
  21525. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21526. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21527. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21528. #else
  21529. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21530. #endif
  21531. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21532. wc_ecc_free(&key);
  21533. #endif
  21534. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21535. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21536. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21537. #else
  21538. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21539. #endif
  21540. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21541. wc_ecc_free(&key);
  21542. #endif
  21543. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21544. #endif
  21545. wc_ecc_free(&key);
  21546. #endif
  21547. return EXPECT_RESULT();
  21548. } /* END test_wc_ecc_import_raw */
  21549. static int test_wc_ecc_import_unsigned(void)
  21550. {
  21551. EXPECT_DECLS;
  21552. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21553. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  21554. HAVE_FIPS_VERSION >= 2))
  21555. ecc_key key;
  21556. const byte qx[] = {
  21557. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21558. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21559. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21560. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21561. };
  21562. const byte qy[] = {
  21563. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21564. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21565. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21566. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21567. };
  21568. const byte d[] = {
  21569. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21570. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21571. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21572. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21573. };
  21574. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21575. const byte nullBytes[32] = {0};
  21576. int ret;
  21577. #endif
  21578. int curveId = ECC_SECP256R1;
  21579. XMEMSET(&key, 0, sizeof(ecc_key));
  21580. ExpectIntEQ(wc_ecc_init(&key), 0);
  21581. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21582. curveId), 0);
  21583. /* Test bad args. */
  21584. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21585. curveId), BAD_FUNC_ARG);
  21586. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21587. curveId), BAD_FUNC_ARG);
  21588. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21589. curveId), BAD_FUNC_ARG);
  21590. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21591. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  21592. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21593. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21594. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  21595. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21596. #endif
  21597. wc_ecc_free(&key);
  21598. #endif
  21599. return EXPECT_RESULT();
  21600. } /* END test_wc_ecc_import_unsigned */
  21601. /*
  21602. * Testing wc_ecc_sig_size()
  21603. */
  21604. static int test_wc_ecc_sig_size(void)
  21605. {
  21606. EXPECT_DECLS;
  21607. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21608. ecc_key key;
  21609. WC_RNG rng;
  21610. int keySz = KEY16;
  21611. int ret;
  21612. XMEMSET(&rng, 0, sizeof(rng));
  21613. XMEMSET(&key, 0, sizeof(key));
  21614. ExpectIntEQ(wc_ecc_init(&key), 0);
  21615. ExpectIntEQ(wc_InitRng(&rng), 0);
  21616. ret = wc_ecc_make_key(&rng, keySz, &key);
  21617. #if defined(WOLFSSL_ASYNC_CRYPT)
  21618. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21619. #endif
  21620. ExpectIntEQ(ret, 0);
  21621. ExpectIntLE(wc_ecc_sig_size(&key),
  21622. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  21623. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21624. wc_ecc_free(&key);
  21625. #endif
  21626. return EXPECT_RESULT();
  21627. } /* END test_wc_ecc_sig_size */
  21628. /*
  21629. * Testing wc_ecc_ctx_new()
  21630. */
  21631. static int test_wc_ecc_ctx_new(void)
  21632. {
  21633. EXPECT_DECLS;
  21634. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21635. WC_RNG rng;
  21636. ecEncCtx* cli = NULL;
  21637. ecEncCtx* srv = NULL;
  21638. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21639. ExpectIntEQ(wc_InitRng(&rng), 0);
  21640. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21641. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21642. wc_ecc_ctx_free(cli);
  21643. cli = NULL;
  21644. wc_ecc_ctx_free(srv);
  21645. /* Test bad args. */
  21646. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21647. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  21648. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  21649. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21650. wc_ecc_ctx_free(cli);
  21651. #endif
  21652. return EXPECT_RESULT();
  21653. } /* END test_wc_ecc_ctx_new */
  21654. /*
  21655. * Tesing wc_ecc_reset()
  21656. */
  21657. static int test_wc_ecc_ctx_reset(void)
  21658. {
  21659. EXPECT_DECLS;
  21660. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21661. ecEncCtx* ctx = NULL;
  21662. WC_RNG rng;
  21663. XMEMSET(&rng, 0, sizeof(rng));
  21664. ExpectIntEQ(wc_InitRng(&rng), 0);
  21665. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21666. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  21667. /* Pass in bad args. */
  21668. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  21669. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  21670. wc_ecc_ctx_free(ctx);
  21671. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21672. #endif
  21673. return EXPECT_RESULT();
  21674. } /* END test_wc_ecc_ctx_reset */
  21675. /*
  21676. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21677. */
  21678. static int test_wc_ecc_ctx_set_peer_salt(void)
  21679. {
  21680. EXPECT_DECLS;
  21681. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21682. WC_RNG rng;
  21683. ecEncCtx* cliCtx = NULL;
  21684. ecEncCtx* servCtx = NULL;
  21685. const byte* cliSalt = NULL;
  21686. const byte* servSalt = NULL;
  21687. XMEMSET(&rng, 0, sizeof(rng));
  21688. ExpectIntEQ(wc_InitRng(&rng), 0);
  21689. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21690. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21691. /* Test bad args. */
  21692. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  21693. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  21694. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  21695. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  21696. /* Test bad args. */
  21697. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  21698. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  21699. wc_ecc_ctx_free(cliCtx);
  21700. wc_ecc_ctx_free(servCtx);
  21701. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21702. #endif
  21703. return EXPECT_RESULT();
  21704. } /* END test_wc_ecc_ctx_set_peer_salt */
  21705. /*
  21706. * Testing wc_ecc_ctx_set_info()
  21707. */
  21708. static int test_wc_ecc_ctx_set_info(void)
  21709. {
  21710. EXPECT_DECLS;
  21711. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21712. ecEncCtx* ctx = NULL;
  21713. WC_RNG rng;
  21714. const char* optInfo = "Optional Test Info.";
  21715. int optInfoSz = (int)XSTRLEN(optInfo);
  21716. const char* badOptInfo = NULL;
  21717. XMEMSET(&rng, 0, sizeof(rng));
  21718. ExpectIntEQ(wc_InitRng(&rng), 0);
  21719. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21720. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  21721. /* Test bad args. */
  21722. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  21723. BAD_FUNC_ARG);
  21724. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  21725. BAD_FUNC_ARG);
  21726. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  21727. wc_ecc_ctx_free(ctx);
  21728. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21729. #endif
  21730. return EXPECT_RESULT();
  21731. } /* END test_wc_ecc_ctx_set_info */
  21732. /*
  21733. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  21734. */
  21735. static int test_wc_ecc_encryptDecrypt(void)
  21736. {
  21737. EXPECT_DECLS;
  21738. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  21739. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21740. ecc_key srvKey;
  21741. ecc_key cliKey;
  21742. ecc_key tmpKey;
  21743. WC_RNG rng;
  21744. int ret;
  21745. const char* msg = "EccBlock Size 16";
  21746. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  21747. #ifdef WOLFSSL_ECIES_OLD
  21748. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21749. #elif defined(WOLFSSL_ECIES_GEN_IV)
  21750. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  21751. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21752. #else
  21753. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  21754. WC_SHA256_DIGEST_SIZE];
  21755. #endif
  21756. word32 outSz = (word32)sizeof(out);
  21757. byte plain[sizeof("EccBlock Size 16")];
  21758. word32 plainSz = (word32)sizeof(plain);
  21759. int keySz = KEY20;
  21760. /* Init stack variables. */
  21761. XMEMSET(out, 0, outSz);
  21762. XMEMSET(plain, 0, plainSz);
  21763. XMEMSET(&rng, 0, sizeof(rng));
  21764. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  21765. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  21766. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  21767. ExpectIntEQ(wc_InitRng(&rng), 0);
  21768. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  21769. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  21770. #if defined(WOLFSSL_ASYNC_CRYPT)
  21771. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21772. #endif
  21773. ExpectIntEQ(ret, 0);
  21774. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  21775. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  21776. #if defined(WOLFSSL_ASYNC_CRYPT)
  21777. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21778. #endif
  21779. ExpectIntEQ(ret, 0);
  21780. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  21781. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21782. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21783. !defined(HAVE_SELFTEST)
  21784. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  21785. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  21786. #endif
  21787. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21788. &outSz, NULL), 0);
  21789. /* Test bad args. */
  21790. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  21791. NULL), BAD_FUNC_ARG);
  21792. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  21793. NULL), BAD_FUNC_ARG);
  21794. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  21795. NULL), BAD_FUNC_ARG);
  21796. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  21797. &outSz, NULL), BAD_FUNC_ARG);
  21798. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  21799. NULL), BAD_FUNC_ARG);
  21800. #ifdef WOLFSSL_ECIES_OLD
  21801. tmpKey.dp = cliKey.dp;
  21802. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  21803. #endif
  21804. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  21805. NULL), 0);
  21806. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  21807. NULL), BAD_FUNC_ARG);
  21808. #ifdef WOLFSSL_ECIES_OLD
  21809. /* NULL parameter allowed in new implementations - public key comes from
  21810. * the message. */
  21811. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  21812. NULL), BAD_FUNC_ARG);
  21813. #endif
  21814. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  21815. NULL), BAD_FUNC_ARG);
  21816. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  21817. NULL), BAD_FUNC_ARG);
  21818. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  21819. BAD_FUNC_ARG);
  21820. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  21821. wc_ecc_free(&tmpKey);
  21822. wc_ecc_free(&srvKey);
  21823. wc_ecc_free(&cliKey);
  21824. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21825. #endif
  21826. return EXPECT_RESULT();
  21827. } /* END test_wc_ecc_encryptDecrypt */
  21828. /*
  21829. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  21830. */
  21831. static int test_wc_ecc_del_point(void)
  21832. {
  21833. EXPECT_DECLS;
  21834. #if defined(HAVE_ECC)
  21835. ecc_point* pt = NULL;
  21836. ExpectNotNull(pt = wc_ecc_new_point());
  21837. wc_ecc_del_point(pt);
  21838. #endif
  21839. return EXPECT_RESULT();
  21840. } /* END test_wc_ecc_del_point */
  21841. /*
  21842. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  21843. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  21844. * and wc_ecc_cmp_point()
  21845. */
  21846. static int test_wc_ecc_pointFns(void)
  21847. {
  21848. EXPECT_DECLS;
  21849. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  21850. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21851. !defined(WOLFSSL_ATECC608A)
  21852. ecc_key key;
  21853. WC_RNG rng;
  21854. int ret;
  21855. ecc_point* point = NULL;
  21856. ecc_point* cpypt = NULL;
  21857. int idx = 0;
  21858. int keySz = KEY32;
  21859. byte der[DER_SZ(KEY32)];
  21860. word32 derlenChk = 0;
  21861. word32 derSz = DER_SZ(KEY32);
  21862. /* Init stack variables. */
  21863. XMEMSET(der, 0, derSz);
  21864. XMEMSET(&key, 0, sizeof(ecc_key));
  21865. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21866. ExpectIntEQ(wc_InitRng(&rng), 0);
  21867. ExpectIntEQ(wc_ecc_init(&key), 0);
  21868. ret = wc_ecc_make_key(&rng, keySz, &key);
  21869. #if defined(WOLFSSL_ASYNC_CRYPT)
  21870. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21871. #endif
  21872. ExpectIntEQ(ret, 0);
  21873. ExpectNotNull(point = wc_ecc_new_point());
  21874. ExpectNotNull(cpypt = wc_ecc_new_point());
  21875. /* Export */
  21876. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  21877. &derlenChk), LENGTH_ONLY_E);
  21878. /* Check length value. */
  21879. ExpectIntEQ(derSz, derlenChk);
  21880. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21881. &derSz), 0);
  21882. /* Test bad args. */
  21883. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  21884. ECC_BAD_ARG_E);
  21885. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  21886. ECC_BAD_ARG_E);
  21887. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21888. NULL), ECC_BAD_ARG_E);
  21889. /* Import */
  21890. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  21891. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  21892. /* Test bad args. */
  21893. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  21894. ECC_BAD_ARG_E);
  21895. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  21896. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  21897. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  21898. ECC_BAD_ARG_E);
  21899. /* Copy */
  21900. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  21901. /* Test bad args. */
  21902. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  21903. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  21904. /* Compare point */
  21905. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  21906. /* Test bad args. */
  21907. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  21908. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  21909. /* At infinity if return == 1, otherwise return == 0. */
  21910. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  21911. /* Test bad args. */
  21912. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  21913. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21914. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  21915. #ifdef USE_ECC_B_PARAM
  21916. /* On curve if ret == 0 */
  21917. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  21918. /* Test bad args. */
  21919. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  21920. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  21921. #endif /* USE_ECC_B_PARAM */
  21922. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  21923. /* Free */
  21924. wc_ecc_del_point(point);
  21925. wc_ecc_del_point(cpypt);
  21926. wc_ecc_free(&key);
  21927. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21928. #endif
  21929. return EXPECT_RESULT();
  21930. } /* END test_wc_ecc_pointFns */
  21931. /*
  21932. * Testing wc_ecc_shared_secret_ssh()
  21933. */
  21934. static int test_wc_ecc_shared_secret_ssh(void)
  21935. {
  21936. EXPECT_DECLS;
  21937. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  21938. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21939. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  21940. !defined(WOLFSSL_CRYPTOCELL)
  21941. ecc_key key;
  21942. ecc_key key2;
  21943. WC_RNG rng;
  21944. int ret;
  21945. int keySz = KEY32;
  21946. int key2Sz = KEY24;
  21947. byte secret[KEY32];
  21948. word32 secretLen = keySz;
  21949. /* Init stack variables. */
  21950. XMEMSET(&key, 0, sizeof(ecc_key));
  21951. XMEMSET(&key2, 0, sizeof(ecc_key));
  21952. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21953. XMEMSET(secret, 0, secretLen);
  21954. /* Make keys */
  21955. ExpectIntEQ(wc_ecc_init(&key), 0);
  21956. ExpectIntEQ(wc_InitRng(&rng), 0);
  21957. ret = wc_ecc_make_key(&rng, keySz, &key);
  21958. #if defined(WOLFSSL_ASYNC_CRYPT)
  21959. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21960. #endif
  21961. ExpectIntEQ(ret, 0);
  21962. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21963. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21964. ExpectIntEQ(wc_InitRng(&rng), 0);
  21965. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  21966. #if defined(WOLFSSL_ASYNC_CRYPT)
  21967. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  21968. #endif
  21969. ExpectIntEQ(ret, 0);
  21970. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21971. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21972. !defined(HAVE_SELFTEST)
  21973. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21974. #endif
  21975. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21976. &secretLen), 0);
  21977. /* Pass in bad args. */
  21978. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  21979. &secretLen), BAD_FUNC_ARG);
  21980. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  21981. BAD_FUNC_ARG);
  21982. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  21983. BAD_FUNC_ARG);
  21984. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  21985. BAD_FUNC_ARG);
  21986. key.type = ECC_PUBLICKEY;
  21987. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21988. &secretLen), ECC_BAD_ARG_E);
  21989. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21990. wc_ecc_free(&key);
  21991. wc_ecc_free(&key2);
  21992. #ifdef FP_ECC
  21993. wc_ecc_fp_free();
  21994. #endif
  21995. #endif
  21996. return EXPECT_RESULT();
  21997. } /* END test_wc_ecc_shared_secret_ssh */
  21998. /*
  21999. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22000. */
  22001. static int test_wc_ecc_verify_hash_ex(void)
  22002. {
  22003. EXPECT_DECLS;
  22004. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22005. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22006. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22007. ecc_key key;
  22008. WC_RNG rng;
  22009. int ret;
  22010. mp_int r;
  22011. mp_int s;
  22012. mp_int z;
  22013. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22014. unsigned char iHash[] = "Everyone gets Friday off.......";
  22015. unsigned char shortHash[] = TEST_STRING;
  22016. word32 hashlen = sizeof(hash);
  22017. word32 iHashLen = sizeof(iHash);
  22018. word32 shortHashLen = sizeof(shortHash);
  22019. int keySz = KEY32;
  22020. int verify_ok = 0;
  22021. XMEMSET(&key, 0, sizeof(ecc_key));
  22022. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22023. XMEMSET(&r, 0, sizeof(mp_int));
  22024. XMEMSET(&s, 0, sizeof(mp_int));
  22025. XMEMSET(&z, 0, sizeof(mp_int));
  22026. /* Initialize r, s and z. */
  22027. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22028. ExpectIntEQ(wc_ecc_init(&key), 0);
  22029. ExpectIntEQ(wc_InitRng(&rng), 0);
  22030. ret = wc_ecc_make_key(&rng, keySz, &key);
  22031. #if defined(WOLFSSL_ASYNC_CRYPT)
  22032. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22033. #endif
  22034. ExpectIntEQ(ret, 0);
  22035. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22036. /* verify_ok should be 1. */
  22037. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22038. 0);
  22039. ExpectIntEQ(verify_ok, 1);
  22040. /* verify_ok should be 0 */
  22041. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22042. &key), 0);
  22043. ExpectIntEQ(verify_ok, 0);
  22044. /* verify_ok should be 0. */
  22045. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22046. &verify_ok, &key), 0);
  22047. ExpectIntEQ(verify_ok, 0);
  22048. /* Test bad args. */
  22049. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22050. ECC_BAD_ARG_E);
  22051. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22052. ECC_BAD_ARG_E);
  22053. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22054. ECC_BAD_ARG_E);
  22055. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22056. ECC_BAD_ARG_E);
  22057. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22058. ECC_BAD_ARG_E);
  22059. /* Test bad args. */
  22060. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22061. &verify_ok, &key), ECC_BAD_ARG_E);
  22062. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22063. &verify_ok, &key), ECC_BAD_ARG_E);
  22064. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22065. &verify_ok, &key), MP_ZERO_E);
  22066. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22067. &verify_ok, &key), MP_ZERO_E);
  22068. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22069. &verify_ok, &key), MP_ZERO_E);
  22070. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22071. &key), ECC_BAD_ARG_E);
  22072. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22073. &key), ECC_BAD_ARG_E);
  22074. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22075. &verify_ok, NULL), ECC_BAD_ARG_E);
  22076. wc_ecc_free(&key);
  22077. mp_free(&r);
  22078. mp_free(&s);
  22079. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22080. #endif
  22081. return EXPECT_RESULT();
  22082. } /* END test_wc_ecc_verify_hash_ex */
  22083. /*
  22084. * Testing wc_ecc_mulmod()
  22085. */
  22086. static int test_wc_ecc_mulmod(void)
  22087. {
  22088. EXPECT_DECLS;
  22089. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22090. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22091. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22092. ecc_key key1;
  22093. ecc_key key2;
  22094. ecc_key key3;
  22095. WC_RNG rng;
  22096. int ret;
  22097. XMEMSET(&key1, 0, sizeof(ecc_key));
  22098. XMEMSET(&key2, 0, sizeof(ecc_key));
  22099. XMEMSET(&key3, 0, sizeof(ecc_key));
  22100. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22101. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22102. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22103. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22104. ExpectIntEQ(wc_InitRng(&rng), 0);
  22105. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22106. #if defined(WOLFSSL_ASYNC_CRYPT)
  22107. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22108. #endif
  22109. ExpectIntEQ(ret, 0);
  22110. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22111. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22112. key1.dp->Af, ECC_SECP256R1), 0);
  22113. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22114. key1.dp->prime, ECC_SECP256R1), 0);
  22115. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22116. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22117. 1), 0);
  22118. /* Test bad args. */
  22119. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22120. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22121. ECC_BAD_ARG_E);
  22122. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22123. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22124. ECC_BAD_ARG_E);
  22125. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22126. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22127. ECC_BAD_ARG_E);
  22128. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22129. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22130. wc_ecc_free(&key1);
  22131. wc_ecc_free(&key2);
  22132. wc_ecc_free(&key3);
  22133. #ifdef FP_ECC
  22134. wc_ecc_fp_free();
  22135. #endif
  22136. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22137. return EXPECT_RESULT();
  22138. } /* END test_wc_ecc_mulmod */
  22139. /*
  22140. * Testing wc_ecc_is_valid_idx()
  22141. */
  22142. static int test_wc_ecc_is_valid_idx(void)
  22143. {
  22144. EXPECT_DECLS;
  22145. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22146. ecc_key key;
  22147. WC_RNG rng;
  22148. int ret;
  22149. int iVal = -2;
  22150. int iVal2 = 3000;
  22151. XMEMSET(&key, 0, sizeof(ecc_key));
  22152. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22153. ExpectIntEQ(wc_ecc_init(&key), 0);
  22154. ExpectIntEQ(wc_InitRng(&rng), 0);
  22155. ret = wc_ecc_make_key(&rng, 32, &key);
  22156. #if defined(WOLFSSL_ASYNC_CRYPT)
  22157. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22158. #endif
  22159. ExpectIntEQ(ret, 0);
  22160. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22161. /* Test bad args. */
  22162. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22163. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22164. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22165. wc_ecc_free(&key);
  22166. #ifdef FP_ECC
  22167. wc_ecc_fp_free();
  22168. #endif
  22169. #endif
  22170. return EXPECT_RESULT();
  22171. } /* END test_wc_ecc_is_valid_idx */
  22172. /*
  22173. * Testing wc_ecc_get_curve_id_from_oid()
  22174. */
  22175. static int test_wc_ecc_get_curve_id_from_oid(void)
  22176. {
  22177. EXPECT_DECLS;
  22178. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22179. !defined(HAVE_FIPS)
  22180. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22181. word32 len = sizeof(oid);
  22182. /* Bad Cases */
  22183. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22184. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22185. /* Good Case */
  22186. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22187. #endif
  22188. return EXPECT_RESULT();
  22189. } /* END test_wc_ecc_get_curve_id_from_oid */
  22190. /*
  22191. * Testing wc_ecc_sig_size_calc()
  22192. */
  22193. static int test_wc_ecc_sig_size_calc(void)
  22194. {
  22195. EXPECT_DECLS;
  22196. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22197. ecc_key key;
  22198. WC_RNG rng;
  22199. int sz = 0;
  22200. int ret;
  22201. XMEMSET(&key, 0, sizeof(ecc_key));
  22202. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22203. ExpectIntEQ(wc_ecc_init(&key), 0);
  22204. ExpectIntEQ(wc_InitRng(&rng), 0);
  22205. ret = wc_ecc_make_key(&rng, 16, &key);
  22206. #if defined(WOLFSSL_ASYNC_CRYPT)
  22207. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22208. #endif
  22209. ExpectIntEQ(ret, 0);
  22210. sz = key.dp->size;
  22211. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22212. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22213. wc_ecc_free(&key);
  22214. #endif
  22215. return EXPECT_RESULT();
  22216. } /* END test_wc_ecc_sig_size_calc */
  22217. /*
  22218. * Testing wc_ecc_sm2_make_key()
  22219. */
  22220. static int test_wc_ecc_sm2_make_key(void)
  22221. {
  22222. int res = TEST_SKIPPED;
  22223. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22224. EXPECT_DECLS;
  22225. WC_RNG rng[1];
  22226. ecc_key key[1];
  22227. XMEMSET(rng, 0, sizeof(*rng));
  22228. XMEMSET(key, 0, sizeof(*key));
  22229. ExpectIntEQ(wc_InitRng(rng), 0);
  22230. ExpectIntEQ(wc_ecc_init(key), 0);
  22231. /* Test invalid parameters. */
  22232. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22233. BAD_FUNC_ARG);
  22234. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22235. BAD_FUNC_ARG);
  22236. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22237. BAD_FUNC_ARG);
  22238. /* Test valid parameters. */
  22239. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22240. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22241. wc_ecc_free(key);
  22242. wc_FreeRng(rng);
  22243. #ifdef FP_ECC
  22244. wc_ecc_fp_free();
  22245. #endif
  22246. res = EXPECT_RESULT();
  22247. #endif
  22248. return res;
  22249. }
  22250. /*
  22251. * Testing wc_ecc_sm2_shared_secret()
  22252. */
  22253. static int test_wc_ecc_sm2_shared_secret(void)
  22254. {
  22255. int res = TEST_SKIPPED;
  22256. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22257. EXPECT_DECLS;
  22258. WC_RNG rng[1];
  22259. ecc_key keyA[1];
  22260. ecc_key keyB[1];
  22261. byte outA[32];
  22262. byte outB[32];
  22263. word32 outALen = 32;
  22264. word32 outBLen = 32;
  22265. XMEMSET(rng, 0, sizeof(*rng));
  22266. XMEMSET(keyA, 0, sizeof(*keyA));
  22267. XMEMSET(keyB, 0, sizeof(*keyB));
  22268. ExpectIntEQ(wc_InitRng(rng), 0);
  22269. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22270. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22271. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22272. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22273. #ifdef ECC_TIMING_RESISTANT
  22274. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22275. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22276. #endif
  22277. /* Test invalid parameters. */
  22278. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22279. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22280. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22281. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22282. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22283. BAD_FUNC_ARG);
  22284. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22285. BAD_FUNC_ARG);
  22286. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22287. BAD_FUNC_ARG);
  22288. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22289. BAD_FUNC_ARG);
  22290. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22291. /* Test valid parameters. */
  22292. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22293. ExpectIntLE(outALen, 32);
  22294. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22295. ExpectIntLE(outBLen, 32);
  22296. ExpectIntEQ(outALen, outBLen);
  22297. ExpectBufEQ(outA, outB, outALen);
  22298. wc_ecc_free(keyB);
  22299. wc_ecc_free(keyA);
  22300. wc_FreeRng(rng);
  22301. #ifdef FP_ECC
  22302. wc_ecc_fp_free();
  22303. #endif
  22304. res = EXPECT_RESULT();
  22305. #endif
  22306. return res;
  22307. }
  22308. /*
  22309. * Testing wc_ecc_sm2_create_digest()
  22310. */
  22311. static int test_wc_ecc_sm2_create_digest(void)
  22312. {
  22313. int res = TEST_SKIPPED;
  22314. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22315. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22316. EXPECT_DECLS;
  22317. ecc_key key[1];
  22318. enum wc_HashType hashType;
  22319. unsigned char pub[] = {
  22320. 0x04,
  22321. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22322. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22323. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22324. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22325. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22326. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22327. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22328. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22329. };
  22330. unsigned char id[] = {
  22331. 0x01, 0x02, 0x03,
  22332. };
  22333. unsigned char msg[] = {
  22334. 0x01, 0x02, 0x03,
  22335. };
  22336. unsigned char hash[32];
  22337. #ifdef WOLFSSL_SM3
  22338. unsigned char expHash[32] = {
  22339. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22340. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22341. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22342. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22343. };
  22344. #else
  22345. unsigned char expHash[32] = {
  22346. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22347. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22348. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22349. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22350. };
  22351. #endif
  22352. #ifdef WOLFSSL_SM3
  22353. hashType = WC_HASH_TYPE_SM3;
  22354. #else
  22355. hashType = WC_HASH_TYPE_SHA256;
  22356. #endif
  22357. XMEMSET(key, 0, sizeof(*key));
  22358. ExpectIntEQ(wc_ecc_init(key), 0);
  22359. /* Test with no curve set. */
  22360. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22361. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22362. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22363. /* Test invalid parameters. */
  22364. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22365. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22366. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22367. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22368. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22369. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22370. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22371. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22372. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22373. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22374. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22375. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22376. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22377. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22378. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22379. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22380. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22381. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22382. /* Bad hash type. */
  22383. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22384. -1, hash, 0, key), BAD_FUNC_ARG);
  22385. /* Bad hash size. */
  22386. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22387. hashType, hash, 0, key), BUFFER_E);
  22388. /* Test valid parameters. */
  22389. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22390. hashType, hash, sizeof(hash), key), 0);
  22391. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22392. wc_ecc_free(key);
  22393. res = EXPECT_RESULT();
  22394. #endif
  22395. return res;
  22396. }
  22397. /*
  22398. * Testing wc_ecc_sm2_verify_hash_ex()
  22399. */
  22400. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22401. {
  22402. int res = TEST_SKIPPED;
  22403. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22404. defined(WOLFSSL_PUBLIC_MP)
  22405. EXPECT_DECLS;
  22406. ecc_key key[1];
  22407. mp_int r[1];
  22408. mp_int s[1];
  22409. int verified;
  22410. unsigned char pub[] = {
  22411. 0x04,
  22412. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22413. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22414. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22415. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22416. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22417. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22418. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22419. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22420. };
  22421. unsigned char hash[] = {
  22422. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22423. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22424. 0x50, 0x69, 0x5B, 0x20
  22425. };
  22426. unsigned char rData[] = {
  22427. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22428. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22429. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22430. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  22431. };
  22432. unsigned char sData[] = {
  22433. 0x1D,
  22434. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22435. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22436. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22437. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22438. };
  22439. unsigned char rBadData[] = {
  22440. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22441. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22442. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22443. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22444. };
  22445. XMEMSET(key, 0, sizeof(*key));
  22446. XMEMSET(r, 0, sizeof(*r));
  22447. XMEMSET(s, 0, sizeof(*s));
  22448. ExpectIntEQ(mp_init(r), 0);
  22449. ExpectIntEQ(mp_init(s), 0);
  22450. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22451. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22452. ExpectIntEQ(wc_ecc_init(key), 0);
  22453. /* Test with no curve set. */
  22454. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22455. &verified, key), BAD_FUNC_ARG);
  22456. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22457. /* Test invalid parameters. */
  22458. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22459. NULL, NULL), BAD_FUNC_ARG);
  22460. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22461. NULL, NULL), BAD_FUNC_ARG);
  22462. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22463. NULL, NULL), BAD_FUNC_ARG);
  22464. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22465. NULL, NULL), BAD_FUNC_ARG);
  22466. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22467. &verified, NULL), BAD_FUNC_ARG);
  22468. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22469. NULL, key), BAD_FUNC_ARG);
  22470. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22471. &verified, key), BAD_FUNC_ARG);
  22472. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22473. &verified, key), BAD_FUNC_ARG);
  22474. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22475. &verified, key), BAD_FUNC_ARG);
  22476. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22477. NULL, key), BAD_FUNC_ARG);
  22478. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22479. &verified, NULL), BAD_FUNC_ARG);
  22480. /* Make key not on the SM2 curve. */
  22481. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22482. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22483. &verified, key), BAD_FUNC_ARG);
  22484. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22485. /* Test valid parameters. */
  22486. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22487. &verified, key), 0);
  22488. ExpectIntEQ(verified, 1);
  22489. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22490. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22491. &verified, key), 0);
  22492. ExpectIntEQ(verified, 0);
  22493. mp_free(s);
  22494. mp_free(r);
  22495. wc_ecc_free(key);
  22496. #ifdef FP_ECC
  22497. wc_ecc_fp_free();
  22498. #endif
  22499. res = EXPECT_RESULT();
  22500. #endif
  22501. return res;
  22502. }
  22503. /*
  22504. * Testing wc_ecc_sm2_verify_hash()
  22505. */
  22506. static int test_wc_ecc_sm2_verify_hash(void)
  22507. {
  22508. int res = TEST_SKIPPED;
  22509. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22510. EXPECT_DECLS;
  22511. ecc_key key[1];
  22512. int verified;
  22513. unsigned char pub[] = {
  22514. 0x04,
  22515. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22516. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22517. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22518. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22519. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22520. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22521. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22522. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22523. };
  22524. unsigned char hash[] = {
  22525. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22526. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22527. 0x50, 0x69, 0x5B, 0x20
  22528. };
  22529. unsigned char sig[] = {
  22530. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22531. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22532. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22533. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22534. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22535. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22536. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22537. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22538. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22539. };
  22540. unsigned char sigBad[] = {
  22541. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22542. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22543. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22544. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22545. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22546. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22547. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22548. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22549. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  22550. };
  22551. XMEMSET(key, 0, sizeof(*key));
  22552. ExpectIntEQ(wc_ecc_init(key), 0);
  22553. /* Test with no curve set. */
  22554. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22555. &verified, key), BAD_FUNC_ARG);
  22556. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22557. /* Test invalid parameters. */
  22558. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22559. NULL, NULL), BAD_FUNC_ARG);
  22560. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22561. NULL, NULL), BAD_FUNC_ARG);
  22562. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22563. NULL, NULL), BAD_FUNC_ARG);
  22564. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22565. &verified, NULL), BAD_FUNC_ARG);
  22566. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22567. NULL, key), BAD_FUNC_ARG);
  22568. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22569. &verified, key), BAD_FUNC_ARG);
  22570. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22571. &verified, key), BAD_FUNC_ARG);
  22572. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22573. NULL, key), BAD_FUNC_ARG);
  22574. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22575. &verified, NULL), BAD_FUNC_ARG);
  22576. /* Make key not on the SM2 curve. */
  22577. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22578. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22579. &verified, key), BAD_FUNC_ARG);
  22580. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22581. /* Test valid parameters. */
  22582. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22583. &verified, key), 0);
  22584. ExpectIntEQ(verified, 1);
  22585. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  22586. sizeof(hash), &verified, key), 0);
  22587. ExpectIntEQ(verified, 0);
  22588. wc_ecc_free(key);
  22589. #ifdef FP_ECC
  22590. wc_ecc_fp_free();
  22591. #endif
  22592. res = EXPECT_RESULT();
  22593. #endif
  22594. return res;
  22595. }
  22596. /*
  22597. * Testing wc_ecc_sm2_verify_hash_ex()
  22598. */
  22599. static int test_wc_ecc_sm2_sign_hash_ex(void)
  22600. {
  22601. int res = TEST_SKIPPED;
  22602. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  22603. defined(WOLFSSL_PUBLIC_MP)
  22604. EXPECT_DECLS;
  22605. WC_RNG rng[1];
  22606. ecc_key key[1];
  22607. mp_int r[1];
  22608. mp_int s[1];
  22609. unsigned char hash[32];
  22610. #ifdef HAVE_ECC_VERIFY
  22611. int verified;
  22612. #endif
  22613. XMEMSET(rng, 0, sizeof(*rng));
  22614. XMEMSET(key, 0, sizeof(*key));
  22615. XMEMSET(r, 0, sizeof(*r));
  22616. XMEMSET(s, 0, sizeof(*s));
  22617. ExpectIntEQ(wc_InitRng(rng), 0);
  22618. ExpectIntEQ(mp_init(r), 0);
  22619. ExpectIntEQ(mp_init(s), 0);
  22620. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22621. ExpectIntEQ(wc_ecc_init(key), 0);
  22622. /* Test with no curve set. */
  22623. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22624. BAD_FUNC_ARG);
  22625. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22626. /* Test invalid parameters. */
  22627. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22628. NULL), BAD_FUNC_ARG);
  22629. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  22630. NULL), BAD_FUNC_ARG);
  22631. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  22632. NULL), BAD_FUNC_ARG);
  22633. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  22634. NULL), BAD_FUNC_ARG);
  22635. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  22636. NULL), BAD_FUNC_ARG);
  22637. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22638. s), BAD_FUNC_ARG);
  22639. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  22640. BAD_FUNC_ARG);
  22641. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  22642. BAD_FUNC_ARG);
  22643. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  22644. BAD_FUNC_ARG);
  22645. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  22646. BAD_FUNC_ARG);
  22647. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  22648. BAD_FUNC_ARG);
  22649. /* Make key not on the SM2 curve. */
  22650. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22651. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22652. BAD_FUNC_ARG);
  22653. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22654. #ifdef WOLFSSL_SP_MATH_ALL
  22655. {
  22656. mp_int smallR[1];
  22657. sp_init_size(smallR, 1);
  22658. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  22659. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  22660. smallR, s), 0);
  22661. }
  22662. #endif
  22663. /* Test valid parameters. */
  22664. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22665. 0);
  22666. #ifdef HAVE_ECC_VERIFY
  22667. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  22668. key), 0);
  22669. ExpectIntEQ(verified, 1);
  22670. #endif
  22671. mp_free(s);
  22672. mp_free(r);
  22673. wc_ecc_free(key);
  22674. wc_FreeRng(rng);
  22675. #ifdef FP_ECC
  22676. wc_ecc_fp_free();
  22677. #endif
  22678. res = EXPECT_RESULT();
  22679. #endif
  22680. return res;
  22681. }
  22682. /*
  22683. * Testing wc_ecc_sm2_verify_hash()
  22684. */
  22685. static int test_wc_ecc_sm2_sign_hash(void)
  22686. {
  22687. int res = TEST_SKIPPED;
  22688. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  22689. EXPECT_DECLS;
  22690. WC_RNG rng[1];
  22691. ecc_key key[1];
  22692. unsigned char hash[32];
  22693. unsigned char sig[72];
  22694. word32 sigSz = sizeof(sig);
  22695. #ifdef HAVE_ECC_VERIFY
  22696. int verified;
  22697. #endif
  22698. XMEMSET(rng, 0, sizeof(*rng));
  22699. XMEMSET(key, 0, sizeof(*key));
  22700. ExpectIntEQ(wc_InitRng(rng), 0);
  22701. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22702. ExpectIntEQ(wc_ecc_init(key), 0);
  22703. /* Test with no curve set. */
  22704. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22705. BAD_FUNC_ARG);
  22706. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22707. /* Test invalid parameters. */
  22708. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22709. NULL), BAD_FUNC_ARG);
  22710. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  22711. NULL), BAD_FUNC_ARG);
  22712. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  22713. NULL), BAD_FUNC_ARG);
  22714. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  22715. NULL), BAD_FUNC_ARG);
  22716. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  22717. NULL), BAD_FUNC_ARG);
  22718. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22719. key), BAD_FUNC_ARG);
  22720. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  22721. key), BAD_FUNC_ARG);
  22722. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  22723. key), BAD_FUNC_ARG);
  22724. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  22725. key), BAD_FUNC_ARG);
  22726. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  22727. key), BAD_FUNC_ARG);
  22728. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  22729. NULL), BAD_FUNC_ARG);
  22730. /* Make key not on the SM2 curve. */
  22731. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22732. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22733. BAD_FUNC_ARG);
  22734. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22735. /* Test valid parameters. */
  22736. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22737. 0);
  22738. #ifdef HAVE_ECC_VERIFY
  22739. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  22740. &verified, key), 0);
  22741. ExpectIntEQ(verified, 1);
  22742. #endif
  22743. wc_ecc_free(key);
  22744. wc_FreeRng(rng);
  22745. #ifdef FP_ECC
  22746. wc_ecc_fp_free();
  22747. #endif
  22748. res = EXPECT_RESULT();
  22749. #endif
  22750. return res;
  22751. }
  22752. /*
  22753. * Testing ToTraditional
  22754. */
  22755. static int test_ToTraditional(void)
  22756. {
  22757. EXPECT_DECLS;
  22758. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22759. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22760. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  22761. XFILE f = XBADFILE;
  22762. byte input[TWOK_BUF];
  22763. word32 sz = 0;
  22764. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  22765. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  22766. if (f != XBADFILE)
  22767. XFCLOSE(f);
  22768. /* Good case */
  22769. ExpectIntGT(ToTraditional(input, sz), 0);
  22770. /* Bad cases */
  22771. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  22772. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  22773. #ifdef WOLFSSL_ASN_TEMPLATE
  22774. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  22775. #else
  22776. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  22777. #endif
  22778. #endif
  22779. return EXPECT_RESULT();
  22780. } /* End test_ToTraditional*/
  22781. /*
  22782. * Testing wc_EccPrivateKeyToDer
  22783. */
  22784. static int test_wc_EccPrivateKeyToDer(void)
  22785. {
  22786. EXPECT_DECLS;
  22787. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22788. byte output[ONEK_BUF];
  22789. ecc_key eccKey;
  22790. WC_RNG rng;
  22791. word32 inLen;
  22792. word32 outLen = 0;
  22793. int ret;
  22794. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22795. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22796. ExpectIntEQ(wc_InitRng(&rng), 0);
  22797. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22798. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22799. #if defined(WOLFSSL_ASYNC_CRYPT)
  22800. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22801. #endif
  22802. ExpectIntEQ(ret, 0);
  22803. inLen = (word32)sizeof(output);
  22804. /* Bad Cases */
  22805. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22806. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22807. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  22808. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  22809. /* Good Case */
  22810. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  22811. wc_ecc_free(&eccKey);
  22812. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22813. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  22814. {
  22815. /* test importing private only into a PKEY struct */
  22816. EC_KEY* ec = NULL;
  22817. EVP_PKEY* pkey = NULL;
  22818. const unsigned char* der;
  22819. der = output;
  22820. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  22821. der = output;
  22822. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  22823. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  22824. if (EXPECT_FAIL()) {
  22825. EC_KEY_free(ec);
  22826. }
  22827. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  22828. }
  22829. #endif
  22830. #endif
  22831. return EXPECT_RESULT();
  22832. } /* End test_wc_EccPrivateKeyToDer*/
  22833. /*
  22834. * Testing wc_DhPublicKeyDecode
  22835. */
  22836. static int test_wc_DhPublicKeyDecode(void)
  22837. {
  22838. EXPECT_DECLS;
  22839. #ifndef NO_DH
  22840. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22841. DhKey key;
  22842. word32 inOutIdx;
  22843. XMEMSET(&key, 0, sizeof(DhKey));
  22844. ExpectIntEQ(wc_InitDhKey(&key), 0);
  22845. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  22846. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22847. BAD_FUNC_ARG);
  22848. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22849. BAD_FUNC_ARG);
  22850. inOutIdx = 0;
  22851. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22852. BAD_FUNC_ARG);
  22853. inOutIdx = 0;
  22854. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22855. BAD_FUNC_ARG);
  22856. inOutIdx = 0;
  22857. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22858. sizeof_dh_pub_key_der_2048), 0);
  22859. ExpectIntNE(key.p.used, 0);
  22860. ExpectIntNE(key.g.used, 0);
  22861. ExpectIntEQ(key.q.used, 0);
  22862. ExpectIntNE(key.pub.used, 0);
  22863. ExpectIntEQ(key.priv.used, 0);
  22864. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  22865. #endif
  22866. #endif /* !NO_DH */
  22867. return EXPECT_RESULT();
  22868. }
  22869. /*
  22870. * Testing wc_Ed25519KeyToDer
  22871. */
  22872. static int test_wc_Ed25519KeyToDer(void)
  22873. {
  22874. EXPECT_DECLS;
  22875. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22876. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22877. byte output[ONEK_BUF];
  22878. ed25519_key ed25519Key;
  22879. WC_RNG rng;
  22880. word32 inLen;
  22881. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22882. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22883. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22884. ExpectIntEQ(wc_InitRng(&rng), 0);
  22885. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22886. inLen = (word32)sizeof(output);
  22887. /* Bad Cases */
  22888. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22889. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22890. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  22891. /* Good Cases */
  22892. /* length only */
  22893. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  22894. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  22895. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22896. wc_ed25519_free(&ed25519Key);
  22897. #endif
  22898. return EXPECT_RESULT();
  22899. } /* End test_wc_Ed25519KeyToDer*/
  22900. /*
  22901. * Testing wc_Ed25519PrivateKeyToDer
  22902. */
  22903. static int test_wc_Ed25519PrivateKeyToDer(void)
  22904. {
  22905. EXPECT_DECLS;
  22906. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22907. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22908. byte output[ONEK_BUF];
  22909. ed25519_key ed25519PrivKey;
  22910. WC_RNG rng;
  22911. word32 inLen;
  22912. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  22913. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22914. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  22915. ExpectIntEQ(wc_InitRng(&rng), 0);
  22916. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  22917. 0);
  22918. inLen = (word32)sizeof(output);
  22919. /* Bad Cases */
  22920. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22921. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22922. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  22923. BAD_FUNC_ARG);
  22924. /* Good Cases */
  22925. /* length only */
  22926. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  22927. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  22928. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22929. wc_ed25519_free(&ed25519PrivKey);
  22930. #endif
  22931. return EXPECT_RESULT();
  22932. } /* End test_wc_Ed25519PrivateKeyToDer*/
  22933. /*
  22934. * Testing wc_Ed448KeyToDer
  22935. */
  22936. static int test_wc_Ed448KeyToDer(void)
  22937. {
  22938. EXPECT_DECLS;
  22939. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22940. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22941. byte output[ONEK_BUF];
  22942. ed448_key ed448Key;
  22943. WC_RNG rng;
  22944. word32 inLen;
  22945. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22946. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22947. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22948. ExpectIntEQ(wc_InitRng(&rng), 0);
  22949. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22950. inLen = (word32)sizeof(output);
  22951. /* Bad Cases */
  22952. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22953. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22954. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  22955. /* Good Cases */
  22956. /* length only */
  22957. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  22958. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  22959. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22960. wc_ed448_free(&ed448Key);
  22961. #endif
  22962. return EXPECT_RESULT();
  22963. } /* End test_wc_Ed448KeyToDer*/
  22964. /*
  22965. * Testing wc_Ed448PrivateKeyToDer
  22966. */
  22967. static int test_wc_Ed448PrivateKeyToDer(void)
  22968. {
  22969. EXPECT_DECLS;
  22970. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22971. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22972. byte output[ONEK_BUF];
  22973. ed448_key ed448PrivKey;
  22974. WC_RNG rng;
  22975. word32 inLen;
  22976. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  22977. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22978. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  22979. ExpectIntEQ(wc_InitRng(&rng), 0);
  22980. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  22981. 0);
  22982. inLen = (word32)sizeof(output);
  22983. /* Bad Cases */
  22984. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22985. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22986. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  22987. BAD_FUNC_ARG);
  22988. /* Good cases */
  22989. /* length only */
  22990. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  22991. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  22992. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22993. wc_ed448_free(&ed448PrivKey);
  22994. #endif
  22995. return EXPECT_RESULT();
  22996. } /* End test_wc_Ed448PrivateKeyToDer*/
  22997. /*
  22998. * Testing wc_SetSubjectBuffer
  22999. */
  23000. static int test_wc_SetSubjectBuffer(void)
  23001. {
  23002. EXPECT_DECLS;
  23003. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23004. Cert cert;
  23005. XFILE file = XBADFILE;
  23006. byte* der = NULL;
  23007. word32 derSz;
  23008. derSz = FOURK_BUF;
  23009. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23010. DYNAMIC_TYPE_TMP_BUFFER));
  23011. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23012. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23013. if (file != XBADFILE)
  23014. XFCLOSE(file);
  23015. ExpectIntEQ(wc_InitCert(&cert), 0);
  23016. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  23017. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  23018. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23019. #endif
  23020. return EXPECT_RESULT();
  23021. } /* End test_wc_SetSubjectBuffer*/
  23022. /*
  23023. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23024. */
  23025. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23026. {
  23027. EXPECT_DECLS;
  23028. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23029. WC_RNG rng;
  23030. Cert cert;
  23031. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23032. RsaKey rsaKey;
  23033. int bits = 2048;
  23034. #endif
  23035. #if defined(HAVE_ECC)
  23036. ecc_key eccKey;
  23037. int ret;
  23038. #endif
  23039. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23040. ed25519_key ed25519Key;
  23041. #endif
  23042. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23043. ed448_key ed448Key;
  23044. #endif
  23045. #ifndef HAVE_FIPS
  23046. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23047. #else
  23048. ExpectIntEQ(wc_InitRng(&rng), 0);
  23049. #endif
  23050. ExpectIntEQ(wc_InitCert(&cert), 0);
  23051. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23052. /* RSA */
  23053. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23054. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23055. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23056. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23057. 0);
  23058. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23059. #endif
  23060. #if defined(HAVE_ECC)
  23061. /* ECC */
  23062. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23063. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23064. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23065. #if defined(WOLFSSL_ASYNC_CRYPT)
  23066. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23067. #endif
  23068. ExpectIntEQ(ret, 0);
  23069. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23070. 0);
  23071. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23072. #endif
  23073. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23074. /* ED25519 */
  23075. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23076. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23077. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23078. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23079. &ed25519Key), 0);
  23080. wc_ed25519_free(&ed25519Key);
  23081. #endif
  23082. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23083. /* ED448 */
  23084. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23085. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23086. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23087. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23088. &ed448Key), 0);
  23089. wc_ed448_free(&ed448Key);
  23090. #endif
  23091. wc_FreeRng(&rng);
  23092. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23093. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23094. return EXPECT_RESULT();
  23095. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23096. /*
  23097. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23098. */
  23099. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23100. {
  23101. EXPECT_DECLS;
  23102. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23103. WC_RNG rng;
  23104. Cert cert;
  23105. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23106. RsaKey rsaKey;
  23107. int bits = 2048;
  23108. #endif
  23109. #if defined(HAVE_ECC)
  23110. ecc_key eccKey;
  23111. int ret;
  23112. #endif
  23113. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23114. ed25519_key ed25519Key;
  23115. #endif
  23116. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23117. ed448_key ed448Key;
  23118. #endif
  23119. #ifndef HAVE_FIPS
  23120. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23121. #else
  23122. ExpectIntEQ(wc_InitRng(&rng), 0);
  23123. #endif
  23124. ExpectIntEQ(wc_InitCert(&cert), 0);
  23125. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23126. /* RSA */
  23127. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23128. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23129. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23130. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23131. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23132. #endif
  23133. #if defined(HAVE_ECC)
  23134. /* ECC */
  23135. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23136. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23137. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23138. #if defined(WOLFSSL_ASYNC_CRYPT)
  23139. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23140. #endif
  23141. ExpectIntEQ(ret, 0);
  23142. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23143. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23144. #endif
  23145. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23146. /* ED25519 */
  23147. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23148. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23149. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23150. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23151. &ed25519Key), 0);
  23152. wc_ed25519_free(&ed25519Key);
  23153. #endif
  23154. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23155. /* ED448 */
  23156. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23157. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23158. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23159. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23160. 0);
  23161. wc_ed448_free(&ed448Key);
  23162. #endif
  23163. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23164. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23165. return EXPECT_RESULT();
  23166. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23167. /*
  23168. * Testing wc_PKCS7_New()
  23169. */
  23170. static int test_wc_PKCS7_New(void)
  23171. {
  23172. EXPECT_DECLS;
  23173. #if defined(HAVE_PKCS7)
  23174. PKCS7* pkcs7 = NULL;
  23175. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23176. wc_PKCS7_Free(pkcs7);
  23177. #endif
  23178. return EXPECT_RESULT();
  23179. } /* END test-wc_PKCS7_New */
  23180. /*
  23181. * Testing wc_PKCS7_Init()
  23182. */
  23183. static int test_wc_PKCS7_Init(void)
  23184. {
  23185. EXPECT_DECLS;
  23186. #if defined(HAVE_PKCS7)
  23187. PKCS7* pkcs7 = NULL;
  23188. void* heap = NULL;
  23189. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23190. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23191. /* Pass in bad args. */
  23192. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23193. wc_PKCS7_Free(pkcs7);
  23194. #endif
  23195. return EXPECT_RESULT();
  23196. } /* END test-wc_PKCS7_Init */
  23197. /*
  23198. * Testing wc_PKCS7_InitWithCert()
  23199. */
  23200. static int test_wc_PKCS7_InitWithCert(void)
  23201. {
  23202. EXPECT_DECLS;
  23203. #if defined(HAVE_PKCS7)
  23204. PKCS7* pkcs7 = NULL;
  23205. #ifndef NO_RSA
  23206. #if defined(USE_CERT_BUFFERS_2048)
  23207. unsigned char cert[sizeof(client_cert_der_2048)];
  23208. int certSz = (int)sizeof(cert);
  23209. XMEMSET(cert, 0, certSz);
  23210. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23211. #elif defined(USE_CERT_BUFFERS_1024)
  23212. unsigned char cert[sizeof(client_cert_der_1024)];
  23213. int certSz = (int)sizeof(cert);
  23214. XMEMSET(cert, 0, certSz);
  23215. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23216. #else
  23217. unsigned char cert[ONEK_BUF];
  23218. XFILE fp = XBADFILE;
  23219. int certSz;
  23220. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23221. XBADFILE);
  23222. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23223. fp), 0);
  23224. if (fp != XBADFILE)
  23225. XFCLOSE(fp);
  23226. #endif
  23227. #elif defined(HAVE_ECC)
  23228. #if defined(USE_CERT_BUFFERS_256)
  23229. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23230. int certSz = (int)sizeof(cert);
  23231. XMEMSET(cert, 0, certSz);
  23232. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23233. #else
  23234. unsigned char cert[ONEK_BUF];
  23235. XFILE fp = XBADFILE;
  23236. int certSz;
  23237. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23238. XBADFILE);
  23239. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23240. fp), 0);
  23241. if (fp != XBADFILE)
  23242. XFCLOSE(fp);
  23243. #endif
  23244. #else
  23245. #error PKCS7 requires ECC or RSA
  23246. #endif
  23247. #ifdef HAVE_ECC
  23248. {
  23249. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23250. static unsigned char certWithInvalidEccKey[] = {
  23251. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23252. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23253. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23254. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23255. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23256. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23257. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23258. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23259. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23260. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23261. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23262. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23263. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23264. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23265. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23266. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23267. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23268. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23269. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23270. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23271. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23272. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23273. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23274. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23275. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23276. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23277. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23278. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23279. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23280. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23281. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23282. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23283. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23284. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23285. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23286. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23287. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23288. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23289. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23290. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23291. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23292. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23293. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23294. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23295. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23296. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23297. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23298. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23299. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23300. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23301. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23302. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23303. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23304. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23305. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23306. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23307. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23308. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23309. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23310. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23311. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23312. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23313. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23314. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23315. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23316. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23317. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23318. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23319. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23320. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23321. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23322. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23323. 0x08, 0xA8, 0xB4
  23324. };
  23325. #endif
  23326. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23327. /* If initialization is not successful, it's free'd in init func. */
  23328. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23329. 0);
  23330. wc_PKCS7_Free(pkcs7);
  23331. pkcs7 = NULL;
  23332. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23333. /* Valid initialization usage. */
  23334. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23335. /* Pass in bad args. No need free for null checks, free at end.*/
  23336. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23337. BAD_FUNC_ARG);
  23338. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23339. BAD_FUNC_ARG);
  23340. #ifdef HAVE_ECC
  23341. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23342. sizeof(certWithInvalidEccKey)), 0);
  23343. }
  23344. #endif
  23345. wc_PKCS7_Free(pkcs7);
  23346. #endif
  23347. return EXPECT_RESULT();
  23348. } /* END test_wc_PKCS7_InitWithCert */
  23349. /*
  23350. * Testing wc_PKCS7_EncodeData()
  23351. */
  23352. static int test_wc_PKCS7_EncodeData(void)
  23353. {
  23354. EXPECT_DECLS;
  23355. #if defined(HAVE_PKCS7)
  23356. PKCS7* pkcs7 = NULL;
  23357. byte output[FOURK_BUF];
  23358. byte data[] = "My encoded DER cert.";
  23359. #ifndef NO_RSA
  23360. #if defined(USE_CERT_BUFFERS_2048)
  23361. unsigned char cert[sizeof(client_cert_der_2048)];
  23362. unsigned char key[sizeof(client_key_der_2048)];
  23363. int certSz = (int)sizeof(cert);
  23364. int keySz = (int)sizeof(key);
  23365. XMEMSET(cert, 0, certSz);
  23366. XMEMSET(key, 0, keySz);
  23367. XMEMCPY(cert, client_cert_der_2048, certSz);
  23368. XMEMCPY(key, client_key_der_2048, keySz);
  23369. #elif defined(USE_CERT_BUFFERS_1024)
  23370. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23371. unsigned char key[sizeof_client_key_der_1024];
  23372. int certSz = (int)sizeof(cert);
  23373. int keySz = (int)sizeof(key);
  23374. XMEMSET(cert, 0, certSz);
  23375. XMEMSET(key, 0, keySz);
  23376. XMEMCPY(cert, client_cert_der_1024, certSz);
  23377. XMEMCPY(key, client_key_der_1024, keySz);
  23378. #else
  23379. unsigned char cert[ONEK_BUF];
  23380. unsigned char key[ONEK_BUF];
  23381. XFILE fp = XBADFILE;
  23382. int certSz;
  23383. int keySz;
  23384. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23385. XBADFILE);
  23386. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23387. fp), 0);
  23388. if (fp != XBADFILE) {
  23389. XFCLOSE(fp);
  23390. fp = XBADFILE;
  23391. }
  23392. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23393. XBADFILE);
  23394. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23395. 0);
  23396. if (fp != XBADFILE)
  23397. XFCLOSE(fp);
  23398. #endif
  23399. #elif defined(HAVE_ECC)
  23400. #if defined(USE_CERT_BUFFERS_256)
  23401. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23402. unsigned char key[sizeof(ecc_clikey_der_256)];
  23403. int certSz = (int)sizeof(cert);
  23404. int keySz = (int)sizeof(key);
  23405. XMEMSET(cert, 0, certSz);
  23406. XMEMSET(key, 0, keySz);
  23407. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23408. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23409. #else
  23410. unsigned char cert[ONEK_BUF];
  23411. unsigned char key[ONEK_BUF];
  23412. XFILE fp = XBADFILE;
  23413. int certSz, keySz;
  23414. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23415. XBADFILE);
  23416. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23417. fp), 0);
  23418. if (fp != XBADFILE) {
  23419. XFCLOSE(fp);
  23420. fp = XBADFILE;
  23421. }
  23422. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23423. XBADFILE);
  23424. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23425. 0);
  23426. if (fp != XBADFILE)
  23427. XFCLOSE(fp);
  23428. #endif
  23429. #endif
  23430. XMEMSET(output, 0, sizeof(output));
  23431. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23432. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23433. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23434. if (pkcs7 != NULL) {
  23435. pkcs7->content = data;
  23436. pkcs7->contentSz = sizeof(data);
  23437. pkcs7->privateKey = key;
  23438. pkcs7->privateKeySz = keySz;
  23439. }
  23440. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23441. /* Test bad args. */
  23442. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23443. BAD_FUNC_ARG);
  23444. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23445. BAD_FUNC_ARG);
  23446. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23447. wc_PKCS7_Free(pkcs7);
  23448. #endif
  23449. return EXPECT_RESULT();
  23450. } /* END test_wc_PKCS7_EncodeData */
  23451. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23452. !defined(NO_RSA) && !defined(NO_SHA256)
  23453. /* RSA sign raw digest callback */
  23454. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23455. byte* out, word32 outSz, byte* privateKey,
  23456. word32 privateKeySz, int devid, int hashOID)
  23457. {
  23458. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23459. byte digInfoEncoding[] = {
  23460. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23461. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23462. 0x00, 0x04, 0x20
  23463. };
  23464. int ret;
  23465. byte digestInfo[ONEK_BUF];
  23466. byte sig[FOURK_BUF];
  23467. word32 digestInfoSz = 0;
  23468. word32 idx = 0;
  23469. RsaKey rsa;
  23470. /* SHA-256 required only for this example callback due to above
  23471. * digInfoEncoding[] */
  23472. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23473. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23474. (hashOID != SHA256h)) {
  23475. return -1;
  23476. }
  23477. /* build DigestInfo */
  23478. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23479. digestInfoSz += sizeof(digInfoEncoding);
  23480. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23481. digestInfoSz += digestSz;
  23482. /* set up RSA key */
  23483. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23484. if (ret != 0) {
  23485. return ret;
  23486. }
  23487. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23488. /* sign DigestInfo */
  23489. if (ret == 0) {
  23490. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23491. &rsa, pkcs7->rng);
  23492. if (ret > 0) {
  23493. if (ret > (int)outSz) {
  23494. /* output buffer too small */
  23495. ret = -1;
  23496. }
  23497. else {
  23498. /* success, ret holds sig size */
  23499. XMEMCPY(out, sig, ret);
  23500. }
  23501. }
  23502. }
  23503. wc_FreeRsaKey(&rsa);
  23504. return ret;
  23505. }
  23506. #endif
  23507. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  23508. typedef struct encodeSignedDataStream {
  23509. byte out[FOURK_BUF*3];
  23510. int idx;
  23511. word32 outIdx;
  23512. } encodeSignedDataStream;
  23513. /* content is 8k of partially created bundle */
  23514. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  23515. {
  23516. int ret = 0;
  23517. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23518. if (strm->outIdx < pkcs7->contentSz) {
  23519. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  23520. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  23521. *content = strm->out + strm->outIdx;
  23522. strm->outIdx += ret;
  23523. }
  23524. (void)pkcs7;
  23525. return ret;
  23526. }
  23527. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  23528. void* ctx)
  23529. {
  23530. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23531. XMEMCPY(strm->out + strm->idx, output, outputSz);
  23532. strm->idx += outputSz;
  23533. (void)pkcs7;
  23534. return 0;
  23535. }
  23536. #endif
  23537. /*
  23538. * Testing wc_PKCS7_EncodeSignedData()
  23539. */
  23540. static int test_wc_PKCS7_EncodeSignedData(void)
  23541. {
  23542. EXPECT_DECLS;
  23543. #if defined(HAVE_PKCS7)
  23544. PKCS7* pkcs7 = NULL;
  23545. WC_RNG rng;
  23546. byte output[FOURK_BUF];
  23547. byte badOut[1];
  23548. word32 outputSz = (word32)sizeof(output);
  23549. word32 badOutSz = 0;
  23550. byte data[] = "Test data to encode.";
  23551. #ifndef NO_RSA
  23552. #if defined(USE_CERT_BUFFERS_2048)
  23553. byte key[sizeof(client_key_der_2048)];
  23554. byte cert[sizeof(client_cert_der_2048)];
  23555. word32 keySz = (word32)sizeof(key);
  23556. word32 certSz = (word32)sizeof(cert);
  23557. XMEMSET(key, 0, keySz);
  23558. XMEMSET(cert, 0, certSz);
  23559. XMEMCPY(key, client_key_der_2048, keySz);
  23560. XMEMCPY(cert, client_cert_der_2048, certSz);
  23561. #elif defined(USE_CERT_BUFFERS_1024)
  23562. byte key[sizeof_client_key_der_1024];
  23563. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23564. word32 keySz = (word32)sizeof(key);
  23565. word32 certSz = (word32)sizeof(cert);
  23566. XMEMSET(key, 0, keySz);
  23567. XMEMSET(cert, 0, certSz);
  23568. XMEMCPY(key, client_key_der_1024, keySz);
  23569. XMEMCPY(cert, client_cert_der_1024, certSz);
  23570. #else
  23571. unsigned char cert[ONEK_BUF];
  23572. unsigned char key[ONEK_BUF];
  23573. XFILE fp = XBADFILE;
  23574. int certSz;
  23575. int keySz;
  23576. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23577. XBADFILE);
  23578. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23579. fp), 0);
  23580. if (fp != XBADFILE) {
  23581. XFCLOSE(fp);
  23582. fp = XBADFILE;
  23583. }
  23584. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23585. XBADFILE);
  23586. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23587. 0);
  23588. if (fp != XBADFILE)
  23589. XFCLOSE(fp);
  23590. #endif
  23591. #elif defined(HAVE_ECC)
  23592. #if defined(USE_CERT_BUFFERS_256)
  23593. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23594. unsigned char key[sizeof(ecc_clikey_der_256)];
  23595. int certSz = (int)sizeof(cert);
  23596. int keySz = (int)sizeof(key);
  23597. XMEMSET(cert, 0, certSz);
  23598. XMEMSET(key, 0, keySz);
  23599. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23600. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23601. #else
  23602. unsigned char cert[ONEK_BUF];
  23603. unsigned char key[ONEK_BUF];
  23604. XFILE fp = XBADFILE;
  23605. int certSz;
  23606. int keySz;
  23607. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23608. XBADFILE);
  23609. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23610. if (fp != XBADFILE) {
  23611. XFCLOSE(fp);
  23612. fp = XBADFILE;
  23613. }
  23614. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23615. XBADFILE);
  23616. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23617. if (fp != XBADFILE)
  23618. XFCLOSE(fp);
  23619. #endif
  23620. #endif
  23621. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23622. XMEMSET(output, 0, outputSz);
  23623. ExpectIntEQ(wc_InitRng(&rng), 0);
  23624. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23625. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23626. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23627. if (pkcs7 != NULL) {
  23628. pkcs7->content = data;
  23629. pkcs7->contentSz = (word32)sizeof(data);
  23630. pkcs7->privateKey = key;
  23631. pkcs7->privateKeySz = (word32)sizeof(key);
  23632. pkcs7->encryptOID = RSAk;
  23633. #ifdef NO_SHA
  23634. pkcs7->hashOID = SHA256h;
  23635. #else
  23636. pkcs7->hashOID = SHAh;
  23637. #endif
  23638. pkcs7->rng = &rng;
  23639. }
  23640. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23641. wc_PKCS7_Free(pkcs7);
  23642. pkcs7 = NULL;
  23643. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23644. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23645. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23646. #ifdef ASN_BER_TO_DER
  23647. wc_PKCS7_Free(pkcs7);
  23648. /* reinitialize and test setting stream mode */
  23649. {
  23650. int signedSz;
  23651. encodeSignedDataStream strm;
  23652. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23653. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23654. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23655. if (pkcs7 != NULL) {
  23656. pkcs7->content = data;
  23657. pkcs7->contentSz = (word32)sizeof(data);
  23658. pkcs7->privateKey = key;
  23659. pkcs7->privateKeySz = (word32)sizeof(key);
  23660. pkcs7->encryptOID = RSAk;
  23661. #ifdef NO_SHA
  23662. pkcs7->hashOID = SHA256h;
  23663. #else
  23664. pkcs7->hashOID = SHAh;
  23665. #endif
  23666. pkcs7->rng = &rng;
  23667. }
  23668. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  23669. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  23670. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  23671. BAD_FUNC_ARG);
  23672. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  23673. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  23674. 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. /* use exact signed buffer size since BER encoded */
  23680. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
  23681. wc_PKCS7_Free(pkcs7);
  23682. /* now try with using callbacks for IO */
  23683. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23684. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23685. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23686. if (pkcs7 != NULL) {
  23687. pkcs7->contentSz = FOURK_BUF*2;
  23688. pkcs7->privateKey = key;
  23689. pkcs7->privateKeySz = (word32)sizeof(key);
  23690. pkcs7->encryptOID = RSAk;
  23691. #ifdef NO_SHA
  23692. pkcs7->hashOID = SHA256h;
  23693. #else
  23694. pkcs7->hashOID = SHAh;
  23695. #endif
  23696. pkcs7->rng = &rng;
  23697. }
  23698. XMEMSET(&strm, 0, sizeof(strm));
  23699. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  23700. StreamOutputCB, (void*)&strm), 0);
  23701. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  23702. wc_PKCS7_Free(pkcs7);
  23703. pkcs7 = NULL;
  23704. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23705. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23706. /* use exact signed buffer size since BER encoded */
  23707. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, signedSz), 0);
  23708. }
  23709. #endif
  23710. #ifndef NO_PKCS7_STREAM
  23711. wc_PKCS7_Free(pkcs7);
  23712. pkcs7 = NULL;
  23713. {
  23714. word32 z;
  23715. int ret;
  23716. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23717. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23718. /* test for streaming mode */
  23719. ret = -1;
  23720. for (z = 0; z < outputSz && ret != 0; z++) {
  23721. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  23722. if (ret < 0){
  23723. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  23724. }
  23725. }
  23726. ExpectIntEQ(ret, 0);
  23727. ExpectIntNE(pkcs7->contentSz, 0);
  23728. ExpectNotNull(pkcs7->contentDynamic);
  23729. }
  23730. #endif /* !NO_PKCS7_STREAM */
  23731. /* Pass in bad args. */
  23732. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  23733. BAD_FUNC_ARG);
  23734. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23735. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23736. badOutSz), BAD_FUNC_ARG);
  23737. if (pkcs7 != NULL) {
  23738. pkcs7->hashOID = 0; /* bad hashOID */
  23739. }
  23740. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  23741. BAD_FUNC_ARG);
  23742. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23743. !defined(NO_RSA) && !defined(NO_SHA256)
  23744. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23745. * Example callback assumes SHA-256, so only run test if compiled in. */
  23746. wc_PKCS7_Free(pkcs7);
  23747. pkcs7 = NULL;
  23748. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23749. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23750. if (pkcs7 != NULL) {
  23751. pkcs7->content = data;
  23752. pkcs7->contentSz = (word32)sizeof(data);
  23753. pkcs7->privateKey = key;
  23754. pkcs7->privateKeySz = (word32)sizeof(key);
  23755. pkcs7->encryptOID = RSAk;
  23756. pkcs7->hashOID = SHA256h;
  23757. pkcs7->rng = &rng;
  23758. }
  23759. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23760. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23761. #endif
  23762. wc_PKCS7_Free(pkcs7);
  23763. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23764. #endif
  23765. return EXPECT_RESULT();
  23766. } /* END test_wc_PKCS7_EncodeSignedData */
  23767. /*
  23768. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23769. */
  23770. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23771. {
  23772. EXPECT_DECLS;
  23773. #if defined(HAVE_PKCS7)
  23774. int i;
  23775. PKCS7* pkcs7 = NULL;
  23776. WC_RNG rng;
  23777. byte outputHead[FOURK_BUF/2];
  23778. byte outputFoot[FOURK_BUF/2];
  23779. word32 outputHeadSz = (word32)sizeof(outputHead);
  23780. word32 outputFootSz = (word32)sizeof(outputFoot);
  23781. byte data[FOURK_BUF];
  23782. wc_HashAlg hash;
  23783. #ifdef NO_SHA
  23784. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23785. #else
  23786. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23787. #endif
  23788. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23789. word32 hashSz = wc_HashGetDigestSize(hashType);
  23790. #ifndef NO_RSA
  23791. #if defined(USE_CERT_BUFFERS_2048)
  23792. byte key[sizeof(client_key_der_2048)];
  23793. byte cert[sizeof(client_cert_der_2048)];
  23794. word32 keySz = (word32)sizeof(key);
  23795. word32 certSz = (word32)sizeof(cert);
  23796. XMEMSET(key, 0, keySz);
  23797. XMEMSET(cert, 0, certSz);
  23798. XMEMCPY(key, client_key_der_2048, keySz);
  23799. XMEMCPY(cert, client_cert_der_2048, certSz);
  23800. #elif defined(USE_CERT_BUFFERS_1024)
  23801. byte key[sizeof_client_key_der_1024];
  23802. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23803. word32 keySz = (word32)sizeof(key);
  23804. word32 certSz = (word32)sizeof(cert);
  23805. XMEMSET(key, 0, keySz);
  23806. XMEMSET(cert, 0, certSz);
  23807. XMEMCPY(key, client_key_der_1024, keySz);
  23808. XMEMCPY(cert, client_cert_der_1024, certSz);
  23809. #else
  23810. unsigned char cert[ONEK_BUF];
  23811. unsigned char key[ONEK_BUF];
  23812. XFILE fp = XBADFILE;
  23813. int certSz;
  23814. int keySz;
  23815. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23816. XBADFILE);
  23817. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23818. fp), 0);
  23819. if (fp != XBADFILE) {
  23820. XFCLOSE(fp);
  23821. fp = XBADFILE;
  23822. }
  23823. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23824. XBADFILE);
  23825. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23826. 0);
  23827. if (fp != XBADFILE)
  23828. XFCLOSE(fp);
  23829. #endif
  23830. #elif defined(HAVE_ECC)
  23831. #if defined(USE_CERT_BUFFERS_256)
  23832. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23833. unsigned char key[sizeof(ecc_clikey_der_256)];
  23834. int certSz = (int)sizeof(cert);
  23835. int keySz = (int)sizeof(key);
  23836. XMEMSET(cert, 0, certSz);
  23837. XMEMSET(key, 0, keySz);
  23838. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23839. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23840. #else
  23841. unsigned char cert[ONEK_BUF];
  23842. unsigned char key[ONEK_BUF];
  23843. XFILE fp = XBADFILE;
  23844. int certSz;
  23845. int keySz;
  23846. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23847. XBADFILE);
  23848. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23849. fp), 0);
  23850. if (fp != XBADFILE) {
  23851. XFCLOSE(fp);
  23852. fp = XBADFILE;
  23853. }
  23854. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23855. XBADFILE);
  23856. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23857. 0);
  23858. if (fp != XBADFILE)
  23859. XFCLOSE(fp);
  23860. #endif
  23861. #endif
  23862. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23863. /* initialize large data with sequence */
  23864. for (i=0; i<(int)sizeof(data); i++)
  23865. data[i] = i & 0xff;
  23866. XMEMSET(outputHead, 0, outputHeadSz);
  23867. XMEMSET(outputFoot, 0, outputFootSz);
  23868. ExpectIntEQ(wc_InitRng(&rng), 0);
  23869. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23870. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23871. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23872. if (pkcs7 != NULL) {
  23873. pkcs7->content = NULL; /* not used for ex */
  23874. pkcs7->contentSz = (word32)sizeof(data);
  23875. pkcs7->privateKey = key;
  23876. pkcs7->privateKeySz = (word32)sizeof(key);
  23877. pkcs7->encryptOID = RSAk;
  23878. #ifdef NO_SHA
  23879. pkcs7->hashOID = SHA256h;
  23880. #else
  23881. pkcs7->hashOID = SHAh;
  23882. #endif
  23883. pkcs7->rng = &rng;
  23884. }
  23885. /* calculate hash for content */
  23886. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23887. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23888. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23889. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23890. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23891. /* Perform PKCS7 sign using hash directly */
  23892. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23893. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23894. ExpectIntGT(outputHeadSz, 0);
  23895. ExpectIntGT(outputFootSz, 0);
  23896. wc_PKCS7_Free(pkcs7);
  23897. pkcs7 = NULL;
  23898. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23899. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23900. /* required parameter even on verify when using _ex, if using outputHead
  23901. * and outputFoot */
  23902. if (pkcs7 != NULL) {
  23903. pkcs7->contentSz = (word32)sizeof(data);
  23904. }
  23905. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23906. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23907. wc_PKCS7_Free(pkcs7);
  23908. pkcs7 = NULL;
  23909. /* assembly complete PKCS7 sign and use normal verify */
  23910. {
  23911. byte* output = NULL;
  23912. word32 outputSz = 0;
  23913. #ifndef NO_PKCS7_STREAM
  23914. word32 z;
  23915. int ret;
  23916. #endif /* !NO_PKCS7_STREAM */
  23917. ExpectNotNull(output = (byte*)XMALLOC(
  23918. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  23919. DYNAMIC_TYPE_TMP_BUFFER));
  23920. if (output != NULL) {
  23921. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23922. outputSz += outputHeadSz;
  23923. XMEMCPY(&output[outputSz], data, sizeof(data));
  23924. outputSz += sizeof(data);
  23925. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23926. outputSz += outputFootSz;
  23927. }
  23928. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23929. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23930. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23931. #ifndef NO_PKCS7_STREAM
  23932. wc_PKCS7_Free(pkcs7);
  23933. pkcs7 = NULL;
  23934. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23935. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23936. /* test for streaming mode */
  23937. ret = -1;
  23938. for (z = 0; z < outputSz && ret != 0; z++) {
  23939. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  23940. if (ret < 0){
  23941. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  23942. }
  23943. }
  23944. ExpectIntEQ(ret, 0);
  23945. ExpectIntNE(pkcs7->contentSz, 0);
  23946. ExpectNotNull(pkcs7->contentDynamic);
  23947. wc_PKCS7_Free(pkcs7);
  23948. pkcs7 = NULL;
  23949. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23950. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23951. #endif /* !NO_PKCS7_STREAM */
  23952. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23953. }
  23954. /* Pass in bad args. */
  23955. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23956. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23957. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23958. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23959. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23960. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23961. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23962. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23963. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23964. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23965. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23966. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23967. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23968. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  23969. if (pkcs7 != NULL) {
  23970. pkcs7->hashOID = 0; /* bad hashOID */
  23971. }
  23972. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23973. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23974. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23975. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23976. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23977. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23978. #ifndef NO_PKCS7_STREAM
  23979. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23980. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23981. #else
  23982. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23983. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  23984. #endif
  23985. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23986. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23987. #ifndef NO_PKCS7_STREAM
  23988. /* can pass in 0 buffer length with streaming API */
  23989. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23990. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23991. #else
  23992. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23993. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23994. #endif
  23995. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23996. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  23997. #ifndef NO_PKCS7_STREAM
  23998. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23999. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24000. #else
  24001. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24002. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24003. #endif
  24004. wc_PKCS7_Free(pkcs7);
  24005. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24006. #endif
  24007. return EXPECT_RESULT();
  24008. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24009. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24010. /**
  24011. * Loads certs/keys from files or buffers into the argument buffers,
  24012. * helper function called by CreatePKCS7SignedData().
  24013. *
  24014. * Returns 0 on success, negative on error.
  24015. */
  24016. static int LoadPKCS7SignedDataCerts(
  24017. int useIntermediateCertChain, int pkAlgoType,
  24018. byte* intCARoot, word32* intCARootSz,
  24019. byte* intCA1, word32* intCA1Sz,
  24020. byte* intCA2, word32* intCA2Sz,
  24021. byte* cert, word32* certSz,
  24022. byte* key, word32* keySz)
  24023. {
  24024. EXPECT_DECLS;
  24025. int ret = 0;
  24026. XFILE fp = XBADFILE;
  24027. #ifndef NO_RSA
  24028. const char* intCARootRSA = "./certs/ca-cert.der";
  24029. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24030. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24031. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24032. const char* intServKeyRSA = "./certs/server-key.der";
  24033. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24034. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24035. const char* cli1024Key = "./certs/1024/client-key.der";
  24036. #endif
  24037. #endif
  24038. #ifdef HAVE_ECC
  24039. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24040. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24041. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24042. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24043. const char* intServKeyECC = "./certs/ecc-key.der";
  24044. #ifndef USE_CERT_BUFFERS_256
  24045. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24046. const char* cliEccKey = "./certs/client-ecc-key.der";
  24047. #endif
  24048. #endif
  24049. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24050. ((useIntermediateCertChain == 1) &&
  24051. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24052. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24053. return BAD_FUNC_ARG;
  24054. }
  24055. /* Read/load certs and keys to use for signing based on PK type and chain */
  24056. switch (pkAlgoType) {
  24057. #ifndef NO_RSA
  24058. case RSA_TYPE:
  24059. if (useIntermediateCertChain == 1) {
  24060. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24061. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24062. if (fp != XBADFILE) {
  24063. XFCLOSE(fp);
  24064. fp = XBADFILE;
  24065. }
  24066. ExpectIntGT(*intCARootSz, 0);
  24067. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24068. if (fp != XBADFILE) {
  24069. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24070. XFCLOSE(fp);
  24071. fp = XBADFILE;
  24072. }
  24073. ExpectIntGT(*intCA1Sz, 0);
  24074. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24075. if (fp != XBADFILE) {
  24076. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24077. XFCLOSE(fp);
  24078. fp = XBADFILE;
  24079. }
  24080. ExpectIntGT(*intCA2Sz, 0);
  24081. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24082. if (fp != XBADFILE) {
  24083. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24084. XFCLOSE(fp);
  24085. fp = XBADFILE;
  24086. }
  24087. ExpectIntGT(*certSz, 0);
  24088. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24089. if (fp != XBADFILE) {
  24090. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24091. XFCLOSE(fp);
  24092. fp = XBADFILE;
  24093. }
  24094. ExpectIntGT(*keySz, 0);
  24095. }
  24096. else {
  24097. #if defined(USE_CERT_BUFFERS_2048)
  24098. *keySz = sizeof_client_key_der_2048;
  24099. *certSz = sizeof_client_cert_der_2048;
  24100. XMEMCPY(key, client_key_der_2048, *keySz);
  24101. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24102. #elif defined(USE_CERT_BUFFERS_1024)
  24103. *keySz = sizeof_client_key_der_1024;
  24104. *certSz = sizeof_client_cert_der_1024;
  24105. XMEMCPY(key, client_key_der_1024, *keySz);
  24106. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24107. #else
  24108. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24109. if (fp != XBADFILE) {
  24110. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24111. XFCLOSE(fp);
  24112. fp = XBADFILE;
  24113. }
  24114. ExpectIntGT(*keySz, 0);
  24115. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24116. if (fp != XBADFILE) {
  24117. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24118. XFCLOSE(fp);
  24119. fp = XBADFILE;
  24120. }
  24121. ExpectIntGT(*certSz, 0);
  24122. #endif /* USE_CERT_BUFFERS_2048 */
  24123. }
  24124. break;
  24125. #endif /* !NO_RSA */
  24126. #ifdef HAVE_ECC
  24127. case ECC_TYPE:
  24128. if (useIntermediateCertChain == 1) {
  24129. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24130. if (fp != XBADFILE) {
  24131. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24132. fp);
  24133. XFCLOSE(fp);
  24134. fp = XBADFILE;
  24135. }
  24136. ExpectIntGT(*intCARootSz, 0);
  24137. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24138. if (fp != XBADFILE) {
  24139. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24140. XFCLOSE(fp);
  24141. fp = XBADFILE;
  24142. }
  24143. ExpectIntGT(*intCA1Sz, 0);
  24144. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24145. if (fp != XBADFILE) {
  24146. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24147. XFCLOSE(fp);
  24148. fp = XBADFILE;
  24149. }
  24150. ExpectIntGT(*intCA2Sz, 0);
  24151. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24152. if (fp != XBADFILE) {
  24153. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24154. XFCLOSE(fp);
  24155. fp = XBADFILE;
  24156. }
  24157. ExpectIntGT(*certSz, 0);
  24158. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24159. if (fp != XBADFILE) {
  24160. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24161. XFCLOSE(fp);
  24162. fp = XBADFILE;
  24163. }
  24164. ExpectIntGT(*keySz, 0);
  24165. }
  24166. else {
  24167. #if defined(USE_CERT_BUFFERS_256)
  24168. *keySz = sizeof_ecc_clikey_der_256;
  24169. *certSz = sizeof_cliecc_cert_der_256;
  24170. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24171. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24172. #else
  24173. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24174. if (fp != XBADFILE) {
  24175. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24176. XFCLOSE(fp);
  24177. fp = XBADFILE;
  24178. }
  24179. ExpectIntGT(*keySz, 0);
  24180. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24181. if (fp != XBADFILE) {
  24182. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24183. XFCLOSE(fp);
  24184. fp = XBADFILE;
  24185. }
  24186. ExpectIntGT(*certSz, 0);
  24187. #endif /* USE_CERT_BUFFERS_256 */
  24188. }
  24189. break;
  24190. #endif /* HAVE_ECC */
  24191. default:
  24192. WOLFSSL_MSG("Unsupported SignedData PK type");
  24193. ret = BAD_FUNC_ARG;
  24194. break;
  24195. }
  24196. if (EXPECT_FAIL() && (ret == 0)) {
  24197. ret = BAD_FUNC_ARG;
  24198. }
  24199. return ret;
  24200. }
  24201. /**
  24202. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24203. *
  24204. * output output buffer to place SignedData
  24205. * outputSz size of output buffer
  24206. * data data buffer to be signed
  24207. * dataSz size of data buffer
  24208. * withAttribs [1/0] include attributes in SignedData message
  24209. * detachedSig [1/0] create detached signature, no content
  24210. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24211. * root CAs in bundle
  24212. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24213. *
  24214. * Return size of bundle created on success, negative on error */
  24215. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24216. byte* data, word32 dataSz,
  24217. int withAttribs, int detachedSig,
  24218. int useIntermediateCertChain,
  24219. int pkAlgoType)
  24220. {
  24221. EXPECT_DECLS;
  24222. int ret = 0;
  24223. WC_RNG rng;
  24224. PKCS7* pkcs7 = NULL;
  24225. static byte messageTypeOid[] =
  24226. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24227. 0x09, 0x02 };
  24228. static byte messageType[] = { 0x13, 2, '1', '9' };
  24229. PKCS7Attrib attribs[] =
  24230. {
  24231. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24232. sizeof(messageType) }
  24233. };
  24234. byte intCARoot[TWOK_BUF];
  24235. byte intCA1[TWOK_BUF];
  24236. byte intCA2[TWOK_BUF];
  24237. byte cert[TWOK_BUF];
  24238. byte key[TWOK_BUF];
  24239. word32 intCARootSz = sizeof(intCARoot);
  24240. word32 intCA1Sz = sizeof(intCA1);
  24241. word32 intCA2Sz = sizeof(intCA2);
  24242. word32 certSz = sizeof(cert);
  24243. word32 keySz = sizeof(key);
  24244. XMEMSET(intCARoot, 0, intCARootSz);
  24245. XMEMSET(intCA1, 0, intCA1Sz);
  24246. XMEMSET(intCA2, 0, intCA2Sz);
  24247. XMEMSET(cert, 0, certSz);
  24248. XMEMSET(key, 0, keySz);
  24249. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24250. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24251. cert, &certSz, key, &keySz);
  24252. ExpectIntEQ(ret, 0);
  24253. XMEMSET(output, 0, outputSz);
  24254. ExpectIntEQ(wc_InitRng(&rng), 0);
  24255. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24256. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24257. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24258. if (useIntermediateCertChain == 1) {
  24259. /* Add intermediate and root CA certs into SignedData Certs SET */
  24260. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24261. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24262. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24263. }
  24264. if (pkcs7 != NULL) {
  24265. pkcs7->content = data;
  24266. pkcs7->contentSz = dataSz;
  24267. pkcs7->privateKey = key;
  24268. pkcs7->privateKeySz = (word32)sizeof(key);
  24269. if (pkAlgoType == RSA_TYPE) {
  24270. pkcs7->encryptOID = RSAk;
  24271. }
  24272. else {
  24273. pkcs7->encryptOID = ECDSAk;
  24274. }
  24275. #ifdef NO_SHA
  24276. pkcs7->hashOID = SHA256h;
  24277. #else
  24278. pkcs7->hashOID = SHAh;
  24279. #endif
  24280. pkcs7->rng = &rng;
  24281. if (withAttribs) {
  24282. /* include a signed attribute */
  24283. pkcs7->signedAttribs = attribs;
  24284. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24285. }
  24286. }
  24287. if (detachedSig) {
  24288. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24289. }
  24290. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24291. ExpectIntGT(outputSz, 0);
  24292. wc_PKCS7_Free(pkcs7);
  24293. pkcs7 = NULL;
  24294. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24295. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24296. if (detachedSig && (pkcs7 != NULL)) {
  24297. pkcs7->content = data;
  24298. pkcs7->contentSz = dataSz;
  24299. }
  24300. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24301. wc_PKCS7_Free(pkcs7);
  24302. wc_FreeRng(&rng);
  24303. if (EXPECT_FAIL()) {
  24304. outputSz = 0;
  24305. }
  24306. return outputSz;
  24307. }
  24308. #endif
  24309. /*
  24310. * Testing wc_PKCS_VerifySignedData()
  24311. */
  24312. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24313. {
  24314. EXPECT_DECLS;
  24315. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24316. PKCS7* pkcs7 = NULL;
  24317. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24318. word32 outputSz = sizeof(output);
  24319. byte data[] = "Test data to encode.";
  24320. byte badOut[1];
  24321. word32 badOutSz = 0;
  24322. byte badContent[] = "This is different content than was signed";
  24323. wc_HashAlg hash;
  24324. #ifdef NO_SHA
  24325. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24326. #else
  24327. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24328. #endif
  24329. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24330. word32 hashSz = wc_HashGetDigestSize(hashType);
  24331. #ifndef NO_RSA
  24332. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24333. /* contentType OID (1.2.840.113549.1.9.3) */
  24334. static const byte contentTypeOid[] =
  24335. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24336. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24337. static const byte dataType[] =
  24338. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24339. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24340. static const byte messageDigestOid[] =
  24341. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24342. #ifndef NO_ASN_TIME
  24343. /* signingTime OID () */
  24344. static const byte signingTimeOid[] =
  24345. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24346. #endif
  24347. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24348. int dateLength = 0;
  24349. byte dateFormat;
  24350. const byte* datePart = NULL;
  24351. struct tm timearg;
  24352. time_t now;
  24353. struct tm* nowTm = NULL;
  24354. #ifdef NEED_TMP_TIME
  24355. struct tm tmpTimeStorage;
  24356. struct tm* tmpTime = &tmpTimeStorage;
  24357. #endif
  24358. #ifndef NO_PKCS7_STREAM
  24359. word32 z;
  24360. int ret;
  24361. #endif /* !NO_PKCS7_STREAM */
  24362. #endif /* !NO_ASN && !NO_ASN_TIME */
  24363. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24364. /* Success test with RSA certs/key */
  24365. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24366. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  24367. /* calculate hash for content, used later */
  24368. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24369. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24370. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24371. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24372. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24373. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24374. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24375. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24376. #ifndef NO_PKCS7_STREAM
  24377. wc_PKCS7_Free(pkcs7);
  24378. pkcs7 = NULL;
  24379. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24380. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24381. /* test for streaming */
  24382. ret = -1;
  24383. for (z = 0; z < outputSz && ret != 0; z++) {
  24384. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24385. if (ret < 0){
  24386. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24387. }
  24388. }
  24389. ExpectIntEQ(ret, 0);
  24390. ExpectIntNE(pkcs7->contentSz, 0);
  24391. ExpectNotNull(pkcs7->contentDynamic);
  24392. #endif /* !NO_PKCS7_STREAM */
  24393. /* Check that decoded signed attributes are correct */
  24394. /* messageDigest should be first */
  24395. if (pkcs7 != NULL) {
  24396. decodedAttrib = pkcs7->decodedAttrib;
  24397. }
  24398. ExpectNotNull(decodedAttrib);
  24399. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  24400. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  24401. decodedAttrib->oidSz), 0);
  24402. /* + 2 for OCTET STRING and length bytes */
  24403. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  24404. ExpectNotNull(decodedAttrib->value);
  24405. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  24406. #ifndef NO_ASN_TIME
  24407. /* signingTime should be second */
  24408. if (decodedAttrib != NULL) {
  24409. decodedAttrib = decodedAttrib->next;
  24410. }
  24411. ExpectNotNull(decodedAttrib);
  24412. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  24413. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  24414. decodedAttrib->oidSz), 0);
  24415. ExpectIntGT(decodedAttrib->valueSz, 0);
  24416. ExpectNotNull(decodedAttrib->value);
  24417. #endif
  24418. /* Verify signingTime if ASN and time are available */
  24419. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24420. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24421. &datePart, &dateFormat, &dateLength), 0);
  24422. ExpectNotNull(datePart);
  24423. ExpectIntGT(dateLength, 0);
  24424. XMEMSET(&timearg, 0, sizeof(timearg));
  24425. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24426. &timearg), 0);
  24427. /* Get current time and compare year/month/day against attribute value */
  24428. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24429. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24430. ExpectNotNull(nowTm);
  24431. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24432. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24433. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24434. #endif /* !NO_ASN && !NO_ASN_TIME */
  24435. /* contentType should be third */
  24436. if (decodedAttrib != NULL) {
  24437. decodedAttrib = decodedAttrib->next;
  24438. }
  24439. ExpectNotNull(decodedAttrib);
  24440. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24441. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24442. decodedAttrib->oidSz), 0);
  24443. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24444. ExpectNotNull(decodedAttrib->value);
  24445. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24446. 0);
  24447. #endif /* !NO_RSA */
  24448. /* Test bad args. */
  24449. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24450. BAD_FUNC_ARG);
  24451. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24452. BAD_FUNC_ARG);
  24453. #ifndef NO_PKCS7_STREAM
  24454. /* can pass in 0 buffer length with streaming API */
  24455. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24456. badOutSz), WC_PKCS7_WANT_READ_E);
  24457. #else
  24458. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24459. badOutSz), BAD_FUNC_ARG);
  24460. #endif
  24461. wc_PKCS7_Free(pkcs7);
  24462. pkcs7 = NULL;
  24463. #ifndef NO_RSA
  24464. /* Try RSA certs/key/sig first */
  24465. outputSz = sizeof(output);
  24466. XMEMSET(output, 0, outputSz);
  24467. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24468. (word32)sizeof(data),
  24469. 1, 1, 0, RSA_TYPE)), 0);
  24470. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24471. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24472. if (pkcs7 != NULL) {
  24473. pkcs7->content = badContent;
  24474. pkcs7->contentSz = sizeof(badContent);
  24475. }
  24476. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24477. SIG_VERIFY_E);
  24478. wc_PKCS7_Free(pkcs7);
  24479. pkcs7 = NULL;
  24480. #ifndef NO_PKCS7_STREAM
  24481. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24482. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24483. if (pkcs7 != NULL) {
  24484. pkcs7->content = badContent;
  24485. pkcs7->contentSz = sizeof(badContent);
  24486. }
  24487. /* test for streaming */
  24488. ret = -1;
  24489. for (z = 0; z < outputSz && ret != 0; z++) {
  24490. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24491. if (ret == WC_PKCS7_WANT_READ_E){
  24492. continue;
  24493. }
  24494. else if (ret < 0) {
  24495. break;
  24496. }
  24497. }
  24498. ExpectIntEQ(ret, SIG_VERIFY_E);
  24499. ExpectIntNE(pkcs7->contentSz, 0);
  24500. ExpectNotNull(pkcs7->contentDynamic);
  24501. wc_PKCS7_Free(pkcs7);
  24502. pkcs7 = NULL;
  24503. #endif /* !NO_PKCS7_STREAM */
  24504. /* Test success case with detached signature and valid content */
  24505. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24506. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24507. if (pkcs7 != NULL) {
  24508. pkcs7->content = data;
  24509. pkcs7->contentSz = sizeof(data);
  24510. }
  24511. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24512. wc_PKCS7_Free(pkcs7);
  24513. pkcs7 = NULL;
  24514. #ifndef NO_PKCS7_STREAM
  24515. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24516. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24517. if (pkcs7 != NULL) {
  24518. pkcs7->content = data;
  24519. pkcs7->contentSz = sizeof(data);
  24520. }
  24521. /* test for streaming */
  24522. ret = -1;
  24523. for (z = 0; z < outputSz && ret != 0; z++) {
  24524. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24525. if (ret < 0){
  24526. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24527. }
  24528. }
  24529. ExpectIntEQ(ret, 0);
  24530. ExpectIntNE(pkcs7->contentSz, 0);
  24531. ExpectNotNull(pkcs7->contentDynamic);
  24532. wc_PKCS7_Free(pkcs7);
  24533. pkcs7 = NULL;
  24534. #endif /* !NO_PKCS7_STREAM */
  24535. /* verify using pre-computed content digest only (no content) */
  24536. {
  24537. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24538. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24539. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24540. output, outputSz, NULL, 0), 0);
  24541. wc_PKCS7_Free(pkcs7);
  24542. pkcs7 = NULL;
  24543. }
  24544. #endif /* !NO_RSA */
  24545. /* Test verify on signedData containing intermediate/root CA certs */
  24546. #ifndef NO_RSA
  24547. outputSz = sizeof(output);
  24548. XMEMSET(output, 0, outputSz);
  24549. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24550. (word32)sizeof(data),
  24551. 0, 0, 1, RSA_TYPE)), 0);
  24552. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24553. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24554. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24555. wc_PKCS7_Free(pkcs7);
  24556. pkcs7 = NULL;
  24557. #ifndef NO_PKCS7_STREAM
  24558. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24559. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24560. /* test for streaming */
  24561. ret = -1;
  24562. for (z = 0; z < outputSz && ret != 0; z++) {
  24563. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24564. if (ret < 0){
  24565. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24566. }
  24567. }
  24568. ExpectIntEQ(ret, 0);
  24569. ExpectIntNE(pkcs7->contentSz, 0);
  24570. ExpectNotNull(pkcs7->contentDynamic);
  24571. wc_PKCS7_Free(pkcs7);
  24572. pkcs7 = NULL;
  24573. #endif /* !NO_PKCS7_STREAM */
  24574. #endif /* !NO_RSA */
  24575. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  24576. !defined(NO_FILESYSTEM)
  24577. {
  24578. XFILE signedBundle = XBADFILE;
  24579. int signedBundleSz = 0;
  24580. int chunkSz = 1;
  24581. int i, rc = 0;
  24582. byte* buf = NULL;
  24583. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  24584. "rb")) != XBADFILE);
  24585. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  24586. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  24587. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  24588. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  24589. DYNAMIC_TYPE_FILE));
  24590. if (buf != NULL) {
  24591. ExpectIntEQ(XFREAD(buf, 1, signedBundleSz, signedBundle),
  24592. signedBundleSz);
  24593. }
  24594. if (signedBundle != XBADFILE) {
  24595. XFCLOSE(signedBundle);
  24596. signedBundle = XBADFILE;
  24597. }
  24598. if (buf != NULL) {
  24599. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24600. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24601. for (i = 0; i < signedBundleSz;) {
  24602. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  24603. chunkSz;
  24604. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  24605. if (rc < 0 ) {
  24606. if (rc == WC_PKCS7_WANT_READ_E) {
  24607. i += sz;
  24608. continue;
  24609. }
  24610. break;
  24611. }
  24612. else {
  24613. break;
  24614. }
  24615. }
  24616. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  24617. wc_PKCS7_Free(pkcs7);
  24618. pkcs7 = NULL;
  24619. }
  24620. /* now try with malformed bundle */
  24621. if (buf != NULL) {
  24622. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24623. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24624. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  24625. for (i = 0; i < signedBundleSz;) {
  24626. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  24627. chunkSz;
  24628. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  24629. if (rc < 0 ) {
  24630. if (rc == WC_PKCS7_WANT_READ_E) {
  24631. i += sz;
  24632. continue;
  24633. }
  24634. break;
  24635. }
  24636. else {
  24637. break;
  24638. }
  24639. }
  24640. ExpectIntEQ(rc, ASN_PARSE_E);
  24641. wc_PKCS7_Free(pkcs7);
  24642. pkcs7 = NULL;
  24643. }
  24644. if (buf != NULL)
  24645. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  24646. }
  24647. #endif /* BER and stream */
  24648. #endif
  24649. return EXPECT_RESULT();
  24650. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  24651. /*
  24652. * Testing wc_PKCS_VerifySignedData()
  24653. */
  24654. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  24655. {
  24656. EXPECT_DECLS;
  24657. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  24658. PKCS7* pkcs7 = NULL;
  24659. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24660. word32 outputSz = sizeof(output);
  24661. byte data[] = "Test data to encode.";
  24662. byte badContent[] = "This is different content than was signed";
  24663. wc_HashAlg hash;
  24664. #ifndef NO_PKCS7_STREAM
  24665. word32 z;
  24666. int ret;
  24667. #endif /* !NO_PKCS7_STREAM */
  24668. #ifdef NO_SHA
  24669. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24670. #else
  24671. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24672. #endif
  24673. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24674. word32 hashSz = wc_HashGetDigestSize(hashType);
  24675. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24676. /* Success test with ECC certs/key */
  24677. outputSz = sizeof(output);
  24678. XMEMSET(output, 0, outputSz);
  24679. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24680. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  24681. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24682. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24683. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24684. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24685. wc_PKCS7_Free(pkcs7);
  24686. pkcs7 = NULL;
  24687. #ifndef NO_PKCS7_STREAM
  24688. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24689. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24690. /* test for streaming */
  24691. ret = -1;
  24692. for (z = 0; z < outputSz && ret != 0; z++) {
  24693. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24694. if (ret < 0){
  24695. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24696. }
  24697. }
  24698. ExpectIntEQ(ret, 0);
  24699. ExpectIntNE(pkcs7->contentSz, 0);
  24700. ExpectNotNull(pkcs7->contentDynamic);
  24701. wc_PKCS7_Free(pkcs7);
  24702. pkcs7 = NULL;
  24703. #endif /* !NO_PKCS7_STREAM */
  24704. /* Invalid content should error, use detached signature so we can
  24705. * easily change content */
  24706. outputSz = sizeof(output);
  24707. XMEMSET(output, 0, outputSz);
  24708. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24709. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  24710. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24711. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24712. if (pkcs7 != NULL) {
  24713. pkcs7->content = badContent;
  24714. pkcs7->contentSz = sizeof(badContent);
  24715. }
  24716. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24717. SIG_VERIFY_E);
  24718. wc_PKCS7_Free(pkcs7);
  24719. pkcs7 = NULL;
  24720. #ifndef NO_PKCS7_STREAM
  24721. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24722. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24723. if (pkcs7 != NULL) {
  24724. pkcs7->content = badContent;
  24725. pkcs7->contentSz = sizeof(badContent);
  24726. }
  24727. /* test for streaming */
  24728. ret = -1;
  24729. for (z = 0; z < outputSz && ret != 0; z++) {
  24730. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24731. if (ret == WC_PKCS7_WANT_READ_E){
  24732. continue;
  24733. }
  24734. else if (ret < 0) {
  24735. break;
  24736. }
  24737. }
  24738. ExpectIntEQ(ret, SIG_VERIFY_E);
  24739. ExpectIntNE(pkcs7->contentSz, 0);
  24740. ExpectNotNull(pkcs7->contentDynamic);
  24741. wc_PKCS7_Free(pkcs7);
  24742. pkcs7 = NULL;
  24743. #endif /* !NO_PKCS7_STREAM */
  24744. /* Test success case with detached signature and valid content */
  24745. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24746. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24747. if (pkcs7 != NULL) {
  24748. pkcs7->content = data;
  24749. pkcs7->contentSz = sizeof(data);
  24750. }
  24751. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24752. wc_PKCS7_Free(pkcs7);
  24753. pkcs7 = NULL;
  24754. #ifndef NO_PKCS7_STREAM
  24755. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24756. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24757. if (pkcs7 != NULL) {
  24758. pkcs7->content = data;
  24759. pkcs7->contentSz = sizeof(data);
  24760. }
  24761. /* test for streaming */
  24762. ret = -1;
  24763. for (z = 0; z < outputSz && ret != 0; z++) {
  24764. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24765. if (ret < 0){
  24766. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24767. }
  24768. }
  24769. ExpectIntEQ(ret, 0);
  24770. ExpectIntNE(pkcs7->contentSz, 0);
  24771. ExpectNotNull(pkcs7->contentDynamic);
  24772. wc_PKCS7_Free(pkcs7);
  24773. pkcs7 = NULL;
  24774. #endif /* !NO_PKCS7_STREAM */
  24775. /* verify using pre-computed content digest only (no content) */
  24776. {
  24777. /* calculate hash for content */
  24778. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24779. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24780. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24781. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24782. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24783. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24784. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24785. output, outputSz, NULL, 0), 0);
  24786. wc_PKCS7_Free(pkcs7);
  24787. pkcs7 = NULL;
  24788. }
  24789. /* Test verify on signedData containing intermediate/root CA certs */
  24790. outputSz = sizeof(output);
  24791. XMEMSET(output, 0, outputSz);
  24792. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24793. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  24794. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24795. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24796. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24797. wc_PKCS7_Free(pkcs7);
  24798. pkcs7 = NULL;
  24799. #ifndef NO_PKCS7_STREAM
  24800. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24801. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24802. /* test for streaming */
  24803. ret = -1;
  24804. for (z = 0; z < outputSz && ret != 0; z++) {
  24805. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24806. if (ret < 0){
  24807. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24808. }
  24809. }
  24810. ExpectIntEQ(ret, 0);
  24811. ExpectIntNE(pkcs7->contentSz, 0);
  24812. ExpectNotNull(pkcs7->contentDynamic);
  24813. wc_PKCS7_Free(pkcs7);
  24814. pkcs7 = NULL;
  24815. #endif /* !NO_PKCS7_STREAM */
  24816. #endif
  24817. return EXPECT_RESULT();
  24818. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  24819. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  24820. !defined(NO_AES_256)
  24821. static const byte defKey[] = {
  24822. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24823. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24824. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24825. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24826. };
  24827. static byte aesHandle[32]; /* simulated hardware key handle */
  24828. /* return 0 on success */
  24829. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  24830. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  24831. byte* in, int inSz, byte* out, void* usrCtx)
  24832. {
  24833. int ret;
  24834. Aes aes;
  24835. if (usrCtx == NULL) {
  24836. /* no simulated handle passed in */
  24837. return -1;
  24838. }
  24839. switch (encryptOID) {
  24840. case AES256CBCb:
  24841. if (ivSz != AES_BLOCK_SIZE)
  24842. return BAD_FUNC_ARG;
  24843. break;
  24844. default:
  24845. WOLFSSL_MSG("Unsupported content cipher type for test");
  24846. return ALGO_ID_E;
  24847. };
  24848. /* simulate using handle to get key */
  24849. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  24850. if (ret == 0) {
  24851. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  24852. if (ret == 0)
  24853. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  24854. wc_AesFree(&aes);
  24855. }
  24856. (void)aad;
  24857. (void)aadSz;
  24858. (void)authTag;
  24859. (void)authTagSz;
  24860. (void)pkcs7;
  24861. return ret;
  24862. }
  24863. /* returns key size on success */
  24864. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24865. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24866. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24867. {
  24868. int ret = -1;
  24869. (void)cekSz;
  24870. (void)cek;
  24871. (void)outSz;
  24872. (void)keyIdSz;
  24873. (void)direction;
  24874. (void)orginKey; /* used with KAKRI */
  24875. (void)orginKeySz;
  24876. if (out == NULL)
  24877. return BAD_FUNC_ARG;
  24878. if (keyId[0] != 0x00) {
  24879. return -1;
  24880. }
  24881. if (type != (int)PKCS7_KEKRI) {
  24882. return -1;
  24883. }
  24884. switch (keyWrapAlgo) {
  24885. case AES256_WRAP:
  24886. /* simulate setting a handle for later decryption but use key
  24887. * as handle in the test case here */
  24888. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24889. aesHandle, sizeof(aesHandle), NULL);
  24890. if (ret < 0)
  24891. return ret;
  24892. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24893. if (ret < 0)
  24894. return ret;
  24895. /* return key size on success */
  24896. return sizeof(defKey);
  24897. default:
  24898. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24899. return BAD_KEYWRAP_ALG_E;
  24900. };
  24901. }
  24902. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24903. /*
  24904. * Testing wc_PKCS7_EncodeEnvelopedData()
  24905. */
  24906. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  24907. {
  24908. EXPECT_DECLS;
  24909. #if defined(HAVE_PKCS7)
  24910. PKCS7* pkcs7 = NULL;
  24911. #ifdef ASN_BER_TO_DER
  24912. int encodedSz = 0;
  24913. #endif
  24914. #ifdef ECC_TIMING_RESISTANT
  24915. WC_RNG rng;
  24916. #endif
  24917. word32 tempWrd32 = 0;
  24918. byte* tmpBytePtr = NULL;
  24919. const char input[] = "Test data to encode.";
  24920. int i;
  24921. int testSz = 0;
  24922. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24923. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24924. byte* rsaCert = NULL;
  24925. byte* rsaPrivKey = NULL;
  24926. word32 rsaCertSz;
  24927. word32 rsaPrivKeySz;
  24928. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24929. !defined(USE_CERT_BUFFERS_2048) )
  24930. static const char* rsaClientCert = "./certs/client-cert.der";
  24931. static const char* rsaClientKey = "./certs/client-key.der";
  24932. rsaCertSz = (word32)sizeof(rsaClientCert);
  24933. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24934. #endif
  24935. #endif
  24936. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24937. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24938. byte* eccCert = NULL;
  24939. byte* eccPrivKey = NULL;
  24940. word32 eccCertSz;
  24941. word32 eccPrivKeySz;
  24942. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24943. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24944. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24945. #endif
  24946. #endif
  24947. /* Generic buffer size. */
  24948. byte output[ONEK_BUF];
  24949. byte decoded[sizeof(input)/sizeof(char)];
  24950. int decodedSz = 0;
  24951. #ifndef NO_FILESYSTEM
  24952. XFILE certFile = XBADFILE;
  24953. XFILE keyFile = XBADFILE;
  24954. #endif
  24955. #ifdef ECC_TIMING_RESISTANT
  24956. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24957. #endif
  24958. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24959. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24960. /* RSA certs and keys. */
  24961. #if defined(USE_CERT_BUFFERS_1024)
  24962. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24963. /* Allocate buffer space. */
  24964. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24965. DYNAMIC_TYPE_TMP_BUFFER));
  24966. /* Init buffer. */
  24967. if (rsaCert != NULL) {
  24968. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24969. }
  24970. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24971. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24972. DYNAMIC_TYPE_TMP_BUFFER));
  24973. if (rsaPrivKey != NULL) {
  24974. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24975. }
  24976. #elif defined(USE_CERT_BUFFERS_2048)
  24977. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24978. /* Allocate buffer */
  24979. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24980. DYNAMIC_TYPE_TMP_BUFFER));
  24981. /* Init buffer. */
  24982. if (rsaCert != NULL) {
  24983. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24984. }
  24985. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24986. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24987. DYNAMIC_TYPE_TMP_BUFFER));
  24988. if (rsaPrivKey != NULL) {
  24989. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24990. }
  24991. #else
  24992. /* File system. */
  24993. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  24994. rsaCertSz = (word32)FOURK_BUF;
  24995. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24996. DYNAMIC_TYPE_TMP_BUFFER));
  24997. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  24998. certFile)) > 0);
  24999. if (certFile != XBADFILE)
  25000. XFCLOSE(certFile);
  25001. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25002. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25003. DYNAMIC_TYPE_TMP_BUFFER));
  25004. rsaPrivKeySz = (word32)FOURK_BUF;
  25005. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25006. keyFile)) > 0);
  25007. if (keyFile != XBADFILE)
  25008. XFCLOSE(keyFile);
  25009. #endif /* USE_CERT_BUFFERS */
  25010. #endif /* NO_RSA */
  25011. /* ECC */
  25012. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25013. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25014. #ifdef USE_CERT_BUFFERS_256
  25015. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25016. DYNAMIC_TYPE_TMP_BUFFER));
  25017. /* Init buffer. */
  25018. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25019. if (eccCert != NULL) {
  25020. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25021. }
  25022. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25023. DYNAMIC_TYPE_TMP_BUFFER));
  25024. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25025. if (eccPrivKey != NULL) {
  25026. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25027. }
  25028. #else /* File system. */
  25029. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25030. eccCertSz = (word32)FOURK_BUF;
  25031. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25032. DYNAMIC_TYPE_TMP_BUFFER));
  25033. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25034. certFile)) > 0);
  25035. if (certFile != XBADFILE) {
  25036. XFCLOSE(certFile);
  25037. }
  25038. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25039. eccPrivKeySz = (word32)FOURK_BUF;
  25040. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25041. DYNAMIC_TYPE_TMP_BUFFER));
  25042. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25043. keyFile)) > 0);
  25044. if (keyFile != XBADFILE) {
  25045. XFCLOSE(keyFile);
  25046. }
  25047. #endif /* USE_CERT_BUFFERS_256 */
  25048. #endif /* END HAVE_ECC */
  25049. #ifndef NO_FILESYSTEM
  25050. /* Silence. */
  25051. (void)keyFile;
  25052. (void)certFile;
  25053. #endif
  25054. {
  25055. const pkcs7EnvelopedVector testVectors[] = {
  25056. /* DATA is a global variable defined in the makefile. */
  25057. #if !defined(NO_RSA)
  25058. #ifndef NO_DES3
  25059. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25060. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25061. #endif /* NO_DES3 */
  25062. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25063. #ifndef NO_AES_128
  25064. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25065. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25066. #endif
  25067. #ifndef NO_AES_192
  25068. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25069. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25070. #endif
  25071. #ifndef NO_AES_256
  25072. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25073. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25074. #endif
  25075. #endif /* NO_AES && HAVE_AES_CBC */
  25076. #endif /* NO_RSA */
  25077. #if defined(HAVE_ECC)
  25078. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25079. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25080. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25081. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25082. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25083. #endif
  25084. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25085. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25086. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25087. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25088. #endif
  25089. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25090. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25091. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25092. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25093. #endif
  25094. #endif /* NO_AES && HAVE_AES_CBC*/
  25095. #endif /* END HAVE_ECC */
  25096. }; /* END pkcs7EnvelopedVector */
  25097. #ifdef ECC_TIMING_RESISTANT
  25098. ExpectIntEQ(wc_InitRng(&rng), 0);
  25099. #endif
  25100. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25101. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25102. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25103. for (i = 0; i < testSz; i++) {
  25104. #ifdef ASN_BER_TO_DER
  25105. encodeSignedDataStream strm;
  25106. /* test setting stream mode, the first one using IO callbacks */
  25107. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25108. (word32)(testVectors + i)->certSz), 0);
  25109. if (pkcs7 != NULL) {
  25110. #ifdef ECC_TIMING_RESISTANT
  25111. pkcs7->rng = &rng;
  25112. #endif
  25113. if (i != 0)
  25114. pkcs7->content = (byte*)(testVectors + i)->content;
  25115. pkcs7->contentSz = (testVectors + i)->contentSz;
  25116. pkcs7->contentOID = (testVectors + i)->contentOID;
  25117. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25118. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25119. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25120. pkcs7->privateKey = (testVectors + i)->privateKey;
  25121. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25122. }
  25123. if (i == 0) {
  25124. XMEMSET(&strm, 0, sizeof(strm));
  25125. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25126. StreamOutputCB, (void*)&strm), 0);
  25127. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25128. }
  25129. else {
  25130. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25131. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25132. (word32)sizeof(output));
  25133. }
  25134. switch ((testVectors + i)->encryptOID) {
  25135. #ifndef NO_DES3
  25136. case DES3b:
  25137. case DESb:
  25138. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25139. break;
  25140. #endif
  25141. #ifdef HAVE_AESCCM
  25142. #ifdef WOLFSSL_AES_128
  25143. case AES128CCMb:
  25144. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25145. break;
  25146. #endif
  25147. #ifdef WOLFSSL_AES_192
  25148. case AES192CCMb:
  25149. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25150. break;
  25151. #endif
  25152. #ifdef WOLFSSL_AES_256
  25153. case AES256CCMb:
  25154. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25155. break;
  25156. #endif
  25157. #endif
  25158. default:
  25159. ExpectIntGE(encodedSz, 0);
  25160. }
  25161. if (encodedSz > 0) {
  25162. if (i == 0) {
  25163. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25164. strm.out, (word32)encodedSz, decoded,
  25165. (word32)sizeof(decoded));
  25166. }
  25167. else {
  25168. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25169. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25170. }
  25171. ExpectIntGE(decodedSz, 0);
  25172. /* Verify the size of each buffer. */
  25173. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25174. }
  25175. wc_PKCS7_Free(pkcs7);
  25176. pkcs7 = NULL;
  25177. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25178. #endif
  25179. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25180. (word32)(testVectors + i)->certSz), 0);
  25181. if (pkcs7 != NULL) {
  25182. #ifdef ECC_TIMING_RESISTANT
  25183. pkcs7->rng = &rng;
  25184. #endif
  25185. pkcs7->content = (byte*)(testVectors + i)->content;
  25186. pkcs7->contentSz = (testVectors + i)->contentSz;
  25187. pkcs7->contentOID = (testVectors + i)->contentOID;
  25188. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25189. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25190. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25191. pkcs7->privateKey = (testVectors + i)->privateKey;
  25192. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25193. }
  25194. #ifdef ASN_BER_TO_DER
  25195. /* test without setting stream mode */
  25196. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25197. #endif
  25198. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25199. (word32)sizeof(output)), 0);
  25200. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25201. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25202. ExpectIntGE(decodedSz, 0);
  25203. /* Verify the size of each buffer. */
  25204. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25205. /* Don't free the last time through the loop. */
  25206. if (i < testSz - 1) {
  25207. wc_PKCS7_Free(pkcs7);
  25208. pkcs7 = NULL;
  25209. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25210. }
  25211. } /* END test loop. */
  25212. }
  25213. /* Test bad args. */
  25214. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25215. (word32)sizeof(output)), BAD_FUNC_ARG);
  25216. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25217. (word32)sizeof(output)), BAD_FUNC_ARG);
  25218. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25219. /* Decode. */
  25220. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25221. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25222. BAD_FUNC_ARG);
  25223. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25224. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25225. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25226. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25227. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25228. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25229. BAD_FUNC_ARG);
  25230. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25231. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25232. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25233. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25234. /* only a failure for KARI test cases */
  25235. if (pkcs7 != NULL) {
  25236. tempWrd32 = pkcs7->singleCertSz;
  25237. pkcs7->singleCertSz = 0;
  25238. }
  25239. #if defined(WOLFSSL_ASN_TEMPLATE)
  25240. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25241. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25242. BUFFER_E);
  25243. #else
  25244. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25245. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25246. ASN_PARSE_E);
  25247. #endif
  25248. if (pkcs7 != NULL) {
  25249. pkcs7->singleCertSz = tempWrd32;
  25250. tmpBytePtr = pkcs7->singleCert;
  25251. pkcs7->singleCert = NULL;
  25252. }
  25253. #ifndef NO_RSA
  25254. #if defined(NO_PKCS7_STREAM)
  25255. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25256. * is returned from kari parse which gets set to bad func arg */
  25257. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25258. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25259. BAD_FUNC_ARG);
  25260. #else
  25261. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25262. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25263. ASN_PARSE_E);
  25264. #endif
  25265. #endif /* !NO_RSA */
  25266. if (pkcs7 != NULL) {
  25267. pkcs7->singleCert = tmpBytePtr;
  25268. }
  25269. #endif
  25270. if (pkcs7 != NULL) {
  25271. tempWrd32 = pkcs7->privateKeySz;
  25272. pkcs7->privateKeySz = 0;
  25273. }
  25274. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25275. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25276. BAD_FUNC_ARG);
  25277. if (pkcs7 != NULL) {
  25278. pkcs7->privateKeySz = tempWrd32;
  25279. tmpBytePtr = pkcs7->privateKey;
  25280. pkcs7->privateKey = NULL;
  25281. }
  25282. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25283. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25284. BAD_FUNC_ARG);
  25285. if (pkcs7 != NULL) {
  25286. pkcs7->privateKey = tmpBytePtr;
  25287. }
  25288. wc_PKCS7_Free(pkcs7);
  25289. pkcs7 = NULL;
  25290. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25291. /* test of decrypt callback with KEKRI enveloped data */
  25292. {
  25293. int envelopedSz = 0;
  25294. const byte keyId[] = { 0x00 };
  25295. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25296. if (pkcs7 != NULL) {
  25297. pkcs7->content = (byte*)input;
  25298. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25299. pkcs7->contentOID = DATA;
  25300. pkcs7->encryptOID = AES256CBCb;
  25301. }
  25302. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25303. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25304. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25305. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25306. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25307. (word32)sizeof(output))), 0);
  25308. wc_PKCS7_Free(pkcs7);
  25309. pkcs7 = NULL;
  25310. /* decode envelopedData */
  25311. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25312. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25313. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25314. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25315. envelopedSz, decoded, sizeof(decoded))), 0);
  25316. wc_PKCS7_Free(pkcs7);
  25317. pkcs7 = NULL;
  25318. }
  25319. #endif /* !NO_AES && !NO_AES_256 */
  25320. #ifndef NO_RSA
  25321. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25322. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25323. #endif /* NO_RSA */
  25324. #ifdef HAVE_ECC
  25325. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25326. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25327. #endif /* HAVE_ECC */
  25328. #ifdef ECC_TIMING_RESISTANT
  25329. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25330. #endif
  25331. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25332. !defined(NO_RSA) && !defined(NO_SHA)
  25333. {
  25334. byte out[7];
  25335. byte *cms = NULL;
  25336. word32 cmsSz;
  25337. XFILE cmsFile = XBADFILE;
  25338. XMEMSET(out, 0, sizeof(out));
  25339. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25340. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25341. != XBADFILE);
  25342. cmsSz = (word32)FOURK_BUF;
  25343. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25344. DYNAMIC_TYPE_TMP_BUFFER));
  25345. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25346. if (cmsFile != XBADFILE)
  25347. XFCLOSE(cmsFile);
  25348. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25349. sizeof_client_cert_der_2048), 0);
  25350. if (pkcs7 != NULL) {
  25351. pkcs7->privateKey = (byte*)client_key_der_2048;
  25352. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25353. }
  25354. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25355. 2), 0);
  25356. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25357. sizeof(out)), 0);
  25358. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25359. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25360. wc_PKCS7_Free(pkcs7);
  25361. pkcs7 = NULL;
  25362. }
  25363. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25364. #endif /* HAVE_PKCS7 */
  25365. return EXPECT_RESULT();
  25366. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  25367. /*
  25368. * Testing wc_PKCS7_EncodeEncryptedData()
  25369. */
  25370. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25371. {
  25372. EXPECT_DECLS;
  25373. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25374. PKCS7* pkcs7 = NULL;
  25375. byte* tmpBytePtr = NULL;
  25376. byte encrypted[TWOK_BUF];
  25377. byte decoded[TWOK_BUF];
  25378. word32 tmpWrd32 = 0;
  25379. int tmpInt = 0;
  25380. int decodedSz = 0;
  25381. int encryptedSz = 0;
  25382. int testSz = 0;
  25383. int i = 0;
  25384. const byte data[] = { /* Hello World */
  25385. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25386. 0x72,0x6c,0x64
  25387. };
  25388. #ifndef NO_DES3
  25389. byte desKey[] = {
  25390. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25391. };
  25392. byte des3Key[] = {
  25393. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25394. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25395. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25396. };
  25397. #endif
  25398. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25399. #ifndef NO_AES_128
  25400. byte aes128Key[] = {
  25401. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25402. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25403. };
  25404. #endif
  25405. #ifndef NO_AES_192
  25406. byte aes192Key[] = {
  25407. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25408. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25409. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25410. };
  25411. #endif
  25412. #ifndef NO_AES_256
  25413. byte aes256Key[] = {
  25414. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25415. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25416. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25417. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25418. };
  25419. #endif
  25420. #endif /* !NO_AES && HAVE_AES_CBC */
  25421. const pkcs7EncryptedVector testVectors[] =
  25422. {
  25423. #ifndef NO_DES3
  25424. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25425. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25426. #endif /* !NO_DES3 */
  25427. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25428. #ifndef NO_AES_128
  25429. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25430. sizeof(aes128Key)},
  25431. #endif
  25432. #ifndef NO_AES_192
  25433. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25434. sizeof(aes192Key)},
  25435. #endif
  25436. #ifndef NO_AES_256
  25437. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25438. sizeof(aes256Key)},
  25439. #endif
  25440. #endif /* !NO_AES && HAVE_AES_CBC */
  25441. };
  25442. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25443. for (i = 0; i < testSz; i++) {
  25444. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25445. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25446. if (pkcs7 != NULL) {
  25447. pkcs7->content = (byte*)testVectors[i].content;
  25448. pkcs7->contentSz = testVectors[i].contentSz;
  25449. pkcs7->contentOID = testVectors[i].contentOID;
  25450. pkcs7->encryptOID = testVectors[i].encryptOID;
  25451. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25452. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25453. pkcs7->heap = HEAP_HINT;
  25454. }
  25455. /* encode encryptedData */
  25456. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25457. sizeof(encrypted)), 0);
  25458. /* Decode encryptedData */
  25459. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  25460. encryptedSz, decoded, sizeof(decoded)), 0);
  25461. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25462. /* Keep values for last itr. */
  25463. if (i < testSz - 1) {
  25464. wc_PKCS7_Free(pkcs7);
  25465. pkcs7 = NULL;
  25466. }
  25467. }
  25468. if (pkcs7 == NULL || testSz == 0) {
  25469. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25470. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25471. }
  25472. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25473. sizeof(encrypted)),BAD_FUNC_ARG);
  25474. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25475. sizeof(encrypted)), BAD_FUNC_ARG);
  25476. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25477. 0), BAD_FUNC_ARG);
  25478. /* Testing the struct. */
  25479. if (pkcs7 != NULL) {
  25480. tmpBytePtr = pkcs7->content;
  25481. pkcs7->content = NULL;
  25482. }
  25483. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25484. sizeof(encrypted)), BAD_FUNC_ARG);
  25485. if (pkcs7 != NULL) {
  25486. pkcs7->content = tmpBytePtr;
  25487. tmpWrd32 = pkcs7->contentSz;
  25488. pkcs7->contentSz = 0;
  25489. }
  25490. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25491. sizeof(encrypted)), BAD_FUNC_ARG);
  25492. if (pkcs7 != NULL) {
  25493. pkcs7->contentSz = tmpWrd32;
  25494. tmpInt = pkcs7->encryptOID;
  25495. pkcs7->encryptOID = 0;
  25496. }
  25497. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25498. sizeof(encrypted)), BAD_FUNC_ARG);
  25499. if (pkcs7 != NULL) {
  25500. pkcs7->encryptOID = tmpInt;
  25501. tmpBytePtr = pkcs7->encryptionKey;
  25502. pkcs7->encryptionKey = NULL;
  25503. }
  25504. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25505. sizeof(encrypted)), BAD_FUNC_ARG);
  25506. if (pkcs7 != NULL) {
  25507. pkcs7->encryptionKey = tmpBytePtr;
  25508. tmpWrd32 = pkcs7->encryptionKeySz;
  25509. pkcs7->encryptionKeySz = 0;
  25510. }
  25511. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25512. sizeof(encrypted)), BAD_FUNC_ARG);
  25513. if (pkcs7 != NULL) {
  25514. pkcs7->encryptionKeySz = tmpWrd32;
  25515. }
  25516. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25517. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25518. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25519. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25520. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25521. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25522. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25523. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25524. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25525. decoded, 0), BAD_FUNC_ARG);
  25526. /* Test struct fields */
  25527. if (pkcs7 != NULL) {
  25528. tmpBytePtr = pkcs7->encryptionKey;
  25529. pkcs7->encryptionKey = NULL;
  25530. }
  25531. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25532. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25533. if (pkcs7 != NULL) {
  25534. pkcs7->encryptionKey = tmpBytePtr;
  25535. pkcs7->encryptionKeySz = 0;
  25536. }
  25537. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25538. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25539. wc_PKCS7_Free(pkcs7);
  25540. #endif
  25541. return EXPECT_RESULT();
  25542. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25543. /*
  25544. * Testing wc_PKCS7_Degenerate()
  25545. */
  25546. static int test_wc_PKCS7_Degenerate(void)
  25547. {
  25548. EXPECT_DECLS;
  25549. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25550. PKCS7* pkcs7 = NULL;
  25551. char fName[] = "./certs/test-degenerate.p7b";
  25552. XFILE f = XBADFILE;
  25553. byte der[4096];
  25554. word32 derSz = 0;
  25555. #ifndef NO_PKCS7_STREAM
  25556. word32 z;
  25557. int ret;
  25558. #endif /* !NO_PKCS7_STREAM */
  25559. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25560. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25561. if (f != XBADFILE)
  25562. XFCLOSE(f);
  25563. /* test degenerate success */
  25564. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25565. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25566. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25567. #ifndef NO_RSA
  25568. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25569. #ifndef NO_PKCS7_STREAM
  25570. wc_PKCS7_Free(pkcs7);
  25571. pkcs7 = NULL;
  25572. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25573. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25574. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25575. /* test for streaming */
  25576. ret = -1;
  25577. for (z = 0; z < derSz && ret != 0; z++) {
  25578. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25579. if (ret < 0){
  25580. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25581. }
  25582. }
  25583. ExpectIntEQ(ret, 0);
  25584. #endif /* !NO_PKCS7_STREAM */
  25585. #else
  25586. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25587. #endif /* NO_RSA */
  25588. wc_PKCS7_Free(pkcs7);
  25589. pkcs7 = NULL;
  25590. /* test with turning off degenerate cases */
  25591. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25592. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25593. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25594. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25595. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  25596. PKCS7_NO_SIGNER_E);
  25597. #ifndef NO_PKCS7_STREAM
  25598. wc_PKCS7_Free(pkcs7);
  25599. pkcs7 = NULL;
  25600. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25601. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25602. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25603. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25604. /* test for streaming */
  25605. ret = -1;
  25606. for (z = 0; z < derSz && ret != 0; z++) {
  25607. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25608. if (ret == WC_PKCS7_WANT_READ_E){
  25609. continue;
  25610. }
  25611. else
  25612. break;
  25613. }
  25614. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  25615. #endif /* !NO_PKCS7_STREAM */
  25616. wc_PKCS7_Free(pkcs7);
  25617. #endif
  25618. return EXPECT_RESULT();
  25619. } /* END test_wc_PKCS7_Degenerate() */
  25620. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25621. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  25622. static byte berContent[] = {
  25623. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25624. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  25625. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  25626. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  25627. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  25628. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  25629. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  25630. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  25631. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  25632. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  25633. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  25634. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  25635. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  25636. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  25637. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  25638. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  25639. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  25640. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  25641. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25642. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25643. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  25644. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25645. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25646. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  25647. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  25648. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  25649. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25650. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  25651. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  25652. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  25653. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  25654. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  25655. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  25656. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  25657. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  25658. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  25659. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  25660. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  25661. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  25662. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  25663. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  25664. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  25665. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  25666. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  25667. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25668. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  25669. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  25670. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  25671. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  25672. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  25673. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  25674. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  25675. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  25676. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  25677. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  25678. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  25679. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  25680. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  25681. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  25682. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  25683. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  25684. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  25685. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  25686. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  25687. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  25688. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  25689. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  25690. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  25691. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  25692. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  25693. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  25694. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  25695. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  25696. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  25697. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  25698. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  25699. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  25700. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  25701. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  25702. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  25703. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  25704. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  25705. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  25706. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  25707. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  25708. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  25709. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  25710. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  25711. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  25712. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  25713. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  25714. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  25715. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  25716. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  25717. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  25718. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  25719. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25720. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25721. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25722. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25723. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25724. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25725. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25726. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25727. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25728. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25729. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25730. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25731. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25732. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25733. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25734. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25735. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25736. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25737. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25738. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25739. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25740. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25741. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25742. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25743. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25744. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25745. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25746. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25747. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25748. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25749. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25750. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25751. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25752. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25753. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25754. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25755. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25756. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25757. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25758. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25759. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25760. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25761. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25762. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25763. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25764. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25765. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25766. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25767. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25768. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25769. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25770. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25771. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25772. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25773. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25774. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25775. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25776. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25777. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25778. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25779. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25780. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25781. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25782. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25783. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25784. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25785. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25786. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25787. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25788. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25789. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25790. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25791. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25792. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25793. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25794. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25795. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25796. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25797. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25798. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25799. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25800. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25801. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25802. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25803. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25804. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25805. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25806. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25807. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25808. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25809. 0x00, 0x00, 0x00, 0x00, 0x00
  25810. };
  25811. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  25812. * !NO_DES3 && !NO_SHA
  25813. */
  25814. /*
  25815. * Testing wc_PKCS7_BER()
  25816. */
  25817. static int test_wc_PKCS7_BER(void)
  25818. {
  25819. EXPECT_DECLS;
  25820. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25821. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  25822. PKCS7* pkcs7 = NULL;
  25823. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25824. XFILE f = XBADFILE;
  25825. byte der[4096];
  25826. #ifndef NO_DES3
  25827. byte decoded[2048];
  25828. #endif
  25829. word32 derSz = 0;
  25830. #ifndef NO_PKCS7_STREAM
  25831. word32 z;
  25832. int ret;
  25833. #endif /* !NO_PKCS7_STREAM */
  25834. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25835. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25836. if (f != XBADFILE) {
  25837. XFCLOSE(f);
  25838. f = XBADFILE;
  25839. }
  25840. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25841. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25842. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25843. #ifndef NO_RSA
  25844. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25845. #ifndef NO_PKCS7_STREAM
  25846. wc_PKCS7_Free(pkcs7);
  25847. pkcs7 = NULL;
  25848. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25849. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25850. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25851. /* test for streaming */
  25852. ret = -1;
  25853. for (z = 0; z < derSz && ret != 0; z++) {
  25854. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25855. if (ret < 0){
  25856. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25857. }
  25858. }
  25859. ExpectIntEQ(ret, 0);
  25860. #endif /* !NO_PKCS7_STREAM */
  25861. #else
  25862. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25863. #endif
  25864. wc_PKCS7_Free(pkcs7);
  25865. pkcs7 = NULL;
  25866. #ifndef NO_DES3
  25867. /* decode BER content */
  25868. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  25869. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25870. if (f != XBADFILE) {
  25871. XFCLOSE(f);
  25872. f = XBADFILE;
  25873. }
  25874. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25875. #ifndef NO_RSA
  25876. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25877. #else
  25878. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25879. #endif
  25880. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  25881. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25882. if (f != XBADFILE) {
  25883. XFCLOSE(f);
  25884. f = XBADFILE;
  25885. }
  25886. if (pkcs7 != NULL) {
  25887. pkcs7->privateKey = der;
  25888. pkcs7->privateKeySz = derSz;
  25889. }
  25890. #ifndef NO_RSA
  25891. #ifdef WOLFSSL_SP_MATH
  25892. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25893. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25894. #else
  25895. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25896. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25897. #endif
  25898. #else
  25899. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25900. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  25901. #endif
  25902. wc_PKCS7_Free(pkcs7);
  25903. #endif /* !NO_DES3 */
  25904. #endif
  25905. return EXPECT_RESULT();
  25906. } /* END test_wc_PKCS7_BER() */
  25907. static int test_wc_PKCS7_signed_enveloped(void)
  25908. {
  25909. EXPECT_DECLS;
  25910. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  25911. !defined(NO_FILESYSTEM)
  25912. XFILE f = XBADFILE;
  25913. PKCS7* pkcs7 = NULL;
  25914. #ifdef HAVE_AES_CBC
  25915. PKCS7* inner = NULL;
  25916. #endif
  25917. WC_RNG rng;
  25918. unsigned char key[FOURK_BUF/2];
  25919. unsigned char cert[FOURK_BUF/2];
  25920. unsigned char env[FOURK_BUF];
  25921. int envSz = FOURK_BUF;
  25922. int keySz = 0;
  25923. int certSz = 0;
  25924. unsigned char sig[FOURK_BUF * 2];
  25925. int sigSz = FOURK_BUF * 2;
  25926. #ifdef HAVE_AES_CBC
  25927. unsigned char decoded[FOURK_BUF];
  25928. int decodedSz = FOURK_BUF;
  25929. #endif
  25930. #ifndef NO_PKCS7_STREAM
  25931. int z;
  25932. int ret;
  25933. #endif /* !NO_PKCS7_STREAM */
  25934. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25935. /* load cert */
  25936. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  25937. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  25938. if (f != XBADFILE) {
  25939. XFCLOSE(f);
  25940. f = XBADFILE;
  25941. }
  25942. /* load key */
  25943. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  25944. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  25945. if (f != XBADFILE) {
  25946. XFCLOSE(f);
  25947. f = XBADFILE;
  25948. }
  25949. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  25950. /* sign cert for envelope */
  25951. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25952. ExpectIntEQ(wc_InitRng(&rng), 0);
  25953. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25954. if (pkcs7 != NULL) {
  25955. pkcs7->content = cert;
  25956. pkcs7->contentSz = certSz;
  25957. pkcs7->contentOID = DATA;
  25958. pkcs7->privateKey = key;
  25959. pkcs7->privateKeySz = keySz;
  25960. pkcs7->encryptOID = RSAk;
  25961. pkcs7->hashOID = SHA256h;
  25962. pkcs7->rng = &rng;
  25963. }
  25964. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25965. wc_PKCS7_Free(pkcs7);
  25966. pkcs7 = NULL;
  25967. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25968. #ifdef HAVE_AES_CBC
  25969. /* create envelope */
  25970. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25971. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25972. if (pkcs7 != NULL) {
  25973. pkcs7->content = sig;
  25974. pkcs7->contentSz = sigSz;
  25975. pkcs7->contentOID = DATA;
  25976. pkcs7->encryptOID = AES256CBCb;
  25977. pkcs7->privateKey = key;
  25978. pkcs7->privateKeySz = keySz;
  25979. }
  25980. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  25981. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  25982. wc_PKCS7_Free(pkcs7);
  25983. pkcs7 = NULL;
  25984. #endif
  25985. /* create bad signed enveloped data */
  25986. sigSz = FOURK_BUF * 2;
  25987. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25988. ExpectIntEQ(wc_InitRng(&rng), 0);
  25989. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25990. if (pkcs7 != NULL) {
  25991. pkcs7->content = env;
  25992. pkcs7->contentSz = envSz;
  25993. pkcs7->contentOID = DATA;
  25994. pkcs7->privateKey = key;
  25995. pkcs7->privateKeySz = keySz;
  25996. pkcs7->encryptOID = RSAk;
  25997. pkcs7->hashOID = SHA256h;
  25998. pkcs7->rng = &rng;
  25999. }
  26000. /* Set no certs in bundle for this test. */
  26001. if (pkcs7 != NULL) {
  26002. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26003. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26004. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26005. }
  26006. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26007. wc_PKCS7_Free(pkcs7);
  26008. pkcs7 = NULL;
  26009. /* check verify fails */
  26010. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26011. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26012. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26013. PKCS7_SIGNEEDS_CHECK);
  26014. /* try verifying the signature manually */
  26015. {
  26016. RsaKey rKey;
  26017. word32 idx = 0;
  26018. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26019. WC_MAX_DIGEST_SIZE];
  26020. int digestSz = 0;
  26021. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26022. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26023. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26024. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26025. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26026. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26027. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26028. /* verify was success */
  26029. }
  26030. wc_PKCS7_Free(pkcs7);
  26031. pkcs7 = NULL;
  26032. /* initializing the PKCS7 struct with the signing certificate should pass */
  26033. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26034. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26035. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26036. #ifndef NO_PKCS7_STREAM
  26037. wc_PKCS7_Free(pkcs7);
  26038. pkcs7 = NULL;
  26039. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26040. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26041. /* test for streaming */
  26042. ret = -1;
  26043. for (z = 0; z < sigSz && ret != 0; z++) {
  26044. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26045. if (ret < 0){
  26046. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26047. }
  26048. }
  26049. ExpectIntEQ(ret, 0);
  26050. #endif /* !NO_PKCS7_STREAM */
  26051. wc_PKCS7_Free(pkcs7);
  26052. pkcs7 = NULL;
  26053. /* create valid degenerate bundle */
  26054. sigSz = FOURK_BUF * 2;
  26055. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26056. if (pkcs7 != NULL) {
  26057. pkcs7->content = env;
  26058. pkcs7->contentSz = envSz;
  26059. pkcs7->contentOID = DATA;
  26060. pkcs7->privateKey = key;
  26061. pkcs7->privateKeySz = keySz;
  26062. pkcs7->encryptOID = RSAk;
  26063. pkcs7->hashOID = SHA256h;
  26064. pkcs7->rng = &rng;
  26065. }
  26066. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26067. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26068. wc_PKCS7_Free(pkcs7);
  26069. pkcs7 = NULL;
  26070. wc_FreeRng(&rng);
  26071. /* check verify */
  26072. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26073. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26074. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26075. ExpectNotNull(pkcs7->content);
  26076. #ifndef NO_PKCS7_STREAM
  26077. wc_PKCS7_Free(pkcs7);
  26078. pkcs7 = NULL;
  26079. /* create valid degenerate bundle */
  26080. sigSz = FOURK_BUF * 2;
  26081. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26082. if (pkcs7 != NULL) {
  26083. pkcs7->content = env;
  26084. pkcs7->contentSz = envSz;
  26085. pkcs7->contentOID = DATA;
  26086. pkcs7->privateKey = key;
  26087. pkcs7->privateKeySz = keySz;
  26088. pkcs7->encryptOID = RSAk;
  26089. pkcs7->hashOID = SHA256h;
  26090. pkcs7->rng = &rng;
  26091. }
  26092. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26093. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26094. wc_PKCS7_Free(pkcs7);
  26095. pkcs7 = NULL;
  26096. wc_FreeRng(&rng);
  26097. /* check verify */
  26098. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26099. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26100. /* test for streaming */
  26101. ret = -1;
  26102. for (z = 0; z < sigSz && ret != 0; z++) {
  26103. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26104. if (ret < 0){
  26105. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26106. }
  26107. }
  26108. ExpectIntEQ(ret, 0);
  26109. #endif /* !NO_PKCS7_STREAM */
  26110. #ifdef HAVE_AES_CBC
  26111. /* check decode */
  26112. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26113. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26114. if (inner != NULL) {
  26115. inner->privateKey = key;
  26116. inner->privateKeySz = keySz;
  26117. }
  26118. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26119. pkcs7->contentSz, decoded, decodedSz)), 0);
  26120. wc_PKCS7_Free(inner);
  26121. inner = NULL;
  26122. #endif
  26123. wc_PKCS7_Free(pkcs7);
  26124. pkcs7 = NULL;
  26125. #ifdef HAVE_AES_CBC
  26126. /* check cert set */
  26127. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26128. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26129. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26130. ExpectNotNull(pkcs7->singleCert);
  26131. ExpectIntNE(pkcs7->singleCertSz, 0);
  26132. wc_PKCS7_Free(pkcs7);
  26133. pkcs7 = NULL;
  26134. #ifndef NO_PKCS7_STREAM
  26135. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26136. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26137. /* test for streaming */
  26138. ret = -1;
  26139. for (z = 0; z < decodedSz && ret != 0; z++) {
  26140. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26141. if (ret < 0){
  26142. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26143. }
  26144. }
  26145. ExpectIntEQ(ret, 0);
  26146. ExpectNotNull(pkcs7->singleCert);
  26147. ExpectIntNE(pkcs7->singleCertSz, 0);
  26148. wc_PKCS7_Free(pkcs7);
  26149. pkcs7 = NULL;
  26150. #endif /* !NO_PKCS7_STREAM */
  26151. #endif
  26152. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26153. return EXPECT_RESULT();
  26154. }
  26155. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26156. {
  26157. EXPECT_DECLS;
  26158. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26159. && !defined(NO_AES)
  26160. PKCS7* pkcs7 = NULL;
  26161. void* heap = NULL;
  26162. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26163. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26164. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26165. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26166. wc_PKCS7_Free(pkcs7);
  26167. #endif
  26168. return EXPECT_RESULT();
  26169. }
  26170. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26171. {
  26172. EXPECT_DECLS;
  26173. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26174. && !defined(NO_AES)
  26175. PKCS7* pkcs7 = NULL;
  26176. void* heap = NULL;
  26177. WOLFSSL_CTX* ctx = NULL;
  26178. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26179. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26180. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26181. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26182. wc_PKCS7_Free(pkcs7);
  26183. #endif
  26184. return EXPECT_RESULT();
  26185. }
  26186. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26187. {
  26188. EXPECT_DECLS;
  26189. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26190. && !defined(NO_AES)
  26191. PKCS7* pkcs7 = NULL;
  26192. void* heap = NULL;
  26193. WOLFSSL_CTX* ctx = NULL;
  26194. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26195. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26196. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26197. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26198. wc_PKCS7_Free(pkcs7);
  26199. #endif
  26200. return EXPECT_RESULT();
  26201. }
  26202. static int test_wc_PKCS7_DecodeCompressedData(void)
  26203. {
  26204. EXPECT_DECLS;
  26205. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26206. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26207. PKCS7* pkcs7 = NULL;
  26208. void* heap = NULL;
  26209. byte out[4096];
  26210. byte* decompressed = NULL;
  26211. int outSz;
  26212. int decompressedSz;
  26213. const char* cert = "./certs/client-cert.pem";
  26214. byte* cert_buf = NULL;
  26215. size_t cert_sz = 0;
  26216. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26217. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26218. DYNAMIC_TYPE_TMP_BUFFER)));
  26219. decompressedSz = (int)cert_sz;
  26220. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26221. if (pkcs7 != NULL) {
  26222. pkcs7->content = (byte*)cert_buf;
  26223. pkcs7->contentSz = (word32)cert_sz;
  26224. pkcs7->contentOID = DATA;
  26225. }
  26226. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26227. sizeof(out))), 0);
  26228. wc_PKCS7_Free(pkcs7);
  26229. pkcs7 = NULL;
  26230. /* compressed key should be smaller than when started */
  26231. ExpectIntLT(outSz, cert_sz);
  26232. /* test decompression */
  26233. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26234. ExpectIntEQ(pkcs7->contentOID, 0);
  26235. /* fail case with out buffer too small */
  26236. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26237. decompressed, outSz), 0);
  26238. /* success case */
  26239. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26240. decompressed, decompressedSz), cert_sz);
  26241. ExpectIntEQ(pkcs7->contentOID, DATA);
  26242. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26243. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26244. decompressed = NULL;
  26245. /* test decompression function with different 'max' inputs */
  26246. outSz = sizeof(out);
  26247. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26248. 0);
  26249. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26250. out, outSz, 0, heap), 0);
  26251. ExpectNull(decompressed);
  26252. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26253. out, outSz, 0, heap), 0);
  26254. ExpectNotNull(decompressed);
  26255. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26256. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26257. decompressed = NULL;
  26258. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26259. out, outSz, 0, heap), 0);
  26260. ExpectNotNull(decompressed);
  26261. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26262. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26263. if (cert_buf != NULL)
  26264. free(cert_buf);
  26265. wc_PKCS7_Free(pkcs7);
  26266. #endif
  26267. return EXPECT_RESULT();
  26268. }
  26269. static int test_wc_i2d_PKCS12(void)
  26270. {
  26271. EXPECT_DECLS;
  26272. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26273. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26274. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26275. WC_PKCS12* pkcs12 = NULL;
  26276. unsigned char der[FOURK_BUF * 2];
  26277. unsigned char* pt;
  26278. int derSz = 0;
  26279. unsigned char out[FOURK_BUF * 2];
  26280. int outSz = FOURK_BUF * 2;
  26281. const char p12_f[] = "./certs/test-servercert.p12";
  26282. XFILE f = XBADFILE;
  26283. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26284. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26285. if (f != XBADFILE)
  26286. XFCLOSE(f);
  26287. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26288. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26289. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26290. ExpectIntEQ(outSz, derSz);
  26291. outSz = derSz - 1;
  26292. pt = out;
  26293. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26294. outSz = derSz;
  26295. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26296. ExpectIntEQ((pt == out), 0);
  26297. pt = NULL;
  26298. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26299. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26300. wc_PKCS12_free(pkcs12);
  26301. pkcs12 = NULL;
  26302. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26303. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26304. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26305. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26306. ExpectIntEQ(outSz, derSz);
  26307. wc_PKCS12_free(pkcs12);
  26308. pkcs12 = NULL;
  26309. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26310. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26311. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26312. ExpectIntEQ(outSz, derSz);
  26313. wc_PKCS12_free(pkcs12);
  26314. pkcs12 = NULL;
  26315. #endif
  26316. return EXPECT_RESULT();
  26317. }
  26318. /* Testing wc_SignatureGetSize() for signature type ECC */
  26319. static int test_wc_SignatureGetSize_ecc(void)
  26320. {
  26321. EXPECT_DECLS;
  26322. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26323. enum wc_SignatureType sig_type;
  26324. word32 key_len;
  26325. ecc_key ecc;
  26326. const char* qx =
  26327. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26328. const char* qy =
  26329. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26330. const char* d =
  26331. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26332. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26333. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26334. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26335. /* Input for signature type ECC */
  26336. sig_type = WC_SIGNATURE_TYPE_ECC;
  26337. key_len = sizeof(ecc_key);
  26338. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26339. /* Test bad args */
  26340. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26341. sig_type = (enum wc_SignatureType) 100;
  26342. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26343. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26344. sig_type = WC_SIGNATURE_TYPE_ECC;
  26345. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26346. key_len = (word32)0;
  26347. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26348. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26349. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26350. return EXPECT_RESULT();
  26351. } /* END test_wc_SignatureGetSize_ecc() */
  26352. /* Testing wc_SignatureGetSize() for signature type rsa */
  26353. static int test_wc_SignatureGetSize_rsa(void)
  26354. {
  26355. EXPECT_DECLS;
  26356. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26357. enum wc_SignatureType sig_type;
  26358. word32 key_len;
  26359. word32 idx = 0;
  26360. RsaKey rsa_key;
  26361. byte* tmp = NULL;
  26362. size_t bytes;
  26363. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26364. #ifdef USE_CERT_BUFFERS_1024
  26365. bytes = (size_t)sizeof_client_key_der_1024;
  26366. if (bytes < (size_t)sizeof_client_key_der_1024)
  26367. bytes = (size_t)sizeof_client_cert_der_1024;
  26368. #elif defined(USE_CERT_BUFFERS_2048)
  26369. bytes = (size_t)sizeof_client_key_der_2048;
  26370. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26371. bytes = (size_t)sizeof_client_cert_der_2048;
  26372. #else
  26373. bytes = FOURK_BUF;
  26374. #endif
  26375. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  26376. DYNAMIC_TYPE_TMP_BUFFER));
  26377. if (tmp != NULL) {
  26378. #ifdef USE_CERT_BUFFERS_1024
  26379. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  26380. #elif defined(USE_CERT_BUFFERS_2048)
  26381. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  26382. #elif !defined(NO_FILESYSTEM)
  26383. XFILE file = XBADFILE;
  26384. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  26385. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  26386. if (file != XBADFILE)
  26387. XFCLOSE(file);
  26388. }
  26389. #else
  26390. ExpectFail();
  26391. #endif
  26392. }
  26393. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  26394. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  26395. /* Input for signature type RSA */
  26396. sig_type = WC_SIGNATURE_TYPE_RSA;
  26397. key_len = sizeof(RsaKey);
  26398. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  26399. /* Test bad args */
  26400. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26401. sig_type = (enum wc_SignatureType)100;
  26402. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26403. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26404. sig_type = WC_SIGNATURE_TYPE_RSA;
  26405. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  26406. key_len = (word32)0;
  26407. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26408. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  26409. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26410. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  26411. return EXPECT_RESULT();
  26412. } /* END test_wc_SignatureGetSize_rsa(void) */
  26413. /*----------------------------------------------------------------------------*
  26414. | hash.h Tests
  26415. *----------------------------------------------------------------------------*/
  26416. static int test_wc_HashInit(void)
  26417. {
  26418. EXPECT_DECLS;
  26419. int i; /* 0 indicates tests passed, 1 indicates failure */
  26420. wc_HashAlg hash;
  26421. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26422. enum wc_HashType enumArray[] = {
  26423. #ifndef NO_MD5
  26424. WC_HASH_TYPE_MD5,
  26425. #endif
  26426. #ifndef NO_SHA
  26427. WC_HASH_TYPE_SHA,
  26428. #endif
  26429. #ifdef WOLFSSL_SHA224
  26430. WC_HASH_TYPE_SHA224,
  26431. #endif
  26432. #ifndef NO_SHA256
  26433. WC_HASH_TYPE_SHA256,
  26434. #endif
  26435. #ifdef WOLFSSL_SHA384
  26436. WC_HASH_TYPE_SHA384,
  26437. #endif
  26438. #ifdef WOLFSSL_SHA512
  26439. WC_HASH_TYPE_SHA512,
  26440. #endif
  26441. };
  26442. /* dynamically finds the length */
  26443. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26444. /* For loop to test various arguments... */
  26445. for (i = 0; i < enumlen; i++) {
  26446. /* check for bad args */
  26447. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26448. wc_HashFree(&hash, enumArray[i]);
  26449. /* check for null ptr */
  26450. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  26451. } /* end of for loop */
  26452. return EXPECT_RESULT();
  26453. } /* end of test_wc_HashInit */
  26454. /*
  26455. * Unit test function for wc_HashSetFlags()
  26456. */
  26457. static int test_wc_HashSetFlags(void)
  26458. {
  26459. EXPECT_DECLS;
  26460. #ifdef WOLFSSL_HASH_FLAGS
  26461. wc_HashAlg hash;
  26462. word32 flags = 0;
  26463. int i, j;
  26464. int notSupportedLen;
  26465. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26466. enum wc_HashType enumArray[] = {
  26467. #ifndef NO_MD5
  26468. WC_HASH_TYPE_MD5,
  26469. #endif
  26470. #ifndef NO_SHA
  26471. WC_HASH_TYPE_SHA,
  26472. #endif
  26473. #ifdef WOLFSSL_SHA224
  26474. WC_HASH_TYPE_SHA224,
  26475. #endif
  26476. #ifndef NO_SHA256
  26477. WC_HASH_TYPE_SHA256,
  26478. #endif
  26479. #ifdef WOLFSSL_SHA384
  26480. WC_HASH_TYPE_SHA384,
  26481. #endif
  26482. #ifdef WOLFSSL_SHA512
  26483. WC_HASH_TYPE_SHA512,
  26484. #endif
  26485. #ifdef WOLFSSL_SHA3
  26486. WC_HASH_TYPE_SHA3_224,
  26487. #endif
  26488. };
  26489. enum wc_HashType notSupported[] = {
  26490. WC_HASH_TYPE_MD5_SHA,
  26491. WC_HASH_TYPE_MD2,
  26492. WC_HASH_TYPE_MD4,
  26493. WC_HASH_TYPE_BLAKE2B,
  26494. WC_HASH_TYPE_BLAKE2S,
  26495. WC_HASH_TYPE_NONE,
  26496. };
  26497. /* dynamically finds the length */
  26498. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26499. /* For loop to test various arguments... */
  26500. for (i = 0; i < enumlen; i++) {
  26501. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26502. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  26503. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26504. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  26505. wc_HashFree(&hash, enumArray[i]);
  26506. }
  26507. /* For loop to test not supported cases */
  26508. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26509. for (j = 0; j < notSupportedLen; j++) {
  26510. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26511. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  26512. BAD_FUNC_ARG);
  26513. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26514. }
  26515. #endif
  26516. return EXPECT_RESULT();
  26517. } /* END test_wc_HashSetFlags */
  26518. /*
  26519. * Unit test function for wc_HashGetFlags()
  26520. */
  26521. static int test_wc_HashGetFlags(void)
  26522. {
  26523. EXPECT_DECLS;
  26524. #ifdef WOLFSSL_HASH_FLAGS
  26525. wc_HashAlg hash;
  26526. word32 flags = 0;
  26527. int i, j;
  26528. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26529. enum wc_HashType enumArray[] = {
  26530. #ifndef NO_MD5
  26531. WC_HASH_TYPE_MD5,
  26532. #endif
  26533. #ifndef NO_SHA
  26534. WC_HASH_TYPE_SHA,
  26535. #endif
  26536. #ifdef WOLFSSL_SHA224
  26537. WC_HASH_TYPE_SHA224,
  26538. #endif
  26539. #ifndef NO_SHA256
  26540. WC_HASH_TYPE_SHA256,
  26541. #endif
  26542. #ifdef WOLFSSL_SHA384
  26543. WC_HASH_TYPE_SHA384,
  26544. #endif
  26545. #ifdef WOLFSSL_SHA512
  26546. WC_HASH_TYPE_SHA512,
  26547. #endif
  26548. #ifdef WOLFSSL_SHA3
  26549. WC_HASH_TYPE_SHA3_224,
  26550. #endif
  26551. };
  26552. enum wc_HashType notSupported[] = {
  26553. WC_HASH_TYPE_MD5_SHA,
  26554. WC_HASH_TYPE_MD2,
  26555. WC_HASH_TYPE_MD4,
  26556. WC_HASH_TYPE_BLAKE2B,
  26557. WC_HASH_TYPE_BLAKE2S,
  26558. WC_HASH_TYPE_NONE,
  26559. };
  26560. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26561. int notSupportedLen;
  26562. /* For loop to test various arguments... */
  26563. for (i = 0; i < enumlen; i++) {
  26564. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26565. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  26566. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26567. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  26568. wc_HashFree(&hash, enumArray[i]);
  26569. }
  26570. /* For loop to test not supported cases */
  26571. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26572. for (j = 0; j < notSupportedLen; j++) {
  26573. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26574. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  26575. BAD_FUNC_ARG);
  26576. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26577. }
  26578. #endif
  26579. return EXPECT_RESULT();
  26580. } /* END test_wc_HashGetFlags */
  26581. /*----------------------------------------------------------------------------*
  26582. | Compatibility Tests
  26583. *----------------------------------------------------------------------------*/
  26584. /*----------------------------------------------------------------------------*
  26585. | ASN.1 Tests
  26586. *----------------------------------------------------------------------------*/
  26587. static int test_wolfSSL_ASN1_BIT_STRING(void)
  26588. {
  26589. EXPECT_DECLS;
  26590. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  26591. ASN1_BIT_STRING* str = NULL;
  26592. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26593. /* Empty data testing. */
  26594. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  26595. ASN1_BIT_STRING_free(str);
  26596. str = NULL;
  26597. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26598. /* Invalid parameter testing. */
  26599. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  26600. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  26601. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  26602. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  26603. /* No bit string - bit is always 0. */
  26604. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  26605. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  26606. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26607. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  26608. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  26609. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  26610. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  26611. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26612. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  26613. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  26614. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  26615. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  26616. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  26617. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  26618. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  26619. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  26620. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  26621. ASN1_BIT_STRING_free(str);
  26622. ASN1_BIT_STRING_free(NULL);
  26623. #endif
  26624. return EXPECT_RESULT();
  26625. }
  26626. static int test_wolfSSL_ASN1_INTEGER(void)
  26627. {
  26628. EXPECT_DECLS;
  26629. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26630. ASN1_INTEGER* a = NULL;
  26631. ASN1_INTEGER* dup = NULL;
  26632. const unsigned char invalidLenDer[] = {
  26633. 0x02, 0x20, 0x00
  26634. };
  26635. const unsigned char longDer[] = {
  26636. 0x02, 0x20,
  26637. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26638. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26639. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26640. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  26641. };
  26642. const unsigned char* p;
  26643. /* Invalid parameter testing. */
  26644. ASN1_INTEGER_free(NULL);
  26645. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  26646. ExpectNotNull(a = ASN1_INTEGER_new());
  26647. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  26648. ASN1_INTEGER_free(dup);
  26649. dup = NULL;
  26650. ASN1_INTEGER_free(a);
  26651. a = NULL;
  26652. p = longDer;
  26653. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  26654. p = longDer;
  26655. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  26656. ExpectPtrNE(p, longDer);
  26657. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  26658. ASN1_INTEGER_free(dup);
  26659. ASN1_INTEGER_free(a);
  26660. #endif
  26661. return EXPECT_RESULT();
  26662. }
  26663. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  26664. {
  26665. EXPECT_DECLS;
  26666. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26667. ASN1_INTEGER* a = NULL;
  26668. ASN1_INTEGER* b = NULL;
  26669. ExpectNotNull(a = ASN1_INTEGER_new());
  26670. ExpectNotNull(b = ASN1_INTEGER_new());
  26671. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  26672. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  26673. /* Invalid parameter testing. */
  26674. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  26675. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  26676. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  26677. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26678. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  26679. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26680. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  26681. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26682. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  26683. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26684. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  26685. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  26686. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26687. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  26688. ASN1_INTEGER_free(b);
  26689. ASN1_INTEGER_free(a);
  26690. #endif
  26691. return EXPECT_RESULT();
  26692. }
  26693. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  26694. {
  26695. EXPECT_DECLS;
  26696. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26697. ASN1_INTEGER* ai = NULL;
  26698. ASN1_INTEGER* ai2 = NULL;
  26699. BIGNUM* bn = NULL;
  26700. BIGNUM* bn2 = NULL;
  26701. ExpectNotNull(ai = ASN1_INTEGER_new());
  26702. ExpectNotNull(bn2 = BN_new());
  26703. /* Invalid parameter testing. */
  26704. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  26705. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  26706. /* at the moment hard setting since no set function */
  26707. if (ai != NULL) {
  26708. ai->data[0] = 0xff; /* No DER encoding. */
  26709. ai->length = 1;
  26710. }
  26711. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26712. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26713. BN_free(bn);
  26714. bn = NULL;
  26715. #else
  26716. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  26717. #endif
  26718. if (ai != NULL) {
  26719. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26720. ai->data[1] = 0x04; /* bad length of integer */
  26721. ai->data[2] = 0x03;
  26722. ai->length = 3;
  26723. }
  26724. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26725. /* Interpreted as a number 0x020403. */
  26726. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26727. BN_free(bn);
  26728. bn = NULL;
  26729. #else
  26730. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  26731. #endif
  26732. if (ai != NULL) {
  26733. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26734. ai->data[1] = 0x01; /* length of integer */
  26735. ai->data[2] = 0x03;
  26736. ai->length = 3;
  26737. }
  26738. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26739. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  26740. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26741. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26742. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26743. if (ai != NULL) {
  26744. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26745. ai->data[1] = 0x02; /* length of integer */
  26746. ai->data[2] = 0x00; /* padding byte to ensure positive */
  26747. ai->data[3] = 0xff;
  26748. ai->length = 4;
  26749. }
  26750. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26751. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26752. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26753. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26754. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26755. if (ai != NULL) {
  26756. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26757. ai->data[1] = 0x01; /* length of integer */
  26758. ai->data[2] = 0x00;
  26759. ai->length = 3;
  26760. }
  26761. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26762. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26763. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26764. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26765. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26766. if (ai != NULL) {
  26767. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26768. ai->data[1] = 0x01; /* length of integer */
  26769. ai->data[2] = 0x01;
  26770. ai->length = 3;
  26771. ai->negative = 1;
  26772. }
  26773. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26774. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26775. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26776. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26777. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26778. BN_free(bn2);
  26779. BN_free(bn);
  26780. ASN1_INTEGER_free(ai2);
  26781. ASN1_INTEGER_free(ai);
  26782. #endif
  26783. return EXPECT_RESULT();
  26784. }
  26785. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  26786. {
  26787. EXPECT_DECLS;
  26788. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26789. ASN1_INTEGER *a = NULL;
  26790. long val;
  26791. ExpectNotNull(a = ASN1_INTEGER_new());
  26792. /* Invalid parameter testing. */
  26793. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  26794. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26795. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  26796. #else
  26797. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  26798. #endif
  26799. ASN1_INTEGER_free(a);
  26800. a = NULL;
  26801. ExpectNotNull(a = ASN1_INTEGER_new());
  26802. val = 0;
  26803. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  26804. ASN1_INTEGER_free(a);
  26805. a = NULL;
  26806. /* 0 */
  26807. ExpectNotNull(a = ASN1_INTEGER_new());
  26808. val = 0;
  26809. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26810. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26811. ASN1_INTEGER_free(a);
  26812. a = NULL;
  26813. /* 40 */
  26814. ExpectNotNull(a = ASN1_INTEGER_new());
  26815. val = 40;
  26816. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26817. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26818. ASN1_INTEGER_free(a);
  26819. a = NULL;
  26820. /* -40 */
  26821. ExpectNotNull(a = ASN1_INTEGER_new());
  26822. val = -40;
  26823. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26824. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26825. ASN1_INTEGER_free(a);
  26826. a = NULL;
  26827. /* 128 */
  26828. ExpectNotNull(a = ASN1_INTEGER_new());
  26829. val = 128;
  26830. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26831. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26832. ASN1_INTEGER_free(a);
  26833. a = NULL;
  26834. /* -128 */
  26835. ExpectNotNull(a = ASN1_INTEGER_new());
  26836. val = -128;
  26837. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26838. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26839. ASN1_INTEGER_free(a);
  26840. a = NULL;
  26841. /* 200 */
  26842. ExpectNotNull(a = ASN1_INTEGER_new());
  26843. val = 200;
  26844. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26845. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26846. ASN1_INTEGER_free(a);
  26847. a = NULL;
  26848. /* int max (2147483647) */
  26849. ExpectNotNull(a = ASN1_INTEGER_new());
  26850. val = 2147483647;
  26851. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26852. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26853. ASN1_INTEGER_free(a);
  26854. a = NULL;
  26855. /* int min (-2147483648) */
  26856. ExpectNotNull(a = ASN1_INTEGER_new());
  26857. val = -2147483647 - 1;
  26858. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26859. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26860. ASN1_INTEGER_free(a);
  26861. a = NULL;
  26862. /* long max positive */
  26863. ExpectNotNull(a = ASN1_INTEGER_new());
  26864. val = (long)(((unsigned long)-1) >> 1);
  26865. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26866. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26867. ASN1_INTEGER_free(a);
  26868. #endif
  26869. return EXPECT_RESULT();
  26870. }
  26871. #if defined(OPENSSL_EXTRA)
  26872. typedef struct ASN1IntTestVector {
  26873. const byte* der;
  26874. const size_t derSz;
  26875. const long value;
  26876. } ASN1IntTestVector;
  26877. #endif
  26878. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  26879. {
  26880. EXPECT_DECLS;
  26881. #if defined(OPENSSL_EXTRA)
  26882. size_t i;
  26883. WOLFSSL_ASN1_INTEGER* a = NULL;
  26884. WOLFSSL_ASN1_INTEGER* b = NULL;
  26885. WOLFSSL_ASN1_INTEGER* c = NULL;
  26886. const byte* p = NULL;
  26887. byte* p2 = NULL;
  26888. byte* reEncoded = NULL;
  26889. int reEncodedSz = 0;
  26890. static const byte zeroDer[] = {
  26891. 0x02, 0x01, 0x00
  26892. };
  26893. static const byte oneDer[] = {
  26894. 0x02, 0x01, 0x01
  26895. };
  26896. static const byte negativeDer[] = {
  26897. 0x02, 0x03, 0xC1, 0x16, 0x0D
  26898. };
  26899. static const byte positiveDer[] = {
  26900. 0x02, 0x03, 0x01, 0x00, 0x01
  26901. };
  26902. static const byte primeDer[] = {
  26903. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  26904. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  26905. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  26906. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  26907. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  26908. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  26909. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  26910. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  26911. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  26912. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  26913. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  26914. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  26915. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  26916. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  26917. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  26918. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  26919. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  26920. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  26921. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  26922. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  26923. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  26924. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  26925. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  26926. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  26927. };
  26928. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  26929. static const ASN1IntTestVector testVectors[] = {
  26930. {zeroDer, sizeof(zeroDer), 0},
  26931. {oneDer, sizeof(oneDer), 1},
  26932. {negativeDer, sizeof(negativeDer), -4123123},
  26933. {positiveDer, sizeof(positiveDer), 65537},
  26934. {primeDer, sizeof(primeDer), 0}
  26935. };
  26936. static const size_t NUM_TEST_VECTORS =
  26937. sizeof(testVectors)/sizeof(testVectors[0]);
  26938. /* Check d2i error conditions */
  26939. /* NULL pointer to input. */
  26940. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  26941. ExpectNull(b);
  26942. /* NULL input. */
  26943. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  26944. ExpectNull(b);
  26945. /* 0 length. */
  26946. p = testVectors[0].der;
  26947. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  26948. ExpectNull(b);
  26949. /* Negative length. */
  26950. p = testVectors[0].der;
  26951. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  26952. ExpectNull(b);
  26953. /* Garbage DER input. */
  26954. p = garbageDer;
  26955. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  26956. ExpectNull(b);
  26957. /* Check i2d error conditions */
  26958. /* NULL input. */
  26959. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  26960. /* 0 length input data buffer (a->length == 0). */
  26961. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  26962. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  26963. if (a != NULL)
  26964. a->data = NULL;
  26965. /* NULL input data buffer. */
  26966. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  26967. if (a != NULL) {
  26968. /* Reset a->data. */
  26969. a->data = a->intData;
  26970. }
  26971. /* Set a to valid value. */
  26972. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  26973. /* NULL output buffer. */
  26974. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  26975. wolfSSL_ASN1_INTEGER_free(a);
  26976. a = NULL;
  26977. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  26978. p = testVectors[i].der;
  26979. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  26980. testVectors[i].derSz));
  26981. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26982. if (testVectors[i].derSz <= sizeof(long)) {
  26983. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  26984. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  26985. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  26986. wolfSSL_ASN1_INTEGER_free(c);
  26987. c = NULL;
  26988. }
  26989. /* Convert to DER without a pre-allocated output buffer. */
  26990. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  26991. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  26992. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  26993. /* Convert to DER with a pre-allocated output buffer. In this case, the
  26994. * output buffer pointer should be incremented just past the end of the
  26995. * encoded data. */
  26996. p2 = reEncoded;
  26997. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  26998. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  26999. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27000. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27001. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27002. reEncoded = NULL;
  27003. wolfSSL_ASN1_INTEGER_free(a);
  27004. a = NULL;
  27005. }
  27006. #endif /* OPENSSL_EXTRA */
  27007. return EXPECT_RESULT();
  27008. }
  27009. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27010. {
  27011. EXPECT_DECLS;
  27012. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27013. BIO* bio = NULL;
  27014. BIO* out = NULL;
  27015. BIO* fixed = NULL;
  27016. ASN1_INTEGER* ai = NULL;
  27017. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27018. char tmp[1024];
  27019. int tmpSz;
  27020. const char expected1[] = "123456";
  27021. const char expected2[] = "112345678912345678901234567890";
  27022. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27023. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27024. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27025. ExpectNotNull(ai = ASN1_INTEGER_new());
  27026. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27027. /* Invalid parameter testing. */
  27028. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27029. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27030. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27031. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27032. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27033. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27034. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27035. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27036. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27037. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27038. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27039. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27040. /* No data to read from BIO. */
  27041. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27042. /* read first line */
  27043. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27044. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27045. XMEMSET(tmp, 0, 1024);
  27046. tmpSz = BIO_read(out, tmp, 1024);
  27047. ExpectIntEQ(tmpSz, 6);
  27048. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27049. /* fail on second line (not % 2) */
  27050. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27051. /* read 3rd long line */
  27052. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27053. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27054. XMEMSET(tmp, 0, 1024);
  27055. tmpSz = BIO_read(out, tmp, 1024);
  27056. ExpectIntEQ(tmpSz, 30);
  27057. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27058. /* fail on empty line */
  27059. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27060. BIO_free(bio);
  27061. bio = NULL;
  27062. /* Make long integer, requiring dynamic memory, even longer. */
  27063. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27064. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27065. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27066. XMEMSET(tmp, 0, 1024);
  27067. tmpSz = BIO_read(out, tmp, 1024);
  27068. ExpectIntEQ(tmpSz, 48);
  27069. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27070. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27071. XMEMSET(tmp, 0, 1024);
  27072. tmpSz = BIO_read(out, tmp, 1024);
  27073. ExpectIntEQ(tmpSz, 56);
  27074. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27075. BIO_free(bio);
  27076. BIO_free(out);
  27077. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27078. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27079. /* Ensure there is 0 bytes available to write into. */
  27080. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27081. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27082. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27083. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27084. BIO_free(fixed);
  27085. ASN1_INTEGER_free(ai);
  27086. #endif
  27087. return EXPECT_RESULT();
  27088. }
  27089. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27090. {
  27091. EXPECT_DECLS;
  27092. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27093. ASN1_INTEGER *a = NULL;
  27094. unsigned char *pp = NULL,*tpp = NULL;
  27095. int ret = 0;
  27096. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27097. /* Invalid parameter testing. */
  27098. /* Set pp to an invalid value. */
  27099. pp = NULL;
  27100. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27101. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27102. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27103. /* 40 */
  27104. if (a != NULL) {
  27105. a->intData[0] = ASN_INTEGER;
  27106. a->intData[1] = 1;
  27107. a->intData[2] = 40;
  27108. }
  27109. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27110. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27111. DYNAMIC_TYPE_TMP_BUFFER));
  27112. tpp = pp;
  27113. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27114. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27115. tpp--;
  27116. ExpectIntEQ(*tpp, 40);
  27117. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27118. pp = NULL;
  27119. /* 128 */
  27120. if (a != NULL) {
  27121. a->intData[0] = ASN_INTEGER;
  27122. a->intData[1] = 1;
  27123. a->intData[2] = 128;
  27124. }
  27125. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27126. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27127. DYNAMIC_TYPE_TMP_BUFFER));
  27128. tpp = pp;
  27129. if (tpp != NULL) {
  27130. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27131. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27132. tpp--;
  27133. ExpectIntEQ(*(tpp--), 128);
  27134. ExpectIntEQ(*tpp, 0);
  27135. }
  27136. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27137. pp = NULL;
  27138. /* -40 */
  27139. if (a != NULL) {
  27140. a->intData[0] = ASN_INTEGER;
  27141. a->intData[1] = 1;
  27142. a->intData[2] = 40;
  27143. a->negative = 1;
  27144. }
  27145. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27146. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27147. DYNAMIC_TYPE_TMP_BUFFER));
  27148. tpp = pp;
  27149. if (tpp != NULL) {
  27150. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27151. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27152. tpp--;
  27153. ExpectIntEQ(*tpp, 216);
  27154. }
  27155. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27156. pp = NULL;
  27157. /* -128 */
  27158. if (a != NULL) {
  27159. a->intData[0] = ASN_INTEGER;
  27160. a->intData[1] = 1;
  27161. a->intData[2] = 128;
  27162. a->negative = 1;
  27163. }
  27164. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27165. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27166. DYNAMIC_TYPE_TMP_BUFFER));
  27167. tpp = pp;
  27168. if (tpp != NULL) {
  27169. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27170. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27171. tpp--;
  27172. ExpectIntEQ(*tpp, 128);
  27173. }
  27174. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27175. pp = NULL;
  27176. /* -200 */
  27177. if (a != NULL) {
  27178. a->intData[0] = ASN_INTEGER;
  27179. a->intData[1] = 1;
  27180. a->intData[2] = 200;
  27181. a->negative = 1;
  27182. }
  27183. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27184. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27185. DYNAMIC_TYPE_TMP_BUFFER));
  27186. tpp = pp;
  27187. if (tpp != NULL) {
  27188. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27189. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27190. tpp--;
  27191. ExpectIntEQ(*(tpp--), 56);
  27192. ExpectIntEQ(*tpp, 255);
  27193. }
  27194. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27195. pp = NULL;
  27196. /* Empty */
  27197. if (a != NULL) {
  27198. a->intData[0] = ASN_INTEGER;
  27199. a->intData[1] = 0;
  27200. a->negative = 0;
  27201. }
  27202. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27203. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27204. DYNAMIC_TYPE_TMP_BUFFER));
  27205. tpp = pp;
  27206. if (tpp != NULL) {
  27207. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27208. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27209. tpp--;
  27210. ExpectIntEQ(*tpp, 0);
  27211. }
  27212. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27213. pp = NULL;
  27214. /* 0 */
  27215. if (a != NULL) {
  27216. a->intData[0] = ASN_INTEGER;
  27217. a->intData[1] = 1;
  27218. a->intData[2] = 0;
  27219. a->negative = 1;
  27220. }
  27221. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27222. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27223. DYNAMIC_TYPE_TMP_BUFFER));
  27224. if (tpp != NULL) {
  27225. tpp = pp;
  27226. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27227. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27228. tpp--;
  27229. ExpectIntEQ(*tpp, 0);
  27230. }
  27231. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27232. pp = NULL;
  27233. /* 0x100 */
  27234. if (a != NULL) {
  27235. a->intData[0] = ASN_INTEGER;
  27236. a->intData[1] = 2;
  27237. a->intData[2] = 0x01;
  27238. a->intData[3] = 0x00;
  27239. a->negative = 0;
  27240. }
  27241. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27242. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27243. DYNAMIC_TYPE_TMP_BUFFER));
  27244. if (tpp != NULL) {
  27245. tpp = pp;
  27246. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27247. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27248. tpp -= 2;
  27249. ExpectIntEQ(tpp[0], 0x01);
  27250. ExpectIntEQ(tpp[1], 0x00);
  27251. }
  27252. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27253. pp = NULL;
  27254. /* -0x8000 => 0x8000 */
  27255. if (a != NULL) {
  27256. a->intData[0] = ASN_INTEGER;
  27257. a->intData[1] = 2;
  27258. a->intData[2] = 0x80;
  27259. a->intData[3] = 0x00;
  27260. a->negative = 1;
  27261. }
  27262. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27263. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27264. DYNAMIC_TYPE_TMP_BUFFER));
  27265. tpp = pp;
  27266. if (tpp != NULL) {
  27267. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27268. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27269. tpp -= 2;
  27270. ExpectIntEQ(tpp[0], 0x80);
  27271. ExpectIntEQ(tpp[1], 0x00);
  27272. }
  27273. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27274. pp = NULL;
  27275. /* -0x8001 => 0xFF7FFF */
  27276. if (a != NULL) {
  27277. a->intData[0] = ASN_INTEGER;
  27278. a->intData[1] = 2;
  27279. a->intData[2] = 0x80;
  27280. a->intData[3] = 0x01;
  27281. a->negative = 1;
  27282. }
  27283. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27284. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27285. DYNAMIC_TYPE_TMP_BUFFER));
  27286. tpp = pp;
  27287. if (tpp != NULL) {
  27288. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27289. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27290. tpp -= 3;
  27291. ExpectIntEQ(tpp[0], 0xFF);
  27292. ExpectIntEQ(tpp[1], 0x7F);
  27293. ExpectIntEQ(tpp[2], 0xFF);
  27294. }
  27295. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27296. wolfSSL_ASN1_INTEGER_free(a);
  27297. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27298. return EXPECT_RESULT();
  27299. }
  27300. static int test_wolfSSL_ASN1_OBJECT(void)
  27301. {
  27302. EXPECT_DECLS;
  27303. #if defined(OPENSSL_EXTRA)
  27304. ASN1_OBJECT* a = NULL;
  27305. ASN1_OBJECT s;
  27306. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27307. /* Invalid parameter testing. */
  27308. ASN1_OBJECT_free(NULL);
  27309. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27310. /* Test that a static ASN1_OBJECT can be freed. */
  27311. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27312. ASN1_OBJECT_free(&s);
  27313. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27314. ASN1_OBJECT_free(a);
  27315. a = NULL;
  27316. s.obj = der;
  27317. s.objSz = sizeof(der);
  27318. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27319. ASN1_OBJECT_free(a);
  27320. ASN1_OBJECT_free(&s);
  27321. #endif /* OPENSSL_EXTRA */
  27322. return EXPECT_RESULT();
  27323. }
  27324. static int test_wolfSSL_ASN1_get_object(void)
  27325. {
  27326. EXPECT_DECLS;
  27327. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27328. const unsigned char* derBuf = cliecc_cert_der_256;
  27329. const unsigned char* nullPtr = NULL;
  27330. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27331. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27332. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27333. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27334. const unsigned char* p;
  27335. unsigned char objDer[8];
  27336. unsigned char* der;
  27337. unsigned char* derPtr;
  27338. int len = sizeof_cliecc_cert_der_256;
  27339. long asnLen = 0;
  27340. int tag = 0;
  27341. int cls = 0;
  27342. ASN1_OBJECT* a = NULL;
  27343. ASN1_OBJECT s;
  27344. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27345. /* Invalid encoding at length. */
  27346. p = objDerInvalidLen;
  27347. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27348. 0x80);
  27349. p = objDerBadLen;
  27350. /* Error = 0x80, Constructed = 0x20 */
  27351. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27352. 0x80 | 0x20);
  27353. /* Read a couple TLV triplets and make sure they match the expected values
  27354. */
  27355. /* SEQUENCE */
  27356. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27357. ExpectIntEQ(asnLen, 861);
  27358. ExpectIntEQ(tag, 0x10);
  27359. ExpectIntEQ(cls, 0);
  27360. /* SEQUENCE */
  27361. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27362. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27363. ExpectIntEQ(asnLen, 772);
  27364. ExpectIntEQ(tag, 0x10);
  27365. ExpectIntEQ(cls, 0);
  27366. /* [0] */
  27367. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27368. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27369. ExpectIntEQ(asnLen, 3);
  27370. ExpectIntEQ(tag, 0);
  27371. ExpectIntEQ(cls, 0x80);
  27372. /* INTEGER */
  27373. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27374. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27375. ExpectIntEQ(asnLen, 1);
  27376. ExpectIntEQ(tag, 0x2);
  27377. ExpectIntEQ(cls, 0);
  27378. derBuf += asnLen;
  27379. /* INTEGER */
  27380. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27381. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27382. ExpectIntEQ(asnLen, 20);
  27383. ExpectIntEQ(tag, 0x2);
  27384. ExpectIntEQ(cls, 0);
  27385. derBuf += asnLen;
  27386. /* SEQUENCE */
  27387. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27388. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27389. ExpectIntEQ(asnLen, 10);
  27390. ExpectIntEQ(tag, 0x10);
  27391. ExpectIntEQ(cls, 0);
  27392. /* Found OBJECT_ID. */
  27393. /* Invalid parameter testing. */
  27394. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  27395. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  27396. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  27397. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  27398. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  27399. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  27400. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  27401. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  27402. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  27403. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  27404. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27405. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27406. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  27407. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  27408. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  27409. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  27410. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  27411. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27412. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27413. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  27414. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  27415. /* Invalid encoding at length. */
  27416. p = objDerInvalidLen;
  27417. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  27418. p = objDerBadLen;
  27419. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  27420. p = objDerNotObj;
  27421. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  27422. p = objDerNoData;
  27423. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  27424. /* Create an ASN OBJECT from content */
  27425. p = derBuf + 2;
  27426. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  27427. ASN1_OBJECT_free(a);
  27428. a = NULL;
  27429. /* Create an ASN OBJECT from DER */
  27430. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  27431. /* Invalid parameter testing. */
  27432. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  27433. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  27434. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  27435. der = NULL;
  27436. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  27437. derPtr = objDer;
  27438. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  27439. ExpectPtrNE(derPtr, objDer);
  27440. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  27441. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  27442. ASN1_OBJECT_free(a);
  27443. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27444. return EXPECT_RESULT();
  27445. }
  27446. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  27447. {
  27448. EXPECT_DECLS;
  27449. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27450. ASN1_OBJECT* obj = NULL;
  27451. ASN1_OBJECT* a = NULL;
  27452. BIO *bio = NULL;
  27453. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  27454. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  27455. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  27456. const unsigned char* p;
  27457. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  27458. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  27459. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  27460. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  27461. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  27462. /* No DER encoding in ASN1_OBJECT. */
  27463. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  27464. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27465. ASN1_OBJECT_free(a);
  27466. a = NULL;
  27467. /* DER encoding - not OBJECT_ID */
  27468. p = notObjDer;
  27469. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27470. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27471. ASN1_OBJECT_free(a);
  27472. a = NULL;
  27473. /* Bad length encoding. */
  27474. p = badLenDer;
  27475. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27476. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27477. ASN1_OBJECT_free(a);
  27478. a = NULL;
  27479. /* Good encoding - but unknown. */
  27480. p = goodDer;
  27481. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27482. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27483. ASN1_OBJECT_free(a);
  27484. BIO_free(bio);
  27485. ASN1_OBJECT_free(obj);
  27486. #endif
  27487. return EXPECT_RESULT();
  27488. }
  27489. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  27490. {
  27491. EXPECT_DECLS;
  27492. #if defined(OPENSSL_EXTRA) && \
  27493. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  27494. char buf[50] = {0};
  27495. ASN1_OBJECT* obj;
  27496. const char* oid = "2.5.29.19";
  27497. const char* ln = "X509v3 Basic Constraints";
  27498. obj = NULL;
  27499. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  27500. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  27501. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  27502. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  27503. XMEMSET(buf, 0, sizeof(buf));
  27504. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  27505. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  27506. ASN1_OBJECT_free(obj);
  27507. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  27508. return EXPECT_RESULT();
  27509. }
  27510. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  27511. {
  27512. EXPECT_DECLS;
  27513. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  27514. WOLFSSL_STACK* sk = NULL;
  27515. WOLFSSL_ASN1_OBJECT* obj;
  27516. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27517. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27518. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27519. sk = NULL;
  27520. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27521. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  27522. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  27523. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  27524. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27525. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  27526. sk = NULL;
  27527. /* obj freed in pop_free call. */
  27528. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27529. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27530. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27531. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  27532. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27533. wolfSSL_ASN1_OBJECT_free(obj);
  27534. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  27535. return EXPECT_RESULT();
  27536. }
  27537. static int test_wolfSSL_ASN1_STRING(void)
  27538. {
  27539. EXPECT_DECLS;
  27540. #if defined(OPENSSL_EXTRA)
  27541. ASN1_STRING* str = NULL;
  27542. ASN1_STRING* c = NULL;
  27543. const char data[] = "hello wolfSSL";
  27544. const char data2[] = "Same len data";
  27545. const char longData[] =
  27546. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  27547. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27548. ASN1_STRING_free(str);
  27549. str = NULL;
  27550. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27551. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  27552. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  27553. /* Check setting to NULL works. */
  27554. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  27555. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  27556. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27557. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  27558. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  27559. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  27560. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  27561. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  27562. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  27563. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  27564. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  27565. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  27566. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  27567. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27568. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  27569. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  27570. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  27571. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  27572. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27573. /* Check setting back to smaller size frees dynamic data. */
  27574. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27575. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  27576. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  27577. ExpectNull(ASN1_STRING_get0_data(NULL));
  27578. ExpectNotNull(ASN1_STRING_get0_data(str));
  27579. ExpectNull(ASN1_STRING_data(NULL));
  27580. ExpectNotNull(ASN1_STRING_data(str));
  27581. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  27582. ExpectIntGT(ASN1_STRING_length(str), 0);
  27583. ASN1_STRING_free(c);
  27584. ASN1_STRING_free(str);
  27585. ASN1_STRING_free(NULL);
  27586. #ifndef NO_WOLFSSL_STUB
  27587. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  27588. #endif
  27589. #endif
  27590. return EXPECT_RESULT();
  27591. }
  27592. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  27593. {
  27594. EXPECT_DECLS;
  27595. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  27596. !defined(NO_FILESYSTEM)
  27597. WOLFSSL_X509* x509 = NULL;
  27598. WOLFSSL_X509_NAME* subject = NULL;
  27599. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  27600. WOLFSSL_ASN1_STRING* a = NULL;
  27601. FILE* file = XBADFILE;
  27602. int idx = 0;
  27603. char targetOutput[16] = "www.wolfssl.com";
  27604. unsigned char* actual_output = NULL;
  27605. int len = 0;
  27606. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27607. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27608. if (file != NULL)
  27609. fclose(file);
  27610. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  27611. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  27612. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  27613. NID_commonName, -1)), 5);
  27614. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  27615. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  27616. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  27617. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  27618. a = NULL;
  27619. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  27620. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  27621. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  27622. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  27623. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  27624. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  27625. wolfSSL_X509_free(x509);
  27626. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27627. ExpectNotNull(a = ASN1_STRING_new());
  27628. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  27629. ASN1_STRING_free(a);
  27630. #endif
  27631. return EXPECT_RESULT();
  27632. }
  27633. static int test_wolfSSL_i2s_ASN1_STRING(void)
  27634. {
  27635. EXPECT_DECLS;
  27636. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27637. WOLFSSL_ASN1_STRING* str = NULL;
  27638. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  27639. char* ret = NULL;
  27640. ExpectNotNull(str = ASN1_STRING_new());
  27641. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  27642. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27643. ret = NULL;
  27644. /* No data. */
  27645. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27646. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27647. ret = NULL;
  27648. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  27649. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27650. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27651. ret = NULL;
  27652. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  27653. /* No type. */
  27654. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27655. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27656. ASN1_STRING_free(str);
  27657. #endif
  27658. return EXPECT_RESULT();
  27659. }
  27660. static int test_wolfSSL_ASN1_STRING_canon(void)
  27661. {
  27662. EXPECT_DECLS;
  27663. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  27664. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  27665. defined(OPENSSL_EXTRA_X509_SMALL))
  27666. WOLFSSL_ASN1_STRING* orig = NULL;
  27667. WOLFSSL_ASN1_STRING* canon = NULL;
  27668. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  27669. const char* whitespaceOnly = "\t\r\n";
  27670. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  27671. const char* canonData = "\x01 \x02 \xff tt";
  27672. const char longData[] =
  27673. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  27674. ExpectNotNull(orig = ASN1_STRING_new());
  27675. ExpectNotNull(canon = ASN1_STRING_new());
  27676. /* Invalid parameter testing. */
  27677. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  27678. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  27679. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  27680. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27681. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27682. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  27683. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27684. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27685. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  27686. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27687. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27688. ASN1_STRING_free(orig);
  27689. orig = NULL;
  27690. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  27691. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  27692. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27693. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  27694. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27695. ASN1_STRING_free(orig);
  27696. orig = NULL;
  27697. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  27698. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  27699. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27700. ASN1_STRING_free(orig);
  27701. orig = NULL;
  27702. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  27703. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27704. ASN1_STRING_free(orig);
  27705. ASN1_STRING_free(canon);
  27706. #endif
  27707. #endif
  27708. return EXPECT_RESULT();
  27709. }
  27710. static int test_wolfSSL_ASN1_STRING_print(void)
  27711. {
  27712. EXPECT_DECLS;
  27713. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  27714. !defined(NO_BIO)
  27715. ASN1_STRING* asnStr = NULL;
  27716. const char HELLO_DATA[]= \
  27717. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  27718. #define MAX_UNPRINTABLE_CHAR 32
  27719. #define MAX_BUF 255
  27720. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  27721. unsigned char expected[sizeof(unprintableData)+1];
  27722. unsigned char rbuf[MAX_BUF];
  27723. BIO *bio = NULL;
  27724. int p_len;
  27725. int i;
  27726. /* setup */
  27727. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  27728. unprintableData[i] = HELLO_DATA[i];
  27729. expected[i] = HELLO_DATA[i];
  27730. }
  27731. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  27732. unprintableData[sizeof(HELLO_DATA)+i] = i;
  27733. if (i == (int)'\n' || i == (int)'\r')
  27734. expected[sizeof(HELLO_DATA)+i] = i;
  27735. else
  27736. expected[sizeof(HELLO_DATA)+i] = '.';
  27737. }
  27738. unprintableData[sizeof(unprintableData)-1] = '\0';
  27739. expected[sizeof(expected)-1] = '\0';
  27740. XMEMSET(rbuf, 0, MAX_BUF);
  27741. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27742. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  27743. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27744. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  27745. (int)sizeof(unprintableData)), 1);
  27746. /* test */
  27747. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  27748. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  27749. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  27750. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  27751. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  27752. ExpectStrEQ((char*)rbuf, (const char*)expected);
  27753. BIO_free(bio);
  27754. bio = NULL;
  27755. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27756. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  27757. /* Ensure there is 0 bytes available to write into. */
  27758. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  27759. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27760. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  27761. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27762. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  27763. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27764. BIO_free(bio);
  27765. ASN1_STRING_free(asnStr);
  27766. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  27767. return EXPECT_RESULT();
  27768. }
  27769. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  27770. {
  27771. EXPECT_DECLS;
  27772. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27773. ASN1_STRING* asn_str = NULL;
  27774. const char data[] = "Hello wolfSSL!";
  27775. ASN1_STRING* esc_str = NULL;
  27776. const char esc_data[] = "a+;<>";
  27777. ASN1_STRING* neg_int = NULL;
  27778. const char neg_int_data[] = "\xff";
  27779. ASN1_STRING* neg_enum = NULL;
  27780. const char neg_enum_data[] = "\xff";
  27781. BIO *bio = NULL;
  27782. BIO *fixed = NULL;
  27783. unsigned long flags;
  27784. int p_len;
  27785. unsigned char rbuf[255];
  27786. /* setup */
  27787. XMEMSET(rbuf, 0, 255);
  27788. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27789. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  27790. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27791. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27792. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  27793. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27794. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  27795. sizeof(esc_data)), 1);
  27796. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  27797. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  27798. sizeof(neg_int_data) - 1), 1);
  27799. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  27800. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  27801. sizeof(neg_enum_data) - 1), 1);
  27802. /* Invalid parameter testing. */
  27803. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  27804. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  27805. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  27806. /* no flags */
  27807. XMEMSET(rbuf, 0, 255);
  27808. flags = 0;
  27809. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  27810. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  27811. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  27812. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27813. /* Ensure there is 0 bytes available to write into. */
  27814. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27815. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27816. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27817. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27818. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  27819. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27820. /* RFC2253 Escape */
  27821. XMEMSET(rbuf, 0, 255);
  27822. flags = ASN1_STRFLGS_ESC_2253;
  27823. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  27824. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  27825. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  27826. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27827. /* Ensure there is 0 bytes available to write into. */
  27828. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27829. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27830. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27831. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27832. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  27833. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27834. /* Show type */
  27835. XMEMSET(rbuf, 0, 255);
  27836. flags = ASN1_STRFLGS_SHOW_TYPE;
  27837. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  27838. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  27839. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  27840. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27841. /* Ensure there is 0 bytes available to write into. */
  27842. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27843. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27844. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27845. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27846. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  27847. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27848. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  27849. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27850. /* Dump All */
  27851. XMEMSET(rbuf, 0, 255);
  27852. flags = ASN1_STRFLGS_DUMP_ALL;
  27853. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  27854. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  27855. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  27856. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27857. /* Ensure there is 0 bytes available to write into. */
  27858. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27859. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27860. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27861. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27862. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  27863. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27864. /* Dump Der */
  27865. XMEMSET(rbuf, 0, 255);
  27866. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  27867. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  27868. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  27869. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  27870. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27871. /* Ensure there is 0 bytes available to write into. */
  27872. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27873. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27874. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27875. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27876. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  27877. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27878. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  27879. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27880. /* Dump All + Show type */
  27881. XMEMSET(rbuf, 0, 255);
  27882. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27883. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  27884. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  27885. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  27886. /* Dump All + Show type - Negative Integer. */
  27887. XMEMSET(rbuf, 0, 255);
  27888. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27889. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  27890. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  27891. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  27892. /* Dump All + Show type - Negative Enumerated. */
  27893. XMEMSET(rbuf, 0, 255);
  27894. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27895. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  27896. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  27897. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  27898. BIO_free(fixed);
  27899. BIO_free(bio);
  27900. ASN1_STRING_free(asn_str);
  27901. ASN1_STRING_free(esc_str);
  27902. ASN1_STRING_free(neg_int);
  27903. ASN1_STRING_free(neg_enum);
  27904. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  27905. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  27906. #endif
  27907. return EXPECT_RESULT();
  27908. }
  27909. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  27910. {
  27911. EXPECT_DECLS;
  27912. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  27913. ASN1_STRING* asn1str_test = NULL;
  27914. ASN1_STRING* asn1str_answer = NULL;
  27915. /* Each character is encoded using 4 bytes */
  27916. char input[] = {
  27917. 0, 0, 0, 'T',
  27918. 0, 0, 0, 'e',
  27919. 0, 0, 0, 's',
  27920. 0, 0, 0, 't',
  27921. };
  27922. char output[] = "Test";
  27923. char badInput[] = {
  27924. 1, 0, 0, 'T',
  27925. 0, 1, 0, 'e',
  27926. 0, 0, 1, 's',
  27927. };
  27928. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  27929. /* Test wrong type. */
  27930. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27931. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27932. ASN1_STRING_free(asn1str_test);
  27933. asn1str_test = NULL;
  27934. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  27935. /* Test bad length. */
  27936. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  27937. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27938. /* Test bad input. */
  27939. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  27940. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27941. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  27942. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27943. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  27944. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27945. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  27946. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  27947. ExpectNotNull(
  27948. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  27949. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  27950. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  27951. ASN1_STRING_free(asn1str_test);
  27952. ASN1_STRING_free(asn1str_answer);
  27953. #endif /* OPENSSL_ALL && !NO_ASN */
  27954. return EXPECT_RESULT();
  27955. }
  27956. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27957. {
  27958. EXPECT_DECLS;
  27959. #if defined(OPENSSL_EXTRA)
  27960. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  27961. unsigned char nullstr[32];
  27962. XMEMSET(nullstr, 0, 32);
  27963. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27964. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  27965. if (asn1_gtime != NULL) {
  27966. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27967. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27968. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  27969. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27970. }
  27971. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  27972. #endif /* OPENSSL_EXTRA */
  27973. return EXPECT_RESULT();
  27974. }
  27975. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  27976. {
  27977. EXPECT_DECLS;
  27978. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27979. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  27980. BIO* bio = NULL;
  27981. unsigned char buf[24];
  27982. int i;
  27983. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27984. BIO_set_write_buf_size(bio, 24);
  27985. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  27986. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  27987. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  27988. /* Type not set. */
  27989. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  27990. gtime.type = V_ASN1_GENERALIZEDTIME;
  27991. /* Invalid parameters testing. */
  27992. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  27993. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  27994. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  27995. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  27996. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  27997. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  27998. BIO_free(bio);
  27999. bio = NULL;
  28000. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28001. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28002. /* Ensure there is 0 bytes available to write into. */
  28003. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28004. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28005. for (i = 1; i < 20; i++) {
  28006. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28007. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28008. }
  28009. BIO_free(bio);
  28010. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  28011. #endif /* OPENSSL_EXTRA */
  28012. return EXPECT_RESULT();
  28013. }
  28014. static int test_wolfSSL_ASN1_TIME(void)
  28015. {
  28016. EXPECT_DECLS;
  28017. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28018. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28019. unsigned char *data;
  28020. ExpectNotNull(asn_time = ASN1_TIME_new());
  28021. #ifndef NO_WOLFSSL_STUB
  28022. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28023. #endif
  28024. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28025. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28026. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28027. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28028. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28029. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28030. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28031. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28032. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28033. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28034. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28035. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28036. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28037. ASN1_TIME_free(asn_time);
  28038. ASN1_TIME_free(NULL);
  28039. #endif
  28040. return EXPECT_RESULT();
  28041. }
  28042. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28043. {
  28044. EXPECT_DECLS;
  28045. #ifndef NO_ASN_TIME
  28046. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28047. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28048. WOLFSSL_ASN1_TIME* t = NULL;
  28049. char buf[ASN_GENERALIZED_TIME_SIZE];
  28050. ExpectNotNull((t = ASN1_TIME_new()));
  28051. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28052. /* Invalid parameter testing. */
  28053. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28054. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28055. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28056. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28057. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28058. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28059. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28060. /* Buffer needs to be longer than minimum of 5 characters. */
  28061. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28062. ASN1_TIME_free(t);
  28063. #endif
  28064. #endif /* NO_ASN_TIME */
  28065. return EXPECT_RESULT();
  28066. }
  28067. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28068. {
  28069. EXPECT_DECLS;
  28070. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28071. ASN1_TIME* fromTime = NULL;
  28072. ASN1_TIME* closeToTime = NULL;
  28073. ASN1_TIME* toTime = NULL;
  28074. ASN1_TIME* invalidTime = NULL;
  28075. int daysDiff = 0;
  28076. int secsDiff = 0;
  28077. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28078. /* Feb 22, 2003, 21:15:15 */
  28079. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28080. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28081. /* Feb 22, 2003, 21:16:15 */
  28082. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28083. ExpectNotNull((toTime = ASN1_TIME_new()));
  28084. /* Dec 19, 2010, 18:10:11 */
  28085. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28086. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28087. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28088. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28089. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28090. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28091. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28092. /* Error conditions. */
  28093. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28094. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28095. /* If both times are NULL, difference is 0. */
  28096. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28097. ExpectIntEQ(daysDiff, 0);
  28098. ExpectIntEQ(secsDiff, 0);
  28099. /* If one time is NULL, it defaults to the current time. */
  28100. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28101. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28102. /* Normal operation. Both times non-NULL. */
  28103. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28104. ExpectIntEQ(daysDiff, 2856);
  28105. ExpectIntEQ(secsDiff, 75296);
  28106. /* Swapping the times should return negative values. */
  28107. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28108. ExpectIntEQ(daysDiff, -2856);
  28109. ExpectIntEQ(secsDiff, -75296);
  28110. /* Compare with invalid time string. */
  28111. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28112. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28113. /* Compare with days difference of 0. */
  28114. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28115. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28116. /* Days and seconds differences not 0. */
  28117. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28118. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28119. /* Same time. */
  28120. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28121. /* Compare regression test: No seconds difference, just difference in days.
  28122. */
  28123. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28124. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28125. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28126. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28127. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28128. /* Edge case with Unix epoch. */
  28129. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28130. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28131. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28132. ExpectIntEQ(daysDiff, 3652);
  28133. ExpectIntEQ(secsDiff, 0);
  28134. /* Edge case with year > 2038 (year 2038 problem). */
  28135. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28136. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28137. ExpectIntEQ(daysDiff, 2932896);
  28138. ExpectIntEQ(secsDiff, 86399);
  28139. ASN1_TIME_free(fromTime);
  28140. ASN1_TIME_free(closeToTime);
  28141. ASN1_TIME_free(toTime);
  28142. ASN1_TIME_free(invalidTime);
  28143. #endif
  28144. return EXPECT_RESULT();
  28145. }
  28146. static int test_wolfSSL_ASN1_TIME_adj(void)
  28147. {
  28148. EXPECT_DECLS;
  28149. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28150. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28151. const int year = 365*24*60*60;
  28152. const int day = 24*60*60;
  28153. const int hour = 60*60;
  28154. const int mini = 60;
  28155. const byte asn_utc_time = ASN_UTC_TIME;
  28156. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28157. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28158. #endif
  28159. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28160. WOLFSSL_ASN1_TIME* s = NULL;
  28161. int offset_day;
  28162. long offset_sec;
  28163. char date_str[CTC_DATE_SIZE + 1];
  28164. time_t t;
  28165. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28166. /* UTC notation test */
  28167. /* 2000/2/15 20:30:00 */
  28168. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28169. offset_day = 7;
  28170. offset_sec = 45 * mini;
  28171. /* offset_sec = -45 * min;*/
  28172. ExpectNotNull(asn_time =
  28173. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28174. ExpectTrue(asn_time->type == asn_utc_time);
  28175. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28176. CTC_DATE_SIZE));
  28177. date_str[CTC_DATE_SIZE] = '\0';
  28178. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28179. /* negative offset */
  28180. offset_sec = -45 * mini;
  28181. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28182. ExpectNotNull(asn_time);
  28183. ExpectTrue(asn_time->type == asn_utc_time);
  28184. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28185. CTC_DATE_SIZE));
  28186. date_str[CTC_DATE_SIZE] = '\0';
  28187. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28188. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28189. s = NULL;
  28190. XMEMSET(date_str, 0, sizeof(date_str));
  28191. /* Generalized time will overflow time_t if not long */
  28192. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28193. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28194. DYNAMIC_TYPE_OPENSSL);
  28195. /* GeneralizedTime notation test */
  28196. /* 2055/03/01 09:00:00 */
  28197. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28198. offset_day = 12;
  28199. offset_sec = 10 * mini;
  28200. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28201. offset_sec));
  28202. ExpectTrue(asn_time->type == asn_gen_time);
  28203. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28204. CTC_DATE_SIZE));
  28205. date_str[CTC_DATE_SIZE] = '\0';
  28206. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28207. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28208. s = NULL;
  28209. XMEMSET(date_str, 0, sizeof(date_str));
  28210. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28211. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28212. s = NULL;
  28213. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28214. offset_day = 7;
  28215. offset_sec = 45 * mini;
  28216. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28217. offset_sec));
  28218. ExpectTrue(asn_time->type == asn_utc_time);
  28219. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28220. CTC_DATE_SIZE));
  28221. date_str[CTC_DATE_SIZE] = '\0';
  28222. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28223. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28224. asn_time = NULL;
  28225. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28226. offset_sec));
  28227. ExpectTrue(asn_time->type == asn_utc_time);
  28228. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28229. CTC_DATE_SIZE));
  28230. date_str[CTC_DATE_SIZE] = '\0';
  28231. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28232. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28233. #endif
  28234. return EXPECT_RESULT();
  28235. }
  28236. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28237. {
  28238. EXPECT_DECLS;
  28239. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28240. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28241. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28242. ASN1_TIME asnTime;
  28243. struct tm tm;
  28244. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28245. XMEMSET(&tm, 0, sizeof(struct tm));
  28246. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28247. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28248. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28249. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28250. ExpectIntEQ(tm.tm_sec, 15);
  28251. ExpectIntEQ(tm.tm_min, 15);
  28252. ExpectIntEQ(tm.tm_hour, 21);
  28253. ExpectIntEQ(tm.tm_mday, 22);
  28254. ExpectIntEQ(tm.tm_mon, 1);
  28255. ExpectIntEQ(tm.tm_year, 100);
  28256. ExpectIntEQ(tm.tm_isdst, 0);
  28257. #ifdef XMKTIME
  28258. ExpectIntEQ(tm.tm_wday, 2);
  28259. ExpectIntEQ(tm.tm_yday, 52);
  28260. #endif
  28261. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28262. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28263. ExpectIntEQ(tm.tm_year, 50);
  28264. /* Get current time. */
  28265. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28266. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28267. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28268. /* 0 length. */
  28269. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28270. /* No type. */
  28271. asnTime.length = 1;
  28272. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28273. /* Not UTCTIME length. */
  28274. asnTime.type = V_ASN1_UTCTIME;
  28275. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28276. /* Not GENERALIZEDTIME length. */
  28277. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28278. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28279. /* Not Zulu timezone. */
  28280. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28281. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28282. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28283. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28284. #ifdef XMKTIME
  28285. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28286. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28287. ExpectIntEQ(tm.tm_sec, 47);
  28288. ExpectIntEQ(tm.tm_min, 22);
  28289. ExpectIntEQ(tm.tm_hour, 21);
  28290. ExpectIntEQ(tm.tm_mday, 12);
  28291. ExpectIntEQ(tm.tm_mon, 4);
  28292. ExpectIntEQ(tm.tm_year, 123);
  28293. ExpectIntEQ(tm.tm_wday, 5);
  28294. ExpectIntEQ(tm.tm_yday, 131);
  28295. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28296. fields are zeroed out as expected. */
  28297. ExpectIntEQ(tm.tm_isdst, 0);
  28298. #endif
  28299. #endif
  28300. return EXPECT_RESULT();
  28301. }
  28302. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28303. {
  28304. EXPECT_DECLS;
  28305. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28306. WOLFSSL_ASN1_TIME *t = NULL;
  28307. WOLFSSL_ASN1_TIME *out = NULL;
  28308. WOLFSSL_ASN1_TIME *gtime = NULL;
  28309. int tlen = 0;
  28310. unsigned char *data = NULL;
  28311. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28312. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28313. /* type not set. */
  28314. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28315. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28316. t = NULL;
  28317. /* UTC Time test */
  28318. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28319. if (t != NULL) {
  28320. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28321. t->type = ASN_UTC_TIME;
  28322. t->length = ASN_UTC_TIME_SIZE;
  28323. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28324. }
  28325. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28326. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28327. out = NULL;
  28328. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28329. wolfSSL_ASN1_TIME_free(gtime);
  28330. gtime = NULL;
  28331. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28332. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28333. ExpectPtrEq(gtime, out);
  28334. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28335. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28336. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28337. /* Generalized Time test */
  28338. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28339. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28340. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28341. if (t != NULL) {
  28342. t->type = ASN_GENERALIZED_TIME;
  28343. t->length = ASN_GENERALIZED_TIME_SIZE;
  28344. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28345. }
  28346. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28347. ASN_GENERALIZED_TIME_SIZE);
  28348. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28349. "20050727123456Z");
  28350. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28351. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28352. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28353. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28354. /* UTC Time to Generalized Time 1900's test */
  28355. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28356. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28357. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28358. if (t != NULL) {
  28359. t->type = ASN_UTC_TIME;
  28360. t->length = ASN_UTC_TIME_SIZE;
  28361. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28362. }
  28363. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28364. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28365. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28366. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28367. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28368. /* Null parameter test */
  28369. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28370. gtime = NULL;
  28371. out = NULL;
  28372. if (t != NULL) {
  28373. t->type = ASN_UTC_TIME;
  28374. t->length = ASN_UTC_TIME_SIZE;
  28375. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28376. }
  28377. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  28378. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28379. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28380. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28381. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28382. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28383. #endif
  28384. return EXPECT_RESULT();
  28385. }
  28386. static int test_wolfSSL_ASN1_TIME_print(void)
  28387. {
  28388. EXPECT_DECLS;
  28389. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  28390. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28391. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28392. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  28393. !defined(NO_ASN_TIME)
  28394. BIO* bio = NULL;
  28395. BIO* fixed = NULL;
  28396. X509* x509 = NULL;
  28397. const unsigned char* der = client_cert_der_2048;
  28398. ASN1_TIME* notAfter = NULL;
  28399. ASN1_TIME* notBefore = NULL;
  28400. unsigned char buf[25];
  28401. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28402. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28403. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  28404. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  28405. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  28406. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  28407. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  28408. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  28409. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  28410. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28411. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  28412. /* Test BIO_write fails. */
  28413. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28414. /* Ensure there is 0 bytes available to write into. */
  28415. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  28416. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28417. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28418. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28419. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  28420. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28421. /* create a bad time and test results */
  28422. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  28423. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  28424. if (EXPECT_SUCCESS()) {
  28425. notAfter->data[8] = 0;
  28426. notAfter->data[3] = 0;
  28427. }
  28428. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  28429. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28430. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28431. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  28432. BIO_free(bio);
  28433. BIO_free(fixed);
  28434. X509_free(x509);
  28435. #endif
  28436. return EXPECT_RESULT();
  28437. }
  28438. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  28439. {
  28440. EXPECT_DECLS;
  28441. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  28442. BIO* bio = NULL;
  28443. ASN1_UTCTIME* utc = NULL;
  28444. unsigned char buf[25];
  28445. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  28446. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  28447. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  28448. /* Valid date */
  28449. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28450. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  28451. DYNAMIC_TYPE_ASN1));
  28452. if (utc != NULL) {
  28453. utc->type = ASN_UTC_TIME;
  28454. utc->length = ASN_UTC_TIME_SIZE;
  28455. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  28456. }
  28457. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  28458. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  28459. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  28460. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  28461. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28462. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  28463. XMEMSET(buf, 0, sizeof(buf));
  28464. BIO_free(bio);
  28465. bio = NULL;
  28466. /* Invalid format */
  28467. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28468. if (utc != NULL) {
  28469. utc->type = ASN_UTC_TIME;
  28470. utc->length = ASN_UTC_TIME_SIZE;
  28471. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  28472. }
  28473. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28474. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28475. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28476. /* Invalid type */
  28477. if (utc != NULL) {
  28478. utc->type = ASN_GENERALIZED_TIME;
  28479. utc->length = ASN_GENERALIZED_TIME_SIZE;
  28480. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  28481. }
  28482. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28483. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  28484. BIO_free(bio);
  28485. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  28486. return EXPECT_RESULT();
  28487. }
  28488. static int test_wolfSSL_ASN1_TYPE(void)
  28489. {
  28490. EXPECT_DECLS;
  28491. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  28492. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  28493. WOLFSSL_ASN1_TYPE* t = NULL;
  28494. WOLFSSL_ASN1_OBJECT* obj = NULL;
  28495. #ifndef NO_ASN_TIME
  28496. WOLFSSL_ASN1_TIME* time = NULL;
  28497. #endif
  28498. WOLFSSL_ASN1_STRING* str = NULL;
  28499. unsigned char data[] = { 0x00 };
  28500. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  28501. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28502. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  28503. wolfSSL_ASN1_TYPE_free(t);
  28504. t = NULL;
  28505. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28506. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  28507. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  28508. wolfSSL_ASN1_TYPE_free(t);
  28509. t = NULL;
  28510. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28511. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28512. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  28513. wolfSSL_ASN1_TYPE_free(t);
  28514. t = NULL;
  28515. #ifndef NO_ASN_TIME
  28516. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28517. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28518. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  28519. wolfSSL_ASN1_TYPE_free(t);
  28520. t = NULL;
  28521. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28522. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28523. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  28524. wolfSSL_ASN1_TYPE_free(t);
  28525. t = NULL;
  28526. #endif
  28527. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28528. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28529. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  28530. wolfSSL_ASN1_TYPE_free(t);
  28531. t = NULL;
  28532. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28533. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28534. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  28535. wolfSSL_ASN1_TYPE_free(t);
  28536. t = NULL;
  28537. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28538. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28539. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  28540. wolfSSL_ASN1_TYPE_free(t);
  28541. t = NULL;
  28542. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28543. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28544. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  28545. wolfSSL_ASN1_TYPE_free(t);
  28546. t = NULL;
  28547. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28548. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28549. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  28550. wolfSSL_ASN1_TYPE_free(t);
  28551. t = NULL;
  28552. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28553. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28554. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  28555. wolfSSL_ASN1_TYPE_free(t);
  28556. t = NULL;
  28557. #endif
  28558. return EXPECT_RESULT();
  28559. }
  28560. /* Testing code used in dpp.c in hostap */
  28561. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28562. typedef struct {
  28563. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  28564. * as an OID identifying the curve */
  28565. X509_ALGOR *alg;
  28566. /* Compressed format public key per ANSI X9.63 */
  28567. ASN1_BIT_STRING *pub_key;
  28568. } DPP_BOOTSTRAPPING_KEY;
  28569. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  28570. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  28571. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  28572. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  28573. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  28574. typedef struct {
  28575. ASN1_INTEGER *integer;
  28576. } TEST_ASN1;
  28577. ASN1_SEQUENCE(TEST_ASN1) = {
  28578. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  28579. } ASN1_SEQUENCE_END(TEST_ASN1)
  28580. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  28581. typedef struct {
  28582. ASN1_OCTET_STRING *octet_string;
  28583. } TEST_FAIL_ASN1;
  28584. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  28585. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  28586. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  28587. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  28588. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  28589. #endif
  28590. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  28591. {
  28592. EXPECT_DECLS;
  28593. /* Testing code used in dpp.c in hostap */
  28594. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28595. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28596. EC_KEY *eckey = NULL;
  28597. EVP_PKEY *key = NULL;
  28598. size_t len = 0;
  28599. unsigned char *der = NULL;
  28600. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  28601. const unsigned char *in = ecc_clikey_der_256;
  28602. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  28603. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  28604. const EC_GROUP *group = NULL;
  28605. const EC_POINT *point = NULL;
  28606. int nid;
  28607. TEST_ASN1 *test_asn1 = NULL;
  28608. TEST_FAIL_ASN1 test_fail_asn1;
  28609. const unsigned char badObjDer[] = { 0x06, 0x00 };
  28610. const unsigned char goodObjDer[] = {
  28611. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  28612. };
  28613. WOLFSSL_ASN1_ITEM emptyTemplate;
  28614. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  28615. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  28616. der = NULL;
  28617. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  28618. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  28619. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28620. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  28621. (long)sizeof_ecc_clikey_der_256));
  28622. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  28623. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  28624. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  28625. nid = EC_GROUP_get_curve_name(group);
  28626. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  28627. group_obj = OBJ_nid2obj(nid);
  28628. if ((ec_obj != NULL) && (group_obj != NULL)) {
  28629. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  28630. group_obj), 1);
  28631. if (EXPECT_SUCCESS()) {
  28632. ec_obj = NULL;
  28633. group_obj = NULL;
  28634. }
  28635. }
  28636. wolfSSL_ASN1_OBJECT_free(group_obj);
  28637. wolfSSL_ASN1_OBJECT_free(ec_obj);
  28638. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  28639. #ifdef HAVE_COMP_KEY
  28640. ExpectIntGT((len = EC_POINT_point2oct(
  28641. group, point, POINT_CONVERSION_COMPRESSED,
  28642. NULL, 0, NULL)), 0);
  28643. #else
  28644. ExpectIntGT((len = EC_POINT_point2oct(
  28645. group, point, POINT_CONVERSION_UNCOMPRESSED,
  28646. NULL, 0, NULL)), 0);
  28647. #endif
  28648. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  28649. #ifdef HAVE_COMP_KEY
  28650. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  28651. der, len-1, NULL), 0);
  28652. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  28653. der, len, NULL), len);
  28654. #else
  28655. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  28656. der, len-1, NULL), 0);
  28657. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  28658. der, len, NULL), len);
  28659. #endif
  28660. if (EXPECT_SUCCESS()) {
  28661. bootstrap->pub_key->data = der;
  28662. bootstrap->pub_key->length = (int)len;
  28663. /* Not actually used */
  28664. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  28665. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  28666. }
  28667. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  28668. der = NULL;
  28669. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28670. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28671. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28672. EVP_PKEY_free(key);
  28673. EC_KEY_free(eckey);
  28674. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  28675. bootstrap = NULL;
  28676. DPP_BOOTSTRAPPING_KEY_free(NULL);
  28677. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  28678. * a NULL and an empty BIT_STRING. */
  28679. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  28680. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  28681. if (EXPECT_SUCCESS()) {
  28682. bootstrap->alg->algorithm->obj = badObjDer;
  28683. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  28684. }
  28685. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  28686. if (EXPECT_SUCCESS()) {
  28687. bootstrap->alg->parameter->type = V_ASN1_NULL;
  28688. bootstrap->alg->parameter->value.ptr = NULL;
  28689. bootstrap->pub_key->data = NULL;
  28690. bootstrap->pub_key->length = 0;
  28691. /* Not actually used */
  28692. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  28693. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  28694. }
  28695. /* Encode with bad OBJECT_ID. */
  28696. der = NULL;
  28697. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28698. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  28699. if (EXPECT_SUCCESS()) {
  28700. bootstrap->alg->algorithm->obj = goodObjDer;
  28701. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  28702. bootstrap->alg->algorithm->grp = 2;
  28703. }
  28704. der = NULL;
  28705. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  28706. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  28707. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28708. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  28709. /* Test integer */
  28710. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  28711. der = NULL;
  28712. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  28713. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  28714. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28715. TEST_ASN1_free(test_asn1);
  28716. /* Test integer cases. */
  28717. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  28718. TEST_ASN1_free(NULL);
  28719. /* Test error cases. */
  28720. ExpectNull(TEST_FAIL_ASN1_new());
  28721. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  28722. TEST_FAIL_ASN1_free(NULL);
  28723. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  28724. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  28725. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  28726. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  28727. return EXPECT_RESULT();
  28728. }
  28729. static int test_wolfSSL_lhash(void)
  28730. {
  28731. EXPECT_DECLS;
  28732. #ifdef OPENSSL_ALL
  28733. const char testStr[] = "Like a true nature's child\n"
  28734. "We were born\n"
  28735. "Born to be wild";
  28736. #ifdef NO_SHA
  28737. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  28738. #else
  28739. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  28740. #endif
  28741. #endif
  28742. return EXPECT_RESULT();
  28743. }
  28744. static int test_wolfSSL_X509_NAME(void)
  28745. {
  28746. EXPECT_DECLS;
  28747. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  28748. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28749. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  28750. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  28751. defined(OPENSSL_EXTRA))
  28752. X509* x509 = NULL;
  28753. const unsigned char* c = NULL;
  28754. unsigned char buf[4096];
  28755. int bytes = 0;
  28756. XFILE f = XBADFILE;
  28757. const X509_NAME* a = NULL;
  28758. const X509_NAME* b = NULL;
  28759. X509_NAME* d2i_name = NULL;
  28760. int sz = 0;
  28761. unsigned char* tmp = NULL;
  28762. char file[] = "./certs/ca-cert.der";
  28763. #ifndef OPENSSL_EXTRA_X509_SMALL
  28764. byte empty[] = { /* CN=empty emailAddress= */
  28765. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  28766. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  28767. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  28768. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  28769. 0x01, 0x16, 0x00
  28770. };
  28771. #endif
  28772. #ifndef OPENSSL_EXTRA_X509_SMALL
  28773. /* test compile of deprecated function, returns 0 */
  28774. ExpectIntEQ(CRYPTO_thread_id(), 0);
  28775. #endif
  28776. ExpectNotNull(a = X509_NAME_new());
  28777. X509_NAME_free((X509_NAME*)a);
  28778. a = NULL;
  28779. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  28780. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  28781. if (f != XBADFILE)
  28782. XFCLOSE(f);
  28783. c = buf;
  28784. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  28785. /* test cmp function */
  28786. ExpectNotNull(a = X509_get_issuer_name(x509));
  28787. ExpectNotNull(b = X509_get_subject_name(x509));
  28788. #ifndef OPENSSL_EXTRA_X509_SMALL
  28789. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  28790. #endif
  28791. tmp = buf;
  28792. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  28793. if (sz > 0 && tmp == buf) {
  28794. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  28795. __LINE__);
  28796. fprintf(stderr, " Expected pointer to be incremented\n");
  28797. abort();
  28798. }
  28799. #ifndef OPENSSL_EXTRA_X509_SMALL
  28800. tmp = buf;
  28801. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  28802. #endif
  28803. /* if output parameter is NULL, should still return required size. */
  28804. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  28805. /* retry but with the function creating a buffer */
  28806. tmp = NULL;
  28807. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  28808. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  28809. tmp = NULL;
  28810. #ifdef WOLFSSL_CERT_NAME_ALL
  28811. /* test for givenName and name */
  28812. {
  28813. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  28814. const byte gName[] = "test-given-name";
  28815. const byte name[] = "test-name";
  28816. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  28817. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  28818. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  28819. 1);
  28820. wolfSSL_X509_NAME_ENTRY_free(entry);
  28821. entry = NULL;
  28822. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  28823. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  28824. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  28825. 1);
  28826. wolfSSL_X509_NAME_ENTRY_free(entry);
  28827. tmp = NULL;
  28828. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  28829. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  28830. }
  28831. #endif
  28832. b = NULL;
  28833. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  28834. #ifndef OPENSSL_EXTRA_X509_SMALL
  28835. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  28836. #endif
  28837. X509_NAME_free((X509_NAME*)b);
  28838. X509_NAME_free(d2i_name);
  28839. d2i_name = NULL;
  28840. X509_free(x509);
  28841. #ifndef OPENSSL_EXTRA_X509_SMALL
  28842. /* test with an empty domain component */
  28843. tmp = empty;
  28844. sz = sizeof(empty);
  28845. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  28846. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  28847. /* size of empty emailAddress will be 0 */
  28848. tmp = buf;
  28849. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  28850. (char*)tmp, sizeof(buf)), 0);
  28851. /* should contain no organization name */
  28852. tmp = buf;
  28853. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  28854. (char*)tmp, sizeof(buf)), -1);
  28855. X509_NAME_free(d2i_name);
  28856. #endif
  28857. #endif
  28858. return EXPECT_RESULT();
  28859. }
  28860. static int test_wolfSSL_X509_NAME_hash(void)
  28861. {
  28862. EXPECT_DECLS;
  28863. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28864. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  28865. BIO* bio = NULL;
  28866. X509* x509 = NULL;
  28867. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  28868. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28869. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  28870. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  28871. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  28872. X509_free(x509);
  28873. BIO_free(bio);
  28874. #endif
  28875. return EXPECT_RESULT();
  28876. }
  28877. static int test_wolfSSL_X509_NAME_print_ex(void)
  28878. {
  28879. EXPECT_DECLS;
  28880. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  28881. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  28882. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  28883. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  28884. !defined(NO_BIO) && !defined(NO_RSA)
  28885. int memSz = 0;
  28886. byte* mem = NULL;
  28887. BIO* bio = NULL;
  28888. BIO* membio = NULL;
  28889. X509* x509 = NULL;
  28890. X509_NAME* name = NULL;
  28891. const char* expNormal = "C=US, CN=wolfssl.com";
  28892. const char* expReverse = "CN=wolfssl.com, C=US";
  28893. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  28894. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  28895. const char* expRFC5523 =
  28896. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  28897. /* Test with real cert (svrCertFile) first */
  28898. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  28899. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28900. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  28901. ExpectNotNull(name = X509_get_subject_name(x509));
  28902. /* Test without flags */
  28903. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28904. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28905. BIO_free(membio);
  28906. membio = NULL;
  28907. /* Test flag: XN_FLAG_RFC2253 */
  28908. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28909. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28910. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28911. BIO_free(membio);
  28912. membio = NULL;
  28913. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  28914. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28915. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28916. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28917. BIO_free(membio);
  28918. membio = NULL;
  28919. X509_free(x509);
  28920. BIO_free(bio);
  28921. name = NULL;
  28922. /* Test normal case without escaped characters */
  28923. {
  28924. /* Create name: "/C=US/CN=wolfssl.com" */
  28925. ExpectNotNull(name = X509_NAME_new());
  28926. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  28927. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  28928. WOLFSSL_SUCCESS);
  28929. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  28930. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  28931. WOLFSSL_SUCCESS);
  28932. /* Test without flags */
  28933. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28934. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28935. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28936. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  28937. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  28938. BIO_free(membio);
  28939. membio = NULL;
  28940. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  28941. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28942. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28943. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28944. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28945. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  28946. BIO_free(membio);
  28947. membio = NULL;
  28948. /* Test flags: XN_FLAG_DN_REV - reversed */
  28949. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28950. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28951. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28952. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28953. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  28954. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  28955. BIO_free(membio);
  28956. membio = NULL;
  28957. X509_NAME_free(name);
  28958. name = NULL;
  28959. }
  28960. /* Test RFC2253 characters are escaped with backslashes */
  28961. {
  28962. ExpectNotNull(name = X509_NAME_new());
  28963. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  28964. /* space at beginning and end, and: ,+"\ */
  28965. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  28966. WOLFSSL_SUCCESS);
  28967. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  28968. /* # at beginning, and: <>;*/
  28969. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  28970. WOLFSSL_SUCCESS);
  28971. /* Test without flags */
  28972. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28973. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28974. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28975. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  28976. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  28977. XSTRLEN(expNotEscaped)), 0);
  28978. BIO_free(membio);
  28979. membio = NULL;
  28980. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  28981. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28982. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28983. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28984. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28985. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  28986. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  28987. BIO_free(membio);
  28988. membio = NULL;
  28989. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  28990. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28991. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28992. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28993. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28994. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  28995. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  28996. XSTRLEN(expNotEscapedRev)), 0);
  28997. BIO_free(membio);
  28998. X509_NAME_free(name);
  28999. }
  29000. #endif
  29001. return EXPECT_RESULT();
  29002. }
  29003. #ifndef NO_BIO
  29004. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29005. {
  29006. EXPECT_DECLS;
  29007. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29008. STACK_OF(X509_INFO) *info_stack = NULL;
  29009. X509_INFO *info = NULL;
  29010. int len;
  29011. int i;
  29012. const char* files[] = {
  29013. cliCertFile,
  29014. cliKeyFile,
  29015. /* This needs to be the order as svrCertFile contains the
  29016. * intermediate cert as well. */
  29017. svrKeyFile,
  29018. svrCertFile,
  29019. NULL,
  29020. };
  29021. const char** curFile;
  29022. BIO *fileBIO = NULL;
  29023. BIO *concatBIO = NULL;
  29024. byte tmp[FOURK_BUF];
  29025. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29026. * to group objects together. */
  29027. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29028. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29029. int fileLen = 0;
  29030. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29031. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29032. if (EXPECT_SUCCESS()) {
  29033. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29034. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29035. fileLen -= len;
  29036. if (EXPECT_FAIL())
  29037. break;
  29038. }
  29039. /* Make sure we read the entire file */
  29040. ExpectIntEQ(fileLen, 0);
  29041. }
  29042. BIO_free(fileBIO);
  29043. fileBIO = NULL;
  29044. }
  29045. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29046. NULL));
  29047. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  29048. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29049. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29050. ExpectNotNull(info->x509);
  29051. ExpectNull(info->crl);
  29052. if (i != 0) {
  29053. ExpectNotNull(info->x_pkey);
  29054. ExpectIntEQ(X509_check_private_key(info->x509,
  29055. info->x_pkey->dec_pkey), 1);
  29056. }
  29057. else {
  29058. ExpectNull(info->x_pkey);
  29059. }
  29060. }
  29061. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29062. BIO_free(concatBIO);
  29063. #endif
  29064. return EXPECT_RESULT();
  29065. }
  29066. #endif
  29067. #ifndef NO_BIO
  29068. static int test_wolfSSL_X509_INFO(void)
  29069. {
  29070. EXPECT_DECLS;
  29071. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29072. STACK_OF(X509_INFO) *info_stack = NULL;
  29073. X509_INFO *info = NULL;
  29074. BIO *cert = NULL;
  29075. int i;
  29076. /* PEM in hex format to avoid null terminator */
  29077. byte data[] = {
  29078. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29079. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29080. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29081. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29082. 0x2d, 0x2d, 0x2d
  29083. };
  29084. /* PEM in hex format to avoid null terminator */
  29085. byte data2[] = {
  29086. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29087. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29088. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29089. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29090. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29091. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29092. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29093. };
  29094. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29095. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29096. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29097. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29098. ExpectNotNull(info->x509);
  29099. ExpectNull(info->crl);
  29100. ExpectNull(info->x_pkey);
  29101. }
  29102. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29103. info_stack = NULL;
  29104. BIO_free(cert);
  29105. cert = NULL;
  29106. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29107. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29108. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29109. info_stack = NULL;
  29110. BIO_free(cert);
  29111. cert = NULL;
  29112. /* This case should fail due to invalid input. */
  29113. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29114. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29115. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29116. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29117. info_stack = NULL;
  29118. BIO_free(cert);
  29119. cert = NULL;
  29120. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29121. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29122. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29123. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29124. BIO_free(cert);
  29125. #endif
  29126. return EXPECT_RESULT();
  29127. }
  29128. #endif
  29129. static int test_wolfSSL_X509_subject_name_hash(void)
  29130. {
  29131. EXPECT_DECLS;
  29132. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29133. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29134. X509* x509 = NULL;
  29135. X509_NAME* subjectName = NULL;
  29136. unsigned long ret1 = 0;
  29137. unsigned long ret2 = 0;
  29138. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29139. SSL_FILETYPE_PEM));
  29140. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29141. /* These two
  29142. * - X509_subject_name_hash(x509)
  29143. * - X509_NAME_hash(X509_get_subject_name(x509))
  29144. * should give the same hash, if !defined(NO_SHA) is true. */
  29145. ret1 = X509_subject_name_hash(x509);
  29146. ExpectIntNE(ret1, 0);
  29147. #if !defined(NO_SHA)
  29148. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29149. ExpectIntNE(ret2, 0);
  29150. ExpectIntEQ(ret1, ret2);
  29151. #else
  29152. (void) ret2;
  29153. #endif
  29154. X509_free(x509);
  29155. #endif
  29156. return EXPECT_RESULT();
  29157. }
  29158. static int test_wolfSSL_X509_issuer_name_hash(void)
  29159. {
  29160. EXPECT_DECLS;
  29161. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29162. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29163. X509* x509 = NULL;
  29164. X509_NAME* issuertName = NULL;
  29165. unsigned long ret1 = 0;
  29166. unsigned long ret2 = 0;
  29167. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29168. SSL_FILETYPE_PEM));
  29169. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29170. /* These two
  29171. * - X509_issuer_name_hash(x509)
  29172. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29173. * should give the same hash, if !defined(NO_SHA) is true. */
  29174. ret1 = X509_issuer_name_hash(x509);
  29175. ExpectIntNE(ret1, 0);
  29176. #if !defined(NO_SHA)
  29177. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29178. ExpectIntNE(ret2, 0);
  29179. ExpectIntEQ(ret1, ret2);
  29180. #else
  29181. (void) ret2;
  29182. #endif
  29183. X509_free(x509);
  29184. #endif
  29185. return EXPECT_RESULT();
  29186. }
  29187. static int test_wolfSSL_X509_check_host(void)
  29188. {
  29189. EXPECT_DECLS;
  29190. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29191. && !defined(NO_SHA) && !defined(NO_RSA)
  29192. X509* x509 = NULL;
  29193. const char altName[] = "example.com";
  29194. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29195. SSL_FILETYPE_PEM));
  29196. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29197. WOLFSSL_SUCCESS);
  29198. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29199. WOLFSSL_FAILURE);
  29200. X509_free(x509);
  29201. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29202. WOLFSSL_FAILURE);
  29203. #endif
  29204. return EXPECT_RESULT();
  29205. }
  29206. static int test_wolfSSL_X509_check_email(void)
  29207. {
  29208. EXPECT_DECLS;
  29209. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29210. X509* x509 = NULL;
  29211. const char goodEmail[] = "info@wolfssl.com";
  29212. const char badEmail[] = "disinfo@wolfssl.com";
  29213. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29214. SSL_FILETYPE_PEM));
  29215. /* Should fail on non-matching email address */
  29216. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29217. WOLFSSL_FAILURE);
  29218. /* Should succeed on matching email address */
  29219. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29220. WOLFSSL_SUCCESS);
  29221. /* Should compute length internally when not provided */
  29222. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29223. WOLFSSL_SUCCESS);
  29224. /* Should fail when email address is NULL */
  29225. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29226. WOLFSSL_FAILURE);
  29227. X509_free(x509);
  29228. /* Should fail when x509 is NULL */
  29229. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29230. WOLFSSL_FAILURE);
  29231. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29232. return EXPECT_RESULT();
  29233. }
  29234. static int test_wc_PemToDer(void)
  29235. {
  29236. EXPECT_DECLS;
  29237. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29238. int ret;
  29239. DerBuffer* pDer = NULL;
  29240. const char* ca_cert = "./certs/server-cert.pem";
  29241. byte* cert_buf = NULL;
  29242. size_t cert_sz = 0;
  29243. int eccKey = 0;
  29244. EncryptedInfo info;
  29245. XMEMSET(&info, 0, sizeof(info));
  29246. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29247. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  29248. &info, &eccKey), 0);
  29249. wc_FreeDer(&pDer);
  29250. pDer = NULL;
  29251. if (cert_buf != NULL) {
  29252. free(cert_buf);
  29253. cert_buf = NULL;
  29254. }
  29255. #ifdef HAVE_ECC
  29256. {
  29257. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29258. byte key_buf[256] = {0};
  29259. /* Test fail of loading a key with cert type */
  29260. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29261. key_buf[0] = '\n';
  29262. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29263. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29264. &pDer, NULL, &info, &eccKey)), 0);
  29265. #ifdef OPENSSL_EXTRA
  29266. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29267. &pDer, NULL, &info, &eccKey)), 0);
  29268. #endif
  29269. wc_FreeDer(&pDer);
  29270. if (cert_buf != NULL)
  29271. free(cert_buf);
  29272. }
  29273. #endif
  29274. #endif
  29275. return EXPECT_RESULT();
  29276. }
  29277. static int test_wc_AllocDer(void)
  29278. {
  29279. EXPECT_DECLS;
  29280. #if !defined(NO_CERTS)
  29281. DerBuffer* pDer = NULL;
  29282. word32 testSize = 1024;
  29283. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29284. BAD_FUNC_ARG);
  29285. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29286. ExpectNotNull(pDer);
  29287. wc_FreeDer(&pDer);
  29288. #endif
  29289. return EXPECT_RESULT();
  29290. }
  29291. static int test_wc_CertPemToDer(void)
  29292. {
  29293. EXPECT_DECLS;
  29294. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29295. const char* ca_cert = "./certs/ca-cert.pem";
  29296. byte* cert_buf = NULL;
  29297. size_t cert_sz = 0;
  29298. size_t cert_dersz = 0;
  29299. byte* cert_der = NULL;
  29300. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29301. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29302. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29303. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29304. (int)cert_dersz, CERT_TYPE), 0);
  29305. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29306. BAD_FUNC_ARG);
  29307. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29308. BAD_FUNC_ARG);
  29309. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29310. BAD_FUNC_ARG);
  29311. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29312. CERT_TYPE), BAD_FUNC_ARG);
  29313. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29314. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29315. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29316. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29317. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29318. CERT_TYPE), BAD_FUNC_ARG);
  29319. if (cert_der != NULL)
  29320. free(cert_der);
  29321. if (cert_buf != NULL)
  29322. free(cert_buf);
  29323. #endif
  29324. return EXPECT_RESULT();
  29325. }
  29326. static int test_wc_KeyPemToDer(void)
  29327. {
  29328. EXPECT_DECLS;
  29329. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29330. int ret = 0;
  29331. const byte cert_buf[] = \
  29332. "-----BEGIN PRIVATE KEY-----\n"
  29333. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29334. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29335. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29336. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29337. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29338. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29339. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29340. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29341. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29342. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29343. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29344. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29345. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29346. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29347. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29348. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29349. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29350. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29351. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29352. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29353. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29354. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29355. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29356. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29357. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29358. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29359. "-----END PRIVATE KEY-----\n";
  29360. const int cert_sz = sizeof(cert_buf);
  29361. const char cert_pw[] = "password";
  29362. int cert_dersz = 0;
  29363. byte* cert_der = NULL;
  29364. /* Bad arg: Cert buffer is NULL */
  29365. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29366. BAD_FUNC_ARG);
  29367. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29368. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29369. BAD_FUNC_ARG);
  29370. /* Test normal operation */
  29371. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29372. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29373. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29374. cert_pw), 0);
  29375. ExpectIntLE(ret, cert_sz);
  29376. if (cert_der != NULL) {
  29377. free(cert_der);
  29378. cert_der = NULL;
  29379. }
  29380. /* Test NULL for DER buffer to return needed DER buffer size */
  29381. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  29382. ExpectIntLE(ret, cert_sz);
  29383. if (EXPECT_SUCCESS())
  29384. cert_dersz = ret;
  29385. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29386. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29387. cert_pw), 0);
  29388. ExpectIntLE(ret, cert_sz);
  29389. if (cert_der != NULL)
  29390. free(cert_der);
  29391. #endif
  29392. return EXPECT_RESULT();
  29393. }
  29394. static int test_wc_PubKeyPemToDer(void)
  29395. {
  29396. EXPECT_DECLS;
  29397. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  29398. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29399. int ret = 0;
  29400. const char* key = "./certs/ecc-client-keyPub.pem";
  29401. byte* cert_buf = NULL;
  29402. size_t cert_sz = 0, cert_dersz = 0;
  29403. byte* cert_der = NULL;
  29404. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  29405. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  29406. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  29407. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29408. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29409. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29410. (int)cert_dersz), 0);
  29411. if (cert_der != NULL) {
  29412. free(cert_der);
  29413. cert_der = NULL;
  29414. }
  29415. /* Test NULL for DER buffer to return needed DER buffer size */
  29416. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  29417. ExpectIntLE(ret, cert_sz);
  29418. cert_dersz = ret;
  29419. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29420. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29421. (int)cert_dersz), 0);
  29422. if (cert_der != NULL) {
  29423. free(cert_der);
  29424. }
  29425. if (cert_buf != NULL) {
  29426. free(cert_buf);
  29427. }
  29428. #endif
  29429. return EXPECT_RESULT();
  29430. }
  29431. static int test_wc_PemPubKeyToDer(void)
  29432. {
  29433. EXPECT_DECLS;
  29434. #if !defined(NO_FILESYSTEM) && \
  29435. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29436. const char* key = "./certs/ecc-client-keyPub.pem";
  29437. size_t cert_dersz = 1024;
  29438. byte* cert_der = NULL;
  29439. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  29440. BAD_FUNC_ARG);
  29441. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29442. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  29443. if (cert_der != NULL) {
  29444. free(cert_der);
  29445. }
  29446. #endif
  29447. return EXPECT_RESULT();
  29448. }
  29449. static int test_wc_GetPubKeyDerFromCert(void)
  29450. {
  29451. EXPECT_DECLS;
  29452. #if !defined(NO_RSA) || defined(HAVE_ECC)
  29453. int ret;
  29454. word32 idx = 0;
  29455. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29456. word32 keyDerSz = (word32)sizeof(keyDer);
  29457. DecodedCert decoded;
  29458. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29459. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  29460. word32 certBufSz = sizeof(certBuf);
  29461. #endif
  29462. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  29463. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29464. XFILE fp = XBADFILE;
  29465. #endif
  29466. #ifndef NO_RSA
  29467. RsaKey rsaKey;
  29468. #if defined(USE_CERT_BUFFERS_2048)
  29469. byte* rsaCertDer = (byte*)client_cert_der_2048;
  29470. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  29471. #elif defined(USE_CERT_BUFFERS_1024)
  29472. byte* rsaCertDer = (byte*)client_cert_der_1024;
  29473. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  29474. #else
  29475. unsigned char rsaCertDer[TWOK_BUF];
  29476. word32 rsaCertDerSz;
  29477. #endif
  29478. #endif
  29479. #ifdef HAVE_ECC
  29480. ecc_key eccKey;
  29481. #if defined(USE_CERT_BUFFERS_256)
  29482. byte* eccCert = (byte*)cliecc_cert_der_256;
  29483. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  29484. #else
  29485. unsigned char eccCert[ONEK_BUF];
  29486. word32 eccCertSz;
  29487. XFILE fp2 = XBADFILE;
  29488. #endif
  29489. #endif
  29490. #ifndef NO_RSA
  29491. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  29492. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  29493. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  29494. fp), 0);
  29495. if (fp != XBADFILE) {
  29496. XFCLOSE(fp);
  29497. fp = XBADFILE;
  29498. }
  29499. #endif
  29500. /* good test case - RSA DER cert */
  29501. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  29502. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29503. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29504. ExpectIntGT(keyDerSz, 0);
  29505. /* sanity check, verify we can import DER public key */
  29506. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29507. ExpectIntEQ(ret, 0);
  29508. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29509. if (ret == 0) {
  29510. wc_FreeRsaKey(&rsaKey);
  29511. }
  29512. /* test LENGTH_ONLY_E case */
  29513. keyDerSz = 0;
  29514. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29515. LENGTH_ONLY_E);
  29516. ExpectIntGT(keyDerSz, 0);
  29517. /* bad args: DecodedCert NULL */
  29518. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  29519. /* bad args: output key buff size */
  29520. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  29521. /* bad args: zero size output key buffer */
  29522. keyDerSz = 0;
  29523. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29524. BAD_FUNC_ARG);
  29525. wc_FreeDecodedCert(&decoded);
  29526. /* Certificate Request Tests */
  29527. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29528. {
  29529. XMEMSET(certBuf, 0, sizeof(certBuf));
  29530. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  29531. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  29532. if (fp != XBADFILE) {
  29533. XFCLOSE(fp);
  29534. }
  29535. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  29536. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  29537. /* good test case - RSA DER certificate request */
  29538. keyDerSz = sizeof(keyDer);
  29539. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29540. 0);
  29541. ExpectIntGT(keyDerSz, 0);
  29542. /* sanity check, verify we can import DER public key */
  29543. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29544. ExpectIntEQ(ret, 0);
  29545. idx = 0;
  29546. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29547. if (ret == 0) {
  29548. wc_FreeRsaKey(&rsaKey);
  29549. }
  29550. wc_FreeDecodedCert(&decoded);
  29551. }
  29552. #endif /* WOLFSSL_CERT_REQ */
  29553. #endif /* NO_RSA */
  29554. #ifdef HAVE_ECC
  29555. #ifndef USE_CERT_BUFFERS_256
  29556. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  29557. XBADFILE);
  29558. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  29559. if (fp2 != XBADFILE) {
  29560. XFCLOSE(fp2);
  29561. }
  29562. #endif
  29563. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  29564. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29565. /* good test case - ECC */
  29566. XMEMSET(keyDer, 0, sizeof(keyDer));
  29567. keyDerSz = sizeof(keyDer);
  29568. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29569. ExpectIntGT(keyDerSz, 0);
  29570. /* sanity check, verify we can import DER public key */
  29571. ret = wc_ecc_init(&eccKey);
  29572. ExpectIntEQ(ret, 0);
  29573. idx = 0; /* reset idx to 0, used above in RSA case */
  29574. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  29575. if (ret == 0) {
  29576. wc_ecc_free(&eccKey);
  29577. }
  29578. /* test LENGTH_ONLY_E case */
  29579. keyDerSz = 0;
  29580. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29581. LENGTH_ONLY_E);
  29582. ExpectIntGT(keyDerSz, 0);
  29583. wc_FreeDecodedCert(&decoded);
  29584. #endif
  29585. #endif /* !NO_RSA || HAVE_ECC */
  29586. return EXPECT_RESULT();
  29587. }
  29588. static int test_wc_CheckCertSigPubKey(void)
  29589. {
  29590. EXPECT_DECLS;
  29591. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29592. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  29593. int ret = 0;
  29594. const char* ca_cert = "./certs/ca-cert.pem";
  29595. byte* cert_buf = NULL;
  29596. size_t cert_sz = 0;
  29597. byte* cert_der = NULL;
  29598. word32 cert_dersz = 0;
  29599. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29600. word32 keyDerSz = (word32)sizeof(keyDer);
  29601. DecodedCert decoded;
  29602. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29603. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  29604. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29605. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29606. (int)cert_dersz, CERT_TYPE), 0);
  29607. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  29608. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29609. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29610. ExpectIntGT(keyDerSz, 0);
  29611. /* Good test case. */
  29612. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  29613. keyDerSz, RSAk), 0);
  29614. /* No certificate. */
  29615. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  29616. ECDSAk), BAD_FUNC_ARG);
  29617. /* Bad cert size. */
  29618. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  29619. RSAk), 0);
  29620. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  29621. /* No public key. */
  29622. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  29623. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  29624. /* Bad public key size. */
  29625. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  29626. RSAk), BAD_FUNC_ARG);
  29627. /* Wrong aglo. */
  29628. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  29629. keyDerSz, ECDSAk), ASN_PARSE_E);
  29630. wc_FreeDecodedCert(&decoded);
  29631. if (cert_der != NULL)
  29632. free(cert_der);
  29633. if (cert_buf != NULL)
  29634. free(cert_buf);
  29635. #endif
  29636. return EXPECT_RESULT();
  29637. }
  29638. static int test_wolfSSL_certs(void)
  29639. {
  29640. EXPECT_DECLS;
  29641. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29642. !defined(NO_RSA)
  29643. X509* x509ext = NULL;
  29644. #ifdef OPENSSL_ALL
  29645. X509* x509 = NULL;
  29646. WOLFSSL_X509_EXTENSION* ext = NULL;
  29647. ASN1_OBJECT* obj = NULL;
  29648. #endif
  29649. WOLFSSL* ssl = NULL;
  29650. WOLFSSL_CTX* ctx = NULL;
  29651. STACK_OF(ASN1_OBJECT)* sk = NULL;
  29652. ASN1_STRING* asn1_str = NULL;
  29653. AUTHORITY_KEYID* akey = NULL;
  29654. BASIC_CONSTRAINTS* bc = NULL;
  29655. int crit = 0;
  29656. #ifndef NO_WOLFSSL_SERVER
  29657. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  29658. #else
  29659. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  29660. #endif
  29661. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29662. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29663. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29664. #if !defined(NO_CHECK_PRIVATE_KEY)
  29665. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  29666. #endif
  29667. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29668. #if !defined(NO_CHECK_PRIVATE_KEY)
  29669. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  29670. #endif
  29671. ExpectNotNull(ssl = SSL_new(ctx));
  29672. #if !defined(NO_CHECK_PRIVATE_KEY)
  29673. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29674. #endif
  29675. #ifdef HAVE_PK_CALLBACKS
  29676. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  29677. #endif /* HAVE_PK_CALLBACKS */
  29678. /* create and use x509 */
  29679. #ifdef OPENSSL_ALL
  29680. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29681. WOLFSSL_FILETYPE_PEM));
  29682. #endif
  29683. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  29684. WOLFSSL_FILETYPE_PEM));
  29685. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  29686. #if !defined(NO_CHECK_PRIVATE_KEY)
  29687. /* with loading in a new cert the check on private key should now fail */
  29688. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29689. #endif
  29690. #if defined(USE_CERT_BUFFERS_2048)
  29691. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  29692. (unsigned char*)server_cert_der_2048,
  29693. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  29694. #endif
  29695. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  29696. /************* Get Digest of Certificate ******************/
  29697. {
  29698. byte digest[64]; /* max digest size */
  29699. word32 digestSz;
  29700. XMEMSET(digest, 0, sizeof(digest));
  29701. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  29702. WOLFSSL_SUCCESS);
  29703. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  29704. WOLFSSL_SUCCESS);
  29705. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  29706. WOLFSSL_FAILURE);
  29707. }
  29708. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  29709. /* test and checkout X509 extensions */
  29710. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  29711. NID_basic_constraints, &crit, NULL));
  29712. ExpectIntEQ(crit, 0);
  29713. #ifdef OPENSSL_ALL
  29714. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  29715. X509_EXTENSION_free(ext);
  29716. ext = NULL;
  29717. ExpectNotNull(ext = X509_EXTENSION_new());
  29718. X509_EXTENSION_set_critical(ext, 1);
  29719. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  29720. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  29721. ASN1_OBJECT_free(obj);
  29722. obj = NULL;
  29723. X509_EXTENSION_free(ext);
  29724. ext = NULL;
  29725. ExpectNotNull(ext = X509_EXTENSION_new());
  29726. X509_EXTENSION_set_critical(ext, 0);
  29727. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  29728. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  29729. NULL);
  29730. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  29731. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  29732. * and X509_get_ext_d2i has created new */
  29733. asn1_str = NULL;
  29734. X509_EXTENSION_free(ext);
  29735. ext = NULL;
  29736. #endif
  29737. BASIC_CONSTRAINTS_free(bc);
  29738. bc = NULL;
  29739. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  29740. NID_key_usage, &crit, NULL));
  29741. ExpectIntEQ(crit, 1);
  29742. ExpectIntEQ(asn1_str->type, NID_key_usage);
  29743. #ifdef OPENSSL_ALL
  29744. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  29745. X509_EXTENSION_free(ext);
  29746. ext = NULL;
  29747. #endif
  29748. ASN1_STRING_free(asn1_str);
  29749. asn1_str = NULL;
  29750. #ifdef OPENSSL_ALL
  29751. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  29752. NID_ext_key_usage, &crit, NULL));
  29753. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  29754. X509_EXTENSION_free(ext);
  29755. ext = NULL;
  29756. EXTENDED_KEY_USAGE_free(sk);
  29757. sk = NULL;
  29758. #else
  29759. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  29760. &crit, NULL);
  29761. ExpectNull(sk);
  29762. #endif
  29763. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  29764. NID_authority_key_identifier, &crit, NULL));
  29765. #ifdef OPENSSL_ALL
  29766. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  29767. akey));
  29768. X509_EXTENSION_free(ext);
  29769. ext = NULL;
  29770. #endif
  29771. wolfSSL_AUTHORITY_KEYID_free(akey);
  29772. akey = NULL;
  29773. /* NID not yet supported */
  29774. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29775. NID_private_key_usage_period, &crit, NULL));
  29776. ExpectIntEQ(crit, -1);
  29777. sk_ASN1_OBJECT_free(sk);
  29778. sk = NULL;
  29779. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  29780. NID_subject_alt_name, &crit, NULL));
  29781. {
  29782. int i;
  29783. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  29784. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  29785. ExpectIntEQ(gen->type, GEN_DNS);
  29786. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  29787. }
  29788. }
  29789. sk_GENERAL_NAME_free(sk);
  29790. sk = NULL;
  29791. /* NID not yet supported */
  29792. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29793. NID_issuer_alt_name, &crit, NULL));
  29794. ExpectIntEQ(crit, -1);
  29795. sk_ASN1_OBJECT_free(sk);
  29796. sk = NULL;
  29797. /* NID not yet supported */
  29798. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29799. NID_info_access, &crit, NULL));
  29800. sk_ASN1_OBJECT_free(sk);
  29801. sk = NULL;
  29802. /* NID not yet supported */
  29803. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29804. NID_sinfo_access, &crit, NULL));
  29805. ExpectIntEQ(crit, -1);
  29806. sk_ASN1_OBJECT_free(sk);
  29807. sk = NULL;
  29808. /* NID not yet supported */
  29809. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29810. NID_name_constraints, &crit, NULL));
  29811. ExpectIntEQ(crit, -1);
  29812. sk_ASN1_OBJECT_free(sk);
  29813. sk = NULL;
  29814. /* no cert policy set */
  29815. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29816. NID_certificate_policies, &crit, NULL));
  29817. sk_ASN1_OBJECT_free(sk);
  29818. sk = NULL;
  29819. /* NID not yet supported */
  29820. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29821. NID_policy_mappings, &crit, NULL));
  29822. ExpectIntEQ(crit, -1);
  29823. sk_ASN1_OBJECT_free(sk);
  29824. sk = NULL;
  29825. /* NID not yet supported */
  29826. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29827. NID_policy_constraints, &crit, NULL));
  29828. ExpectIntEQ(crit, -1);
  29829. sk_ASN1_OBJECT_free(sk);
  29830. sk = NULL;
  29831. /* NID not yet supported */
  29832. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29833. NID_inhibit_any_policy, &crit, NULL));
  29834. ExpectIntEQ(crit, -1);
  29835. sk_ASN1_OBJECT_free(sk);
  29836. sk = NULL;
  29837. /* NID not yet supported */
  29838. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29839. NID_tlsfeature, &crit, NULL));
  29840. ExpectIntEQ(crit, -1);
  29841. sk_ASN1_OBJECT_free(sk);
  29842. sk = NULL;
  29843. /* test invalid cases */
  29844. crit = 0;
  29845. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  29846. NULL));
  29847. ExpectIntEQ(crit, -1);
  29848. /* NULL passed for criticality. */
  29849. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  29850. NID_tlsfeature, NULL, NULL));
  29851. ExpectIntEQ(SSL_get_hit(ssl), 0);
  29852. #ifdef OPENSSL_ALL
  29853. X509_free(x509);
  29854. #endif
  29855. X509_free(x509ext);
  29856. SSL_free(ssl);
  29857. SSL_CTX_free(ctx);
  29858. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  29859. return EXPECT_RESULT();
  29860. }
  29861. static int test_wolfSSL_X509_check_private_key(void)
  29862. {
  29863. EXPECT_DECLS;
  29864. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  29865. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  29866. X509* x509 = NULL;
  29867. EVP_PKEY* pkey = NULL;
  29868. const byte* key;
  29869. /* Check with correct key */
  29870. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  29871. SSL_FILETYPE_PEM)));
  29872. key = client_key_der_2048;
  29873. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  29874. (long)sizeof_client_key_der_2048));
  29875. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  29876. EVP_PKEY_free(pkey);
  29877. pkey = NULL;
  29878. /* Check with wrong key */
  29879. key = server_key_der_2048;
  29880. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  29881. (long)sizeof_server_key_der_2048));
  29882. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  29883. /* test for incorrect parameter */
  29884. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  29885. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  29886. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  29887. EVP_PKEY_free(pkey);
  29888. X509_free(x509);
  29889. #endif
  29890. return EXPECT_RESULT();
  29891. }
  29892. static int test_wolfSSL_private_keys(void)
  29893. {
  29894. EXPECT_DECLS;
  29895. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29896. !defined(NO_FILESYSTEM)
  29897. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29898. WOLFSSL* ssl = NULL;
  29899. WOLFSSL_CTX* ctx = NULL;
  29900. EVP_PKEY* pkey = NULL;
  29901. OpenSSL_add_all_digests();
  29902. OpenSSL_add_all_algorithms();
  29903. #ifndef NO_RSA
  29904. #ifndef NO_WOLFSSL_SERVER
  29905. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29906. #else
  29907. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29908. #endif
  29909. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  29910. /* Have to load a cert before you can check the private key against that
  29911. * certificates public key! */
  29912. #if !defined(NO_CHECK_PRIVATE_KEY)
  29913. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  29914. #endif
  29915. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  29916. #if !defined(NO_CHECK_PRIVATE_KEY)
  29917. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29918. #endif
  29919. ExpectNotNull(ssl = SSL_new(ctx));
  29920. #if !defined(NO_CHECK_PRIVATE_KEY)
  29921. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29922. #endif
  29923. #ifdef USE_CERT_BUFFERS_2048
  29924. {
  29925. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29926. unsigned char buf[FOURK_BUF];
  29927. word32 bufSz;
  29928. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  29929. (unsigned char*)client_key_der_2048,
  29930. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  29931. #if !defined(NO_CHECK_PRIVATE_KEY)
  29932. /* Should mismatch now that a different private key loaded */
  29933. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29934. #endif
  29935. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  29936. (unsigned char*)server_key,
  29937. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  29938. #if !defined(NO_CHECK_PRIVATE_KEY)
  29939. /* After loading back in DER format of original key, should match */
  29940. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29941. #endif
  29942. /* test loading private key to the WOLFSSL_CTX */
  29943. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  29944. (unsigned char*)client_key_der_2048,
  29945. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  29946. #if !defined(NO_CHECK_PRIVATE_KEY)
  29947. /* Should mismatch now that a different private key loaded */
  29948. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29949. #endif
  29950. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  29951. (unsigned char*)server_key,
  29952. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  29953. #if !defined(NO_CHECK_PRIVATE_KEY)
  29954. /* After loading back in DER format of original key, should match */
  29955. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29956. #endif
  29957. /* pkey not set yet, expecting to fail */
  29958. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  29959. /* set PKEY and test again */
  29960. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29961. &server_key, (long)sizeof_server_key_der_2048));
  29962. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  29963. /* reuse PKEY structure and test
  29964. * this should be checked with a memory management sanity checker */
  29965. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  29966. server_key = (const unsigned char*)server_key_der_2048;
  29967. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29968. &server_key, (long)sizeof_server_key_der_2048));
  29969. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  29970. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  29971. bufSz = FOURK_BUF;
  29972. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  29973. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  29974. RSAk, NULL, 0)), 0);
  29975. server_key = (const unsigned char*)buf;
  29976. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  29977. (long)bufSz));
  29978. }
  29979. #endif
  29980. EVP_PKEY_free(pkey);
  29981. pkey = NULL;
  29982. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  29983. ssl = NULL;
  29984. SSL_CTX_free(ctx);
  29985. ctx = NULL;
  29986. #endif /* end of RSA private key match tests */
  29987. #ifdef HAVE_ECC
  29988. #ifndef NO_WOLFSSL_SERVER
  29989. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29990. #else
  29991. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29992. #endif
  29993. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  29994. WOLFSSL_FILETYPE_PEM));
  29995. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  29996. WOLFSSL_FILETYPE_PEM));
  29997. ExpectNotNull(ssl = SSL_new(ctx));
  29998. #if !defined(NO_CHECK_PRIVATE_KEY)
  29999. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30000. #endif
  30001. SSL_free(ssl);
  30002. ssl = NULL;
  30003. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30004. WOLFSSL_FILETYPE_PEM));
  30005. ExpectNotNull(ssl = SSL_new(ctx));
  30006. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30007. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30008. #endif
  30009. SSL_free(ssl);
  30010. ssl = NULL;
  30011. SSL_CTX_free(ctx);
  30012. ctx = NULL;
  30013. #endif /* end of ECC private key match tests */
  30014. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30015. #ifndef NO_WOLFSSL_SERVER
  30016. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30017. #else
  30018. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30019. #endif
  30020. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30021. WOLFSSL_FILETYPE_PEM));
  30022. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30023. WOLFSSL_FILETYPE_PEM));
  30024. ExpectNotNull(ssl = SSL_new(ctx));
  30025. #if !defined(NO_CHECK_PRIVATE_KEY)
  30026. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30027. #endif
  30028. SSL_free(ssl);
  30029. ssl = NULL;
  30030. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30031. WOLFSSL_FILETYPE_PEM));
  30032. ExpectNotNull(ssl = SSL_new(ctx));
  30033. #if !defined(NO_CHECK_PRIVATE_KEY)
  30034. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30035. #endif
  30036. SSL_free(ssl);
  30037. ssl = NULL;
  30038. SSL_CTX_free(ctx);
  30039. ctx = NULL;
  30040. #endif /* end of Ed25519 private key match tests */
  30041. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30042. #ifndef NO_WOLFSSL_SERVER
  30043. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30044. #else
  30045. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30046. #endif
  30047. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30048. WOLFSSL_FILETYPE_PEM));
  30049. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30050. WOLFSSL_FILETYPE_PEM));
  30051. ExpectNotNull(ssl = SSL_new(ctx));
  30052. #if !defined(NO_CHECK_PRIVATE_KEY)
  30053. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30054. #endif
  30055. SSL_free(ssl);
  30056. ssl = NULL;
  30057. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30058. WOLFSSL_FILETYPE_PEM));
  30059. ExpectNotNull(ssl = SSL_new(ctx));
  30060. #if !defined(NO_CHECK_PRIVATE_KEY)
  30061. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30062. #endif
  30063. SSL_free(ssl);
  30064. ssl = NULL;
  30065. SSL_CTX_free(ctx);
  30066. ctx = NULL;
  30067. #endif /* end of Ed448 private key match tests */
  30068. EVP_cleanup();
  30069. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30070. CONF_modules_free();
  30071. ENGINE_cleanup();
  30072. CONF_modules_unload();
  30073. (void)ssl;
  30074. (void)ctx;
  30075. (void)pkey;
  30076. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30077. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30078. return EXPECT_RESULT();
  30079. }
  30080. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30081. {
  30082. EXPECT_DECLS;
  30083. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30084. && !defined(NO_FILESYSTEM)
  30085. XFILE file = XBADFILE;
  30086. const char* fname = "./certs/server-key.pem";
  30087. EVP_PKEY* pkey = NULL;
  30088. RSA* rsa = NULL;
  30089. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30090. unsigned char* sig = NULL;
  30091. size_t sigLen = 0;
  30092. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30093. size_t tbsLen = sizeof(tbs);
  30094. /* Check error case. */
  30095. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30096. /* Read in an RSA key. */
  30097. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30098. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30099. if (file != XBADFILE)
  30100. XFCLOSE(file);
  30101. /* Make sure the key is usable by signing some data with it. */
  30102. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30103. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30104. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30105. DYNAMIC_TYPE_TMP_BUFFER));
  30106. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30107. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30108. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30109. WOLFSSL_SUCCESS);
  30110. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30111. EVP_PKEY_CTX_free(ctx);
  30112. EVP_PKEY_free(pkey);
  30113. #endif
  30114. return EXPECT_RESULT();
  30115. }
  30116. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30117. {
  30118. EXPECT_DECLS;
  30119. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30120. && !defined(NO_FILESYSTEM)
  30121. XFILE file = XBADFILE;
  30122. const char* fname = "./certs/client-keyPub.pem";
  30123. EVP_PKEY* pkey = NULL;
  30124. /* Check error case. */
  30125. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30126. /* Read in an RSA key. */
  30127. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30128. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30129. EVP_PKEY_free(pkey);
  30130. if (file != XBADFILE)
  30131. XFCLOSE(file);
  30132. #endif
  30133. return EXPECT_RESULT();
  30134. }
  30135. static int test_wolfSSL_PEM_PrivateKey(void)
  30136. {
  30137. EXPECT_DECLS;
  30138. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30139. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  30140. #ifndef NO_BIO
  30141. BIO* bio = NULL;
  30142. #endif
  30143. EVP_PKEY* pkey = NULL;
  30144. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30145. #ifndef NO_BIO
  30146. /* test creating new EVP_PKEY with bad arg */
  30147. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  30148. /* test loading RSA key using BIO */
  30149. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30150. {
  30151. XFILE file = XBADFILE;
  30152. const char* fname = "./certs/server-key.pem";
  30153. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30154. size_t sz = 0;
  30155. byte* buf = NULL;
  30156. EVP_PKEY* pkey2 = NULL;
  30157. EVP_PKEY* pkey3 = NULL;
  30158. RSA* rsa_key = NULL;
  30159. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30160. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30161. ExpectIntGT(sz = XFTELL(file), 0);
  30162. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30163. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30164. if (buf != NULL) {
  30165. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30166. }
  30167. if (file != XBADFILE) {
  30168. XFCLOSE(file);
  30169. file = XBADFILE;
  30170. }
  30171. /* Test using BIO new mem and loading PEM private key */
  30172. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30173. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30174. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30175. buf = NULL;
  30176. BIO_free(bio);
  30177. bio = NULL;
  30178. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30179. if (pkey2 != NULL) {
  30180. pkey2->type = EVP_PKEY_RSA;
  30181. }
  30182. /* Test parameter copy */
  30183. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30184. EVP_PKEY_free(pkey2);
  30185. EVP_PKEY_free(pkey);
  30186. pkey = NULL;
  30187. /* Qt unit test case : rsa pkcs8 key */
  30188. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30189. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30190. ExpectIntGT(sz = XFTELL(file), 0);
  30191. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30192. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30193. if (buf) {
  30194. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30195. }
  30196. if (file != XBADFILE) {
  30197. XFCLOSE(file);
  30198. file = XBADFILE;
  30199. }
  30200. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30201. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30202. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30203. buf = NULL;
  30204. BIO_free(bio);
  30205. bio = NULL;
  30206. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30207. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30208. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30209. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30210. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30211. #else
  30212. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30213. #endif
  30214. RSA_free(rsa_key);
  30215. EVP_PKEY_free(pkey3);
  30216. EVP_PKEY_free(pkey);
  30217. pkey = NULL;
  30218. }
  30219. #endif
  30220. /* test loading ECC key using BIO */
  30221. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30222. {
  30223. XFILE file = XBADFILE;
  30224. const char* fname = "./certs/ecc-key.pem";
  30225. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30226. size_t sz = 0;
  30227. byte* buf = NULL;
  30228. EVP_PKEY* pkey2 = NULL;
  30229. EVP_PKEY* pkey3 = NULL;
  30230. EC_KEY* ec_key = NULL;
  30231. int nid = 0;
  30232. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30233. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30234. ExpectIntGT(sz = XFTELL(file), 0);
  30235. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30236. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30237. if (buf) {
  30238. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30239. }
  30240. if (file != XBADFILE) {
  30241. XFCLOSE(file);
  30242. file = XBADFILE;
  30243. }
  30244. /* Test using BIO new mem and loading PEM private key */
  30245. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30246. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30247. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30248. buf = NULL;
  30249. BIO_free(bio);
  30250. bio = NULL;
  30251. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30252. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30253. if (pkey2 != NULL) {
  30254. pkey2->type = EVP_PKEY_EC;
  30255. }
  30256. /* Test parameter copy */
  30257. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30258. /* Qt unit test case 1*/
  30259. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30260. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30261. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30262. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30263. #else
  30264. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30265. #endif
  30266. /* Test default digest */
  30267. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30268. ExpectIntEQ(nid, NID_sha256);
  30269. EC_KEY_free(ec_key);
  30270. ec_key = NULL;
  30271. EVP_PKEY_free(pkey3);
  30272. pkey3 = NULL;
  30273. EVP_PKEY_free(pkey2);
  30274. pkey2 = NULL;
  30275. EVP_PKEY_free(pkey);
  30276. pkey = NULL;
  30277. /* Qt unit test case ec pkcs8 key */
  30278. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  30279. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30280. ExpectIntGT(sz = XFTELL(file), 0);
  30281. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30282. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30283. if (buf) {
  30284. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30285. }
  30286. if (file != XBADFILE) {
  30287. XFCLOSE(file);
  30288. file = XBADFILE;
  30289. }
  30290. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30291. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30292. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30293. buf = NULL;
  30294. BIO_free(bio);
  30295. bio = NULL;
  30296. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30297. /* Qt unit test case */
  30298. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30299. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30300. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30301. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30302. #else
  30303. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30304. #endif
  30305. EC_KEY_free(ec_key);
  30306. EVP_PKEY_free(pkey3);
  30307. EVP_PKEY_free(pkey);
  30308. pkey = NULL;
  30309. }
  30310. #endif
  30311. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  30312. defined(WOLFSSL_CERT_GEN))
  30313. {
  30314. #define BIO_PEM_TEST_CHAR 'a'
  30315. EVP_PKEY* pkey2 = NULL;
  30316. unsigned char extra[10];
  30317. int i;
  30318. BIO* pub_bio = NULL;
  30319. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30320. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30321. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30322. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30323. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30324. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  30325. &server_key, (long)sizeof_server_key_der_2048));
  30326. ExpectNull(pkey);
  30327. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30328. &server_key, (long)sizeof_server_key_der_2048));
  30329. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  30330. NULL), WOLFSSL_FAILURE);
  30331. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  30332. NULL), WOLFSSL_FAILURE);
  30333. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30334. NULL), WOLFSSL_SUCCESS);
  30335. ExpectIntGT(BIO_pending(bio), 0);
  30336. ExpectIntEQ(BIO_pending(bio), 1679);
  30337. /* Check if the pubkey API writes only the public key */
  30338. #ifdef WOLFSSL_KEY_GEN
  30339. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30340. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30341. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30342. ExpectIntGT(BIO_pending(pub_bio), 0);
  30343. /* Previously both the private key and the pubkey calls would write
  30344. * out the private key and the PEM header was the only difference.
  30345. * The public PEM should be significantly shorter than the
  30346. * private key versison. */
  30347. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30348. #endif
  30349. /* test creating new EVP_PKEY with good args */
  30350. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30351. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  30352. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  30353. /* test of reuse of EVP_PKEY */
  30354. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30355. ExpectIntEQ(BIO_pending(bio), 0);
  30356. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30357. SSL_SUCCESS);
  30358. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  30359. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30360. ExpectNotNull(pkey);
  30361. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30362. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  30363. }
  30364. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  30365. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30366. for (i = 0; i < 10; i++) {
  30367. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  30368. }
  30369. BIO_free(pub_bio);
  30370. BIO_free(bio);
  30371. bio = NULL;
  30372. EVP_PKEY_free(pkey);
  30373. pkey = NULL;
  30374. EVP_PKEY_free(pkey2);
  30375. }
  30376. #endif
  30377. /* key is DES encrypted */
  30378. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  30379. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  30380. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  30381. {
  30382. XFILE f = XBADFILE;
  30383. wc_pem_password_cb* passwd_cb = NULL;
  30384. void* passwd_cb_userdata;
  30385. SSL_CTX* ctx = NULL;
  30386. char passwd[] = "bad password";
  30387. #ifndef WOLFSSL_NO_TLS12
  30388. #ifndef NO_WOLFSSL_SERVER
  30389. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  30390. #else
  30391. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  30392. #endif
  30393. #else
  30394. #ifndef NO_WOLFSSL_SERVER
  30395. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  30396. #else
  30397. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  30398. #endif
  30399. #endif
  30400. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  30401. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  30402. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  30403. ExpectNull(passwd_cb_userdata =
  30404. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  30405. /* fail case with password call back */
  30406. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  30407. (void*)passwd));
  30408. BIO_free(bio);
  30409. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  30410. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  30411. (void*)passwd));
  30412. BIO_free(bio);
  30413. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  30414. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  30415. if ((bio == NULL) && (f != XBADFILE)) {
  30416. XFCLOSE(f);
  30417. }
  30418. /* use callback that works */
  30419. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  30420. (void*)"yassl123"));
  30421. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  30422. EVP_PKEY_free(pkey);
  30423. pkey = NULL;
  30424. BIO_free(bio);
  30425. bio = NULL;
  30426. SSL_CTX_free(ctx);
  30427. }
  30428. #endif /* !defined(NO_DES3) */
  30429. #endif /* !NO_BIO */
  30430. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30431. {
  30432. unsigned char buf[2048];
  30433. size_t bytes = 0;
  30434. XFILE f = XBADFILE;
  30435. SSL_CTX* ctx = NULL;
  30436. #ifndef WOLFSSL_NO_TLS12
  30437. #ifndef NO_WOLFSSL_SERVER
  30438. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  30439. #else
  30440. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  30441. #endif
  30442. #else
  30443. #ifndef NO_WOLFSSL_SERVER
  30444. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  30445. #else
  30446. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  30447. #endif
  30448. #endif
  30449. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30450. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  30451. if (f != XBADFILE)
  30452. XFCLOSE(f);
  30453. server_key = buf;
  30454. pkey = NULL;
  30455. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  30456. ExpectNull(pkey);
  30457. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  30458. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  30459. EVP_PKEY_free(pkey);
  30460. pkey = NULL;
  30461. SSL_CTX_free(ctx);
  30462. server_key = NULL;
  30463. }
  30464. #endif
  30465. #ifndef NO_BIO
  30466. (void)bio;
  30467. #endif
  30468. (void)pkey;
  30469. (void)server_key;
  30470. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  30471. return EXPECT_RESULT();
  30472. }
  30473. static int test_wolfSSL_PEM_file_RSAKey(void)
  30474. {
  30475. EXPECT_DECLS;
  30476. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30477. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  30478. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  30479. RSA* rsa = NULL;
  30480. XFILE fp = XBADFILE;
  30481. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  30482. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  30483. if (fp != XBADFILE)
  30484. XFCLOSE(fp);
  30485. ExpectIntEQ(RSA_size(rsa), 256);
  30486. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  30487. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  30488. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  30489. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  30490. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  30491. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  30492. RSA_free(rsa);
  30493. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30494. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  30495. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  30496. return EXPECT_RESULT();
  30497. }
  30498. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  30499. {
  30500. EXPECT_DECLS;
  30501. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  30502. !defined(NO_FILESYSTEM) && \
  30503. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  30504. RSA* rsa = NULL;
  30505. XFILE f = NULL;
  30506. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  30507. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  30508. ExpectIntEQ(RSA_size(rsa), 256);
  30509. if (f != XBADFILE) {
  30510. XFCLOSE(f);
  30511. f = XBADFILE;
  30512. }
  30513. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  30514. NULL), WOLFSSL_FAILURE);
  30515. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  30516. NULL), WOLFSSL_FAILURE);
  30517. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  30518. WOLFSSL_SUCCESS);
  30519. RSA_free(rsa);
  30520. #ifdef HAVE_ECC
  30521. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  30522. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  30523. if (f != XBADFILE)
  30524. XFCLOSE(f);
  30525. #endif /* HAVE_ECC */
  30526. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30527. return EXPECT_RESULT();
  30528. }
  30529. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  30530. {
  30531. EXPECT_DECLS;
  30532. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30533. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30534. XFILE file = XBADFILE;
  30535. const char* fname = "./certs/client-keyPub.pem";
  30536. RSA *rsa = NULL;
  30537. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  30538. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30539. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  30540. ExpectIntEQ(RSA_size(rsa), 256);
  30541. RSA_free(rsa);
  30542. if (file != XBADFILE)
  30543. XFCLOSE(file);
  30544. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30545. return EXPECT_RESULT();
  30546. }
  30547. #ifndef NO_BIO
  30548. static int test_wolfSSL_PEM_bio_RSAKey(void)
  30549. {
  30550. EXPECT_DECLS;
  30551. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30552. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  30553. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  30554. RSA* rsa = NULL;
  30555. BIO* bio = NULL;
  30556. /* PrivateKey */
  30557. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  30558. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  30559. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  30560. ExpectNotNull(rsa);
  30561. ExpectIntEQ(RSA_size(rsa), 256);
  30562. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  30563. NULL), WOLFSSL_FAILURE);
  30564. BIO_free(bio);
  30565. bio = NULL;
  30566. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30567. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  30568. NULL), WOLFSSL_SUCCESS);
  30569. BIO_free(bio);
  30570. bio = NULL;
  30571. RSA_free(rsa);
  30572. rsa = NULL;
  30573. /* PUBKEY */
  30574. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  30575. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  30576. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30577. ExpectIntEQ(RSA_size(rsa), 256);
  30578. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30579. BIO_free(bio);
  30580. bio = NULL;
  30581. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30582. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  30583. BIO_free(bio);
  30584. bio = NULL;
  30585. RSA_free(rsa);
  30586. rsa = NULL;
  30587. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  30588. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  30589. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30590. BIO_free(bio);
  30591. bio = NULL;
  30592. RSA_free(rsa);
  30593. rsa = NULL;
  30594. #ifdef HAVE_ECC
  30595. /* ensure that non-rsa keys do not work */
  30596. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  30597. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30598. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30599. BIO_free(bio);
  30600. bio = NULL;
  30601. RSA_free(rsa);
  30602. rsa = NULL;
  30603. #endif /* HAVE_ECC */
  30604. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30605. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  30606. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  30607. return EXPECT_RESULT();
  30608. }
  30609. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  30610. {
  30611. EXPECT_DECLS;
  30612. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30613. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30614. RSA* rsa = NULL;
  30615. RSA* rsa_dup = NULL;
  30616. BIO* bio = NULL;
  30617. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  30618. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30619. ExpectIntEQ(RSA_size(rsa), 256);
  30620. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  30621. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  30622. /* Test duplicating empty key. */
  30623. ExpectNotNull(rsa_dup = RSA_new());
  30624. ExpectNull(RSAPublicKey_dup(rsa_dup));
  30625. RSA_free(rsa_dup);
  30626. rsa_dup = NULL;
  30627. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  30628. ExpectPtrNE(rsa_dup, rsa);
  30629. #endif
  30630. /* test if valgrind complains about unreleased memory */
  30631. RSA_up_ref(rsa);
  30632. RSA_free(rsa);
  30633. BIO_free(bio);
  30634. bio = NULL;
  30635. RSA_free(rsa);
  30636. rsa = NULL;
  30637. RSA_free(rsa_dup);
  30638. rsa_dup = NULL;
  30639. #ifdef HAVE_ECC
  30640. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  30641. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30642. BIO_free(bio);
  30643. #endif /* HAVE_ECC */
  30644. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30645. return EXPECT_RESULT();
  30646. }
  30647. static int test_wolfSSL_PEM_bio_DSAKey(void)
  30648. {
  30649. EXPECT_DECLS;
  30650. #ifndef HAVE_SELFTEST
  30651. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  30652. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  30653. DSA* dsa = NULL;
  30654. BIO* bio = NULL;
  30655. /* PrivateKey */
  30656. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  30657. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  30658. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  30659. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  30660. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  30661. NULL), WOLFSSL_FAILURE);
  30662. BIO_free(bio);
  30663. bio = NULL;
  30664. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30665. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  30666. NULL), WOLFSSL_SUCCESS);
  30667. BIO_free(bio);
  30668. bio = NULL;
  30669. DSA_free(dsa);
  30670. dsa = NULL;
  30671. /* PUBKEY */
  30672. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  30673. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  30674. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  30675. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  30676. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30677. BIO_free(bio);
  30678. bio = NULL;
  30679. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30680. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  30681. BIO_free(bio);
  30682. bio = NULL;
  30683. DSA_free(dsa);
  30684. dsa = NULL;
  30685. #ifdef HAVE_ECC
  30686. /* ensure that non-dsa keys do not work */
  30687. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  30688. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  30689. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  30690. BIO_free(bio);
  30691. bio = NULL;
  30692. DSA_free(dsa);
  30693. dsa = NULL;
  30694. #endif /* HAVE_ECC */
  30695. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  30696. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  30697. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  30698. #endif /* HAVE_SELFTEST */
  30699. return EXPECT_RESULT();
  30700. }
  30701. static int test_wolfSSL_PEM_bio_ECKey(void)
  30702. {
  30703. EXPECT_DECLS;
  30704. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30705. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  30706. EC_KEY* ec = NULL;
  30707. EC_KEY* ec2;
  30708. BIO* bio = NULL;
  30709. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  30710. unsigned char* pem = NULL;
  30711. int pLen;
  30712. #endif
  30713. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  30714. "MAA=\n"
  30715. "-----END PUBLIC KEY-----";
  30716. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  30717. "MAA=\n"
  30718. "-----END EC PRIVATE KEY-----";
  30719. /* PrivateKey */
  30720. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  30721. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  30722. ec2 = NULL;
  30723. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  30724. ExpectIntEQ(ec == ec2, 1);
  30725. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  30726. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  30727. NULL), WOLFSSL_FAILURE);
  30728. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  30729. NULL), WOLFSSL_FAILURE);
  30730. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  30731. WOLFSSL_FAILURE);
  30732. BIO_free(bio);
  30733. bio = NULL;
  30734. /* Public key data - fail. */
  30735. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  30736. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  30737. BIO_free(bio);
  30738. bio = NULL;
  30739. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30740. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  30741. NULL), WOLFSSL_SUCCESS);
  30742. BIO_free(bio);
  30743. bio = NULL;
  30744. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  30745. NULL),WOLFSSL_FAILURE);
  30746. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  30747. WOLFSSL_FAILURE);
  30748. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  30749. WOLFSSL_FAILURE);
  30750. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  30751. WOLFSSL_SUCCESS);
  30752. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  30753. NULL), 0);
  30754. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  30755. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  30756. NULL), 0);
  30757. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  30758. NULL), 0);
  30759. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  30760. &pLen), 0);
  30761. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  30762. &pLen), 0);
  30763. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  30764. &pLen), 0);
  30765. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  30766. NULL), 0);
  30767. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  30768. &pLen), 1);
  30769. ExpectIntGT(pLen, 0);
  30770. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30771. #endif
  30772. EC_KEY_free(ec);
  30773. ec = NULL;
  30774. /* PUBKEY */
  30775. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  30776. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  30777. ec2 = NULL;
  30778. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  30779. ExpectIntEQ(ec == ec2, 1);
  30780. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  30781. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30782. BIO_free(bio);
  30783. bio = NULL;
  30784. /* Test 0x30, 0x00 fails. */
  30785. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  30786. sizeof(ec_key_bad_1)));
  30787. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  30788. BIO_free(bio);
  30789. bio = NULL;
  30790. /* Private key data - fail. */
  30791. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  30792. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  30793. BIO_free(bio);
  30794. bio = NULL;
  30795. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30796. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  30797. BIO_free(bio);
  30798. bio = NULL;
  30799. /* Same test as above, but with a file pointer rather than a BIO. */
  30800. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30801. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  30802. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  30803. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  30804. EC_KEY_free(ec);
  30805. ec = NULL;
  30806. #ifndef NO_RSA
  30807. /* ensure that non-ec keys do not work */
  30808. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  30809. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  30810. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  30811. BIO_free(bio);
  30812. bio = NULL;
  30813. EC_KEY_free(ec);
  30814. ec = NULL;
  30815. #endif /* !NO_RSA */
  30816. /* Test 0x30, 0x00 fails. */
  30817. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  30818. sizeof(ec_priv_key_bad_1)));
  30819. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  30820. BIO_free(bio);
  30821. bio = NULL;
  30822. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30823. return EXPECT_RESULT();
  30824. }
  30825. static int test_wolfSSL_PEM_PUBKEY(void)
  30826. {
  30827. EXPECT_DECLS;
  30828. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  30829. BIO* bio = NULL;
  30830. EVP_PKEY* pkey = NULL;
  30831. /* test creating new EVP_PKEY with bad arg */
  30832. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  30833. /* test loading ECC key using BIO */
  30834. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30835. {
  30836. XFILE file = XBADFILE;
  30837. const char* fname = "./certs/ecc-client-keyPub.pem";
  30838. size_t sz;
  30839. byte* buf = NULL;
  30840. EVP_PKEY* pkey2 = NULL;
  30841. EC_KEY* ec_key = NULL;
  30842. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30843. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  30844. ExpectIntGT(sz = XFTELL(file), 0);
  30845. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  30846. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30847. if (buf != NULL) {
  30848. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30849. }
  30850. if (file != XBADFILE) {
  30851. XFCLOSE(file);
  30852. }
  30853. /* Test using BIO new mem and loading PEM private key */
  30854. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30855. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  30856. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30857. BIO_free(bio);
  30858. bio = NULL;
  30859. /* Qt unit test case*/
  30860. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30861. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30862. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  30863. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30864. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  30865. #else
  30866. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  30867. #endif
  30868. EC_KEY_free(ec_key);
  30869. EVP_PKEY_free(pkey2);
  30870. EVP_PKEY_free(pkey);
  30871. pkey = NULL;
  30872. }
  30873. #endif
  30874. (void)bio;
  30875. (void)pkey;
  30876. #endif
  30877. return EXPECT_RESULT();
  30878. }
  30879. #endif /* !NO_BIO */
  30880. static int test_DSA_do_sign_verify(void)
  30881. {
  30882. EXPECT_DECLS;
  30883. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  30884. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30885. !defined(NO_DSA)
  30886. unsigned char digest[WC_SHA_DIGEST_SIZE];
  30887. DSA_SIG* sig = NULL;
  30888. DSA* dsa = NULL;
  30889. word32 bytes;
  30890. byte sigBin[DSA_SIG_SIZE];
  30891. int dsacheck;
  30892. #ifdef USE_CERT_BUFFERS_1024
  30893. byte tmp[ONEK_BUF];
  30894. XMEMSET(tmp, 0, sizeof(tmp));
  30895. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  30896. bytes = sizeof_dsa_key_der_1024;
  30897. #elif defined(USE_CERT_BUFFERS_2048)
  30898. byte tmp[TWOK_BUF];
  30899. XMEMSET(tmp, 0, sizeof(tmp));
  30900. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  30901. bytes = sizeof_dsa_key_der_2048;
  30902. #else
  30903. byte tmp[TWOK_BUF];
  30904. XFILE fp = XBADFILE;
  30905. XMEMSET(tmp, 0, sizeof(tmp));
  30906. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  30907. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  30908. if (fp != XBADFILE)
  30909. XFCLOSE(fp);
  30910. #endif /* END USE_CERT_BUFFERS_1024 */
  30911. XMEMSET(digest, 202, sizeof(digest));
  30912. ExpectNotNull(dsa = DSA_new());
  30913. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  30914. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  30915. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  30916. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  30917. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  30918. DSA_SIG_free(sig);
  30919. DSA_free(dsa);
  30920. #endif
  30921. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  30922. return EXPECT_RESULT();
  30923. }
  30924. static int test_wolfSSL_tmp_dh(void)
  30925. {
  30926. EXPECT_DECLS;
  30927. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30928. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  30929. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30930. byte buff[6000];
  30931. char file[] = "./certs/dsaparams.pem";
  30932. XFILE f = XBADFILE;
  30933. int bytes = 0;
  30934. DSA* dsa = NULL;
  30935. DH* dh = NULL;
  30936. #if defined(WOLFSSL_DH_EXTRA) && \
  30937. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30938. DH* dh2 = NULL;
  30939. #endif
  30940. BIO* bio = NULL;
  30941. SSL* ssl = NULL;
  30942. SSL_CTX* ctx = NULL;
  30943. #ifndef NO_WOLFSSL_SERVER
  30944. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30945. #else
  30946. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30947. #endif
  30948. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30949. WOLFSSL_FILETYPE_PEM));
  30950. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30951. WOLFSSL_FILETYPE_PEM));
  30952. ExpectNotNull(ssl = SSL_new(ctx));
  30953. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  30954. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  30955. if (f != XBADFILE)
  30956. XFCLOSE(f);
  30957. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  30958. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  30959. ExpectNotNull(dsa);
  30960. dh = wolfSSL_DSA_dup_DH(dsa);
  30961. ExpectNotNull(dh);
  30962. #if defined(WOLFSSL_DH_EXTRA) && \
  30963. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30964. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  30965. #endif
  30966. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  30967. #ifndef NO_WOLFSSL_SERVER
  30968. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  30969. #else
  30970. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  30971. #endif
  30972. BIO_free(bio);
  30973. DSA_free(dsa);
  30974. DH_free(dh);
  30975. dh = NULL;
  30976. #if defined(WOLFSSL_DH_EXTRA) && \
  30977. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30978. DH_free(dh2);
  30979. dh2 = NULL;
  30980. #endif
  30981. SSL_free(ssl);
  30982. SSL_CTX_free(ctx);
  30983. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30984. #endif
  30985. return EXPECT_RESULT();
  30986. }
  30987. static int test_wolfSSL_ctrl(void)
  30988. {
  30989. EXPECT_DECLS;
  30990. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  30991. byte buff[6000];
  30992. BIO* bio = NULL;
  30993. int bytes;
  30994. BUF_MEM* ptr = NULL;
  30995. XMEMSET(buff, 0, sizeof(buff));
  30996. bytes = sizeof(buff);
  30997. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  30998. ExpectNotNull(BIO_s_socket());
  30999. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31000. /* needs tested after stubs filled out @TODO
  31001. SSL_ctrl
  31002. SSL_CTX_ctrl
  31003. */
  31004. BIO_free(bio);
  31005. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31006. return EXPECT_RESULT();
  31007. }
  31008. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31009. {
  31010. EXPECT_DECLS;
  31011. #ifdef OPENSSL_EXTRA
  31012. static const unsigned char pw[] = "password";
  31013. static const int pwSz = sizeof(pw) - 1;
  31014. size_t checkPwSz = 0;
  31015. const unsigned char* checkPw = NULL;
  31016. WOLFSSL_EVP_PKEY* key = NULL;
  31017. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31018. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31019. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31020. pwSz));
  31021. if (key != NULL) {
  31022. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31023. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31024. ExpectIntEQ(key->pkey_sz, pwSz);
  31025. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31026. }
  31027. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31028. ExpectIntEQ((int)checkPwSz, pwSz);
  31029. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31030. wolfSSL_EVP_PKEY_free(key);
  31031. key = NULL;
  31032. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31033. 0));
  31034. ExpectIntEQ(key->pkey_sz, 0);
  31035. if (EXPECT_SUCCESS()) {
  31036. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31037. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31038. }
  31039. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31040. ExpectIntEQ((int)checkPwSz, 0);
  31041. wolfSSL_EVP_PKEY_free(key);
  31042. key = NULL;
  31043. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31044. 0));
  31045. ExpectIntEQ(key->pkey_sz, 0);
  31046. if (EXPECT_SUCCESS()) {
  31047. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31048. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31049. }
  31050. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31051. ExpectIntEQ((int)checkPwSz, 0);
  31052. wolfSSL_EVP_PKEY_free(key);
  31053. key = NULL;
  31054. #endif /* OPENSSL_EXTRA */
  31055. return EXPECT_RESULT();
  31056. }
  31057. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31058. {
  31059. EXPECT_DECLS;
  31060. #ifdef OPENSSL_EXTRA
  31061. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31062. const char *priv = "ABCDEFGHIJKLMNOP";
  31063. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31064. WOLFSSL_EVP_PKEY* key = NULL;
  31065. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31066. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31067. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31068. NULL, (const unsigned char *)priv, 0, cipher));
  31069. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31070. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31071. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31072. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31073. wolfSSL_EVP_PKEY_free(key);
  31074. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31075. #endif /* OPENSSL_EXTRA */
  31076. return EXPECT_RESULT();
  31077. }
  31078. static int test_wolfSSL_EVP_Digest(void)
  31079. {
  31080. EXPECT_DECLS;
  31081. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31082. const char* in = "abc";
  31083. int inLen = (int)XSTRLEN(in);
  31084. byte out[WC_SHA256_DIGEST_SIZE];
  31085. unsigned int outLen;
  31086. const char* expOut =
  31087. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31088. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31089. "\x15\xAD";
  31090. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31091. "SHA256", NULL), 1);
  31092. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31093. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31094. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31095. return EXPECT_RESULT();
  31096. }
  31097. static int test_wolfSSL_EVP_Digest_all(void)
  31098. {
  31099. EXPECT_DECLS;
  31100. #ifdef OPENSSL_EXTRA
  31101. const char* digests[] = {
  31102. #ifndef NO_MD5
  31103. "MD5",
  31104. #endif
  31105. #ifndef NO_SHA
  31106. "SHA",
  31107. #endif
  31108. #ifdef WOLFSSL_SHA224
  31109. "SHA224",
  31110. #endif
  31111. #ifndef NO_SHA256
  31112. "SHA256",
  31113. #endif
  31114. #ifdef WOLFSSL_SHA384
  31115. "SHA384",
  31116. #endif
  31117. #ifdef WOLFSSL_SHA512
  31118. "SHA512",
  31119. #endif
  31120. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31121. "SHA512_224",
  31122. #endif
  31123. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31124. "SHA512_256",
  31125. #endif
  31126. #ifdef WOLFSSL_SHA3
  31127. #ifndef WOLFSSL_NOSHA3_224
  31128. "SHA3_224",
  31129. #endif
  31130. #ifndef WOLFSSL_NOSHA3_256
  31131. "SHA3_256",
  31132. #endif
  31133. "SHA3_384",
  31134. #ifndef WOLFSSL_NOSHA3_512
  31135. "SHA3_512",
  31136. #endif
  31137. #endif /* WOLFSSL_SHA3 */
  31138. NULL
  31139. };
  31140. const char** d;
  31141. const unsigned char in[] = "abc";
  31142. int inLen = XSTR_SIZEOF(in);
  31143. byte out[WC_MAX_DIGEST_SIZE];
  31144. unsigned int outLen;
  31145. for (d = digests; *d != NULL; d++) {
  31146. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31147. ExpectIntGT(outLen, 0);
  31148. ExpectIntEQ(EVP_MD_size(*d), outLen);
  31149. }
  31150. #endif
  31151. return EXPECT_RESULT();
  31152. }
  31153. static int test_wolfSSL_EVP_MD_size(void)
  31154. {
  31155. EXPECT_DECLS;
  31156. #ifdef OPENSSL_EXTRA
  31157. WOLFSSL_EVP_MD_CTX mdCtx;
  31158. #ifdef WOLFSSL_SHA3
  31159. #ifndef WOLFSSL_NOSHA3_224
  31160. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31161. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31162. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31163. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31164. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31165. #endif
  31166. #ifndef WOLFSSL_NOSHA3_256
  31167. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31168. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31169. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31170. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31171. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31172. #endif
  31173. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31174. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31175. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31176. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31177. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31178. #ifndef WOLFSSL_NOSHA3_512
  31179. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31180. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31181. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31182. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31183. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31184. #endif
  31185. #endif /* WOLFSSL_SHA3 */
  31186. #ifndef NO_SHA256
  31187. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31188. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31189. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31190. WC_SHA256_DIGEST_SIZE);
  31191. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31192. WC_SHA256_BLOCK_SIZE);
  31193. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31194. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31195. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31196. #endif
  31197. #ifndef NO_MD5
  31198. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31199. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31200. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31201. WC_MD5_DIGEST_SIZE);
  31202. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31203. WC_MD5_BLOCK_SIZE);
  31204. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31205. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31206. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31207. #endif
  31208. #ifdef WOLFSSL_SHA224
  31209. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31210. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31211. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31212. WC_SHA224_DIGEST_SIZE);
  31213. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31214. WC_SHA224_BLOCK_SIZE);
  31215. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31216. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31217. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31218. #endif
  31219. #ifdef WOLFSSL_SHA384
  31220. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31221. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31222. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31223. WC_SHA384_DIGEST_SIZE);
  31224. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31225. WC_SHA384_BLOCK_SIZE);
  31226. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31227. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31228. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31229. #endif
  31230. #ifdef WOLFSSL_SHA512
  31231. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31232. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31233. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31234. WC_SHA512_DIGEST_SIZE);
  31235. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31236. WC_SHA512_BLOCK_SIZE);
  31237. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  31238. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  31239. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31240. #endif
  31241. #ifndef NO_SHA
  31242. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31243. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  31244. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31245. WC_SHA_DIGEST_SIZE);
  31246. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31247. WC_SHA_BLOCK_SIZE);
  31248. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31249. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31250. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31251. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31252. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  31253. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31254. WC_SHA_DIGEST_SIZE);
  31255. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31256. WC_SHA_BLOCK_SIZE);
  31257. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31258. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31259. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31260. #endif
  31261. /* error case */
  31262. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31263. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  31264. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31265. BAD_FUNC_ARG);
  31266. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  31267. /* Cleanup is valid on uninit'ed struct */
  31268. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31269. #endif /* OPENSSL_EXTRA */
  31270. return EXPECT_RESULT();
  31271. }
  31272. static int test_wolfSSL_EVP_MD_pkey_type(void)
  31273. {
  31274. EXPECT_DECLS;
  31275. #ifdef OPENSSL_EXTRA
  31276. const WOLFSSL_EVP_MD* md;
  31277. #ifndef NO_MD5
  31278. ExpectNotNull(md = EVP_md5());
  31279. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  31280. #endif
  31281. #ifndef NO_SHA
  31282. ExpectNotNull(md = EVP_sha1());
  31283. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  31284. #endif
  31285. #ifdef WOLFSSL_SHA224
  31286. ExpectNotNull(md = EVP_sha224());
  31287. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  31288. #endif
  31289. ExpectNotNull(md = EVP_sha256());
  31290. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  31291. #ifdef WOLFSSL_SHA384
  31292. ExpectNotNull(md = EVP_sha384());
  31293. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  31294. #endif
  31295. #ifdef WOLFSSL_SHA512
  31296. ExpectNotNull(md = EVP_sha512());
  31297. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  31298. #endif
  31299. #endif
  31300. return EXPECT_RESULT();
  31301. }
  31302. #ifdef OPENSSL_EXTRA
  31303. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  31304. size_t testKeySz, const char* testData, size_t testDataSz,
  31305. const byte* testResult, size_t testResultSz)
  31306. {
  31307. EXPECT_DECLS;
  31308. unsigned char check[WC_MAX_DIGEST_SIZE];
  31309. size_t checkSz = -1;
  31310. WOLFSSL_EVP_PKEY* key = NULL;
  31311. WOLFSSL_EVP_MD_CTX mdCtx;
  31312. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  31313. testKey, (int)testKeySz));
  31314. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31315. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31316. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31317. (unsigned int)testDataSz), 1);
  31318. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31319. ExpectIntEQ((int)checkSz, (int)testResultSz);
  31320. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31321. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31322. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31323. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31324. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31325. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31326. (unsigned int)testDataSz), 1);
  31327. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31328. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31329. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31330. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31331. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31332. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31333. ExpectIntEQ((int)checkSz, (int)testResultSz);
  31334. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31335. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31336. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31337. (unsigned int)testDataSz - 4), 1);
  31338. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31339. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31340. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31341. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31342. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31343. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31344. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31345. (unsigned int)testDataSz - 4), 1);
  31346. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31347. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31348. wolfSSL_EVP_PKEY_free(key);
  31349. return EXPECT_RESULT();
  31350. }
  31351. #endif
  31352. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  31353. {
  31354. EXPECT_DECLS;
  31355. #ifdef OPENSSL_EXTRA
  31356. static const unsigned char testKey[] =
  31357. {
  31358. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31359. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31360. 0x0b, 0x0b, 0x0b, 0x0b
  31361. };
  31362. static const char testData[] = "Hi There";
  31363. #ifdef WOLFSSL_SHA224
  31364. static const unsigned char testResultSha224[] =
  31365. {
  31366. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  31367. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  31368. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  31369. 0x53, 0x68, 0x4b, 0x22
  31370. };
  31371. #endif
  31372. #ifndef NO_SHA256
  31373. static const unsigned char testResultSha256[] =
  31374. {
  31375. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  31376. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  31377. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  31378. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  31379. };
  31380. #endif
  31381. #ifdef WOLFSSL_SHA384
  31382. static const unsigned char testResultSha384[] =
  31383. {
  31384. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  31385. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  31386. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  31387. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  31388. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  31389. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  31390. };
  31391. #endif
  31392. #ifdef WOLFSSL_SHA512
  31393. static const unsigned char testResultSha512[] =
  31394. {
  31395. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  31396. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  31397. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  31398. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  31399. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  31400. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  31401. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  31402. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  31403. };
  31404. #endif
  31405. #ifdef WOLFSSL_SHA3
  31406. #ifndef WOLFSSL_NOSHA3_224
  31407. static const unsigned char testResultSha3_224[] =
  31408. {
  31409. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  31410. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  31411. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  31412. 0xf3, 0xc8, 0x60, 0xf7
  31413. };
  31414. #endif
  31415. #ifndef WOLFSSL_NOSHA3_256
  31416. static const unsigned char testResultSha3_256[] =
  31417. {
  31418. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  31419. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  31420. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  31421. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  31422. };
  31423. #endif
  31424. #ifndef WOLFSSL_NOSHA3_384
  31425. static const unsigned char testResultSha3_384[] =
  31426. {
  31427. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  31428. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  31429. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  31430. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  31431. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  31432. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  31433. };
  31434. #endif
  31435. #ifndef WOLFSSL_NOSHA3_512
  31436. static const unsigned char testResultSha3_512[] =
  31437. {
  31438. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  31439. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  31440. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  31441. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  31442. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  31443. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  31444. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  31445. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  31446. };
  31447. #endif
  31448. #endif
  31449. #ifndef NO_SHA256
  31450. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  31451. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  31452. sizeof(testResultSha256)), TEST_SUCCESS);
  31453. #endif
  31454. #ifdef WOLFSSL_SHA224
  31455. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  31456. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  31457. sizeof(testResultSha224)), TEST_SUCCESS);
  31458. #endif
  31459. #ifdef WOLFSSL_SHA384
  31460. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  31461. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  31462. sizeof(testResultSha384)), TEST_SUCCESS);
  31463. #endif
  31464. #ifdef WOLFSSL_SHA512
  31465. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  31466. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  31467. sizeof(testResultSha512)), TEST_SUCCESS);
  31468. #endif
  31469. #ifdef WOLFSSL_SHA3
  31470. #ifndef WOLFSSL_NOSHA3_224
  31471. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  31472. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  31473. sizeof(testResultSha3_224)), TEST_SUCCESS);
  31474. #endif
  31475. #ifndef WOLFSSL_NOSHA3_256
  31476. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  31477. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  31478. sizeof(testResultSha3_256)), TEST_SUCCESS);
  31479. #endif
  31480. #ifndef WOLFSSL_NOSHA3_384
  31481. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  31482. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  31483. sizeof(testResultSha3_384)), TEST_SUCCESS);
  31484. #endif
  31485. #ifndef WOLFSSL_NOSHA3_512
  31486. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  31487. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  31488. sizeof(testResultSha3_512)), TEST_SUCCESS);
  31489. #endif
  31490. #endif
  31491. #endif /* OPENSSL_EXTRA */
  31492. return EXPECT_RESULT();
  31493. }
  31494. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  31495. {
  31496. EXPECT_DECLS;
  31497. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  31498. WOLFSSL_EVP_PKEY* privKey = NULL;
  31499. WOLFSSL_EVP_PKEY* pubKey = NULL;
  31500. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  31501. const char testData[] = "Hi There";
  31502. WOLFSSL_EVP_MD_CTX mdCtx;
  31503. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  31504. int ret;
  31505. size_t checkSz = -1;
  31506. int sz = 2048 / 8;
  31507. const unsigned char* cp;
  31508. const unsigned char* p;
  31509. unsigned char check[2048/8];
  31510. size_t i;
  31511. int paddings[] = {
  31512. RSA_PKCS1_PADDING,
  31513. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  31514. RSA_PKCS1_PSS_PADDING,
  31515. #endif
  31516. };
  31517. cp = client_key_der_2048;
  31518. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  31519. sizeof_client_key_der_2048)));
  31520. p = client_keypub_der_2048;
  31521. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  31522. sizeof_client_keypub_der_2048)));
  31523. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31524. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  31525. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31526. NULL, privKey), 1);
  31527. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31528. (unsigned int)XSTRLEN(testData)), 1);
  31529. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31530. ExpectIntEQ((int)checkSz, sz);
  31531. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31532. ExpectIntEQ((int)checkSz,sz);
  31533. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  31534. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  31535. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  31536. ExpectIntEQ(ret, 1);
  31537. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31538. ExpectIntEQ(ret, 1);
  31539. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31540. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31541. NULL, pubKey), 1);
  31542. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31543. (unsigned int)XSTRLEN(testData)),
  31544. 1);
  31545. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31546. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31547. ExpectIntEQ(ret, 1);
  31548. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31549. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31550. NULL, privKey), 1);
  31551. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31552. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31553. ExpectIntEQ((int)checkSz, sz);
  31554. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31555. ExpectIntEQ((int)checkSz, sz);
  31556. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31557. (unsigned int)XSTRLEN(testData) - 4), 1);
  31558. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31559. ExpectIntEQ((int)checkSz, sz);
  31560. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31561. ExpectIntEQ(ret, 1);
  31562. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31563. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31564. NULL, pubKey), 1);
  31565. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31566. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31567. (unsigned int)XSTRLEN(testData) - 4),
  31568. 1);
  31569. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31570. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31571. ExpectIntEQ(ret, 1);
  31572. /* Check all signing padding types */
  31573. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  31574. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31575. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  31576. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  31577. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  31578. paddings[i]), 1);
  31579. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31580. (unsigned int)XSTRLEN(testData)), 1);
  31581. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31582. ExpectIntEQ((int)checkSz, sz);
  31583. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31584. ExpectIntEQ((int)checkSz,sz);
  31585. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31586. ExpectIntEQ(ret, 1);
  31587. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31588. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  31589. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  31590. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  31591. paddings[i]), 1);
  31592. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31593. (unsigned int)XSTRLEN(testData)), 1);
  31594. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31595. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31596. ExpectIntEQ(ret, 1);
  31597. }
  31598. wolfSSL_EVP_PKEY_free(pubKey);
  31599. wolfSSL_EVP_PKEY_free(privKey);
  31600. #endif
  31601. return EXPECT_RESULT();
  31602. }
  31603. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  31604. {
  31605. EXPECT_DECLS;
  31606. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  31607. WOLFSSL_EVP_PKEY* privKey = NULL;
  31608. WOLFSSL_EVP_PKEY* pubKey = NULL;
  31609. const char testData[] = "Hi There";
  31610. WOLFSSL_EVP_MD_CTX mdCtx;
  31611. int ret;
  31612. size_t checkSz = -1;
  31613. const unsigned char* cp;
  31614. const unsigned char* p;
  31615. unsigned char check[2048/8];
  31616. cp = ecc_clikey_der_256;
  31617. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  31618. sizeof_ecc_clikey_der_256));
  31619. p = ecc_clikeypub_der_256;
  31620. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  31621. sizeof_ecc_clikeypub_der_256)));
  31622. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31623. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31624. NULL, privKey), 1);
  31625. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31626. (unsigned int)XSTRLEN(testData)), 1);
  31627. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31628. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31629. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31630. ExpectIntEQ(ret, 1);
  31631. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31632. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31633. NULL, pubKey), 1);
  31634. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31635. (unsigned int)XSTRLEN(testData)),
  31636. 1);
  31637. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31638. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31639. ExpectIntEQ(ret, 1);
  31640. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31641. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31642. NULL, privKey), 1);
  31643. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31644. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31645. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31646. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31647. (unsigned int)XSTRLEN(testData) - 4), 1);
  31648. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31649. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31650. ExpectIntEQ(ret, 1);
  31651. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31652. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31653. NULL, pubKey), 1);
  31654. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31655. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31656. (unsigned int)XSTRLEN(testData) - 4),
  31657. 1);
  31658. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31659. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31660. ExpectIntEQ(ret, 1);
  31661. wolfSSL_EVP_PKEY_free(pubKey);
  31662. wolfSSL_EVP_PKEY_free(privKey);
  31663. #endif
  31664. return EXPECT_RESULT();
  31665. }
  31666. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  31667. {
  31668. EXPECT_DECLS;
  31669. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31670. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  31671. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31672. char caFile[] = "./certs/client-ca.pem";
  31673. char clientFile[] = "./certs/client-cert.pem";
  31674. SSL_CTX* ctx = NULL;
  31675. X509* x509 = NULL;
  31676. BIO *bio = NULL;
  31677. X509 *cert = NULL;
  31678. X509 *ca = NULL;
  31679. STACK_OF(X509) *chain = NULL;
  31680. STACK_OF(X509) *chain2 = NULL;
  31681. #ifndef NO_WOLFSSL_SERVER
  31682. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31683. #else
  31684. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31685. #endif
  31686. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  31687. WOLFSSL_FILETYPE_PEM));
  31688. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  31689. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  31690. WOLFSSL_FILETYPE_PEM));
  31691. /* additional test of getting EVP_PKEY key size from X509
  31692. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  31693. * allowed with user RSA */
  31694. {
  31695. EVP_PKEY* pkey = NULL;
  31696. #if defined(HAVE_ECC)
  31697. X509* ecX509 = NULL;
  31698. #endif /* HAVE_ECC */
  31699. ExpectNotNull(pkey = X509_get_pubkey(x509));
  31700. /* current RSA key is 2048 bit (256 bytes) */
  31701. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  31702. EVP_PKEY_free(pkey);
  31703. pkey = NULL;
  31704. #if defined(HAVE_ECC)
  31705. #if defined(USE_CERT_BUFFERS_256)
  31706. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  31707. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  31708. SSL_FILETYPE_ASN1));
  31709. #else
  31710. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  31711. cliEccCertFile, SSL_FILETYPE_PEM));
  31712. #endif
  31713. pkey = X509_get_pubkey(ecX509);
  31714. ExpectNotNull(pkey);
  31715. /* current ECC key is 256 bit (32 bytes) */
  31716. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  31717. X509_free(ecX509);
  31718. ecX509 = NULL;
  31719. EVP_PKEY_free(pkey);
  31720. pkey = NULL;
  31721. #endif /* HAVE_ECC */
  31722. }
  31723. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  31724. if (EXPECT_SUCCESS()) {
  31725. x509 = NULL;
  31726. }
  31727. #ifdef WOLFSSL_ENCRYPTED_KEYS
  31728. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  31729. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31730. #endif
  31731. SSL_CTX_free(ctx);
  31732. ctx = NULL;
  31733. #ifndef NO_WOLFSSL_SERVER
  31734. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31735. #else
  31736. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31737. #endif
  31738. /* Test haproxy use case */
  31739. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  31740. /* Read Certificate */
  31741. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  31742. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  31743. ExpectNotNull(chain = sk_X509_new_null());
  31744. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  31745. if (EXPECT_SUCCESS()) {
  31746. ca = NULL;
  31747. }
  31748. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  31749. ExpectNotNull(ca = sk_X509_shift(chain2));
  31750. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  31751. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  31752. if (EXPECT_SUCCESS()) {
  31753. ca = NULL;
  31754. }
  31755. BIO_free(bio);
  31756. X509_free(cert);
  31757. X509_free(ca);
  31758. X509_free(x509);
  31759. sk_X509_pop_free(chain, X509_free);
  31760. sk_X509_pop_free(chain2, X509_free);
  31761. SSL_CTX_free(ctx);
  31762. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31763. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31764. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  31765. return EXPECT_RESULT();
  31766. }
  31767. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  31768. static int test_wolfSSL_ERR_peek_last_error_line(void)
  31769. {
  31770. EXPECT_DECLS;
  31771. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31772. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31773. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  31774. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  31775. callback_functions client_cb;
  31776. callback_functions server_cb;
  31777. int line = 0;
  31778. int flag = ERR_TXT_STRING;
  31779. const char* file = NULL;
  31780. const char* data = NULL;
  31781. /* create a failed connection and inspect the error */
  31782. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31783. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31784. client_cb.method = wolfTLSv1_1_client_method;
  31785. server_cb.method = wolfTLSv1_2_server_method;
  31786. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  31787. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  31788. ExpectNotNull(data);
  31789. /* check clearing error state */
  31790. ERR_remove_state(0);
  31791. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  31792. ERR_peek_last_error_line(NULL, &line);
  31793. ExpectIntEQ(line, 0);
  31794. ERR_peek_last_error_line(&file, NULL);
  31795. ExpectNull(file);
  31796. /* retry connection to fill error queue */
  31797. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31798. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31799. client_cb.method = wolfTLSv1_1_client_method;
  31800. server_cb.method = wolfTLSv1_2_server_method;
  31801. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  31802. /* check that error code was stored */
  31803. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  31804. ERR_peek_last_error_line(NULL, &line);
  31805. ExpectIntNE(line, 0);
  31806. ERR_peek_last_error_line(&file, NULL);
  31807. ExpectNotNull(file);
  31808. fprintf(stderr, "\nTesting error print out\n");
  31809. ERR_print_errors_fp(stderr);
  31810. fprintf(stderr, "Done testing print out\n\n");
  31811. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  31812. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  31813. return EXPECT_RESULT();
  31814. }
  31815. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  31816. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31817. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31818. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  31819. {
  31820. (void) ok;
  31821. (void) ctx;
  31822. fprintf(stderr, "ENTER verify_cb\n");
  31823. return SSL_SUCCESS;
  31824. }
  31825. #endif
  31826. static int test_wolfSSL_X509_Name_canon(void)
  31827. {
  31828. EXPECT_DECLS;
  31829. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31830. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  31831. defined(WOLFSSL_CERT_GEN) && \
  31832. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  31833. const long ex_hash1 = 0x0fdb2da4;
  31834. const long ex_hash2 = 0x9f3e8c9e;
  31835. X509_NAME *name = NULL;
  31836. X509 *x509 = NULL;
  31837. XFILE file = XBADFILE;
  31838. unsigned long hash = 0;
  31839. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  31840. byte *pbuf = NULL;
  31841. word32 len = 0;
  31842. (void) ex_hash2;
  31843. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  31844. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  31845. ExpectNotNull(name = X509_get_issuer_name(x509));
  31846. /* When output buffer is NULL, should return necessary output buffer
  31847. * length.*/
  31848. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  31849. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  31850. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  31851. hash = (((unsigned long)digest[3] << 24) |
  31852. ((unsigned long)digest[2] << 16) |
  31853. ((unsigned long)digest[1] << 8) |
  31854. ((unsigned long)digest[0]));
  31855. ExpectIntEQ(hash, ex_hash1);
  31856. if (file != XBADFILE) {
  31857. XFCLOSE(file);
  31858. file = XBADFILE;
  31859. }
  31860. X509_free(x509);
  31861. x509 = NULL;
  31862. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  31863. pbuf = NULL;
  31864. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  31865. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  31866. ExpectNotNull(name = X509_get_issuer_name(x509));
  31867. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  31868. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  31869. hash = (((unsigned long)digest[3] << 24) |
  31870. ((unsigned long)digest[2] << 16) |
  31871. ((unsigned long)digest[1] << 8) |
  31872. ((unsigned long)digest[0]));
  31873. ExpectIntEQ(hash, ex_hash2);
  31874. if (file != XBADFILE)
  31875. XFCLOSE(file);
  31876. X509_free(x509);
  31877. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  31878. #endif
  31879. return EXPECT_RESULT();
  31880. }
  31881. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  31882. {
  31883. EXPECT_DECLS;
  31884. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  31885. const int MAX_DIR = 4;
  31886. const char paths[][32] = {
  31887. "./certs/ed25519",
  31888. "./certs/ecc",
  31889. "./certs/crl",
  31890. "./certs/",
  31891. };
  31892. char CertCrl_path[MAX_FILENAME_SZ];
  31893. char *p;
  31894. X509_STORE* str = NULL;
  31895. X509_LOOKUP* lookup = NULL;
  31896. WOLFSSL_STACK* sk = NULL;
  31897. int len, total_len, i;
  31898. (void)sk;
  31899. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  31900. /* illegal string */
  31901. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31902. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31903. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  31904. SSL_FILETYPE_PEM,NULL), 0);
  31905. /* free store */
  31906. X509_STORE_free(str);
  31907. str = NULL;
  31908. /* short folder string */
  31909. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31910. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31911. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  31912. SSL_FILETYPE_PEM,NULL), 1);
  31913. #if defined(WOLFSSL_INT_H)
  31914. /* only available when including internal.h */
  31915. ExpectNotNull(sk = lookup->dirs->dir_entry);
  31916. #endif
  31917. /* free store */
  31918. X509_STORE_free(str);
  31919. str = NULL;
  31920. /* typical function check */
  31921. p = &CertCrl_path[0];
  31922. total_len = 0;
  31923. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  31924. len = (int)XSTRLEN((const char*)&paths[i]);
  31925. total_len += len;
  31926. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  31927. p += len;
  31928. if (i != 0) *(p++) = SEPARATOR_CHAR;
  31929. }
  31930. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31931. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31932. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  31933. SSL_FILETYPE_PEM,NULL), 1);
  31934. #if defined(WOLFSSL_INT_H)
  31935. /* only available when including internal.h */
  31936. ExpectNotNull(sk = lookup->dirs->dir_entry);
  31937. #endif
  31938. X509_STORE_free(str);
  31939. #endif
  31940. return EXPECT_RESULT();
  31941. }
  31942. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  31943. {
  31944. EXPECT_DECLS;
  31945. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31946. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31947. defined(WOLFSSL_SIGNER_DER_CERT)
  31948. X509_STORE_CTX* ctx = NULL;
  31949. X509_STORE* str = NULL;
  31950. X509_LOOKUP* lookup = NULL;
  31951. X509* cert1 = NULL;
  31952. X509* x509Ca = NULL;
  31953. X509* x509Svr = NULL;
  31954. X509* issuer = NULL;
  31955. WOLFSSL_STACK* sk = NULL;
  31956. X509_NAME* caName = NULL;
  31957. X509_NAME* issuerName = NULL;
  31958. XFILE file1 = XBADFILE;
  31959. int i;
  31960. int cert_count = 0;
  31961. int cmp;
  31962. char der[] = "certs/ca-cert.der";
  31963. #ifdef HAVE_CRL
  31964. char pem[][100] = {
  31965. "./certs/crl/crl.pem",
  31966. "./certs/crl/crl2.pem",
  31967. "./certs/crl/caEccCrl.pem",
  31968. "./certs/crl/eccCliCRL.pem",
  31969. "./certs/crl/eccSrvCRL.pem",
  31970. ""
  31971. };
  31972. #endif
  31973. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  31974. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  31975. if (file1 != XBADFILE)
  31976. XFCLOSE(file1);
  31977. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31978. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31979. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31980. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  31981. SSL_FILETYPE_PEM,NULL), 1);
  31982. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  31983. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  31984. /* check if CA cert is loaded into the store */
  31985. for (i = 0; i < cert_count; i++) {
  31986. x509Ca = sk_X509_value(sk, i);
  31987. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  31988. }
  31989. ExpectNotNull((x509Svr =
  31990. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31991. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  31992. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  31993. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  31994. ExpectNotNull(issuer);
  31995. caName = X509_get_subject_name(x509Ca);
  31996. ExpectNotNull(caName);
  31997. issuerName = X509_get_subject_name(issuer);
  31998. ExpectNotNull(issuerName);
  31999. cmp = X509_NAME_cmp(caName, issuerName);
  32000. ExpectIntEQ(cmp, 0);
  32001. /* load der format */
  32002. X509_free(issuer);
  32003. issuer = NULL;
  32004. X509_STORE_CTX_free(ctx);
  32005. ctx = NULL;
  32006. X509_STORE_free(str);
  32007. str = NULL;
  32008. sk_X509_pop_free(sk, NULL);
  32009. sk = NULL;
  32010. X509_free(x509Svr);
  32011. x509Svr = NULL;
  32012. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32013. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32014. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32015. SSL_FILETYPE_ASN1,NULL), 1);
  32016. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32017. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32018. /* check if CA cert is loaded into the store */
  32019. for (i = 0; i < cert_count; i++) {
  32020. x509Ca = sk_X509_value(sk, i);
  32021. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32022. }
  32023. X509_STORE_free(str);
  32024. str = NULL;
  32025. sk_X509_pop_free(sk, NULL);
  32026. sk = NULL;
  32027. X509_free(cert1);
  32028. cert1 = NULL;
  32029. #ifdef HAVE_CRL
  32030. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32031. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32032. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32033. SSL_FILETYPE_PEM,NULL), 1);
  32034. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32035. "certs/server-revoked-cert.pem",
  32036. SSL_FILETYPE_PEM,NULL), 1);
  32037. if (str) {
  32038. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32039. WOLFSSL_FILETYPE_PEM), 1);
  32040. /* since store hasn't yet known the revoked cert*/
  32041. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32042. "certs/server-revoked-cert.pem",
  32043. WOLFSSL_FILETYPE_PEM), 1);
  32044. }
  32045. for (i = 0; pem[i][0] != '\0'; i++)
  32046. {
  32047. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32048. SSL_FILETYPE_PEM, NULL), 1);
  32049. }
  32050. if (str) {
  32051. /* since store knows crl list */
  32052. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32053. "certs/server-revoked-cert.pem",
  32054. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32055. }
  32056. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32057. X509_STORE_free(str);
  32058. #endif
  32059. #endif
  32060. return EXPECT_RESULT();
  32061. }
  32062. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32063. {
  32064. int res = TEST_SKIPPED;
  32065. #if defined(OPENSSL_EXTRA)
  32066. X509_STORE_CTX_cleanup(NULL);
  32067. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32068. res = TEST_SUCCESS;
  32069. #endif
  32070. return res;
  32071. }
  32072. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32073. {
  32074. EXPECT_DECLS;
  32075. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32076. X509_STORE_CTX* ctx = NULL;
  32077. X509_STORE* str = NULL;
  32078. X509* x509Ca = NULL;
  32079. X509* x509Svr = NULL;
  32080. X509* issuer = NULL;
  32081. X509_NAME* caName = NULL;
  32082. X509_NAME* issuerName = NULL;
  32083. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32084. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32085. ExpectNotNull((x509Ca =
  32086. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32087. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32088. ExpectNotNull((x509Svr =
  32089. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32090. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32091. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32092. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32093. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32094. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32095. #ifdef WOLFSSL_SIGNER_DER_CERT
  32096. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32097. #endif
  32098. X509_free(issuer);
  32099. X509_STORE_CTX_free(ctx);
  32100. X509_free(x509Svr);
  32101. X509_STORE_free(str);
  32102. X509_free(x509Ca);
  32103. #endif
  32104. return EXPECT_RESULT();
  32105. }
  32106. static int test_wolfSSL_PKCS7_certs(void)
  32107. {
  32108. EXPECT_DECLS;
  32109. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32110. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32111. STACK_OF(X509)* sk = NULL;
  32112. STACK_OF(X509_INFO)* info_sk = NULL;
  32113. PKCS7 *p7 = NULL;
  32114. BIO* bio = NULL;
  32115. const byte* p = NULL;
  32116. int buflen = 0;
  32117. int i;
  32118. /* Test twice. Once with d2i and once without to test
  32119. * that everything is free'd correctly. */
  32120. for (i = 0; i < 2; i++) {
  32121. ExpectNotNull(p7 = PKCS7_new());
  32122. if (p7 != NULL) {
  32123. p7->version = 1;
  32124. #ifdef NO_SHA
  32125. p7->hashOID = SHA256h;
  32126. #else
  32127. p7->hashOID = SHAh;
  32128. #endif
  32129. }
  32130. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32131. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32132. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32133. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32134. ExpectNotNull(sk = sk_X509_new_null());
  32135. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  32136. X509_INFO* info = NULL;
  32137. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  32138. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  32139. if (EXPECT_SUCCESS() && (info != NULL)) {
  32140. info->x509 = NULL;
  32141. }
  32142. X509_INFO_free(info);
  32143. }
  32144. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  32145. info_sk = NULL;
  32146. BIO_free(bio);
  32147. bio = NULL;
  32148. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32149. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32150. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  32151. sk_X509_pop_free(sk, X509_free);
  32152. }
  32153. sk = NULL;
  32154. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32155. if (i == 0) {
  32156. PKCS7_free(p7);
  32157. p7 = NULL;
  32158. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  32159. if (p7 != NULL) {
  32160. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  32161. * them */
  32162. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32163. }
  32164. /* PKCS7_free free's the certs */
  32165. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  32166. }
  32167. BIO_free(bio);
  32168. bio = NULL;
  32169. PKCS7_free(p7);
  32170. p7 = NULL;
  32171. }
  32172. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32173. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32174. return EXPECT_RESULT();
  32175. }
  32176. static int test_wolfSSL_X509_STORE_CTX(void)
  32177. {
  32178. EXPECT_DECLS;
  32179. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32180. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32181. X509_STORE_CTX* ctx = NULL;
  32182. X509_STORE* str = NULL;
  32183. X509* x509 = NULL;
  32184. #ifdef OPENSSL_ALL
  32185. X509* x5092 = NULL;
  32186. STACK_OF(X509) *sk = NULL;
  32187. STACK_OF(X509) *sk2 = NULL;
  32188. STACK_OF(X509) *sk3 = NULL;
  32189. #endif
  32190. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32191. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32192. ExpectNotNull((x509 =
  32193. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32194. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32195. #ifdef OPENSSL_ALL
  32196. /* sk_X509_new only in OPENSSL_ALL */
  32197. sk = sk_X509_new_null();
  32198. ExpectNotNull(sk);
  32199. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32200. #else
  32201. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32202. #endif
  32203. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32204. X509_STORE_CTX_set_error(ctx, -5);
  32205. X509_STORE_CTX_set_error(NULL, -5);
  32206. X509_STORE_CTX_free(ctx);
  32207. ctx = NULL;
  32208. #ifdef OPENSSL_ALL
  32209. sk_X509_pop_free(sk, NULL);
  32210. sk = NULL;
  32211. #endif
  32212. X509_STORE_free(str);
  32213. str = NULL;
  32214. X509_free(x509);
  32215. x509 = NULL;
  32216. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32217. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32218. X509_STORE_CTX_free(ctx);
  32219. ctx = NULL;
  32220. #ifdef OPENSSL_ALL
  32221. /* test X509_STORE_CTX_get(1)_chain */
  32222. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32223. SSL_FILETYPE_PEM)));
  32224. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32225. SSL_FILETYPE_PEM)));
  32226. ExpectNotNull((sk = sk_X509_new_null()));
  32227. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  32228. if (EXPECT_FAIL()) {
  32229. X509_free(x509);
  32230. x509 = NULL;
  32231. }
  32232. ExpectNotNull((str = X509_STORE_new()));
  32233. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  32234. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32235. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32236. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32237. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  32238. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  32239. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  32240. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  32241. X509_STORE_CTX_free(ctx);
  32242. ctx = NULL;
  32243. X509_STORE_free(str);
  32244. str = NULL;
  32245. /* CTX certs not freed yet */
  32246. X509_free(x5092);
  32247. x5092 = NULL;
  32248. sk_X509_pop_free(sk, NULL);
  32249. sk = NULL;
  32250. /* sk3 is dup so free here */
  32251. sk_X509_pop_free(sk3, NULL);
  32252. sk3 = NULL;
  32253. #endif
  32254. /* test X509_STORE_CTX_get/set_ex_data */
  32255. {
  32256. int i = 0, tmpData = 5;
  32257. void* tmpDataRet;
  32258. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32259. #ifdef HAVE_EX_DATA
  32260. for (i = 0; i < MAX_EX_DATA; i++) {
  32261. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32262. WOLFSSL_SUCCESS);
  32263. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32264. ExpectNotNull(tmpDataRet);
  32265. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32266. }
  32267. #else
  32268. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32269. WOLFSSL_FAILURE);
  32270. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32271. ExpectNull(tmpDataRet);
  32272. #endif
  32273. X509_STORE_CTX_free(ctx);
  32274. ctx = NULL;
  32275. }
  32276. /* test X509_STORE_get/set_ex_data */
  32277. {
  32278. int i = 0, tmpData = 99;
  32279. void* tmpDataRet;
  32280. ExpectNotNull(str = X509_STORE_new());
  32281. #ifdef HAVE_EX_DATA
  32282. for (i = 0; i < MAX_EX_DATA; i++) {
  32283. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32284. WOLFSSL_SUCCESS);
  32285. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32286. ExpectNotNull(tmpDataRet);
  32287. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32288. }
  32289. #else
  32290. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32291. WOLFSSL_FAILURE);
  32292. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32293. ExpectNull(tmpDataRet);
  32294. #endif
  32295. X509_STORE_free(str);
  32296. str = NULL;
  32297. }
  32298. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32299. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32300. return EXPECT_RESULT();
  32301. }
  32302. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32303. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  32304. STACK_OF(X509)* chain)
  32305. {
  32306. EXPECT_DECLS;
  32307. XFILE fp = XBADFILE;
  32308. X509* cert = NULL;
  32309. ExpectTrue((fp = XFOPEN(filename, "rb"))
  32310. != XBADFILE);
  32311. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  32312. if (fp != XBADFILE) {
  32313. XFCLOSE(fp);
  32314. fp = XBADFILE;
  32315. }
  32316. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  32317. if (EXPECT_FAIL())
  32318. X509_free(cert);
  32319. return EXPECT_RESULT();
  32320. }
  32321. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  32322. int err, int loadCA)
  32323. {
  32324. EXPECT_DECLS;
  32325. X509_STORE_CTX* ctx = NULL;
  32326. X509_STORE* str = NULL;
  32327. XFILE fp = XBADFILE;
  32328. X509* cert = NULL;
  32329. STACK_OF(X509)* untrusted = NULL;
  32330. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  32331. != XBADFILE);
  32332. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  32333. if (fp != XBADFILE) {
  32334. XFCLOSE(fp);
  32335. fp = XBADFILE;
  32336. }
  32337. ExpectNotNull(str = X509_STORE_new());
  32338. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32339. ExpectNotNull(untrusted = sk_X509_new_null());
  32340. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  32341. if (loadCA) {
  32342. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  32343. 1);
  32344. }
  32345. for (; *filenames; filenames++) {
  32346. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  32347. untrusted), TEST_SUCCESS);
  32348. }
  32349. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  32350. ExpectIntEQ(X509_verify_cert(ctx), ret);
  32351. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  32352. X509_free(cert);
  32353. X509_STORE_free(str);
  32354. X509_STORE_CTX_free(ctx);
  32355. sk_X509_pop_free(untrusted, NULL);
  32356. return EXPECT_RESULT();
  32357. }
  32358. #endif
  32359. static int test_X509_STORE_untrusted(void)
  32360. {
  32361. EXPECT_DECLS;
  32362. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32363. const char* untrusted1[] = {
  32364. "./certs/intermediate/ca-int2-cert.pem",
  32365. NULL
  32366. };
  32367. const char* untrusted2[] = {
  32368. "./certs/intermediate/ca-int-cert.pem",
  32369. "./certs/intermediate/ca-int2-cert.pem",
  32370. NULL
  32371. };
  32372. const char* untrusted3[] = {
  32373. "./certs/intermediate/ca-int-cert.pem",
  32374. "./certs/intermediate/ca-int2-cert.pem",
  32375. "./certs/ca-cert.pem",
  32376. NULL
  32377. };
  32378. /* Adding unrelated certs that should be ignored */
  32379. const char* untrusted4[] = {
  32380. "./certs/client-ca.pem",
  32381. "./certs/intermediate/ca-int-cert.pem",
  32382. "./certs/server-cert.pem",
  32383. "./certs/intermediate/ca-int2-cert.pem",
  32384. NULL
  32385. };
  32386. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  32387. * to loaded CA. */
  32388. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  32389. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  32390. /* Succeeds because path to loaded CA is available. */
  32391. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  32392. TEST_SUCCESS);
  32393. /* Fails because root CA is in the untrusted stack */
  32394. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  32395. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  32396. /* Succeeds because path to loaded CA is available. */
  32397. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  32398. TEST_SUCCESS);
  32399. #endif
  32400. return EXPECT_RESULT();
  32401. }
  32402. static int test_wolfSSL_X509_STORE_set_flags(void)
  32403. {
  32404. EXPECT_DECLS;
  32405. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32406. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32407. X509_STORE* store = NULL;
  32408. X509* x509 = NULL;
  32409. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  32410. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  32411. WOLFSSL_FILETYPE_PEM)));
  32412. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  32413. #ifdef HAVE_CRL
  32414. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  32415. WOLFSSL_SUCCESS);
  32416. #else
  32417. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  32418. NOT_COMPILED_IN);
  32419. #endif
  32420. wolfSSL_X509_free(x509);
  32421. wolfSSL_X509_STORE_free(store);
  32422. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32423. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32424. return EXPECT_RESULT();
  32425. }
  32426. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  32427. {
  32428. EXPECT_DECLS;
  32429. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  32430. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32431. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  32432. WOLFSSL_X509_STORE* store = NULL;
  32433. WOLFSSL_X509_LOOKUP* lookup = NULL;
  32434. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  32435. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  32436. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  32437. X509_FILETYPE_PEM), 1);
  32438. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  32439. X509_FILETYPE_PEM), 1);
  32440. if (store != NULL) {
  32441. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  32442. WOLFSSL_FILETYPE_PEM), 1);
  32443. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  32444. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  32445. }
  32446. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  32447. X509_FILETYPE_PEM), 1);
  32448. if (store != NULL) {
  32449. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  32450. WOLFSSL_FILETYPE_PEM), 1);
  32451. }
  32452. wolfSSL_X509_STORE_free(store);
  32453. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  32454. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32455. return EXPECT_RESULT();
  32456. }
  32457. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  32458. {
  32459. EXPECT_DECLS;
  32460. #if defined(OPENSSL_EXTRA)
  32461. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  32462. time_t c_time;
  32463. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  32464. c_time = 365*24*60*60;
  32465. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  32466. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  32467. WOLFSSL_USE_CHECK_TIME);
  32468. ExpectTrue(ctx->param->check_time == c_time);
  32469. wolfSSL_X509_STORE_CTX_free(ctx);
  32470. #endif /* OPENSSL_EXTRA */
  32471. return EXPECT_RESULT();
  32472. }
  32473. static int test_wolfSSL_CTX_get0_set1_param(void)
  32474. {
  32475. EXPECT_DECLS;
  32476. #if defined(OPENSSL_EXTRA)
  32477. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32478. SSL_CTX* ctx = NULL;
  32479. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  32480. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  32481. char testIPv4[] = "127.0.0.1";
  32482. char testhostName[] = "foo.hoge.com";
  32483. #ifndef NO_WOLFSSL_SERVER
  32484. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32485. #else
  32486. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32487. #endif
  32488. ExpectNull(SSL_CTX_get0_param(NULL));
  32489. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  32490. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  32491. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  32492. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  32493. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  32494. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  32495. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  32496. WOLFSSL_SUCCESS);
  32497. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  32498. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  32499. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  32500. (int)XSTRLEN(testhostName)));
  32501. ExpectIntEQ(0x01, pParam->hostFlags);
  32502. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  32503. /* test for incorrect parameter */
  32504. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  32505. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  32506. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  32507. SSL_CTX_free(ctx);
  32508. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  32509. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32510. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  32511. return EXPECT_RESULT();
  32512. }
  32513. static int test_wolfSSL_get0_param(void)
  32514. {
  32515. EXPECT_DECLS;
  32516. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32517. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32518. SSL_CTX* ctx = NULL;
  32519. SSL* ssl = NULL;
  32520. #ifndef NO_WOLFSSL_SERVER
  32521. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32522. #else
  32523. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32524. #endif
  32525. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32526. SSL_FILETYPE_PEM));
  32527. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32528. ExpectNotNull(ssl = SSL_new(ctx));
  32529. ExpectNotNull(SSL_get0_param(ssl));
  32530. SSL_free(ssl);
  32531. SSL_CTX_free(ctx);
  32532. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32533. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  32534. return EXPECT_RESULT();
  32535. }
  32536. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  32537. {
  32538. EXPECT_DECLS;
  32539. #if defined(OPENSSL_EXTRA)
  32540. const char host[] = "www.example.com";
  32541. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  32542. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  32543. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32544. if (pParam != NULL) {
  32545. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  32546. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  32547. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32548. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  32549. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32550. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32551. }
  32552. #endif /* OPENSSL_EXTRA */
  32553. return EXPECT_RESULT();
  32554. }
  32555. static int test_wolfSSL_set1_host(void)
  32556. {
  32557. EXPECT_DECLS;
  32558. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32559. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32560. const char host[] = "www.test_wolfSSL_set1_host.com";
  32561. const char emptyStr[] = "";
  32562. SSL_CTX* ctx = NULL;
  32563. SSL* ssl = NULL;
  32564. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  32565. #ifndef NO_WOLFSSL_SERVER
  32566. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32567. #else
  32568. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32569. #endif
  32570. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32571. SSL_FILETYPE_PEM));
  32572. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32573. ExpectNotNull(ssl = SSL_new(ctx));
  32574. pParam = SSL_get0_param(ssl);
  32575. /* we should get back host string */
  32576. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  32577. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32578. /* we should get back empty string */
  32579. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  32580. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  32581. /* we should get back host string */
  32582. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  32583. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32584. /* we should get back empty string */
  32585. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  32586. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  32587. SSL_free(ssl);
  32588. SSL_CTX_free(ctx);
  32589. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32590. #endif /* OPENSSL_EXTRA */
  32591. return EXPECT_RESULT();
  32592. }
  32593. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  32594. {
  32595. EXPECT_DECLS;
  32596. #if defined(OPENSSL_EXTRA)
  32597. unsigned char buf[16] = {0};
  32598. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  32599. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  32600. /* test 127.0.0.1 */
  32601. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  32602. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  32603. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  32604. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  32605. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32606. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  32607. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  32608. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  32609. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32610. ExpectIntEQ(XSTRNCMP(param->ipasc,
  32611. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  32612. /* test 2001:db8:: */
  32613. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32614. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32615. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32616. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  32617. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32618. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  32619. /* test ::1234:5678 */
  32620. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  32621. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32622. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32623. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  32624. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32625. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  32626. /* test 2001:db8::1234:5678 */
  32627. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32628. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32629. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32630. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  32631. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32632. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  32633. sizeof(param->ipasc)), 0);
  32634. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  32635. /* 2001:db8:1::ab9:c0a8:102 */
  32636. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32637. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  32638. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  32639. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  32640. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32641. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  32642. sizeof(param->ipasc)), 0);
  32643. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32644. #endif /* OPENSSL_EXTRA */
  32645. return EXPECT_RESULT();
  32646. }
  32647. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  32648. {
  32649. EXPECT_DECLS;
  32650. #if defined(OPENSSL_EXTRA)
  32651. X509_STORE* store = NULL;
  32652. X509_STORE_CTX* ctx = NULL;
  32653. X509_STORE_CTX* ctx_no_init = NULL;
  32654. ExpectNotNull((store = X509_STORE_new()));
  32655. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32656. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  32657. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  32658. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  32659. /* should return NULL if ctx has not bee initialized */
  32660. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  32661. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  32662. wolfSSL_X509_STORE_CTX_free(ctx);
  32663. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  32664. X509_STORE_free(store);
  32665. #endif /* OPENSSL_EXTRA */
  32666. return EXPECT_RESULT();
  32667. }
  32668. static int test_wolfSSL_CTX_set_client_CA_list(void)
  32669. {
  32670. EXPECT_DECLS;
  32671. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32672. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  32673. WOLFSSL_CTX* ctx = NULL;
  32674. WOLFSSL* ssl = NULL;
  32675. X509_NAME* name = NULL;
  32676. STACK_OF(X509_NAME)* names = NULL;
  32677. STACK_OF(X509_NAME)* ca_list = NULL;
  32678. int names_len = 0;
  32679. int i;
  32680. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  32681. /* Send two X501 names in cert request */
  32682. names = SSL_load_client_CA_file(cliCertFile);
  32683. ExpectNotNull(names);
  32684. ca_list = SSL_load_client_CA_file(caCertFile);
  32685. ExpectNotNull(ca_list);
  32686. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  32687. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  32688. if (EXPECT_FAIL()) {
  32689. wolfSSL_X509_NAME_free(name);
  32690. name = NULL;
  32691. }
  32692. SSL_CTX_set_client_CA_list(ctx, names);
  32693. /* This should only free the stack structure */
  32694. sk_X509_NAME_free(ca_list);
  32695. ca_list = NULL;
  32696. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  32697. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  32698. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  32699. for (i = 0; i < names_len; i++) {
  32700. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  32701. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  32702. }
  32703. /* Needed to be able to create ssl object */
  32704. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32705. SSL_FILETYPE_PEM));
  32706. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32707. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32708. /* load again as old names are responsibility of ctx to free*/
  32709. names = SSL_load_client_CA_file(cliCertFile);
  32710. ExpectNotNull(names);
  32711. SSL_set_client_CA_list(ssl, names);
  32712. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  32713. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  32714. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  32715. for (i = 0; i < names_len; i++) {
  32716. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  32717. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  32718. }
  32719. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  32720. {
  32721. tcp_ready ready;
  32722. func_args server_args;
  32723. callback_functions server_cb;
  32724. THREAD_TYPE serverThread;
  32725. WOLFSSL* ssl_client = NULL;
  32726. WOLFSSL_CTX* ctx_client = NULL;
  32727. SOCKET_T sockfd = 0;
  32728. /* wolfSSL_get_client_CA_list() with handshake */
  32729. StartTCP();
  32730. InitTcpReady(&ready);
  32731. XMEMSET(&server_args, 0, sizeof(func_args));
  32732. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32733. server_args.signal = &ready;
  32734. server_args.callbacks = &server_cb;
  32735. /* we are responsible for free'ing WOLFSSL_CTX */
  32736. server_cb.ctx = ctx;
  32737. server_cb.isSharedCtx = 1;
  32738. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  32739. cliCertFile, 0));
  32740. start_thread(test_server_nofail, &server_args, &serverThread);
  32741. wait_tcp_ready(&server_args);
  32742. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32743. ExpectNotNull(ctx_client =
  32744. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  32745. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  32746. ctx_client, caCertFile, 0));
  32747. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  32748. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  32749. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  32750. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  32751. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  32752. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  32753. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  32754. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  32755. /* We are expecting two cert names to be sent */
  32756. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  32757. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  32758. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  32759. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  32760. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  32761. }
  32762. wolfSSL_shutdown(ssl_client);
  32763. wolfSSL_free(ssl_client);
  32764. wolfSSL_CTX_free(ctx_client);
  32765. CloseSocket(sockfd);
  32766. join_thread(serverThread);
  32767. FreeTcpReady(&ready);
  32768. }
  32769. #endif
  32770. wolfSSL_free(ssl);
  32771. wolfSSL_CTX_free(ctx);
  32772. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  32773. * !NO_BIO */
  32774. return EXPECT_RESULT();
  32775. }
  32776. static int test_wolfSSL_CTX_add_client_CA(void)
  32777. {
  32778. EXPECT_DECLS;
  32779. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32780. !defined(NO_WOLFSSL_CLIENT)
  32781. WOLFSSL_CTX* ctx = NULL;
  32782. WOLFSSL_X509* x509 = NULL;
  32783. WOLFSSL_X509* x509_a = NULL;
  32784. STACK_OF(X509_NAME)* ca_list = NULL;
  32785. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32786. /* Add client cert */
  32787. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  32788. SSL_FILETYPE_PEM));
  32789. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  32790. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  32791. /* Add another client cert */
  32792. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  32793. SSL_FILETYPE_PEM));
  32794. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  32795. /* test for incorrect parameter */
  32796. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  32797. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  32798. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  32799. X509_free(x509);
  32800. X509_free(x509_a);
  32801. SSL_CTX_free(ctx);
  32802. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  32803. return EXPECT_RESULT();
  32804. }
  32805. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  32806. defined(HAVE_IO_TESTS_DEPENDENCIES)
  32807. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  32808. {
  32809. callback_functions* callbacks = ((func_args*)args)->callbacks;
  32810. WOLFSSL_CTX* ctx = callbacks->ctx;
  32811. WOLFSSL* ssl = NULL;
  32812. SOCKET_T sfd = 0;
  32813. SOCKET_T cfd = 0;
  32814. word16 port;
  32815. char input[1024];
  32816. int idx;
  32817. int ret, err = 0;
  32818. const char* privateName = "ech-private-name.com";
  32819. int privateNameLen = (int)XSTRLEN(privateName);
  32820. ((func_args*)args)->return_code = TEST_FAIL;
  32821. port = ((func_args*)args)->signal->port;
  32822. AssertIntEQ(WOLFSSL_SUCCESS,
  32823. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  32824. AssertIntEQ(WOLFSSL_SUCCESS,
  32825. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  32826. WOLFSSL_FILETYPE_PEM));
  32827. AssertIntEQ(WOLFSSL_SUCCESS,
  32828. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32829. WOLFSSL_FILETYPE_PEM));
  32830. if (callbacks->ctx_ready)
  32831. callbacks->ctx_ready(ctx);
  32832. ssl = wolfSSL_new(ctx);
  32833. /* set the sni for the server */
  32834. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  32835. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  32836. CloseSocket(sfd);
  32837. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  32838. if (callbacks->ssl_ready)
  32839. callbacks->ssl_ready(ssl);
  32840. do {
  32841. err = 0; /* Reset error */
  32842. ret = wolfSSL_accept(ssl);
  32843. if (ret != WOLFSSL_SUCCESS) {
  32844. err = wolfSSL_get_error(ssl, 0);
  32845. }
  32846. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  32847. if (ret != WOLFSSL_SUCCESS) {
  32848. char buff[WOLFSSL_MAX_ERROR_SZ];
  32849. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  32850. }
  32851. else {
  32852. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  32853. input[idx] = 0;
  32854. fprintf(stderr, "Client message: %s\n", input);
  32855. }
  32856. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  32857. privateNameLen));
  32858. ((func_args*)args)->return_code = TEST_SUCCESS;
  32859. }
  32860. if (callbacks->on_result)
  32861. callbacks->on_result(ssl);
  32862. wolfSSL_shutdown(ssl);
  32863. wolfSSL_free(ssl);
  32864. wolfSSL_CTX_free(ctx);
  32865. CloseSocket(cfd);
  32866. #ifdef FP_ECC
  32867. wc_ecc_fp_free();
  32868. #endif
  32869. WOLFSSL_RETURN_FROM_THREAD(0);
  32870. }
  32871. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32872. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32873. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  32874. {
  32875. AssertNotNull(ssl);
  32876. AssertNotNull(line);
  32877. XFILE fp;
  32878. const byte lf = '\n';
  32879. fp = XFOPEN("./MyKeyLog.txt", "a");
  32880. XFWRITE( line, 1, strlen(line),fp);
  32881. XFWRITE( (void*)&lf,1,1,fp);
  32882. XFFLUSH(fp);
  32883. XFCLOSE(fp);
  32884. }
  32885. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  32886. static int test_wolfSSL_CTX_set_keylog_callback(void)
  32887. {
  32888. EXPECT_DECLS;
  32889. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  32890. !defined(NO_WOLFSSL_CLIENT)
  32891. SSL_CTX* ctx = NULL;
  32892. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32893. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  32894. SSL_CTX_free(ctx);
  32895. SSL_CTX_set_keylog_callback(NULL, NULL);
  32896. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  32897. return EXPECT_RESULT();
  32898. }
  32899. static int test_wolfSSL_CTX_get_keylog_callback(void)
  32900. {
  32901. EXPECT_DECLS;
  32902. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  32903. !defined(NO_WOLFSSL_CLIENT)
  32904. SSL_CTX* ctx = NULL;
  32905. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32906. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  32907. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  32908. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  32909. SSL_CTX_set_keylog_callback(ctx, NULL );
  32910. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  32911. SSL_CTX_free(ctx);
  32912. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  32913. return EXPECT_RESULT();
  32914. }
  32915. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32916. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  32917. {
  32918. /* set keylog callback */
  32919. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  32920. return TEST_SUCCESS;
  32921. }
  32922. #endif
  32923. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  32924. {
  32925. EXPECT_DECLS;
  32926. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32927. /* This test is intended for checking whether keylog callback is called
  32928. * in client during TLS handshake between the client and a server.
  32929. */
  32930. test_ssl_cbf server_cbf;
  32931. test_ssl_cbf client_cbf;
  32932. XFILE fp = XBADFILE;
  32933. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  32934. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  32935. server_cbf.method = wolfTLSv1_2_server_method;
  32936. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  32937. /* clean up keylog file */
  32938. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  32939. if (fp != XBADFILE) {
  32940. XFFLUSH(fp);
  32941. XFCLOSE(fp);
  32942. fp = XBADFILE;
  32943. }
  32944. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32945. &server_cbf, NULL), TEST_SUCCESS);
  32946. XSLEEP_MS(100);
  32947. /* check if the keylog file exists */
  32948. char buff[300] = {0};
  32949. int found = 0;
  32950. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  32951. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  32952. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  32953. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  32954. found = 1;
  32955. break;
  32956. }
  32957. }
  32958. if (fp != XBADFILE) {
  32959. XFCLOSE(fp);
  32960. }
  32961. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  32962. ExpectIntEQ(found, 1);
  32963. /* clean up */
  32964. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  32965. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  32966. return EXPECT_RESULT();
  32967. }
  32968. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  32969. defined(HAVE_SECRET_CALLBACK)
  32970. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  32971. {
  32972. /* set keylog callback */
  32973. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  32974. return TEST_SUCCESS;
  32975. }
  32976. #endif
  32977. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  32978. {
  32979. EXPECT_DECLS;
  32980. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  32981. defined(HAVE_SECRET_CALLBACK)
  32982. /* This test is intended for checking whether keylog callback is called
  32983. * in client during TLS handshake between the client and a server.
  32984. */
  32985. test_ssl_cbf server_cbf;
  32986. test_ssl_cbf client_cbf;
  32987. XFILE fp = XBADFILE;
  32988. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  32989. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  32990. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  32991. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  32992. /* clean up keylog file */
  32993. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  32994. if (fp != XBADFILE) {
  32995. XFCLOSE(fp);
  32996. fp = XBADFILE;
  32997. }
  32998. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32999. &server_cbf, NULL), TEST_SUCCESS);
  33000. /* check if the keylog file exists */
  33001. {
  33002. char buff[300] = {0};
  33003. int found[4] = {0};
  33004. int numfnd = 0;
  33005. int i;
  33006. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33007. while (EXPECT_SUCCESS() &&
  33008. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33009. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33010. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33011. found[0] = 1;
  33012. continue;
  33013. }
  33014. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33015. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33016. found[1] = 1;
  33017. continue;
  33018. }
  33019. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33020. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33021. found[2] = 1;
  33022. continue;
  33023. }
  33024. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33025. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33026. found[3] = 1;
  33027. continue;
  33028. }
  33029. }
  33030. if (fp != XBADFILE)
  33031. XFCLOSE(fp);
  33032. for (i = 0; i < 4; i++) {
  33033. if (found[i] != 0)
  33034. numfnd++;
  33035. }
  33036. ExpectIntEQ(numfnd, 4);
  33037. }
  33038. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33039. return EXPECT_RESULT();
  33040. }
  33041. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33042. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33043. static int test_wolfSSL_Tls13_ECH_params(void)
  33044. {
  33045. EXPECT_DECLS;
  33046. #if !defined(NO_WOLFSSL_CLIENT)
  33047. word32 outputLen = 0;
  33048. byte testBuf[72];
  33049. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33050. WOLFSSL *ssl = wolfSSL_new(ctx);
  33051. ExpectNotNull(ctx);
  33052. ExpectNotNull(ssl);
  33053. /* invalid ctx */
  33054. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33055. "ech-public-name.com", 0, 0, 0));
  33056. /* invalid public name */
  33057. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33058. 0, 0));
  33059. /* invalid algorithms */
  33060. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33061. "ech-public-name.com", 1000, 1000, 1000));
  33062. /* invalid ctx */
  33063. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33064. &outputLen));
  33065. /* invalid output len */
  33066. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33067. /* invalid ssl */
  33068. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33069. (char*)testBuf, sizeof(testBuf)));
  33070. /* invalid configs64 */
  33071. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33072. sizeof(testBuf)));
  33073. /* invalid size */
  33074. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33075. (char*)testBuf, 0));
  33076. /* invalid ssl */
  33077. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33078. sizeof(testBuf)));
  33079. /* invalid configs */
  33080. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33081. sizeof(testBuf)));
  33082. /* invalid size */
  33083. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33084. /* invalid ssl */
  33085. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33086. /* invalid size */
  33087. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33088. wolfSSL_free(ssl);
  33089. wolfSSL_CTX_free(ctx);
  33090. #endif /* !NO_WOLFSSL_CLIENT */
  33091. return EXPECT_RESULT();
  33092. }
  33093. static int test_wolfSSL_Tls13_ECH(void)
  33094. {
  33095. EXPECT_DECLS;
  33096. tcp_ready ready;
  33097. func_args client_args;
  33098. func_args server_args;
  33099. THREAD_TYPE serverThread;
  33100. callback_functions server_cbf;
  33101. callback_functions client_cbf;
  33102. SOCKET_T sockfd = 0;
  33103. WOLFSSL_CTX* ctx = NULL;
  33104. WOLFSSL* ssl = NULL;
  33105. const char* publicName = "ech-public-name.com";
  33106. const char* privateName = "ech-private-name.com";
  33107. int privateNameLen = 20;
  33108. char reply[1024];
  33109. int replyLen = 0;
  33110. byte rawEchConfig[128];
  33111. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33112. InitTcpReady(&ready);
  33113. ready.port = 22222;
  33114. XMEMSET(&client_args, 0, sizeof(func_args));
  33115. XMEMSET(&server_args, 0, sizeof(func_args));
  33116. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33117. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33118. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33119. /* create the server context here so we can get the ech config */
  33120. ExpectNotNull(server_cbf.ctx =
  33121. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33122. /* generate ech config */
  33123. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33124. publicName, 0, 0, 0));
  33125. /* get the config for the client to use */
  33126. ExpectIntEQ(WOLFSSL_SUCCESS,
  33127. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33128. &rawEchConfigLen));
  33129. server_args.callbacks = &server_cbf;
  33130. server_args.signal = &ready;
  33131. /* start server task */
  33132. start_thread(server_task_ech, &server_args, &serverThread);
  33133. wait_tcp_ready(&server_args);
  33134. /* run as a TLS1.3 client */
  33135. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33136. ExpectIntEQ(WOLFSSL_SUCCESS,
  33137. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33138. ExpectIntEQ(WOLFSSL_SUCCESS,
  33139. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33140. ExpectIntEQ(WOLFSSL_SUCCESS,
  33141. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33142. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33143. /* get connected the server task */
  33144. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33145. /* set the ech configs for the client */
  33146. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33147. rawEchConfigLen));
  33148. /* set the sni for the client */
  33149. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33150. privateName, privateNameLen));
  33151. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33152. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33153. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33154. privateNameLen);
  33155. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33156. /* add th null terminator for string compare */
  33157. reply[replyLen] = 0;
  33158. /* check that the server replied with the private name */
  33159. ExpectStrEQ(privateName, reply);
  33160. wolfSSL_free(ssl);
  33161. wolfSSL_CTX_free(ctx);
  33162. CloseSocket(sockfd);
  33163. join_thread(serverThread);
  33164. FreeTcpReady(&ready);
  33165. return EXPECT_RESULT();
  33166. }
  33167. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33168. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33169. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33170. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33171. static int post_auth_version_cb(WOLFSSL* ssl)
  33172. {
  33173. EXPECT_DECLS;
  33174. /* do handshake and then test version error */
  33175. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33176. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33177. return EXPECT_RESULT();
  33178. }
  33179. static int post_auth_version_client_cb(WOLFSSL* ssl)
  33180. {
  33181. EXPECT_DECLS;
  33182. /* do handshake and then test version error */
  33183. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33184. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33185. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33186. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33187. /* check was added to error queue */
  33188. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33189. /* check the string matches expected string */
  33190. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33191. "WRONG_SSL_VERSION");
  33192. #endif
  33193. return EXPECT_RESULT();
  33194. }
  33195. static int post_auth_cb(WOLFSSL* ssl)
  33196. {
  33197. EXPECT_DECLS;
  33198. WOLFSSL_X509* x509 = NULL;
  33199. /* do handshake and then test version error */
  33200. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33201. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33202. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33203. wolfSSL_X509_free(x509);
  33204. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33205. return EXPECT_RESULT();
  33206. }
  33207. static int set_post_auth_cb(WOLFSSL* ssl)
  33208. {
  33209. if (!wolfSSL_is_server(ssl)) {
  33210. EXPECT_DECLS;
  33211. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33212. return EXPECT_RESULT();
  33213. }
  33214. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33215. return TEST_SUCCESS;
  33216. }
  33217. #endif
  33218. static int test_wolfSSL_Tls13_postauth(void)
  33219. {
  33220. EXPECT_DECLS;
  33221. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33222. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33223. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33224. test_ssl_cbf server_cbf;
  33225. test_ssl_cbf client_cbf;
  33226. /* test version failure doing post auth with TLS 1.2 connection */
  33227. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33228. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33229. server_cbf.method = wolfTLSv1_2_server_method;
  33230. server_cbf.ssl_ready = set_post_auth_cb;
  33231. server_cbf.on_result = post_auth_version_cb;
  33232. client_cbf.ssl_ready = set_post_auth_cb;
  33233. client_cbf.on_result = post_auth_version_client_cb;
  33234. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33235. &server_cbf, NULL), TEST_SUCCESS);
  33236. /* tests on post auth with TLS 1.3 */
  33237. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33238. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33239. server_cbf.method = wolfTLSv1_3_server_method;
  33240. server_cbf.ssl_ready = set_post_auth_cb;
  33241. client_cbf.ssl_ready = set_post_auth_cb;
  33242. server_cbf.on_result = post_auth_cb;
  33243. client_cbf.on_result = NULL;
  33244. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33245. &server_cbf, NULL), TEST_SUCCESS);
  33246. #endif
  33247. return EXPECT_RESULT();
  33248. }
  33249. static int test_wolfSSL_X509_NID(void)
  33250. {
  33251. EXPECT_DECLS;
  33252. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  33253. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  33254. int sigType;
  33255. int nameSz;
  33256. X509* cert = NULL;
  33257. EVP_PKEY* pubKeyTmp = NULL;
  33258. X509_NAME* name = NULL;
  33259. char commonName[80];
  33260. char countryName[80];
  33261. char localityName[80];
  33262. char stateName[80];
  33263. char orgName[80];
  33264. char orgUnit[80];
  33265. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  33266. /* convert cert from DER to internal WOLFSSL_X509 struct */
  33267. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  33268. sizeof_client_cert_der_2048, HEAP_HINT));
  33269. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  33270. /* extract PUBLIC KEY from cert */
  33271. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  33272. /* extract signatureType */
  33273. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  33274. /* extract subjectName info */
  33275. ExpectNotNull(name = X509_get_subject_name(cert));
  33276. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  33277. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33278. NULL, 0)), 0);
  33279. ExpectIntEQ(nameSz, 15);
  33280. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33281. commonName, sizeof(commonName))), 0);
  33282. ExpectIntEQ(nameSz, 15);
  33283. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  33284. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33285. commonName, 9)), 0);
  33286. ExpectIntEQ(nameSz, 8);
  33287. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  33288. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  33289. countryName, sizeof(countryName))), 0);
  33290. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  33291. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  33292. localityName, sizeof(localityName))), 0);
  33293. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  33294. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33295. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  33296. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  33297. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  33298. orgName, sizeof(orgName))), 0);
  33299. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  33300. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33301. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  33302. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  33303. EVP_PKEY_free(pubKeyTmp);
  33304. X509_free(cert);
  33305. #endif
  33306. return EXPECT_RESULT();
  33307. }
  33308. static int test_wolfSSL_CTX_set_srp_username(void)
  33309. {
  33310. EXPECT_DECLS;
  33311. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  33312. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33313. WOLFSSL_CTX* ctx = NULL;
  33314. WOLFSSL* ssl = NULL;
  33315. const char *username = "TESTUSER";
  33316. const char *password = "TESTPASSWORD";
  33317. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33318. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33319. SSL_SUCCESS);
  33320. wolfSSL_CTX_free(ctx);
  33321. ctx = NULL;
  33322. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33323. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33324. SSL_SUCCESS);
  33325. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33326. SSL_SUCCESS);
  33327. ExpectNotNull(ssl = SSL_new(ctx));
  33328. ExpectNotNull(SSL_get_srp_username(ssl));
  33329. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  33330. wolfSSL_free(ssl);
  33331. wolfSSL_CTX_free(ctx);
  33332. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  33333. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  33334. return EXPECT_RESULT();
  33335. }
  33336. static int test_wolfSSL_CTX_set_srp_password(void)
  33337. {
  33338. EXPECT_DECLS;
  33339. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  33340. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33341. WOLFSSL_CTX* ctx = NULL;
  33342. const char *username = "TESTUSER";
  33343. const char *password = "TESTPASSWORD";
  33344. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33345. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33346. SSL_SUCCESS);
  33347. wolfSSL_CTX_free(ctx);
  33348. ctx = NULL;
  33349. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33350. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33351. SSL_SUCCESS);
  33352. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33353. SSL_SUCCESS);
  33354. wolfSSL_CTX_free(ctx);
  33355. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  33356. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  33357. return EXPECT_RESULT();
  33358. }
  33359. static int test_wolfSSL_X509_STORE(void)
  33360. {
  33361. EXPECT_DECLS;
  33362. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33363. X509_STORE *store = NULL;
  33364. #ifdef HAVE_CRL
  33365. X509_STORE_CTX *storeCtx = NULL;
  33366. X509_CRL *crl = NULL;
  33367. X509 *ca = NULL;
  33368. X509 *cert = NULL;
  33369. const char crlPem[] = "./certs/crl/crl.revoked";
  33370. const char srvCert[] = "./certs/server-revoked-cert.pem";
  33371. const char caCert[] = "./certs/ca-cert.pem";
  33372. XFILE fp = XBADFILE;
  33373. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33374. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  33375. SSL_FILETYPE_PEM)));
  33376. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  33377. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  33378. SSL_FILETYPE_PEM)));
  33379. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  33380. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  33381. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  33382. X509_STORE_free(store);
  33383. store = NULL;
  33384. X509_STORE_CTX_free(storeCtx);
  33385. storeCtx = NULL;
  33386. X509_free(cert);
  33387. cert = NULL;
  33388. X509_free(ca);
  33389. ca = NULL;
  33390. /* should fail to verify now after adding in CRL */
  33391. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33392. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  33393. SSL_FILETYPE_PEM)));
  33394. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  33395. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  33396. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  33397. NULL, NULL));
  33398. if (fp != XBADFILE)
  33399. XFCLOSE(fp);
  33400. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  33401. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  33402. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  33403. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  33404. SSL_FILETYPE_PEM)));
  33405. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  33406. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  33407. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  33408. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  33409. X509_CRL_free(crl);
  33410. crl = NULL;
  33411. X509_STORE_free(store);
  33412. store = NULL;
  33413. X509_STORE_CTX_free(storeCtx);
  33414. storeCtx = NULL;
  33415. X509_free(cert);
  33416. cert = NULL;
  33417. X509_free(ca);
  33418. ca = NULL;
  33419. #endif /* HAVE_CRL */
  33420. #ifndef WOLFCRYPT_ONLY
  33421. {
  33422. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33423. SSL_CTX* ctx = NULL;
  33424. SSL* ssl = NULL;
  33425. int i;
  33426. for (i = 0; i < 2; i++) {
  33427. #ifndef NO_WOLFSSL_SERVER
  33428. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33429. #else
  33430. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33431. #endif
  33432. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33433. SSL_CTX_set_cert_store(ctx, store);
  33434. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33435. SSL_CTX_set_cert_store(ctx, store);
  33436. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33437. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33438. SSL_FILETYPE_PEM), SSL_SUCCESS);
  33439. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33440. SSL_FILETYPE_PEM), SSL_SUCCESS);
  33441. ExpectNotNull(ssl = SSL_new(ctx));
  33442. if (i == 0) {
  33443. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  33444. SSL_SUCCESS);
  33445. }
  33446. else {
  33447. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  33448. #ifdef OPENSSL_ALL
  33449. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  33450. #endif
  33451. }
  33452. if (EXPECT_FAIL() || (i == 1)) {
  33453. X509_STORE_free(store);
  33454. store = NULL;
  33455. }
  33456. SSL_free(ssl);
  33457. ssl = NULL;
  33458. SSL_CTX_free(ctx);
  33459. ctx = NULL;
  33460. }
  33461. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33462. }
  33463. #endif
  33464. #endif
  33465. return EXPECT_RESULT();
  33466. }
  33467. static int test_wolfSSL_X509_STORE_load_locations(void)
  33468. {
  33469. EXPECT_DECLS;
  33470. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33471. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  33472. SSL_CTX *ctx = NULL;
  33473. X509_STORE *store = NULL;
  33474. const char ca_file[] = "./certs/ca-cert.pem";
  33475. const char client_pem_file[] = "./certs/client-cert.pem";
  33476. const char client_der_file[] = "./certs/client-cert.der";
  33477. const char ecc_file[] = "./certs/ecc-key.pem";
  33478. const char certs_path[] = "./certs/";
  33479. const char bad_path[] = "./bad-path/";
  33480. #ifdef HAVE_CRL
  33481. const char crl_path[] = "./certs/crl/";
  33482. const char crl_file[] = "./certs/crl/crl.pem";
  33483. #endif
  33484. #ifndef NO_WOLFSSL_SERVER
  33485. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  33486. #else
  33487. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  33488. #endif
  33489. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  33490. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  33491. WOLFSSL_SUCCESS);
  33492. /* Test bad arguments */
  33493. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  33494. WOLFSSL_FAILURE);
  33495. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  33496. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  33497. WOLFSSL_FAILURE);
  33498. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  33499. WOLFSSL_FAILURE);
  33500. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  33501. WOLFSSL_FAILURE);
  33502. #ifdef HAVE_CRL
  33503. /* Test with CRL */
  33504. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  33505. WOLFSSL_SUCCESS);
  33506. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  33507. WOLFSSL_SUCCESS);
  33508. #endif
  33509. /* Test with CA */
  33510. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  33511. WOLFSSL_SUCCESS);
  33512. /* Test with client_cert and certs path */
  33513. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  33514. WOLFSSL_SUCCESS);
  33515. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  33516. WOLFSSL_SUCCESS);
  33517. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  33518. /* Clear nodes */
  33519. ERR_clear_error();
  33520. #endif
  33521. SSL_CTX_free(ctx);
  33522. #endif
  33523. return EXPECT_RESULT();
  33524. }
  33525. static int test_X509_STORE_get0_objects(void)
  33526. {
  33527. EXPECT_DECLS;
  33528. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  33529. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  33530. X509_STORE *store = NULL;
  33531. X509_STORE *store_cpy = NULL;
  33532. SSL_CTX *ctx = NULL;
  33533. X509_OBJECT *obj = NULL;
  33534. STACK_OF(X509_OBJECT) *objs = NULL;
  33535. int i;
  33536. /* Setup store */
  33537. #ifndef NO_WOLFSSL_SERVER
  33538. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  33539. #else
  33540. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  33541. #endif
  33542. ExpectNotNull(store_cpy = X509_STORE_new());
  33543. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  33544. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  33545. WOLFSSL_SUCCESS);
  33546. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  33547. WOLFSSL_SUCCESS);
  33548. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  33549. WOLFSSL_SUCCESS);
  33550. #ifdef HAVE_CRL
  33551. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  33552. WOLFSSL_SUCCESS);
  33553. #endif
  33554. /* Store ready */
  33555. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  33556. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  33557. #ifdef HAVE_CRL
  33558. #ifdef WOLFSSL_SIGNER_DER_CERT
  33559. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  33560. #else
  33561. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  33562. #endif
  33563. #else
  33564. #ifdef WOLFSSL_SIGNER_DER_CERT
  33565. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  33566. #else
  33567. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  33568. #endif
  33569. #endif
  33570. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  33571. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  33572. switch (X509_OBJECT_get_type(obj)) {
  33573. case X509_LU_X509:
  33574. {
  33575. WOLFSSL_X509* x509;
  33576. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  33577. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  33578. break;
  33579. }
  33580. case X509_LU_CRL:
  33581. #ifdef HAVE_CRL
  33582. {
  33583. WOLFSSL_CRL* crl = NULL;
  33584. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  33585. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  33586. break;
  33587. }
  33588. #endif
  33589. case X509_LU_NONE:
  33590. default:
  33591. Fail(("X509_OBJECT_get_type should return x509 or crl "
  33592. "(when built with crl support)"),
  33593. ("Unrecognized X509_OBJECT type or none"));
  33594. }
  33595. }
  33596. X509_STORE_free(store_cpy);
  33597. SSL_CTX_free(ctx);
  33598. #endif
  33599. return EXPECT_RESULT();
  33600. }
  33601. static int test_wolfSSL_BN_CTX(void)
  33602. {
  33603. EXPECT_DECLS;
  33604. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33605. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33606. WOLFSSL_BN_CTX* bn_ctx = NULL;
  33607. WOLFSSL_BIGNUM* t = NULL;
  33608. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  33609. /* No implementation. */
  33610. BN_CTX_init(NULL);
  33611. ExpectNotNull(t = BN_CTX_get(NULL));
  33612. BN_free(t);
  33613. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  33614. BN_free(t);
  33615. #ifndef NO_WOLFSSL_STUB
  33616. /* No implementation. */
  33617. BN_CTX_start(NULL);
  33618. BN_CTX_start(bn_ctx);
  33619. #endif
  33620. BN_CTX_free(NULL);
  33621. BN_CTX_free(bn_ctx);
  33622. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33623. return EXPECT_RESULT();
  33624. }
  33625. static int test_wolfSSL_BN(void)
  33626. {
  33627. EXPECT_DECLS;
  33628. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33629. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33630. BIGNUM* a = NULL;
  33631. BIGNUM* b = NULL;
  33632. BIGNUM* c = NULL;
  33633. BIGNUM* d = NULL;
  33634. BIGNUM emptyBN;
  33635. /* Setup */
  33636. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33637. /* internal not set emptyBN. */
  33638. ExpectNotNull(a = BN_new());
  33639. ExpectNotNull(b = BN_new());
  33640. ExpectNotNull(c = BN_dup(b));
  33641. ExpectNotNull(d = BN_new());
  33642. /* Invalid parameter testing. */
  33643. BN_free(NULL);
  33644. ExpectNull(BN_dup(NULL));
  33645. ExpectNull(BN_dup(&emptyBN));
  33646. ExpectNull(BN_copy(NULL, NULL));
  33647. ExpectNull(BN_copy(b, NULL));
  33648. ExpectNull(BN_copy(NULL, c));
  33649. ExpectNull(BN_copy(b, &emptyBN));
  33650. ExpectNull(BN_copy(&emptyBN, c));
  33651. BN_clear(NULL);
  33652. BN_clear(&emptyBN);
  33653. ExpectIntEQ(BN_num_bytes(NULL), 0);
  33654. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  33655. ExpectIntEQ(BN_num_bits(NULL), 0);
  33656. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  33657. ExpectIntEQ(BN_is_negative(NULL), 0);
  33658. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  33659. /* END Invalid Parameters */
  33660. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  33661. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  33662. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  33663. ExpectIntEQ(BN_num_bits(a), 2);
  33664. ExpectIntEQ(BN_num_bytes(a), 1);
  33665. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  33666. defined(WOLFSSL_SP_INT_NEGATIVE))
  33667. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  33668. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  33669. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  33670. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  33671. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  33672. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33673. {
  33674. /* Do additional tests on negative BN conversions. */
  33675. char* ret = NULL;
  33676. ASN1_INTEGER* asn1 = NULL;
  33677. BIGNUM* tmp = NULL;
  33678. /* Sanity check we have a negative BN. */
  33679. ExpectIntEQ(BN_is_negative(c), 1);
  33680. ExpectNotNull(ret = BN_bn2dec(c));
  33681. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  33682. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  33683. ret = NULL;
  33684. /* Convert to ASN1_INTEGER and back to BN. */
  33685. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  33686. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  33687. /* After converting back BN should be negative and correct. */
  33688. ExpectIntEQ(BN_is_negative(tmp), 1);
  33689. ExpectNotNull(ret = BN_bn2dec(tmp));
  33690. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  33691. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  33692. ASN1_INTEGER_free(asn1);
  33693. BN_free(tmp);
  33694. }
  33695. #endif
  33696. ExpectIntEQ(BN_get_word(c), 4);
  33697. #endif
  33698. ExpectIntEQ(BN_set_word(a, 3), 1);
  33699. ExpectIntEQ(BN_set_word(b, 3), 1);
  33700. ExpectIntEQ(BN_set_word(c, 4), 1);
  33701. /* NULL == NULL, NULL < num, num > NULL */
  33702. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  33703. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  33704. ExpectIntLT(BN_cmp(NULL, b), 0);
  33705. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  33706. ExpectIntGT(BN_cmp(a, NULL), 0);
  33707. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  33708. ExpectIntEQ(BN_cmp(a, b), 0);
  33709. ExpectIntLT(BN_cmp(a, c), 0);
  33710. ExpectIntGT(BN_cmp(c, b), 0);
  33711. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  33712. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  33713. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  33714. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  33715. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  33716. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  33717. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  33718. #endif
  33719. BN_clear(a);
  33720. BN_free(a);
  33721. BN_free(b);
  33722. BN_free(c);
  33723. BN_clear_free(d);
  33724. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33725. return EXPECT_RESULT();
  33726. }
  33727. static int test_wolfSSL_BN_init(void)
  33728. {
  33729. EXPECT_DECLS;
  33730. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33731. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33732. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  33733. BIGNUM* ap = NULL;
  33734. BIGNUM bv;
  33735. BIGNUM cv;
  33736. BIGNUM dv;
  33737. ExpectNotNull(ap = BN_new());
  33738. BN_init(NULL);
  33739. XMEMSET(&bv, 0, sizeof(bv));
  33740. ExpectNull(BN_dup(&bv));
  33741. BN_init(&bv);
  33742. BN_init(&cv);
  33743. BN_init(&dv);
  33744. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  33745. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  33746. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  33747. /* a^b mod c = */
  33748. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  33749. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  33750. /* check result 3^2 mod 5 */
  33751. ExpectIntEQ(BN_get_word(&dv), 4);
  33752. /* a*b mod c = */
  33753. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  33754. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  33755. /* check result 3*2 mod 5 */
  33756. ExpectIntEQ(BN_get_word(&dv), 1);
  33757. BN_free(ap);
  33758. #endif
  33759. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33760. return EXPECT_RESULT();
  33761. }
  33762. static int test_wolfSSL_BN_enc_dec(void)
  33763. {
  33764. EXPECT_DECLS;
  33765. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  33766. BIGNUM* a = NULL;
  33767. BIGNUM* b = NULL;
  33768. BIGNUM* c = NULL;
  33769. BIGNUM emptyBN;
  33770. char* str = NULL;
  33771. const char* emptyStr = "";
  33772. const char* numberStr = "12345";
  33773. const char* badStr = "g12345";
  33774. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33775. const char* twoStr = "2";
  33776. #endif
  33777. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  33778. unsigned char outNum[5];
  33779. /* Setup */
  33780. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33781. ExpectNotNull(a = BN_new());
  33782. ExpectNotNull(b = BN_new());
  33783. ExpectIntEQ(BN_set_word(a, 2), 1);
  33784. /* Invalid parameters */
  33785. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  33786. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  33787. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  33788. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  33789. ExpectNull(BN_bn2hex(NULL));
  33790. ExpectNull(BN_bn2hex(&emptyBN));
  33791. ExpectNull(BN_bn2dec(NULL));
  33792. ExpectNull(BN_bn2dec(&emptyBN));
  33793. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  33794. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  33795. ExpectNull(BN_bin2bn(binNum, -1, a));
  33796. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  33797. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  33798. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  33799. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  33800. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  33801. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  33802. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  33803. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  33804. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  33805. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  33806. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  33807. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  33808. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  33809. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  33810. ExpectIntEQ(BN_set_word(a, 2), 1);
  33811. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  33812. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  33813. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  33814. ExpectIntEQ(BN_cmp(a, b), 0);
  33815. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  33816. ExpectIntEQ(BN_cmp(a, b), -1);
  33817. ExpectNotNull(str = BN_bn2hex(a));
  33818. ExpectNotNull(BN_hex2bn(&b, str));
  33819. ExpectIntEQ(BN_cmp(a, b), 0);
  33820. ExpectNotNull(BN_hex2bn(&b, numberStr));
  33821. ExpectIntEQ(BN_cmp(a, b), -1);
  33822. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33823. str = NULL;
  33824. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33825. ExpectNotNull(str = BN_bn2dec(a));
  33826. ExpectStrEQ(str, twoStr);
  33827. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33828. str = NULL;
  33829. #ifndef NO_RSA
  33830. ExpectNotNull(str = BN_bn2dec(a));
  33831. ExpectNotNull(BN_dec2bn(&b, str));
  33832. ExpectIntEQ(BN_cmp(a, b), 0);
  33833. ExpectNotNull(BN_dec2bn(&b, numberStr));
  33834. ExpectIntEQ(BN_cmp(a, b), -1);
  33835. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33836. str = NULL;
  33837. #else
  33838. /* No implementation - fail with good parameters. */
  33839. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  33840. #endif
  33841. #endif
  33842. BN_free(b);
  33843. BN_free(a);
  33844. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33845. return EXPECT_RESULT();
  33846. }
  33847. static int test_wolfSSL_BN_word(void)
  33848. {
  33849. EXPECT_DECLS;
  33850. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  33851. BIGNUM* a = NULL;
  33852. BIGNUM* b = NULL;
  33853. BIGNUM* c = NULL;
  33854. BIGNUM av;
  33855. ExpectNotNull(a = BN_new());
  33856. ExpectNotNull(b = BN_new());
  33857. ExpectNotNull(c = BN_new());
  33858. XMEMSET(&av, 0, sizeof(av));
  33859. /* Invalid parameter. */
  33860. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  33861. ExpectIntEQ(BN_add_word(&av, 3), 0);
  33862. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  33863. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  33864. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  33865. ExpectIntEQ(BN_set_word(&av, 3), 0);
  33866. ExpectIntEQ(BN_get_word(NULL), 0);
  33867. ExpectIntEQ(BN_get_word(&av), 0);
  33868. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  33869. ExpectIntEQ(BN_is_word(&av, 3), 0);
  33870. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  33871. !defined(NO_DSA))
  33872. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  33873. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  33874. #endif
  33875. ExpectIntEQ(BN_one(NULL), 0);
  33876. ExpectIntEQ(BN_one(&av), 0);
  33877. BN_zero(NULL);
  33878. BN_zero(&av);
  33879. ExpectIntEQ(BN_is_one(NULL), 0);
  33880. ExpectIntEQ(BN_is_one(&av), 0);
  33881. ExpectIntEQ(BN_is_zero(NULL), 0);
  33882. ExpectIntEQ(BN_is_zero(&av), 0);
  33883. ExpectIntEQ(BN_set_word(a, 3), 1);
  33884. ExpectIntEQ(BN_set_word(b, 2), 1);
  33885. ExpectIntEQ(BN_set_word(c, 5), 1);
  33886. /* a + 3 = */
  33887. ExpectIntEQ(BN_add_word(a, 3), 1);
  33888. /* check result 3 + 3*/
  33889. ExpectIntEQ(BN_get_word(a), 6);
  33890. ExpectIntEQ(BN_is_word(a, 6), 1);
  33891. ExpectIntEQ(BN_is_word(a, 5), 0);
  33892. /* set a back to 3 */
  33893. ExpectIntEQ(BN_set_word(a, 3), 1);
  33894. /* a - 3 = */
  33895. ExpectIntEQ(BN_sub_word(a, 3), 1);
  33896. /* check result 3 - 3*/
  33897. ExpectIntEQ(BN_get_word(a), 0);
  33898. ExpectIntEQ(BN_one(a), 1);
  33899. ExpectIntEQ(BN_is_word(a, 1), 1);
  33900. ExpectIntEQ(BN_is_word(a, 0), 0);
  33901. ExpectIntEQ(BN_is_one(a), 1);
  33902. ExpectIntEQ(BN_is_zero(a), 0);
  33903. BN_zero(a);
  33904. ExpectIntEQ(BN_is_word(a, 0), 1);
  33905. ExpectIntEQ(BN_is_word(a, 1), 0);
  33906. ExpectIntEQ(BN_is_zero(a), 1);
  33907. ExpectIntEQ(BN_is_one(a), 0);
  33908. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  33909. !defined(NO_DSA))
  33910. ExpectIntEQ(BN_set_word(a, 5), 1);
  33911. ExpectIntEQ(BN_mod_word(a, 3), 2);
  33912. ExpectIntEQ(BN_mod_word(a, 0), -1);
  33913. #endif
  33914. BN_free(c);
  33915. BN_free(b);
  33916. BN_free(a);
  33917. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33918. return EXPECT_RESULT();
  33919. }
  33920. static int test_wolfSSL_BN_bits(void)
  33921. {
  33922. EXPECT_DECLS;
  33923. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33924. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33925. BIGNUM* a = NULL;
  33926. BIGNUM emptyBN;
  33927. /* Setup */
  33928. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33929. ExpectNotNull(a = BN_new());
  33930. /* Invalid parameters. */
  33931. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  33932. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  33933. ExpectIntEQ(BN_set_bit(a, -1), 0);
  33934. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  33935. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  33936. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  33937. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  33938. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  33939. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  33940. ExpectIntEQ(BN_is_odd(NULL), 0);
  33941. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  33942. ExpectIntEQ(BN_set_word(a, 0), 1);
  33943. ExpectIntEQ(BN_is_zero(a), 1);
  33944. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  33945. ExpectIntEQ(BN_is_zero(a), 0);
  33946. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  33947. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  33948. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  33949. ExpectIntEQ(BN_is_zero(a), 1);
  33950. ExpectIntEQ(BN_set_bit(a, 0), 1);
  33951. ExpectIntEQ(BN_is_odd(a), 1);
  33952. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33953. ExpectIntEQ(BN_is_odd(a), 0);
  33954. ExpectIntEQ(BN_set_bit(a, 1), 1);
  33955. ExpectIntEQ(BN_is_odd(a), 0);
  33956. ExpectIntEQ(BN_set_bit(a, 129), 1);
  33957. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  33958. #ifndef NO_WOLFSSL_STUB
  33959. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  33960. #endif
  33961. BN_free(a);
  33962. #endif
  33963. return EXPECT_RESULT();
  33964. }
  33965. static int test_wolfSSL_BN_shift(void)
  33966. {
  33967. EXPECT_DECLS;
  33968. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33969. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33970. BIGNUM* a = NULL;
  33971. BIGNUM* b = NULL;
  33972. BIGNUM emptyBN;
  33973. /* Setup */
  33974. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33975. ExpectNotNull(a = BN_new());
  33976. ExpectNotNull(b = BN_new());
  33977. /* Invalid parameters. */
  33978. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  33979. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  33980. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  33981. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  33982. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  33983. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  33984. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  33985. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  33986. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  33987. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  33988. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  33989. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  33990. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  33991. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  33992. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  33993. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  33994. ExpectIntEQ(BN_set_word(a, 1), 1);
  33995. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  33996. ExpectIntEQ(BN_is_word(b, 2), 1);
  33997. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  33998. ExpectIntEQ(BN_is_word(a, 2), 1);
  33999. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34000. ExpectIntEQ(BN_is_word(b, 1), 1);
  34001. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34002. ExpectIntEQ(BN_is_word(a, 1), 1);
  34003. BN_free(b);
  34004. BN_free(a);
  34005. #endif
  34006. return EXPECT_RESULT();
  34007. }
  34008. static int test_wolfSSL_BN_math(void)
  34009. {
  34010. EXPECT_DECLS;
  34011. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34012. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34013. BIGNUM* a = NULL;
  34014. BIGNUM* b = NULL;
  34015. BIGNUM* r = NULL;
  34016. BIGNUM* rem = NULL;
  34017. BIGNUM emptyBN;
  34018. BN_ULONG val1;
  34019. BN_ULONG val2;
  34020. /* Setup */
  34021. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34022. ExpectNotNull(a = BN_new());
  34023. ExpectNotNull(b = BN_new());
  34024. ExpectNotNull(r = BN_new());
  34025. ExpectNotNull(rem = BN_new());
  34026. /* Invalid parameters. */
  34027. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34028. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34029. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34030. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34031. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34032. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34033. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34034. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34035. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34036. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34037. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34038. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34039. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34040. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34041. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34042. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34043. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34044. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34045. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34046. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34047. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34048. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34049. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34050. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34051. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34052. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34053. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34054. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34055. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34056. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34057. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34058. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34059. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34060. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34061. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34062. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34063. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34064. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34065. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34066. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34067. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34068. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34069. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34070. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34071. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34072. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34073. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34074. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34075. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34076. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34077. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34078. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34079. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34080. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34081. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34082. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34083. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34084. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34085. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34086. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34087. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34088. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34089. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34090. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34091. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34092. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34093. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34094. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34095. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34096. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34097. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34098. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34099. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34100. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34101. /* END Invalid parameters. */
  34102. val1 = 8;
  34103. val2 = 3;
  34104. ExpectIntEQ(BN_set_word(a, val1), 1);
  34105. ExpectIntEQ(BN_set_word(b, val2), 1);
  34106. ExpectIntEQ(BN_add(r, a, b), 1);
  34107. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34108. ExpectIntEQ(BN_sub(r, a, b), 1);
  34109. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34110. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34111. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34112. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34113. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34114. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34115. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34116. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34117. BN_free(rem);
  34118. BN_free(r);
  34119. BN_free(b);
  34120. BN_free(a);
  34121. #endif
  34122. return EXPECT_RESULT();
  34123. }
  34124. static int test_wolfSSL_BN_math_mod(void)
  34125. {
  34126. EXPECT_DECLS;
  34127. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34128. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34129. BIGNUM* a = NULL;
  34130. BIGNUM* b = NULL;
  34131. BIGNUM* m = NULL;
  34132. BIGNUM* r = NULL;
  34133. BIGNUM* t = NULL;
  34134. BIGNUM emptyBN;
  34135. BN_ULONG val1;
  34136. BN_ULONG val2;
  34137. BN_ULONG val3;
  34138. /* Setup */
  34139. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34140. ExpectNotNull(a = BN_new());
  34141. ExpectNotNull(b = BN_new());
  34142. ExpectNotNull(m = BN_new());
  34143. ExpectNotNull(r = BN_new());
  34144. /* Invalid parameters. */
  34145. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34146. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34147. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34148. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34149. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34150. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34151. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34152. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34153. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34154. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34155. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34156. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34157. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34158. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34159. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34160. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34161. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34162. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34163. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34164. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34165. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34166. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34167. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34168. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34169. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34170. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34171. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34172. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34173. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34174. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34175. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34176. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34177. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34178. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34179. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34180. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34181. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34182. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34183. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34184. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34185. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34186. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34187. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34188. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34189. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34190. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34191. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34192. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34193. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34194. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34195. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34196. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34197. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34198. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34199. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34200. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34201. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34202. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34203. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34204. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34205. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34206. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34207. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34208. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34209. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34210. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34211. /* END Invalid parameters. */
  34212. val1 = 9;
  34213. val2 = 13;
  34214. val3 = 5;
  34215. ExpectIntEQ(BN_set_word(a, val1), 1);
  34216. ExpectIntEQ(BN_set_word(b, val2), 1);
  34217. ExpectIntEQ(BN_set_word(m, val3), 1);
  34218. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34219. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34220. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34221. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34222. ExpectIntEQ(BN_set_word(a, 2), 1);
  34223. ExpectIntEQ(BN_set_word(b, 3), 1);
  34224. ExpectIntEQ(BN_set_word(m, 5), 1);
  34225. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34226. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34227. ExpectIntEQ(BN_is_word(r, 3), 1);
  34228. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34229. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  34230. ExpectIntEQ(BN_is_word(r, 3), 1);
  34231. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34232. ExpectIntEQ(BN_is_word(t, 3), 1);
  34233. BN_free(t);
  34234. /* No inverse case. No inverse when a divides b. */
  34235. ExpectIntEQ(BN_set_word(a, 3), 1);
  34236. ExpectIntEQ(BN_set_word(m, 9), 1);
  34237. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  34238. BN_free(r);
  34239. BN_free(m);
  34240. BN_free(b);
  34241. BN_free(a);
  34242. #endif
  34243. return EXPECT_RESULT();
  34244. }
  34245. static int test_wolfSSL_BN_math_other(void)
  34246. {
  34247. EXPECT_DECLS;
  34248. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34249. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34250. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  34251. BIGNUM* a = NULL;
  34252. BIGNUM* b = NULL;
  34253. BIGNUM* r = NULL;
  34254. BIGNUM emptyBN;
  34255. /* Setup */
  34256. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34257. ExpectNotNull(a = BN_new());
  34258. ExpectNotNull(b = BN_new());
  34259. ExpectNotNull(r = BN_new());
  34260. /* Invalid parameters. */
  34261. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  34262. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  34263. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  34264. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  34265. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  34266. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  34267. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  34268. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34269. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  34270. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  34271. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  34272. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  34273. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  34274. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  34275. /* END Invalid parameters. */
  34276. /* No common factors between 2 and 3. */
  34277. ExpectIntEQ(BN_set_word(a, 2), 1);
  34278. ExpectIntEQ(BN_set_word(b, 3), 1);
  34279. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34280. ExpectIntEQ(BN_is_word(r, 1), 1);
  34281. /* 3 is largest value that divides both 6 and 9. */
  34282. ExpectIntEQ(BN_set_word(a, 6), 1);
  34283. ExpectIntEQ(BN_set_word(b, 9), 1);
  34284. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34285. ExpectIntEQ(BN_is_word(r, 3), 1);
  34286. /* GCD of 0 and 0 is undefined. */
  34287. ExpectIntEQ(BN_set_word(a, 0), 1);
  34288. ExpectIntEQ(BN_set_word(b, 0), 1);
  34289. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  34290. /* Teardown */
  34291. BN_free(r);
  34292. BN_free(b);
  34293. BN_free(a);
  34294. #endif
  34295. #endif
  34296. return EXPECT_RESULT();
  34297. }
  34298. static int test_wolfSSL_BN_rand(void)
  34299. {
  34300. EXPECT_DECLS;
  34301. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  34302. BIGNUM* bn = NULL;
  34303. BIGNUM* range = NULL;
  34304. BIGNUM emptyBN;
  34305. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34306. ExpectNotNull(bn = BN_new());
  34307. ExpectNotNull(range = BN_new());
  34308. /* Invalid parameters. */
  34309. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  34310. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34311. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  34312. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  34313. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34314. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  34315. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  34316. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34317. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  34318. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  34319. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34320. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  34321. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  34322. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  34323. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  34324. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  34325. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  34326. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  34327. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  34328. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34329. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34330. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34331. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34332. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34333. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34334. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34335. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34336. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34337. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34338. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34339. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34340. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34341. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34342. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34343. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34344. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34345. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34346. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34347. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34348. /* 1 bit random value must have no more than one top bit set. */
  34349. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34350. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34351. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34352. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34353. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34354. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34355. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34356. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34357. /* END Invalid parameters. */
  34358. /* 0 bit random: 0. */
  34359. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34360. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34361. ExpectIntEQ(BN_is_zero(bn), 1);
  34362. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  34363. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34364. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34365. ExpectIntEQ(BN_is_zero(bn), 1);
  34366. /* 1 bit random: 0 or 1. */
  34367. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  34368. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34369. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  34370. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34371. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34372. ExpectIntEQ(BN_get_word(bn), 1);
  34373. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34374. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34375. ExpectIntEQ(BN_get_word(bn), 1);
  34376. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  34377. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34378. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  34379. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34380. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34381. ExpectIntEQ(BN_get_word(bn), 1);
  34382. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34383. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34384. ExpectIntEQ(BN_get_word(bn), 1);
  34385. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  34386. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34387. ExpectIntEQ(BN_num_bits(bn), 8);
  34388. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  34389. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  34390. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34391. ExpectIntEQ(BN_num_bits(bn), 8);
  34392. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  34393. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  34394. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34395. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  34396. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  34397. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  34398. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34399. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  34400. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  34401. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  34402. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34403. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  34404. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  34405. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34406. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  34407. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  34408. * requested number of bits up to the nearest multiple of 8. E.g. in this
  34409. * case, requesting a 13-bit random number would actually return a 16-bit
  34410. * random number. */
  34411. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  34412. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34413. ExpectIntEQ(BN_num_bits(bn), 13);
  34414. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  34415. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34416. ExpectIntEQ(BN_rand_range(bn, range), 1);
  34417. ExpectIntEQ(BN_set_word(range, 0), 1);
  34418. ExpectIntEQ(BN_rand_range(bn, range), 1);
  34419. ExpectIntEQ(BN_set_word(range, 1), 1);
  34420. ExpectIntEQ(BN_rand_range(bn, range), 1);
  34421. BN_free(bn);
  34422. BN_free(range);
  34423. #endif
  34424. return EXPECT_RESULT();
  34425. }
  34426. static int test_wolfSSL_BN_prime(void)
  34427. {
  34428. EXPECT_DECLS;
  34429. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34430. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34431. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  34432. BIGNUM* a = NULL;
  34433. BIGNUM* add = NULL;
  34434. BIGNUM* rem = NULL;
  34435. BIGNUM emptyBN;
  34436. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34437. ExpectNotNull(a = BN_new());
  34438. ExpectNotNull(add = BN_new());
  34439. ExpectNotNull(rem = BN_new());
  34440. /* Invalid parameters. */
  34441. /* BN_generate_prime_ex()
  34442. * prime - must have valid BIGNUM
  34443. * bits - Greater then 0
  34444. * safe - not supported, must be 0
  34445. * add - not supported, must be NULL
  34446. * rem - not supported, must be NULL
  34447. * cb - anything
  34448. */
  34449. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  34450. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  34451. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  34452. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  34453. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  34454. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  34455. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  34456. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  34457. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  34458. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  34459. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  34460. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  34461. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  34462. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  34463. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  34464. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  34465. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  34466. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  34467. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  34468. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  34469. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  34470. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  34471. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  34472. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  34473. /* END Invalid parameters. */
  34474. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  34475. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  34476. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34477. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  34478. BN_free(rem);
  34479. BN_free(add);
  34480. BN_free(a);
  34481. #endif
  34482. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34483. return EXPECT_RESULT();
  34484. }
  34485. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34486. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34487. #define TEST_ARG 0x1234
  34488. static void msg_cb(int write_p, int version, int content_type,
  34489. const void *buf, size_t len, SSL *ssl, void *arg)
  34490. {
  34491. (void)write_p;
  34492. (void)version;
  34493. (void)content_type;
  34494. (void)buf;
  34495. (void)len;
  34496. (void)ssl;
  34497. AssertTrue(arg == (void*)TEST_ARG);
  34498. }
  34499. #endif
  34500. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  34501. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34502. #if defined(SESSION_CERTS)
  34503. #include "wolfssl/internal.h"
  34504. #endif
  34505. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  34506. {
  34507. EXPECT_DECLS;
  34508. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  34509. STACK_OF(X509)* sk = NULL;
  34510. X509* x509 = NULL;
  34511. int i, num;
  34512. BIO* bio = NULL;
  34513. #endif
  34514. ExpectNotNull(ctx);
  34515. ExpectNotNull(ssl);
  34516. fprintf(stderr, "\n===== msgcb called ====\n");
  34517. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  34518. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  34519. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  34520. ExpectNotNull(SSL_get0_verified_chain(ssl));
  34521. #endif
  34522. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  34523. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  34524. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  34525. if (sk == NULL) {
  34526. BIO_free(bio);
  34527. return TEST_FAIL;
  34528. }
  34529. num = sk_X509_num(sk);
  34530. ExpectTrue(num > 0);
  34531. for (i = 0; i < num; i++) {
  34532. ExpectNotNull(x509 = sk_X509_value(sk,i));
  34533. if (x509 == NULL)
  34534. break;
  34535. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  34536. X509_print(bio,x509);
  34537. fprintf(stderr, "\n\n");
  34538. }
  34539. BIO_free(bio);
  34540. #endif
  34541. return EXPECT_RESULT();
  34542. }
  34543. #endif
  34544. static int test_wolfSSL_msgCb(void)
  34545. {
  34546. EXPECT_DECLS;
  34547. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  34548. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34549. test_ssl_cbf client_cb;
  34550. test_ssl_cbf server_cb;
  34551. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34552. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34553. #ifndef WOLFSSL_NO_TLS12
  34554. client_cb.method = wolfTLSv1_2_client_method;
  34555. server_cb.method = wolfTLSv1_2_server_method;
  34556. #else
  34557. client_cb.method = wolfTLSv1_3_client_method;
  34558. server_cb.method = wolfTLSv1_3_server_method;
  34559. #endif
  34560. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34561. &server_cb, msgCb), TEST_SUCCESS);
  34562. #endif
  34563. return EXPECT_RESULT();
  34564. }
  34565. static int test_wolfSSL_either_side(void)
  34566. {
  34567. EXPECT_DECLS;
  34568. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  34569. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34570. test_ssl_cbf client_cb;
  34571. test_ssl_cbf server_cb;
  34572. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34573. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34574. /* Use different CTX for client and server */
  34575. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  34576. ExpectNotNull(client_cb.ctx);
  34577. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  34578. ExpectNotNull(server_cb.ctx);
  34579. /* we are responsible for free'ing WOLFSSL_CTX */
  34580. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  34581. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34582. &server_cb, NULL), TEST_SUCCESS);
  34583. wolfSSL_CTX_free(client_cb.ctx);
  34584. wolfSSL_CTX_free(server_cb.ctx);
  34585. #endif
  34586. return EXPECT_RESULT();
  34587. }
  34588. static int test_wolfSSL_DTLS_either_side(void)
  34589. {
  34590. EXPECT_DECLS;
  34591. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  34592. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  34593. test_ssl_cbf client_cb;
  34594. test_ssl_cbf server_cb;
  34595. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34596. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34597. /* Use different CTX for client and server */
  34598. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  34599. ExpectNotNull(client_cb.ctx);
  34600. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  34601. ExpectNotNull(server_cb.ctx);
  34602. /* we are responsible for free'ing WOLFSSL_CTX */
  34603. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  34604. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34605. &server_cb, NULL), TEST_SUCCESS);
  34606. wolfSSL_CTX_free(client_cb.ctx);
  34607. wolfSSL_CTX_free(server_cb.ctx);
  34608. #endif
  34609. return EXPECT_RESULT();
  34610. }
  34611. static int test_generate_cookie(void)
  34612. {
  34613. EXPECT_DECLS;
  34614. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  34615. SSL_CTX* ctx = NULL;
  34616. SSL* ssl = NULL;
  34617. byte buf[FOURK_BUF] = {0};
  34618. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  34619. ExpectNotNull(ssl = SSL_new(ctx));
  34620. /* Test unconnected */
  34621. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  34622. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  34623. wolfSSL_SetCookieCtx(ssl, ctx);
  34624. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  34625. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  34626. SSL_free(ssl);
  34627. SSL_CTX_free(ctx);
  34628. #endif
  34629. return EXPECT_RESULT();
  34630. }
  34631. static int test_wolfSSL_set_options(void)
  34632. {
  34633. EXPECT_DECLS;
  34634. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34635. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34636. WOLFSSL* ssl = NULL;
  34637. WOLFSSL_CTX* ctx = NULL;
  34638. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  34639. char appData[] = "extra msg";
  34640. #endif
  34641. #ifdef OPENSSL_EXTRA
  34642. unsigned char protos[] = {
  34643. 7, 't', 'l', 's', '/', '1', '.', '2',
  34644. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  34645. };
  34646. unsigned int len = sizeof(protos);
  34647. void *arg = (void *)TEST_ARG;
  34648. #endif
  34649. #ifndef NO_WOLFSSL_SERVER
  34650. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34651. #else
  34652. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34653. #endif
  34654. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  34655. WOLFSSL_FILETYPE_PEM));
  34656. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34657. WOLFSSL_FILETYPE_PEM));
  34658. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  34659. == WOLFSSL_OP_NO_TLSv1);
  34660. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  34661. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  34662. WOLFSSL_OP_NO_SSLv2)), 0);
  34663. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  34664. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  34665. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  34666. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  34667. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  34668. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  34669. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  34670. WOLFSSL_OP_NO_COMPRESSION));
  34671. wolfSSL_CTX_free(ctx);
  34672. ctx = NULL;
  34673. #ifndef NO_WOLFSSL_SERVER
  34674. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34675. #else
  34676. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34677. #endif
  34678. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  34679. WOLFSSL_FILETYPE_PEM));
  34680. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34681. WOLFSSL_FILETYPE_PEM));
  34682. #ifdef OPENSSL_EXTRA
  34683. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  34684. #endif
  34685. ExpectNotNull(ssl = wolfSSL_new(ctx));
  34686. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  34687. #ifdef HAVE_EX_DATA
  34688. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  34689. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  34690. if (ssl != NULL) {
  34691. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  34692. appData, sizeof(appData)), 0);
  34693. }
  34694. #else
  34695. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  34696. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  34697. #endif
  34698. #endif
  34699. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  34700. WOLFSSL_OP_NO_TLSv1);
  34701. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  34702. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  34703. WOLFSSL_OP_NO_SSLv2)), 0);
  34704. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  34705. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  34706. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  34707. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  34708. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  34709. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  34710. #ifdef OPENSSL_EXTRA
  34711. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  34712. WOLFSSL_OP_NO_COMPRESSION));
  34713. #endif
  34714. #ifdef OPENSSL_EXTRA
  34715. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  34716. wolfSSL_set_msg_callback_arg(ssl, arg);
  34717. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  34718. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  34719. #else
  34720. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  34721. #endif
  34722. #endif
  34723. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  34724. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  34725. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  34726. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  34727. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  34728. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  34729. #else
  34730. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  34731. #endif
  34732. #endif /* HAVE_ALPN && !NO_BIO */
  34733. #endif
  34734. wolfSSL_free(ssl);
  34735. wolfSSL_CTX_free(ctx);
  34736. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34737. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34738. return EXPECT_RESULT();
  34739. }
  34740. static int test_wolfSSL_sk_SSL_CIPHER(void)
  34741. {
  34742. EXPECT_DECLS;
  34743. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34744. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34745. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34746. SSL* ssl = NULL;
  34747. SSL_CTX* ctx = NULL;
  34748. STACK_OF(SSL_CIPHER) *sk = NULL;
  34749. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  34750. #ifndef NO_WOLFSSL_SERVER
  34751. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34752. #else
  34753. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34754. #endif
  34755. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  34756. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34757. ExpectNotNull(ssl = SSL_new(ctx));
  34758. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  34759. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  34760. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  34761. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  34762. /* error case because connection has not been established yet */
  34763. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  34764. sk_SSL_CIPHER_free(dupSk);
  34765. /* sk is pointer to internal struct that should be free'd in SSL_free */
  34766. SSL_free(ssl);
  34767. SSL_CTX_free(ctx);
  34768. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34769. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34770. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34771. return EXPECT_RESULT();
  34772. }
  34773. static int test_wolfSSL_set1_curves_list(void)
  34774. {
  34775. EXPECT_DECLS;
  34776. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  34777. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34778. SSL* ssl = NULL;
  34779. SSL_CTX* ctx = NULL;
  34780. #ifndef NO_WOLFSSL_SERVER
  34781. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34782. #else
  34783. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34784. #endif
  34785. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  34786. SSL_FILETYPE_PEM));
  34787. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  34788. ExpectNotNull(ssl = SSL_new(ctx));
  34789. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  34790. #ifdef HAVE_ECC
  34791. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  34792. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  34793. #endif
  34794. #ifdef HAVE_CURVE25519
  34795. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  34796. #else
  34797. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  34798. #endif
  34799. #ifdef HAVE_CURVE448
  34800. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  34801. #else
  34802. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  34803. #endif
  34804. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  34805. #ifdef HAVE_ECC
  34806. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  34807. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  34808. #endif
  34809. #ifdef HAVE_CURVE25519
  34810. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  34811. #else
  34812. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  34813. #endif
  34814. #ifdef HAVE_CURVE448
  34815. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  34816. #else
  34817. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  34818. #endif
  34819. SSL_free(ssl);
  34820. SSL_CTX_free(ctx);
  34821. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34822. #endif
  34823. return EXPECT_RESULT();
  34824. }
  34825. static int test_wolfSSL_set1_sigalgs_list(void)
  34826. {
  34827. EXPECT_DECLS;
  34828. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  34829. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34830. SSL* ssl = NULL;
  34831. SSL_CTX* ctx = NULL;
  34832. #ifndef NO_WOLFSSL_SERVER
  34833. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34834. #else
  34835. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34836. #endif
  34837. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34838. SSL_FILETYPE_PEM));
  34839. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34840. ExpectNotNull(ssl = SSL_new(ctx));
  34841. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  34842. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  34843. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  34844. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  34845. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  34846. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  34847. #ifndef NO_RSA
  34848. #ifndef NO_SHA256
  34849. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  34850. WOLFSSL_FAILURE);
  34851. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  34852. WOLFSSL_FAILURE);
  34853. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  34854. WOLFSSL_SUCCESS);
  34855. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  34856. WOLFSSL_SUCCESS);
  34857. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  34858. WOLFSSL_FAILURE);
  34859. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  34860. WOLFSSL_FAILURE);
  34861. #ifdef WC_RSA_PSS
  34862. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  34863. WOLFSSL_SUCCESS);
  34864. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  34865. WOLFSSL_SUCCESS);
  34866. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  34867. WOLFSSL_SUCCESS);
  34868. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  34869. WOLFSSL_SUCCESS);
  34870. #endif
  34871. #ifdef WOLFSSL_SHA512
  34872. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34873. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  34874. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34875. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  34876. #elif defined(WOLFSSL_SHA384)
  34877. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34878. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  34879. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34880. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  34881. #endif
  34882. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  34883. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  34884. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  34885. WOLFSSL_FAILURE);
  34886. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  34887. WOLFSSL_FAILURE);
  34888. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  34889. WOLFSSL_FAILURE);
  34890. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  34891. WOLFSSL_FAILURE);
  34892. #endif
  34893. #endif
  34894. #ifdef HAVE_ECC
  34895. #ifndef NO_SHA256
  34896. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  34897. WOLFSSL_SUCCESS);
  34898. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  34899. WOLFSSL_SUCCESS);
  34900. #ifdef WOLFSSL_SHA512
  34901. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34902. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  34903. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34904. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  34905. #elif defined(WOLFSSL_SHA384)
  34906. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34907. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  34908. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34909. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  34910. #endif
  34911. #endif
  34912. #endif
  34913. #ifdef HAVE_ED25519
  34914. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  34915. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  34916. #endif
  34917. #ifdef HAVE_ED448
  34918. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  34919. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  34920. #endif
  34921. #ifndef NO_DSA
  34922. #ifndef NO_SHA256
  34923. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  34924. WOLFSSL_SUCCESS);
  34925. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  34926. WOLFSSL_SUCCESS);
  34927. #endif
  34928. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  34929. defined(WOLFSSL_ALLOW_TLS_SHA1))
  34930. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  34931. WOLFSSL_SUCCESS);
  34932. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  34933. WOLFSSL_SUCCESS);
  34934. #endif
  34935. #endif
  34936. SSL_free(ssl);
  34937. SSL_CTX_free(ctx);
  34938. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34939. #endif
  34940. return EXPECT_RESULT();
  34941. }
  34942. /* Testing wolfSSL_set_tlsext_status_type function.
  34943. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  34944. */
  34945. static int test_wolfSSL_set_tlsext_status_type(void)
  34946. {
  34947. EXPECT_DECLS;
  34948. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  34949. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  34950. SSL* ssl = NULL;
  34951. SSL_CTX* ctx = NULL;
  34952. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34953. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34954. SSL_FILETYPE_PEM));
  34955. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34956. ExpectNotNull(ssl = SSL_new(ctx));
  34957. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  34958. SSL_SUCCESS);
  34959. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  34960. SSL_free(ssl);
  34961. SSL_CTX_free(ctx);
  34962. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  34963. return EXPECT_RESULT();
  34964. }
  34965. #ifndef NO_BIO
  34966. static int test_wolfSSL_PEM_read_bio(void)
  34967. {
  34968. EXPECT_DECLS;
  34969. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34970. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34971. byte buff[6000];
  34972. XFILE f = XBADFILE;
  34973. int bytes;
  34974. X509* x509 = NULL;
  34975. BIO* bio = NULL;
  34976. BUF_MEM* buf = NULL;
  34977. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  34978. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  34979. if (f != XBADFILE)
  34980. XFCLOSE(f);
  34981. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  34982. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  34983. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  34984. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  34985. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  34986. /* BIO should return the set EOF value */
  34987. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  34988. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  34989. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  34990. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  34991. BIO_free(bio);
  34992. BUF_MEM_free(buf);
  34993. X509_free(x509);
  34994. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  34995. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34996. return EXPECT_RESULT();
  34997. }
  34998. #if defined(OPENSSL_EXTRA)
  34999. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35000. long argl, long ret)
  35001. {
  35002. (void)bio;
  35003. (void)cmd;
  35004. (void)argp;
  35005. (void)argi;
  35006. (void)argl;
  35007. return ret;
  35008. }
  35009. #endif
  35010. static int test_wolfSSL_BIO(void)
  35011. {
  35012. EXPECT_DECLS;
  35013. #if defined(OPENSSL_EXTRA)
  35014. const unsigned char* p = NULL;
  35015. byte buff[20];
  35016. BIO* bio1 = NULL;
  35017. BIO* bio2 = NULL;
  35018. BIO* bio3 = NULL;
  35019. char* bufPt = NULL;
  35020. int i;
  35021. for (i = 0; i < 20; i++) {
  35022. buff[i] = i;
  35023. }
  35024. /* test BIO_free with NULL */
  35025. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35026. /* Creating and testing type BIO_s_bio */
  35027. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35028. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35029. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35030. /* read/write before set up */
  35031. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35032. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35033. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35034. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35035. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35036. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35037. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35038. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35039. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35040. /* write buffer full */
  35041. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35042. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35043. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35044. /* write the other direction with pair */
  35045. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35046. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35047. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35048. /* try read */
  35049. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35050. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35051. /* try read using ctrl function */
  35052. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35053. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35054. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35055. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35056. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35057. for (i = 0; i < 20; i++) {
  35058. ExpectIntEQ((int)bufPt[i], i);
  35059. }
  35060. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35061. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35062. for (i = 0; i < 8; i++) {
  35063. ExpectIntEQ((int)bufPt[i], i);
  35064. }
  35065. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  35066. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35067. /* new pair */
  35068. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35069. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35070. bio2 = NULL;
  35071. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35072. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35073. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  35074. /* test wrap around... */
  35075. ExpectIntEQ(BIO_reset(bio1), 0);
  35076. ExpectIntEQ(BIO_reset(bio3), 0);
  35077. /* fill write buffer, read only small amount then write again */
  35078. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35079. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35080. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35081. for (i = 0; i < 4; i++) {
  35082. ExpectIntEQ(bufPt[i], i);
  35083. }
  35084. /* try writing over read index */
  35085. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35086. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  35087. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35088. /* read and write 0 bytes */
  35089. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35090. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35091. /* should read only to end of write buffer then need to read again */
  35092. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35093. for (i = 0; i < 16; i++) {
  35094. ExpectIntEQ(bufPt[i], buff[4 + i]);
  35095. }
  35096. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35097. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35098. for (i = 0; i < 4; i++) {
  35099. ExpectIntEQ(bufPt[i], 0);
  35100. }
  35101. /* read index should not have advanced with nread0 */
  35102. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35103. for (i = 0; i < 4; i++) {
  35104. ExpectIntEQ(bufPt[i], 0);
  35105. }
  35106. /* write and fill up buffer checking reset of index state */
  35107. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35108. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35109. /* test reset on data in bio1 write buffer */
  35110. ExpectIntEQ(BIO_reset(bio1), 0);
  35111. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35112. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  35113. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35114. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35115. ExpectNotNull(p);
  35116. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35117. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35118. for (i = 0; i < 6; i++) {
  35119. ExpectIntEQ(bufPt[i], i);
  35120. }
  35121. /* test case of writing twice with offset read index */
  35122. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35123. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35124. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35125. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35126. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35127. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35128. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35129. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35130. BIO_free(bio1);
  35131. bio1 = NULL;
  35132. BIO_free(bio3);
  35133. bio3 = NULL;
  35134. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35135. {
  35136. BIO* bioA = NULL;
  35137. BIO* bioB = NULL;
  35138. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35139. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35140. BIO_free(bioA);
  35141. bioA = NULL;
  35142. BIO_free(bioB);
  35143. bioB = NULL;
  35144. }
  35145. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35146. /* BIOs with file pointers */
  35147. #if !defined(NO_FILESYSTEM)
  35148. {
  35149. XFILE f1 = XBADFILE;
  35150. XFILE f2 = XBADFILE;
  35151. BIO* f_bio1 = NULL;
  35152. BIO* f_bio2 = NULL;
  35153. unsigned char cert[300];
  35154. char testFile[] = "tests/bio_write_test.txt";
  35155. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35156. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35157. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35158. /* Failure due to wrong BIO type */
  35159. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35160. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35161. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  35162. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35163. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  35164. WOLFSSL_SUCCESS);
  35165. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35166. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  35167. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  35168. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  35169. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  35170. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  35171. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  35172. ExpectIntEQ(BIO_reset(f_bio2), 0);
  35173. ExpectIntEQ(BIO_tell(NULL),-1);
  35174. ExpectIntEQ(BIO_tell(f_bio2),0);
  35175. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  35176. ExpectIntEQ(BIO_tell(f_bio2),4);
  35177. BIO_free(f_bio1);
  35178. f_bio1 = NULL;
  35179. BIO_free(f_bio2);
  35180. f_bio2 = NULL;
  35181. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  35182. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35183. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35184. BIO_free(f_bio1);
  35185. f_bio1 = NULL;
  35186. }
  35187. #endif /* !defined(NO_FILESYSTEM) */
  35188. /* BIO info callback */
  35189. {
  35190. const char* testArg = "test";
  35191. BIO* cb_bio = NULL;
  35192. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  35193. BIO_set_callback(cb_bio, bioCallback);
  35194. ExpectNotNull(BIO_get_callback(cb_bio));
  35195. BIO_set_callback(cb_bio, NULL);
  35196. ExpectNull(BIO_get_callback(cb_bio));
  35197. BIO_set_callback_arg(cb_bio, (char*)testArg);
  35198. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  35199. ExpectNull(BIO_get_callback_arg(NULL));
  35200. BIO_free(cb_bio);
  35201. cb_bio = NULL;
  35202. }
  35203. /* BIO_vfree */
  35204. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35205. BIO_vfree(NULL);
  35206. BIO_vfree(bio1);
  35207. #endif
  35208. return EXPECT_RESULT();
  35209. }
  35210. static int test_wolfSSL_BIO_BIO_ring_read(void)
  35211. {
  35212. EXPECT_DECLS;
  35213. #if defined(OPENSSL_ALL)
  35214. BIO* bio1 = NULL;
  35215. BIO* bio2 = NULL;
  35216. byte data[50];
  35217. byte tmp[50];
  35218. XMEMSET(data, 42, sizeof(data));
  35219. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  35220. SSL_SUCCESS);
  35221. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  35222. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  35223. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  35224. ExpectBufEQ(tmp, data, 20);
  35225. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  35226. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  35227. ExpectBufEQ(tmp, data, 40);
  35228. BIO_free(bio1);
  35229. BIO_free(bio2);
  35230. #endif
  35231. return EXPECT_RESULT();
  35232. }
  35233. #endif /* !NO_BIO */
  35234. static int test_wolfSSL_a2i_IPADDRESS(void)
  35235. {
  35236. EXPECT_DECLS;
  35237. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  35238. const unsigned char* data = NULL;
  35239. int dataSz = 0;
  35240. ASN1_OCTET_STRING *st = NULL;
  35241. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  35242. const unsigned char ipv6_exp[] = {
  35243. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35244. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  35245. };
  35246. const unsigned char ipv6_home[] = {
  35247. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35248. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  35249. };
  35250. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  35251. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  35252. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35253. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  35254. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  35255. ASN1_STRING_free(st);
  35256. st = NULL;
  35257. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  35258. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35259. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35260. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  35261. ASN1_STRING_free(st);
  35262. st = NULL;
  35263. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  35264. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35265. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35266. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  35267. ASN1_STRING_free(st);
  35268. #endif
  35269. return EXPECT_RESULT();
  35270. }
  35271. static int test_wolfSSL_X509_cmp_time(void)
  35272. {
  35273. EXPECT_DECLS;
  35274. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  35275. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  35276. WOLFSSL_ASN1_TIME asn_time;
  35277. time_t t;
  35278. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  35279. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  35280. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  35281. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  35282. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  35283. #endif
  35284. return EXPECT_RESULT();
  35285. }
  35286. static int test_wolfSSL_X509_time_adj(void)
  35287. {
  35288. EXPECT_DECLS;
  35289. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  35290. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  35291. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  35292. !defined(NO_ASN_TIME)
  35293. X509* x509 = NULL;
  35294. time_t t;
  35295. time_t not_before;
  35296. time_t not_after;
  35297. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  35298. client_cert_der_2048, sizeof_client_cert_der_2048,
  35299. WOLFSSL_FILETYPE_ASN1));
  35300. t = 0;
  35301. not_before = wc_Time(0);
  35302. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  35303. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  35304. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  35305. /* Check X509_gmtime_adj, too. */
  35306. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  35307. X509_free(x509);
  35308. #endif
  35309. return EXPECT_RESULT();
  35310. }
  35311. static int test_wolfSSL_X509(void)
  35312. {
  35313. EXPECT_DECLS;
  35314. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35315. !defined(NO_RSA)
  35316. X509* x509 = NULL;
  35317. #ifndef NO_BIO
  35318. BIO* bio = NULL;
  35319. X509_STORE_CTX* ctx = NULL;
  35320. X509_STORE* store = NULL;
  35321. #endif
  35322. char der[] = "certs/ca-cert.der";
  35323. XFILE fp = XBADFILE;
  35324. ExpectNotNull(x509 = X509_new());
  35325. X509_free(x509);
  35326. x509 = NULL;
  35327. #ifndef NO_BIO
  35328. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  35329. SSL_FILETYPE_PEM));
  35330. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35331. #ifdef WOLFSSL_CERT_GEN
  35332. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  35333. #endif
  35334. ExpectNotNull(ctx = X509_STORE_CTX_new());
  35335. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  35336. ExpectNotNull(store = X509_STORE_new());
  35337. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  35338. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  35339. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  35340. X509_STORE_CTX_free(ctx);
  35341. X509_STORE_free(store);
  35342. X509_free(x509);
  35343. x509 = NULL;
  35344. BIO_free(bio);
  35345. #endif
  35346. /** d2i_X509_fp test **/
  35347. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  35348. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  35349. ExpectNotNull(x509);
  35350. X509_free(x509);
  35351. x509 = NULL;
  35352. if (fp != XBADFILE) {
  35353. XFCLOSE(fp);
  35354. fp = XBADFILE;
  35355. }
  35356. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  35357. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  35358. ExpectNotNull(x509);
  35359. X509_free(x509);
  35360. if (fp != XBADFILE)
  35361. XFCLOSE(fp);
  35362. /* X509_up_ref test */
  35363. ExpectIntEQ(X509_up_ref(NULL), 0);
  35364. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  35365. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  35366. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  35367. X509_free(x509); /* refCount = 2 */
  35368. X509_free(x509); /* refCount = 1 */
  35369. X509_free(x509); /* refCount = 0, free */
  35370. #endif
  35371. return EXPECT_RESULT();
  35372. }
  35373. static int test_wolfSSL_X509_get_ext_count(void)
  35374. {
  35375. EXPECT_DECLS;
  35376. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35377. !defined(NO_RSA)
  35378. int ret = 0;
  35379. WOLFSSL_X509* x509 = NULL;
  35380. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  35381. XFILE f = XBADFILE;
  35382. /* NULL parameter check */
  35383. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  35384. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  35385. SSL_FILETYPE_PEM));
  35386. ExpectIntEQ(X509_get_ext_count(x509), 5);
  35387. wolfSSL_X509_free(x509);
  35388. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  35389. SSL_FILETYPE_PEM));
  35390. ExpectIntEQ(X509_get_ext_count(x509), 5);
  35391. wolfSSL_X509_free(x509);
  35392. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  35393. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35394. if (f != XBADFILE)
  35395. XFCLOSE(f);
  35396. /* wolfSSL_X509_get_ext_count() valid input */
  35397. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  35398. /* wolfSSL_X509_get_ext_count() NULL argument */
  35399. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  35400. wolfSSL_X509_free(x509);
  35401. #endif
  35402. return EXPECT_RESULT();
  35403. }
  35404. static int test_wolfSSL_X509_sign2(void)
  35405. {
  35406. EXPECT_DECLS;
  35407. /* test requires WOLFSSL_AKID_NAME to match expected output */
  35408. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  35409. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  35410. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  35411. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  35412. defined(WOLFSSL_IP_ALT_NAME))
  35413. WOLFSSL_X509 *x509 = NULL;
  35414. WOLFSSL_X509 *ca = NULL;
  35415. const unsigned char *der = NULL;
  35416. const unsigned char *pt = NULL;
  35417. WOLFSSL_EVP_PKEY *priv = NULL;
  35418. WOLFSSL_X509_NAME *name = NULL;
  35419. int derSz;
  35420. #ifndef NO_ASN_TIME
  35421. WOLFSSL_ASN1_TIME *notBefore = NULL;
  35422. WOLFSSL_ASN1_TIME *notAfter = NULL;
  35423. const int year = 365*24*60*60;
  35424. const int day = 24*60*60;
  35425. const int hour = 60*60;
  35426. const int mini = 60;
  35427. time_t t;
  35428. #endif
  35429. const unsigned char expected[] = {
  35430. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  35431. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  35432. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  35433. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  35434. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  35435. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  35436. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  35437. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  35438. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  35439. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  35440. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  35441. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  35442. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  35443. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  35444. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35445. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  35446. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  35447. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  35448. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  35449. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  35450. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  35451. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  35452. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  35453. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  35454. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  35455. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  35456. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  35457. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  35458. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  35459. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  35460. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  35461. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  35462. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  35463. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35464. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  35465. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  35466. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  35467. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  35468. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  35469. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  35470. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  35471. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  35472. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  35473. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  35474. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  35475. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  35476. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  35477. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  35478. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  35479. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  35480. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  35481. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  35482. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  35483. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  35484. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  35485. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  35486. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  35487. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  35488. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  35489. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  35490. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  35491. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  35492. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  35493. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  35494. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  35495. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  35496. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  35497. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  35498. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  35499. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  35500. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  35501. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  35502. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  35503. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  35504. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  35505. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  35506. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  35507. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  35508. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  35509. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  35510. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  35511. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  35512. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  35513. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  35514. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  35515. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  35516. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  35517. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  35518. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  35519. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  35520. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  35521. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  35522. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  35523. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  35524. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  35525. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  35526. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  35527. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  35528. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  35529. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  35530. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  35531. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  35532. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  35533. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  35534. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  35535. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  35536. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  35537. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  35538. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  35539. };
  35540. pt = ca_key_der_2048;
  35541. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  35542. sizeof_ca_key_der_2048));
  35543. pt = client_cert_der_2048;
  35544. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  35545. sizeof_client_cert_der_2048));
  35546. pt = ca_cert_der_2048;
  35547. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  35548. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  35549. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  35550. #ifndef NO_ASN_TIME
  35551. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  35552. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  35553. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  35554. ExpectIntEQ(notAfter->length, 13);
  35555. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  35556. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  35557. #endif
  35558. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  35559. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  35560. ExpectIntEQ(derSz, sizeof(expected));
  35561. #ifndef NO_ASN_TIME
  35562. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  35563. #endif
  35564. wolfSSL_X509_free(ca);
  35565. wolfSSL_X509_free(x509);
  35566. wolfSSL_EVP_PKEY_free(priv);
  35567. #ifndef NO_ASN_TIME
  35568. wolfSSL_ASN1_TIME_free(notBefore);
  35569. wolfSSL_ASN1_TIME_free(notAfter);
  35570. #endif
  35571. #endif
  35572. return EXPECT_RESULT();
  35573. }
  35574. static int test_wolfSSL_X509_sign(void)
  35575. {
  35576. EXPECT_DECLS;
  35577. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  35578. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  35579. int ret = 0;
  35580. char *cn = NULL;
  35581. word32 cnSz = 0;
  35582. X509_NAME *name = NULL;
  35583. X509 *x509 = NULL;
  35584. X509 *ca = NULL;
  35585. DecodedCert dCert;
  35586. EVP_PKEY *pub = NULL;
  35587. EVP_PKEY *priv = NULL;
  35588. EVP_MD_CTX *mctx = NULL;
  35589. #if defined(USE_CERT_BUFFERS_1024)
  35590. const unsigned char* rsaPriv = client_key_der_1024;
  35591. const unsigned char* rsaPub = client_keypub_der_1024;
  35592. const unsigned char* certIssuer = client_cert_der_1024;
  35593. long clientKeySz = (long)sizeof_client_key_der_1024;
  35594. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  35595. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  35596. #elif defined(USE_CERT_BUFFERS_2048)
  35597. const unsigned char* rsaPriv = client_key_der_2048;
  35598. const unsigned char* rsaPub = client_keypub_der_2048;
  35599. const unsigned char* certIssuer = client_cert_der_2048;
  35600. long clientKeySz = (long)sizeof_client_key_der_2048;
  35601. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  35602. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  35603. #endif
  35604. byte sn[16];
  35605. int snSz = sizeof(sn);
  35606. /* Set X509_NAME fields */
  35607. ExpectNotNull(name = X509_NAME_new());
  35608. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  35609. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  35610. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  35611. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  35612. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  35613. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  35614. /* Get private and public keys */
  35615. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  35616. clientKeySz));
  35617. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  35618. ExpectNotNull(x509 = X509_new());
  35619. /* Set version 3 */
  35620. ExpectIntNE(X509_set_version(x509, 2L), 0);
  35621. /* Set subject name, add pubkey, and sign certificate */
  35622. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  35623. X509_NAME_free(name);
  35624. name = NULL;
  35625. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  35626. #ifdef WOLFSSL_ALT_NAMES
  35627. /* Add some subject alt names */
  35628. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  35629. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  35630. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35631. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  35632. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35633. "sphygmomanometer",
  35634. ASN_DNS_TYPE), SSL_SUCCESS);
  35635. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35636. "supercalifragilisticexpialidocious",
  35637. ASN_DNS_TYPE), SSL_SUCCESS);
  35638. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35639. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  35640. ASN_DNS_TYPE), SSL_SUCCESS);
  35641. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35642. {
  35643. unsigned char ip4_type[] = {127,128,0,255};
  35644. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  35645. 0xff, 0xee, 0x99, 0x88,
  35646. 0x77, 0x66, 0x55, 0x44,
  35647. 0x00, 0x33, 0x22, 0x11};
  35648. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  35649. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  35650. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  35651. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  35652. }
  35653. #endif
  35654. #endif /* WOLFSSL_ALT_NAMES */
  35655. /* test valid sign case */
  35656. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  35657. /* test valid X509_sign_ctx case */
  35658. ExpectNotNull(mctx = EVP_MD_CTX_new());
  35659. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  35660. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  35661. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  35662. ExpectIntEQ(X509_get_ext_count(x509), 1);
  35663. #endif
  35664. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  35665. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  35666. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  35667. #endif
  35668. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  35669. WOLFSSL_SUCCESS);
  35670. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  35671. /* Variation in size depends on ASN.1 encoding when MSB is set.
  35672. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  35673. * with the MSB set. See GenerateInteger in asn.c */
  35674. #ifndef USE_CERT_BUFFERS_1024
  35675. #ifndef WOLFSSL_ALT_NAMES
  35676. /* Valid case - size should be 781-786 with 16 byte serial number */
  35677. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  35678. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35679. /* Valid case - size should be 955-960 with 16 byte serial number */
  35680. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  35681. #else
  35682. /* Valid case - size should be 926-931 with 16 byte serial number */
  35683. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  35684. #endif
  35685. #else
  35686. #ifndef WOLFSSL_ALT_NAMES
  35687. /* Valid case - size should be 537-542 with 16 byte serial number */
  35688. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  35689. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35690. /* Valid case - size should be 695-670 with 16 byte serial number */
  35691. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  35692. #else
  35693. /* Valid case - size should be 666-671 with 16 byte serial number */
  35694. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  35695. #endif
  35696. #endif
  35697. /* check that issuer name is as expected after signature */
  35698. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  35699. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  35700. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  35701. ExpectNotNull(name = X509_get_subject_name(ca));
  35702. cnSz = X509_NAME_get_sz(name);
  35703. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  35704. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  35705. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  35706. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  35707. cn = NULL;
  35708. #ifdef WOLFSSL_MULTI_ATTRIB
  35709. /* test adding multiple OU's to the signer */
  35710. ExpectNotNull(name = X509_get_subject_name(ca));
  35711. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  35712. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  35713. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  35714. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  35715. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  35716. #endif
  35717. ExpectNotNull(name = X509_get_subject_name(ca));
  35718. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  35719. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  35720. ExpectNotNull(name = X509_get_issuer_name(x509));
  35721. cnSz = X509_NAME_get_sz(name);
  35722. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  35723. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  35724. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  35725. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  35726. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  35727. cn = NULL;
  35728. FreeDecodedCert(&dCert);
  35729. /* Test invalid parameters */
  35730. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  35731. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  35732. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  35733. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  35734. EVP_MD_CTX_free(mctx);
  35735. mctx = NULL;
  35736. ExpectNotNull(mctx = EVP_MD_CTX_new());
  35737. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  35738. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  35739. /* test invalid version number */
  35740. #if defined(OPENSSL_ALL)
  35741. ExpectIntNE(X509_set_version(x509, 6L), 0);
  35742. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  35743. /* uses ParseCert which fails on bad version number */
  35744. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  35745. #endif
  35746. EVP_MD_CTX_free(mctx);
  35747. EVP_PKEY_free(priv);
  35748. EVP_PKEY_free(pub);
  35749. X509_free(x509);
  35750. X509_free(ca);
  35751. #endif
  35752. return EXPECT_RESULT();
  35753. }
  35754. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  35755. {
  35756. EXPECT_DECLS;
  35757. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  35758. X509* x509 = NULL;
  35759. const X509_ALGOR* alg;
  35760. ExpectNotNull(x509 = X509_new());
  35761. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  35762. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  35763. X509_free(x509);
  35764. #endif
  35765. return EXPECT_RESULT();
  35766. }
  35767. static int test_wolfSSL_X509_ALGOR_get0(void)
  35768. {
  35769. EXPECT_DECLS;
  35770. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35771. !defined(NO_SHA256) && !defined(NO_RSA)
  35772. X509* x509 = NULL;
  35773. const ASN1_OBJECT* obj = NULL;
  35774. const X509_ALGOR* alg = NULL;
  35775. int pptype = 0;
  35776. const void *ppval = NULL;
  35777. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  35778. SSL_FILETYPE_PEM));
  35779. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  35780. /* Invalid case */
  35781. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  35782. ExpectNull(obj);
  35783. /* Valid case */
  35784. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  35785. ExpectNotNull(obj);
  35786. ExpectNull(ppval);
  35787. ExpectIntNE(pptype, 0);
  35788. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  35789. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  35790. X509_free(x509);
  35791. #endif
  35792. return EXPECT_RESULT();
  35793. }
  35794. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  35795. {
  35796. EXPECT_DECLS;
  35797. #if defined(OPENSSL_EXTRA)
  35798. X509_VERIFY_PARAM *paramTo = NULL;
  35799. X509_VERIFY_PARAM *paramFrom = NULL;
  35800. char testIPv4[] = "127.0.0.1";
  35801. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  35802. char testhostName1[] = "foo.hoge.com";
  35803. char testhostName2[] = "foobar.hoge.com";
  35804. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  35805. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  35806. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  35807. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  35808. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  35809. (int)XSTRLEN(testhostName1)), 1);
  35810. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  35811. (int)XSTRLEN(testhostName1)));
  35812. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  35813. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  35814. ExpectIntEQ(0x01, paramFrom->hostFlags);
  35815. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  35816. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  35817. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  35818. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  35819. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  35820. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35821. /* null pointer */
  35822. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  35823. /* in the case of "from" null, returns success */
  35824. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  35825. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  35826. /* inherit flags test : VPARAM_DEFAULT */
  35827. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35828. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35829. (int)XSTRLEN(testhostName1)));
  35830. ExpectIntEQ(0x01, paramTo->hostFlags);
  35831. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35832. /* inherit flags test : VPARAM OVERWRITE */
  35833. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35834. (int)XSTRLEN(testhostName2)), 1);
  35835. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35836. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  35837. if (paramTo != NULL) {
  35838. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  35839. }
  35840. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35841. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35842. (int)XSTRLEN(testhostName1)));
  35843. ExpectIntEQ(0x01, paramTo->hostFlags);
  35844. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35845. /* inherit flags test : VPARAM_RESET_FLAGS */
  35846. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35847. (int)XSTRLEN(testhostName2)), 1);
  35848. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35849. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  35850. if (paramTo != NULL) {
  35851. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  35852. }
  35853. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35854. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35855. (int)XSTRLEN(testhostName1)));
  35856. ExpectIntEQ(0x01, paramTo->hostFlags);
  35857. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35858. /* inherit flags test : VPARAM_LOCKED */
  35859. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35860. (int)XSTRLEN(testhostName2)), 1);
  35861. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35862. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  35863. if (paramTo != NULL) {
  35864. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  35865. }
  35866. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35867. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  35868. (int)XSTRLEN(testhostName2)));
  35869. ExpectIntEQ(0x00, paramTo->hostFlags);
  35870. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  35871. /* test for incorrect parameters */
  35872. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  35873. 0);
  35874. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  35875. /* inherit flags test : VPARAM_ONCE, not testable yet */
  35876. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  35877. 1);
  35878. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  35879. X509_V_FLAG_CRL_CHECK_ALL);
  35880. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  35881. X509_V_FLAG_CRL_CHECK_ALL), 1);
  35882. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  35883. X509_VERIFY_PARAM_free(paramTo);
  35884. X509_VERIFY_PARAM_free(paramFrom);
  35885. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  35886. #endif
  35887. return EXPECT_RESULT();
  35888. }
  35889. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35890. static int test_wolfSSL_check_domain_verify_count = 0;
  35891. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  35892. WOLFSSL_X509_STORE_CTX* store)
  35893. {
  35894. EXPECT_DECLS;
  35895. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  35896. ExpectIntEQ(preverify, 1);
  35897. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  35898. return EXPECT_SUCCESS();
  35899. }
  35900. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  35901. {
  35902. EXPECT_DECLS;
  35903. X509_VERIFY_PARAM *param = NULL;
  35904. ExpectNotNull(param = SSL_get0_param(ssl));
  35905. /* Domain check should only be done on the leaf cert */
  35906. X509_VERIFY_PARAM_set_hostflags(param,
  35907. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  35908. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  35909. "wolfSSL Server Chain", 0), 1);
  35910. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  35911. test_wolfSSL_check_domain_verify_cb);
  35912. return EXPECT_RESULT();
  35913. }
  35914. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  35915. {
  35916. EXPECT_DECLS;
  35917. /* Use a cert with different domains in chain */
  35918. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  35919. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  35920. return EXPECT_RESULT();
  35921. }
  35922. static int test_wolfSSL_check_domain(void)
  35923. {
  35924. EXPECT_DECLS;
  35925. test_ssl_cbf func_cb_client;
  35926. test_ssl_cbf func_cb_server;
  35927. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35928. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35929. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  35930. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  35931. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35932. &func_cb_server, NULL), TEST_SUCCESS);
  35933. /* Should have been called once for each cert in sent chain */
  35934. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  35935. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  35936. #else
  35937. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  35938. #endif
  35939. return EXPECT_RESULT();
  35940. }
  35941. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  35942. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  35943. {
  35944. EXPECT_DECLS;
  35945. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  35946. X509* x509 = NULL;
  35947. X509_PUBKEY* pubKey;
  35948. ExpectNotNull(x509 = X509_new());
  35949. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  35950. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  35951. X509_free(x509);
  35952. #endif
  35953. return EXPECT_RESULT();
  35954. }
  35955. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  35956. {
  35957. EXPECT_DECLS;
  35958. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35959. !defined(NO_SHA256) && !defined(NO_RSA)
  35960. X509* x509 = NULL;
  35961. ASN1_OBJECT* obj = NULL;
  35962. const ASN1_OBJECT* pa_oid = NULL;
  35963. X509_PUBKEY* pubKey = NULL;
  35964. X509_PUBKEY* pubKey2 = NULL;
  35965. EVP_PKEY* evpKey = NULL;
  35966. const unsigned char *pk = NULL;
  35967. int ppklen;
  35968. int pptype;
  35969. X509_ALGOR *pa = NULL;
  35970. const void *pval;
  35971. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  35972. SSL_FILETYPE_PEM));
  35973. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  35974. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  35975. ExpectNotNull(pk);
  35976. ExpectNotNull(pa);
  35977. ExpectNotNull(pubKey);
  35978. ExpectIntGT(ppklen, 0);
  35979. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  35980. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  35981. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  35982. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  35983. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  35984. ExpectNotNull(pk);
  35985. ExpectNotNull(pa);
  35986. ExpectIntGT(ppklen, 0);
  35987. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35988. ExpectNotNull(pa_oid);
  35989. ExpectNull(pval);
  35990. ExpectIntEQ(pptype, V_ASN1_NULL);
  35991. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  35992. X509_PUBKEY_free(pubKey2);
  35993. X509_free(x509);
  35994. EVP_PKEY_free(evpKey);
  35995. #endif
  35996. return EXPECT_RESULT();
  35997. }
  35998. static int test_wolfSSL_X509_PUBKEY_EC(void)
  35999. {
  36000. EXPECT_DECLS;
  36001. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36002. X509* x509 = NULL;
  36003. ASN1_OBJECT* obj = NULL;
  36004. ASN1_OBJECT* poid = NULL;
  36005. const ASN1_OBJECT* pa_oid = NULL;
  36006. X509_PUBKEY* pubKey = NULL;
  36007. X509_PUBKEY* pubKey2 = NULL;
  36008. EVP_PKEY* evpKey = NULL;
  36009. const unsigned char *pk = NULL;
  36010. int ppklen;
  36011. int pptype;
  36012. X509_ALGOR *pa = NULL;
  36013. const void *pval;
  36014. char buf[50];
  36015. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36016. SSL_FILETYPE_PEM));
  36017. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36018. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36019. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36020. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36021. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36022. ExpectNotNull(pk);
  36023. ExpectNotNull(pa);
  36024. ExpectIntGT(ppklen, 0);
  36025. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36026. ExpectNotNull(pa_oid);
  36027. ExpectNotNull(pval);
  36028. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  36029. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36030. poid = (ASN1_OBJECT *)pval;
  36031. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36032. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36033. X509_PUBKEY_free(pubKey2);
  36034. X509_free(x509);
  36035. EVP_PKEY_free(evpKey);
  36036. #endif
  36037. return EXPECT_RESULT();
  36038. }
  36039. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36040. {
  36041. EXPECT_DECLS;
  36042. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36043. word32 bytes;
  36044. #ifdef USE_CERT_BUFFERS_1024
  36045. byte tmp[ONEK_BUF];
  36046. #elif defined(USE_CERT_BUFFERS_2048)
  36047. byte tmp[TWOK_BUF];
  36048. #else
  36049. byte tmp[TWOK_BUF];
  36050. #endif /* END USE_CERT_BUFFERS_1024 */
  36051. const unsigned char* dsaKeyDer = tmp;
  36052. ASN1_OBJECT* obj = NULL;
  36053. ASN1_STRING* str;
  36054. const ASN1_OBJECT* pa_oid = NULL;
  36055. X509_PUBKEY* pubKey = NULL;
  36056. EVP_PKEY* evpKey = NULL;
  36057. const unsigned char *pk = NULL;
  36058. int ppklen, pptype;
  36059. X509_ALGOR *pa = NULL;
  36060. const void *pval;
  36061. #ifdef USE_CERT_BUFFERS_1024
  36062. XMEMSET(tmp, 0, sizeof(tmp));
  36063. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36064. bytes = sizeof_dsa_key_der_1024;
  36065. #elif defined(USE_CERT_BUFFERS_2048)
  36066. XMEMSET(tmp, 0, sizeof(tmp));
  36067. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36068. bytes = sizeof_dsa_key_der_2048;
  36069. #else
  36070. {
  36071. XFILE fp = XBADFILE;
  36072. XMEMSET(tmp, 0, sizeof(tmp));
  36073. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  36074. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  36075. if (fp != XBADFILE)
  36076. XFCLOSE(fp);
  36077. }
  36078. #endif
  36079. /* Initialize pkey with der format dsa key */
  36080. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  36081. ExpectNotNull(pubKey = X509_PUBKEY_new());
  36082. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  36083. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36084. ExpectNotNull(pk);
  36085. ExpectNotNull(pa);
  36086. ExpectIntGT(ppklen, 0);
  36087. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36088. ExpectNotNull(pa_oid);
  36089. ExpectNotNull(pval);
  36090. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  36091. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  36092. str = (ASN1_STRING *)pval;
  36093. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  36094. #ifdef USE_CERT_BUFFERS_1024
  36095. ExpectIntEQ(ASN1_STRING_length(str), 291);
  36096. #else
  36097. ExpectIntEQ(ASN1_STRING_length(str), 549);
  36098. #endif /* END USE_CERT_BUFFERS_1024 */
  36099. X509_PUBKEY_free(pubKey);
  36100. EVP_PKEY_free(evpKey);
  36101. #endif
  36102. return EXPECT_RESULT();
  36103. }
  36104. static int test_wolfSSL_BUF(void)
  36105. {
  36106. EXPECT_DECLS;
  36107. #if defined(OPENSSL_EXTRA)
  36108. BUF_MEM* buf = NULL;
  36109. ExpectNotNull(buf = BUF_MEM_new());
  36110. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  36111. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  36112. BUF_MEM_free(buf);
  36113. #endif
  36114. return EXPECT_RESULT();
  36115. }
  36116. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36117. static int stub_rand_seed(const void *buf, int num)
  36118. {
  36119. (void)buf;
  36120. (void)num;
  36121. return 123;
  36122. }
  36123. static int stub_rand_bytes(unsigned char *buf, int num)
  36124. {
  36125. (void)buf;
  36126. (void)num;
  36127. return 456;
  36128. }
  36129. static byte* was_stub_rand_cleanup_called(void)
  36130. {
  36131. static byte was_called = 0;
  36132. return &was_called;
  36133. }
  36134. static void stub_rand_cleanup(void)
  36135. {
  36136. byte* was_called = was_stub_rand_cleanup_called();
  36137. *was_called = 1;
  36138. return;
  36139. }
  36140. static byte* was_stub_rand_add_called(void)
  36141. {
  36142. static byte was_called = 0;
  36143. return &was_called;
  36144. }
  36145. static int stub_rand_add(const void *buf, int num, double entropy)
  36146. {
  36147. byte* was_called = was_stub_rand_add_called();
  36148. (void)buf;
  36149. (void)num;
  36150. (void)entropy;
  36151. *was_called = 1;
  36152. return 0;
  36153. }
  36154. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  36155. {
  36156. (void)buf;
  36157. (void)num;
  36158. return 9876;
  36159. }
  36160. static int stub_rand_status(void)
  36161. {
  36162. return 5432;
  36163. }
  36164. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36165. static int test_wolfSSL_RAND_set_rand_method(void)
  36166. {
  36167. EXPECT_DECLS;
  36168. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36169. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  36170. unsigned char* buf = NULL;
  36171. int num = 0;
  36172. double entropy = 0;
  36173. int ret;
  36174. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  36175. byte* was_add_called = was_stub_rand_add_called();
  36176. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  36177. DYNAMIC_TYPE_TMP_BUFFER));
  36178. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  36179. ExpectIntEQ(*was_cleanup_called, 0);
  36180. RAND_cleanup();
  36181. ExpectIntEQ(*was_cleanup_called, 0);
  36182. rand_methods.seed = &stub_rand_seed;
  36183. rand_methods.bytes = &stub_rand_bytes;
  36184. rand_methods.cleanup = &stub_rand_cleanup;
  36185. rand_methods.add = &stub_rand_add;
  36186. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  36187. rand_methods.status = &stub_rand_status;
  36188. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  36189. ExpectIntEQ(RAND_seed(buf, num), 123);
  36190. ExpectIntEQ(RAND_bytes(buf, num), 456);
  36191. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  36192. ExpectIntEQ(RAND_status(), 5432);
  36193. ExpectIntEQ(*was_add_called, 0);
  36194. /* The function pointer for RAND_add returns int, but RAND_add itself
  36195. * returns void. */
  36196. RAND_add(buf, num, entropy);
  36197. ExpectIntEQ(*was_add_called, 1);
  36198. was_add_called = 0;
  36199. ExpectIntEQ(*was_cleanup_called, 0);
  36200. RAND_cleanup();
  36201. ExpectIntEQ(*was_cleanup_called, 1);
  36202. *was_cleanup_called = 0;
  36203. ret = RAND_set_rand_method(NULL);
  36204. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  36205. ExpectIntNE(RAND_status(), 5432);
  36206. ExpectIntEQ(*was_cleanup_called, 0);
  36207. RAND_cleanup();
  36208. ExpectIntEQ(*was_cleanup_called, 0);
  36209. RAND_set_rand_method(NULL);
  36210. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36211. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36212. return EXPECT_RESULT();
  36213. }
  36214. static int test_wolfSSL_RAND_bytes(void)
  36215. {
  36216. EXPECT_DECLS;
  36217. #if defined(OPENSSL_EXTRA)
  36218. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  36219. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  36220. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  36221. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  36222. int max_bufsize;
  36223. byte *my_buf = NULL;
  36224. /* sanity check */
  36225. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  36226. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  36227. max_bufsize = size4;
  36228. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  36229. DYNAMIC_TYPE_TMP_BUFFER));
  36230. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  36231. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  36232. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  36233. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  36234. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  36235. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  36236. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  36237. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36238. #endif
  36239. return EXPECT_RESULT();
  36240. }
  36241. static int test_wolfSSL_RAND(void)
  36242. {
  36243. EXPECT_DECLS;
  36244. #if defined(OPENSSL_EXTRA)
  36245. byte seed[16];
  36246. XMEMSET(seed, 0, sizeof(seed));
  36247. /* No global methods set. */
  36248. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  36249. ExpectIntEQ(RAND_poll(), 1);
  36250. RAND_cleanup();
  36251. ExpectIntEQ(RAND_egd(NULL), -1);
  36252. #ifndef NO_FILESYSTEM
  36253. {
  36254. char fname[100];
  36255. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  36256. ExpectIntEQ(RAND_write_file(NULL), 0);
  36257. }
  36258. #endif
  36259. #endif
  36260. return EXPECT_RESULT();
  36261. }
  36262. static int test_wolfSSL_PKCS8_Compat(void)
  36263. {
  36264. EXPECT_DECLS;
  36265. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  36266. !defined(NO_BIO)
  36267. PKCS8_PRIV_KEY_INFO* pt = NULL;
  36268. BIO* bio = NULL;
  36269. XFILE f = XBADFILE;
  36270. int bytes;
  36271. char pkcs8_buffer[512];
  36272. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36273. EVP_PKEY *pkey = NULL;
  36274. #endif
  36275. /* file from wolfssl/certs/ directory */
  36276. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  36277. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  36278. 0);
  36279. if (f != XBADFILE)
  36280. XFCLOSE(f);
  36281. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36282. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  36283. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36284. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  36285. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  36286. /* gets PKCS8 pointer to pkey */
  36287. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  36288. EVP_PKEY_free(pkey);
  36289. #endif
  36290. BIO_free(bio);
  36291. PKCS8_PRIV_KEY_INFO_free(pt);
  36292. #endif
  36293. return EXPECT_RESULT();
  36294. }
  36295. static int test_wolfSSL_PKCS8_d2i(void)
  36296. {
  36297. EXPECT_DECLS;
  36298. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  36299. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  36300. * requires a 12 byte password in FIPS mode. This test ends up
  36301. * trying to use an 8 byte password. */
  36302. #ifndef NO_FILESYSTEM
  36303. unsigned char pkcs8_buffer[2048];
  36304. const unsigned char* p = NULL;
  36305. int bytes = 0;
  36306. XFILE file = XBADFILE;
  36307. WOLFSSL_EVP_PKEY* pkey = NULL;
  36308. #ifndef NO_BIO
  36309. BIO* bio = NULL;
  36310. #if defined(OPENSSL_ALL) && \
  36311. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  36312. defined(HAVE_ECC)) && \
  36313. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36314. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  36315. #endif
  36316. #endif
  36317. #ifndef NO_RSA
  36318. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  36319. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  36320. #ifndef NO_DES3
  36321. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  36322. #endif
  36323. #endif /* NO_RSA */
  36324. #ifdef HAVE_ECC
  36325. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  36326. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  36327. #ifndef NO_DES3
  36328. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  36329. #endif
  36330. #endif /* HAVE_ECC */
  36331. #endif /* !NO_FILESYSTEM */
  36332. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  36333. #ifndef NO_RSA
  36334. #ifdef USE_CERT_BUFFERS_1024
  36335. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  36336. int rsaSz = sizeof_server_key_der_1024;
  36337. #else
  36338. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  36339. int rsaSz = sizeof_server_key_der_2048;
  36340. #endif
  36341. #endif
  36342. #ifdef HAVE_ECC
  36343. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  36344. int ecSz = sizeof_ecc_key_der_256;
  36345. #endif
  36346. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  36347. #ifndef NO_FILESYSTEM
  36348. (void)pkcs8_buffer;
  36349. (void)p;
  36350. (void)bytes;
  36351. (void)file;
  36352. #ifndef NO_BIO
  36353. (void)bio;
  36354. #endif
  36355. #endif
  36356. #ifdef OPENSSL_ALL
  36357. #ifndef NO_RSA
  36358. /* Try to auto-detect normal RSA private key */
  36359. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  36360. EVP_PKEY_free(pkey);
  36361. pkey = NULL;
  36362. #endif
  36363. #ifdef HAVE_ECC
  36364. /* Try to auto-detect normal EC private key */
  36365. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  36366. EVP_PKEY_free(pkey);
  36367. pkey = NULL;
  36368. #endif
  36369. #endif /* OPENSSL_ALL */
  36370. #ifndef NO_FILESYSTEM
  36371. #ifndef NO_RSA
  36372. /* Get DER encoded RSA PKCS#8 data. */
  36373. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  36374. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36375. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36376. file)), 0);
  36377. if (file != XBADFILE) {
  36378. XFCLOSE(file);
  36379. file = XBADFILE;
  36380. }
  36381. p = pkcs8_buffer;
  36382. #ifdef OPENSSL_ALL
  36383. /* Try to decode - auto-detect key type. */
  36384. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  36385. #else
  36386. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  36387. #endif
  36388. /* Get PEM encoded RSA PKCS#8 data. */
  36389. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  36390. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36391. file)), 0);
  36392. if (file != XBADFILE) {
  36393. XFCLOSE(file);
  36394. file = XBADFILE;
  36395. }
  36396. #if defined(OPENSSL_ALL) && \
  36397. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36398. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36399. /* Write PKCS#8 PEM to BIO. */
  36400. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  36401. NULL), bytes);
  36402. /* Compare file and written data */
  36403. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  36404. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  36405. BIO_free(bio);
  36406. bio = NULL;
  36407. #if !defined(NO_DES3) && !defined(NO_SHA)
  36408. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36409. /* Write Encrypted PKCS#8 PEM to BIO. */
  36410. bytes = 1834;
  36411. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  36412. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  36413. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  36414. (void*)"yassl123"));
  36415. EVP_PKEY_free(evpPkey);
  36416. evpPkey = NULL;
  36417. BIO_free(bio);
  36418. bio = NULL;
  36419. #endif /* !NO_DES3 && !NO_SHA */
  36420. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  36421. EVP_PKEY_free(pkey);
  36422. pkey = NULL;
  36423. /* PKCS#8 encrypted RSA key */
  36424. #ifndef NO_DES3
  36425. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  36426. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36427. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36428. file)), 0);
  36429. if (file != XBADFILE) {
  36430. XFCLOSE(file);
  36431. file = XBADFILE;
  36432. }
  36433. #if defined(OPENSSL_ALL) && \
  36434. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36435. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36436. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  36437. (void*)"yassl123"));
  36438. EVP_PKEY_free(pkey);
  36439. pkey = NULL;
  36440. BIO_free(bio);
  36441. bio = NULL;
  36442. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  36443. #endif /* !NO_DES3 */
  36444. #endif /* NO_RSA */
  36445. #ifdef HAVE_ECC
  36446. /* PKCS#8 encode EC key */
  36447. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  36448. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36449. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36450. file)), 0);
  36451. if (file != XBADFILE) {
  36452. XFCLOSE(file);
  36453. file = XBADFILE;
  36454. }
  36455. p = pkcs8_buffer;
  36456. #ifdef OPENSSL_ALL
  36457. /* Try to decode - auto-detect key type. */
  36458. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  36459. #else
  36460. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  36461. #endif
  36462. /* Get PEM encoded RSA PKCS#8 data. */
  36463. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  36464. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36465. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36466. file)), 0);
  36467. if (file != XBADFILE) {
  36468. XFCLOSE(file);
  36469. file = XBADFILE;
  36470. }
  36471. #if defined(OPENSSL_ALL) && \
  36472. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  36473. defined(HAVE_AES_CBC)
  36474. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36475. /* Write PKCS#8 PEM to BIO. */
  36476. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  36477. NULL), bytes);
  36478. /* Compare file and written data */
  36479. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  36480. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  36481. BIO_free(bio);
  36482. bio = NULL;
  36483. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36484. /* Write Encrypted PKCS#8 PEM to BIO. */
  36485. bytes = 379;
  36486. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  36487. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  36488. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  36489. (void*)"yassl123"));
  36490. EVP_PKEY_free(evpPkey);
  36491. evpPkey = NULL;
  36492. BIO_free(bio);
  36493. bio = NULL;
  36494. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  36495. EVP_PKEY_free(pkey);
  36496. pkey = NULL;
  36497. /* PKCS#8 encrypted EC key */
  36498. #ifndef NO_DES3
  36499. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  36500. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36501. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36502. file)), 0);
  36503. if (file != XBADFILE) {
  36504. XFCLOSE(file);
  36505. file = XBADFILE;
  36506. }
  36507. #if defined(OPENSSL_ALL) && \
  36508. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36509. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36510. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  36511. (void*)"yassl123"));
  36512. EVP_PKEY_free(pkey);
  36513. pkey = NULL;
  36514. BIO_free(bio);
  36515. bio = NULL;
  36516. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  36517. #endif /* !NO_DES3 */
  36518. #endif /* HAVE_ECC */
  36519. #endif /* !NO_FILESYSTEM */
  36520. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  36521. return EXPECT_RESULT();
  36522. }
  36523. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  36524. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  36525. #define LOGGING_THREADS 5
  36526. #define ERROR_COUNT 10
  36527. /* copied from logging.c since this is not exposed otherwise */
  36528. #ifndef ERROR_QUEUE_MAX
  36529. #ifdef ERROR_QUEUE_PER_THREAD
  36530. #define ERROR_QUEUE_MAX 16
  36531. #else
  36532. /* this breaks from compat of unlimited error queue size */
  36533. #define ERROR_QUEUE_MAX 100
  36534. #endif
  36535. #endif
  36536. static volatile int loggingThreadsReady;
  36537. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  36538. {
  36539. const char* file;
  36540. int line;
  36541. unsigned long err;
  36542. int errorCount = 0;
  36543. int i;
  36544. (void)args;
  36545. while (!loggingThreadsReady);
  36546. for (i = 0; i < ERROR_COUNT; i++)
  36547. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  36548. while ((err = ERR_get_error_line(&file, &line))) {
  36549. AssertIntEQ(err, 990 + errorCount);
  36550. errorCount++;
  36551. }
  36552. AssertIntEQ(errorCount, ERROR_COUNT);
  36553. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  36554. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  36555. errorCount = 0;
  36556. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  36557. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  36558. while ((err = ERR_get_error_line(&file, &line))) {
  36559. AssertIntEQ(err, 990 + errorCount);
  36560. errorCount++;
  36561. }
  36562. /* test that the 3 errors over the max were dropped */
  36563. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  36564. WOLFSSL_RETURN_FROM_THREAD(0);
  36565. }
  36566. #endif
  36567. static int test_error_queue_per_thread(void)
  36568. {
  36569. int res = TEST_SKIPPED;
  36570. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  36571. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  36572. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  36573. int i;
  36574. ERR_clear_error(); /* clear out any error nodes */
  36575. loggingThreadsReady = 0;
  36576. for (i = 0; i < LOGGING_THREADS; i++)
  36577. start_thread(test_logging, NULL, &loggingThreads[i]);
  36578. loggingThreadsReady = 1;
  36579. for (i = 0; i < LOGGING_THREADS; i++)
  36580. join_thread(loggingThreads[i]);
  36581. res = TEST_SUCCESS;
  36582. #endif
  36583. return res;
  36584. }
  36585. static int test_wolfSSL_ERR_put_error(void)
  36586. {
  36587. EXPECT_DECLS;
  36588. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36589. defined(DEBUG_WOLFSSL)
  36590. const char* file;
  36591. int line;
  36592. ERR_clear_error(); /* clear out any error nodes */
  36593. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  36594. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36595. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  36596. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  36597. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  36598. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  36599. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  36600. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  36601. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  36602. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  36603. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  36604. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  36605. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  36606. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  36607. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  36608. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  36609. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  36610. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  36611. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  36612. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  36613. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  36614. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  36615. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  36616. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  36617. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  36618. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  36619. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  36620. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  36621. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  36622. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  36623. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  36624. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  36625. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  36626. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  36627. "this file", 100);
  36628. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  36629. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  36630. ExpectIntEQ(line, 100);
  36631. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  36632. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  36633. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  36634. #endif
  36635. /* try reading past end of error queue */
  36636. file = NULL;
  36637. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36638. ExpectNull(file);
  36639. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  36640. PEMerr(4,4);
  36641. ExpectIntEQ(ERR_get_error(), 4);
  36642. /* Empty and free up all error nodes */
  36643. ERR_clear_error();
  36644. /* Verify all nodes are cleared */
  36645. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  36646. ERR_clear_error();
  36647. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36648. #endif
  36649. return EXPECT_RESULT();
  36650. }
  36651. /*
  36652. * This is a regression test for a bug where the peek/get error functions were
  36653. * drawing from the end of the queue rather than the front.
  36654. */
  36655. static int test_wolfSSL_ERR_get_error_order(void)
  36656. {
  36657. EXPECT_DECLS;
  36658. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  36659. /* Empty the queue. */
  36660. wolfSSL_ERR_clear_error();
  36661. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  36662. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  36663. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  36664. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  36665. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  36666. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  36667. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  36668. return EXPECT_RESULT();
  36669. }
  36670. #ifndef NO_BIO
  36671. static int test_wolfSSL_ERR_print_errors(void)
  36672. {
  36673. EXPECT_DECLS;
  36674. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36675. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  36676. BIO* bio = NULL;
  36677. char buf[1024];
  36678. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36679. ERR_clear_error(); /* clear out any error nodes */
  36680. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  36681. /* Choosing -600 as an unused errno. */
  36682. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  36683. ERR_print_errors(bio);
  36684. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  36685. ExpectIntEQ(XSTRNCMP(
  36686. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  36687. buf, 55), 0);
  36688. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  36689. ExpectIntEQ(XSTRNCMP(
  36690. "error:600:wolfSSL library:unknown error number:asn.c:100",
  36691. buf, 56), 0);
  36692. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  36693. ExpectIntEQ(buf[0], '\0');
  36694. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  36695. BIO_free(bio);
  36696. #endif
  36697. return EXPECT_RESULT();
  36698. }
  36699. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36700. defined(DEBUG_WOLFSSL)
  36701. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  36702. {
  36703. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  36704. return 0;
  36705. }
  36706. #endif
  36707. static int test_wolfSSL_ERR_print_errors_cb(void)
  36708. {
  36709. EXPECT_DECLS;
  36710. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36711. defined(DEBUG_WOLFSSL)
  36712. BIO* bio = NULL;
  36713. char buf[1024];
  36714. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36715. ERR_clear_error(); /* clear out any error nodes */
  36716. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  36717. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  36718. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  36719. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  36720. ExpectIntEQ(XSTRNCMP(
  36721. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  36722. buf, 53), 0);
  36723. ExpectIntEQ(XSTRNCMP(
  36724. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  36725. buf + 53, 55), 0);
  36726. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  36727. BIO_free(bio);
  36728. #endif
  36729. return EXPECT_RESULT();
  36730. }
  36731. /*
  36732. * Testing WOLFSSL_ERROR_MSG
  36733. */
  36734. static int test_WOLFSSL_ERROR_MSG(void)
  36735. {
  36736. int res = TEST_SKIPPED;
  36737. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  36738. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  36739. const char* msg = TEST_STRING;
  36740. WOLFSSL_ERROR_MSG(msg);
  36741. res = TEST_SUCCESS;
  36742. #endif
  36743. return res;
  36744. } /* End test_WOLFSSL_ERROR_MSG */
  36745. /*
  36746. * Testing wc_ERR_remove_state
  36747. */
  36748. static int test_wc_ERR_remove_state(void)
  36749. {
  36750. int res = TEST_SKIPPED;
  36751. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  36752. wc_ERR_remove_state();
  36753. res = TEST_SUCCESS;
  36754. #endif
  36755. return res;
  36756. } /* End test_wc_ERR_remove_state */
  36757. /*
  36758. * Testing wc_ERR_print_errors_fp
  36759. */
  36760. static int test_wc_ERR_print_errors_fp(void)
  36761. {
  36762. EXPECT_DECLS;
  36763. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  36764. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  36765. long sz;
  36766. XFILE fp = XBADFILE;
  36767. WOLFSSL_ERROR(BAD_FUNC_ARG);
  36768. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  36769. XBADFILE);
  36770. wc_ERR_print_errors_fp(fp);
  36771. #if defined(DEBUG_WOLFSSL)
  36772. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  36773. #ifdef NO_ERROR_QUEUE
  36774. ExpectIntEQ(sz = XFTELL(fp), 0);
  36775. #else
  36776. ExpectIntNE(sz = XFTELL(fp), 0);
  36777. #endif
  36778. #endif
  36779. if (fp != XBADFILE)
  36780. XFCLOSE(fp);
  36781. (void)sz;
  36782. #endif
  36783. return EXPECT_RESULT();
  36784. } /* End test_wc_ERR_print_errors_fp */
  36785. #ifdef DEBUG_WOLFSSL
  36786. static void Logging_cb(const int logLevel, const char *const logMessage)
  36787. {
  36788. (void)logLevel;
  36789. (void)logMessage;
  36790. }
  36791. #endif
  36792. /*
  36793. * Testing wolfSSL_GetLoggingCb
  36794. */
  36795. static int test_wolfSSL_GetLoggingCb(void)
  36796. {
  36797. EXPECT_DECLS;
  36798. #ifdef DEBUG_WOLFSSL
  36799. /* Testing without wolfSSL_SetLoggingCb() */
  36800. ExpectNull(wolfSSL_GetLoggingCb());
  36801. /* Testing with wolfSSL_SetLoggingCb() */
  36802. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  36803. ExpectNotNull(wolfSSL_GetLoggingCb());
  36804. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  36805. #endif
  36806. ExpectNull(wolfSSL_GetLoggingCb());
  36807. return EXPECT_RESULT();
  36808. } /* End test_wolfSSL_GetLoggingCb */
  36809. #endif /* !NO_BIO */
  36810. static int test_wolfSSL_MD4(void)
  36811. {
  36812. EXPECT_DECLS;
  36813. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36814. MD4_CTX md4;
  36815. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36816. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36817. "789012345678901234567890";
  36818. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36819. "\xcc\x05\x36";
  36820. int msgSz = (int)XSTRLEN(msg);
  36821. XMEMSET(out, 0, sizeof(out));
  36822. MD4_Init(&md4);
  36823. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36824. MD4_Final(out, &md4);
  36825. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36826. #endif
  36827. return EXPECT_RESULT();
  36828. }
  36829. static int test_wolfSSL_MD5(void)
  36830. {
  36831. EXPECT_DECLS;
  36832. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36833. byte input1[] = "";
  36834. byte input2[] = "message digest";
  36835. byte hash[WC_MD5_DIGEST_SIZE];
  36836. unsigned char output1[] =
  36837. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  36838. unsigned char output2[] =
  36839. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  36840. WOLFSSL_MD5_CTX md5;
  36841. XMEMSET(&md5, 0, sizeof(md5));
  36842. /* Test cases for illegal parameters */
  36843. ExpectIntEQ(MD5_Init(NULL), 0);
  36844. ExpectIntEQ(MD5_Init(&md5), 1);
  36845. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  36846. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  36847. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  36848. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  36849. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  36850. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  36851. /* Init MD5 CTX */
  36852. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36853. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  36854. 1);
  36855. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36856. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36857. /* Init MD5 CTX */
  36858. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36859. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  36860. (int)XSTRLEN((const char*)input2)), 1);
  36861. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36862. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36863. #if !defined(NO_OLD_NAMES) && \
  36864. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  36865. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  36866. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  36867. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  36868. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  36869. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  36870. &hash);
  36871. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36872. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  36873. &hash);
  36874. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36875. {
  36876. byte data[] = "Data to be hashed.";
  36877. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  36878. ExpectNotNull(MD5(data, sizeof(data), NULL));
  36879. ExpectNotNull(MD5(data, sizeof(data), hash));
  36880. ExpectNotNull(MD5(NULL, 0, hash));
  36881. ExpectNull(MD5(NULL, sizeof(data), hash));
  36882. }
  36883. #endif
  36884. #endif
  36885. return EXPECT_RESULT();
  36886. }
  36887. static int test_wolfSSL_MD5_Transform(void)
  36888. {
  36889. EXPECT_DECLS;
  36890. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36891. byte input1[] = "";
  36892. byte input2[] = "abc";
  36893. byte local[WC_MD5_BLOCK_SIZE];
  36894. word32 sLen = 0;
  36895. #ifdef BIG_ENDIAN_ORDER
  36896. unsigned char output1[] =
  36897. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  36898. unsigned char output2[] =
  36899. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  36900. #else
  36901. unsigned char output1[] =
  36902. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  36903. unsigned char output2[] =
  36904. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  36905. #endif
  36906. union {
  36907. wc_Md5 native;
  36908. MD5_CTX compat;
  36909. } md5;
  36910. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  36911. XMEMSET(&local, 0, sizeof(local));
  36912. /* sanity check */
  36913. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  36914. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  36915. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  36916. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  36917. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36918. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  36919. /* Init MD5 CTX */
  36920. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  36921. /* Do Transform*/
  36922. sLen = (word32)XSTRLEN((char*)input1);
  36923. XMEMCPY(local, input1, sLen);
  36924. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36925. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  36926. /* Init MD5 CTX */
  36927. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  36928. sLen = (word32)XSTRLEN((char*)input2);
  36929. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  36930. XMEMCPY(local, input2, sLen);
  36931. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36932. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  36933. #endif
  36934. return EXPECT_RESULT();
  36935. }
  36936. static int test_wolfSSL_SHA(void)
  36937. {
  36938. EXPECT_DECLS;
  36939. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  36940. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  36941. (!defined(HAVE_FIPS) || \
  36942. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36943. {
  36944. const unsigned char in[] = "abc";
  36945. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  36946. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  36947. unsigned char out[WC_SHA_DIGEST_SIZE];
  36948. unsigned char* p = NULL;
  36949. WOLFSSL_SHA_CTX sha;
  36950. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36951. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  36952. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36953. /* SHA interface test */
  36954. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36955. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  36956. ExpectNotNull(SHA(in, 0, out));
  36957. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  36958. ExpectNotNull(SHA(NULL, 0, out));
  36959. ExpectNotNull(SHA(NULL, 0, NULL));
  36960. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  36961. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36962. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  36963. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  36964. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  36965. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  36966. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  36967. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36968. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  36969. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  36970. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  36971. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36972. }
  36973. #endif
  36974. #if !defined(NO_SHA256)
  36975. {
  36976. const unsigned char in[] = "abc";
  36977. unsigned char expected[] =
  36978. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  36979. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  36980. "\x15\xAD";
  36981. unsigned char out[WC_SHA256_DIGEST_SIZE];
  36982. unsigned char* p = NULL;
  36983. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  36984. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36985. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  36986. #else
  36987. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  36988. #endif
  36989. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  36990. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36991. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  36992. #else
  36993. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  36994. #endif
  36995. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  36996. }
  36997. #endif
  36998. #if defined(WOLFSSL_SHA384)
  36999. {
  37000. const unsigned char in[] = "abc";
  37001. unsigned char expected[] =
  37002. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37003. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37004. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37005. "\xc8\x25\xa7";
  37006. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37007. unsigned char* p = NULL;
  37008. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37009. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37010. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37011. #else
  37012. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37013. #endif
  37014. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37015. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37016. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  37017. #else
  37018. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  37019. #endif
  37020. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  37021. }
  37022. #endif
  37023. #if defined(WOLFSSL_SHA512)
  37024. {
  37025. const unsigned char in[] = "abc";
  37026. unsigned char expected[] =
  37027. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37028. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37029. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37030. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37031. "\xa5\x4c\xa4\x9f";
  37032. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37033. unsigned char* p = NULL;
  37034. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37035. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37036. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37037. #else
  37038. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37039. #endif
  37040. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37041. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37042. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  37043. #else
  37044. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  37045. #endif
  37046. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  37047. }
  37048. #endif
  37049. #endif
  37050. return EXPECT_RESULT();
  37051. }
  37052. static int test_wolfSSL_SHA_Transform(void)
  37053. {
  37054. EXPECT_DECLS;
  37055. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37056. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37057. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37058. byte input1[] = "";
  37059. byte input2[] = "abc";
  37060. byte local[WC_SHA_BLOCK_SIZE];
  37061. word32 sLen = 0;
  37062. #ifdef BIG_ENDIAN_ORDER
  37063. unsigned char output1[] =
  37064. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37065. "\x09\xf7\x3a\x93";
  37066. unsigned char output2[] =
  37067. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37068. "\x7c\x6e\x08\xb8";
  37069. #else
  37070. unsigned char output1[] =
  37071. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37072. "\x93\x3a\xf7\x09";
  37073. unsigned char output2[] =
  37074. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37075. "\xb8\x08\x6e\x7c";
  37076. #endif
  37077. union {
  37078. wc_Sha native;
  37079. SHA_CTX compat;
  37080. } sha;
  37081. union {
  37082. wc_Sha native;
  37083. SHA_CTX compat;
  37084. } sha1;
  37085. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37086. XMEMSET(&local, 0, sizeof(local));
  37087. /* sanity check */
  37088. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  37089. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37090. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37091. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  37092. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37093. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37094. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37095. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37096. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37097. /* Init SHA CTX */
  37098. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37099. /* Do Transform*/
  37100. sLen = (word32)XSTRLEN((char*)input1);
  37101. XMEMCPY(local, input1, sLen);
  37102. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37103. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37104. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37105. /* Init SHA CTX */
  37106. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37107. sLen = (word32)XSTRLEN((char*)input2);
  37108. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37109. XMEMCPY(local, input2, sLen);
  37110. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37111. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37112. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37113. /* SHA1 */
  37114. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37115. /* Init SHA CTX */
  37116. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37117. /* Do Transform*/
  37118. sLen = (word32)XSTRLEN((char*)input1);
  37119. XMEMCPY(local, input1, sLen);
  37120. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37121. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37122. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  37123. /* Init SHA CTX */
  37124. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37125. sLen = (word32)XSTRLEN((char*)input2);
  37126. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37127. XMEMCPY(local, input2, sLen);
  37128. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37129. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37130. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37131. #endif
  37132. #endif
  37133. return EXPECT_RESULT();
  37134. }
  37135. static int test_wolfSSL_SHA224(void)
  37136. {
  37137. EXPECT_DECLS;
  37138. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37139. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37140. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37141. unsigned char input[] =
  37142. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37143. unsigned char output[] =
  37144. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37145. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37146. size_t inLen;
  37147. byte hash[WC_SHA224_DIGEST_SIZE];
  37148. unsigned char* p;
  37149. inLen = XSTRLEN((char*)input);
  37150. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37151. ExpectNull(SHA224(NULL, inLen, hash));
  37152. ExpectNotNull(SHA224(input, 0, hash));
  37153. ExpectNotNull(SHA224(input, inLen, NULL));
  37154. ExpectNotNull(SHA224(NULL, 0, hash));
  37155. ExpectNotNull(SHA224(NULL, 0, NULL));
  37156. ExpectNotNull(SHA224(input, inLen, hash));
  37157. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37158. ExpectNotNull(p = SHA224(input, inLen, NULL));
  37159. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  37160. #endif
  37161. return EXPECT_RESULT();
  37162. }
  37163. static int test_wolfSSL_SHA256(void)
  37164. {
  37165. EXPECT_DECLS;
  37166. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37167. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37168. unsigned char input[] =
  37169. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37170. unsigned char output[] =
  37171. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37172. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37173. "\x06\xC1";
  37174. size_t inLen;
  37175. byte hash[WC_SHA256_DIGEST_SIZE];
  37176. inLen = XSTRLEN((char*)input);
  37177. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37178. ExpectNotNull(SHA256(input, inLen, hash));
  37179. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37180. #endif
  37181. return EXPECT_RESULT();
  37182. }
  37183. static int test_wolfSSL_SHA256_Transform(void)
  37184. {
  37185. EXPECT_DECLS;
  37186. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37187. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37188. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37189. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37190. !defined(WOLFSSL_KCAPI_HASH)
  37191. byte input1[] = "";
  37192. byte input2[] = "abc";
  37193. byte local[WC_SHA256_BLOCK_SIZE];
  37194. word32 sLen = 0;
  37195. #ifdef BIG_ENDIAN_ORDER
  37196. unsigned char output1[] =
  37197. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37198. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37199. unsigned char output2[] =
  37200. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37201. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37202. #else
  37203. unsigned char output1[] =
  37204. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37205. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37206. unsigned char output2[] =
  37207. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37208. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37209. #endif
  37210. union {
  37211. wc_Sha256 native;
  37212. SHA256_CTX compat;
  37213. } sha256;
  37214. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37215. XMEMSET(&local, 0, sizeof(local));
  37216. /* sanity check */
  37217. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  37218. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37219. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37220. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37221. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37222. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37223. /* Init SHA256 CTX */
  37224. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37225. /* Do Transform*/
  37226. sLen = (word32)XSTRLEN((char*)input1);
  37227. XMEMCPY(local, input1, sLen);
  37228. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37229. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  37230. 0);
  37231. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37232. /* Init SHA256 CTX */
  37233. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37234. sLen = (word32)XSTRLEN((char*)input2);
  37235. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37236. XMEMCPY(local, input2, sLen);
  37237. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37238. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  37239. 0);
  37240. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37241. #endif
  37242. #endif
  37243. return EXPECT_RESULT();
  37244. }
  37245. static int test_wolfSSL_SHA512_Transform(void)
  37246. {
  37247. EXPECT_DECLS;
  37248. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37249. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37250. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37251. !defined(WOLFSSL_KCAPI_HASH)
  37252. byte input1[] = "";
  37253. byte input2[] = "abc";
  37254. byte local[WC_SHA512_BLOCK_SIZE];
  37255. word32 sLen = 0;
  37256. #ifdef BIG_ENDIAN_ORDER
  37257. unsigned char output1[] =
  37258. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37259. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37260. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37261. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37262. unsigned char output2[] =
  37263. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37264. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37265. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37266. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37267. #else
  37268. unsigned char output1[] =
  37269. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37270. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37271. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37272. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37273. unsigned char output2[] =
  37274. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37275. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37276. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37277. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37278. #endif
  37279. union {
  37280. wc_Sha512 native;
  37281. SHA512_CTX compat;
  37282. } sha512;
  37283. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37284. XMEMSET(&local, 0, sizeof(local));
  37285. /* sanity check */
  37286. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  37287. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37288. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37289. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37290. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37291. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37292. /* Init SHA512 CTX */
  37293. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37294. /* Do Transform*/
  37295. sLen = (word32)XSTRLEN((char*)input1);
  37296. XMEMCPY(local, input1, sLen);
  37297. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37298. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37299. WC_SHA512_DIGEST_SIZE), 0);
  37300. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37301. /* Init SHA512 CTX */
  37302. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  37303. sLen = (word32)XSTRLEN((char*)input2);
  37304. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37305. XMEMCPY(local, input2, sLen);
  37306. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37307. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37308. WC_SHA512_DIGEST_SIZE), 0);
  37309. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37310. (void)input1;
  37311. #endif
  37312. #endif
  37313. return EXPECT_RESULT();
  37314. }
  37315. static int test_wolfSSL_SHA512_224_Transform(void)
  37316. {
  37317. EXPECT_DECLS;
  37318. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  37319. !defined(WOLFSSL_NOSHA512_224)
  37320. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37321. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37322. !defined(WOLFSSL_KCAPI_HASH)
  37323. byte input1[] = "";
  37324. byte input2[] = "abc";
  37325. byte local[WC_SHA512_BLOCK_SIZE];
  37326. word32 sLen = 0;
  37327. unsigned char output1[] =
  37328. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  37329. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  37330. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  37331. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  37332. unsigned char output2[] =
  37333. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  37334. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  37335. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  37336. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  37337. union {
  37338. wc_Sha512 native;
  37339. SHA512_CTX compat;
  37340. } sha512;
  37341. #ifdef BIG_ENDIAN_ORDER
  37342. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  37343. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  37344. #endif
  37345. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37346. XMEMSET(&local, 0, sizeof(local));
  37347. /* sanity check */
  37348. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  37349. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  37350. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  37351. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  37352. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  37353. BAD_FUNC_ARG);
  37354. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37355. /* Init SHA512 CTX */
  37356. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  37357. /* Do Transform*/
  37358. sLen = (word32)XSTRLEN((char*)input1);
  37359. XMEMCPY(local, input1, sLen);
  37360. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  37361. 1);
  37362. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37363. WC_SHA512_DIGEST_SIZE), 0);
  37364. /* frees resources */
  37365. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  37366. /* Init SHA512 CTX */
  37367. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  37368. sLen = (word32)XSTRLEN((char*)input2);
  37369. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37370. XMEMCPY(local, input2, sLen);
  37371. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  37372. 1);
  37373. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37374. WC_SHA512_DIGEST_SIZE), 0);
  37375. /* frees resources */
  37376. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  37377. #endif
  37378. #endif
  37379. return EXPECT_RESULT();
  37380. }
  37381. static int test_wolfSSL_SHA512_256_Transform(void)
  37382. {
  37383. EXPECT_DECLS;
  37384. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  37385. !defined(WOLFSSL_NOSHA512_256)
  37386. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37387. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37388. !defined(WOLFSSL_KCAPI_HASH)
  37389. byte input1[] = "";
  37390. byte input2[] = "abc";
  37391. byte local[WC_SHA512_BLOCK_SIZE];
  37392. word32 sLen = 0;
  37393. unsigned char output1[] =
  37394. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  37395. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  37396. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  37397. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  37398. unsigned char output2[] =
  37399. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  37400. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  37401. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  37402. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  37403. union {
  37404. wc_Sha512 native;
  37405. SHA512_CTX compat;
  37406. } sha512;
  37407. #ifdef BIG_ENDIAN_ORDER
  37408. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  37409. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  37410. #endif
  37411. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37412. XMEMSET(&local, 0, sizeof(local));
  37413. /* sanity check */
  37414. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  37415. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  37416. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  37417. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  37418. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  37419. BAD_FUNC_ARG);
  37420. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37421. /* Init SHA512 CTX */
  37422. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  37423. /* Do Transform*/
  37424. sLen = (word32)XSTRLEN((char*)input1);
  37425. XMEMCPY(local, input1, sLen);
  37426. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  37427. 1);
  37428. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37429. WC_SHA512_DIGEST_SIZE), 0);
  37430. /* frees resources */
  37431. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  37432. /* Init SHA512 CTX */
  37433. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  37434. sLen = (word32)XSTRLEN((char*)input2);
  37435. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37436. XMEMCPY(local, input2, sLen);
  37437. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  37438. 1);
  37439. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37440. WC_SHA512_DIGEST_SIZE), 0);
  37441. /* frees resources */
  37442. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  37443. #endif
  37444. #endif
  37445. return EXPECT_RESULT();
  37446. }
  37447. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  37448. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  37449. * buffer of 64 bytes.
  37450. *
  37451. * returns the size of the digest buffer on success and a negative value on
  37452. * failure.
  37453. */
  37454. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  37455. int* sz)
  37456. {
  37457. EXPECT_DECLS;
  37458. HMAC_CTX ctx1;
  37459. HMAC_CTX ctx2;
  37460. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  37461. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  37462. unsigned char long_key[] =
  37463. "0123456789012345678901234567890123456789"
  37464. "0123456789012345678901234567890123456789"
  37465. "0123456789012345678901234567890123456789"
  37466. "0123456789012345678901234567890123456789";
  37467. unsigned char msg[] = "message to hash";
  37468. unsigned int digestSz = 64;
  37469. int keySz = sizeof(key);
  37470. int long_keySz = sizeof(long_key);
  37471. int msgSz = sizeof(msg);
  37472. unsigned char digest2[64];
  37473. unsigned int digestSz2 = 64;
  37474. HMAC_CTX_init(&ctx1);
  37475. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37476. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37477. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37478. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37479. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37480. HMAC_CTX_cleanup(&ctx1);
  37481. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37482. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  37483. HMAC_CTX_cleanup(&ctx2);
  37484. ExpectIntEQ(digestSz, digestSz2);
  37485. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37486. /* test HMAC_Init with NULL key */
  37487. /* init after copy */
  37488. HMAC_CTX_init(&ctx1);
  37489. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37490. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37491. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37492. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37493. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37494. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37495. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37496. HMAC_CTX_cleanup(&ctx1);
  37497. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  37498. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37499. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37500. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37501. HMAC_CTX_cleanup(&ctx2);
  37502. ExpectIntEQ(digestSz, digestSz2);
  37503. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37504. /* long key */
  37505. HMAC_CTX_init(&ctx1);
  37506. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  37507. SSL_SUCCESS);
  37508. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37509. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37510. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37511. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37512. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37513. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37514. HMAC_CTX_cleanup(&ctx1);
  37515. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  37516. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37517. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37518. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37519. HMAC_CTX_cleanup(&ctx2);
  37520. ExpectIntEQ(digestSz, digestSz2);
  37521. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37522. /* init before copy */
  37523. HMAC_CTX_init(&ctx1);
  37524. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37525. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37526. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37527. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37528. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37529. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37530. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37531. HMAC_CTX_cleanup(&ctx1);
  37532. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37533. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37534. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37535. HMAC_CTX_cleanup(&ctx2);
  37536. ExpectIntEQ(digestSz, digestSz2);
  37537. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37538. *sz = digestSz;
  37539. return EXPECT_RESULT();
  37540. }
  37541. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  37542. static int test_wolfSSL_HMAC_CTX(void)
  37543. {
  37544. EXPECT_DECLS;
  37545. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  37546. unsigned char digest[64];
  37547. int digestSz;
  37548. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  37549. WOLFSSL_HMAC_CTX ctx1;
  37550. WOLFSSL_HMAC_CTX ctx2;
  37551. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  37552. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  37553. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  37554. wolfSSL_HMAC_CTX_free(NULL);
  37555. wolfSSL_HMAC_CTX_free(hmac_ctx);
  37556. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  37557. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  37558. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  37559. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  37560. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  37561. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  37562. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  37563. ((! defined(HAVE_FIPS_VERSION)) || \
  37564. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  37565. /* Copy object that hasn't had a digest set - MD5. */
  37566. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  37567. #else
  37568. /* Copy object that hasn't had a digest set. */
  37569. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  37570. #endif
  37571. HMAC_CTX_cleanup(NULL);
  37572. HMAC_CTX_cleanup(&ctx2);
  37573. ExpectNull(HMAC_CTX_get_md(NULL));
  37574. #ifndef NO_SHA
  37575. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  37576. TEST_SUCCESS);
  37577. ExpectIntEQ(digestSz, 20);
  37578. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  37579. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  37580. #endif /* !NO_SHA */
  37581. #ifdef WOLFSSL_SHA224
  37582. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  37583. TEST_SUCCESS);
  37584. ExpectIntEQ(digestSz, 28);
  37585. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  37586. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  37587. "\x02\x0E", digest, digestSz), 0);
  37588. #endif /* WOLFSSL_SHA224 */
  37589. #ifndef NO_SHA256
  37590. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  37591. TEST_SUCCESS);
  37592. ExpectIntEQ(digestSz, 32);
  37593. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  37594. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  37595. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  37596. #endif /* !NO_SHA256 */
  37597. #ifdef WOLFSSL_SHA384
  37598. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  37599. TEST_SUCCESS);
  37600. ExpectIntEQ(digestSz, 48);
  37601. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  37602. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  37603. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  37604. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  37605. digest, digestSz), 0);
  37606. #endif /* WOLFSSL_SHA384 */
  37607. #ifdef WOLFSSL_SHA512
  37608. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  37609. TEST_SUCCESS);
  37610. ExpectIntEQ(digestSz, 64);
  37611. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  37612. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  37613. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  37614. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  37615. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  37616. digest, digestSz), 0);
  37617. #endif /* WOLFSSL_SHA512 */
  37618. #ifdef WOLFSSL_SHA3
  37619. #ifndef WOLFSSL_NOSHA3_224
  37620. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  37621. TEST_SUCCESS);
  37622. ExpectIntEQ(digestSz, 28);
  37623. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  37624. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  37625. "\x29\x0f", digest, digestSz), 0);
  37626. #endif
  37627. #ifndef WOLFSSL_NOSHA3_256
  37628. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  37629. TEST_SUCCESS);
  37630. ExpectIntEQ(digestSz, 32);
  37631. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  37632. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  37633. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  37634. #endif
  37635. #ifndef WOLFSSL_NOSHA3_384
  37636. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  37637. TEST_SUCCESS);
  37638. ExpectIntEQ(digestSz, 48);
  37639. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  37640. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  37641. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  37642. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  37643. digest, digestSz), 0);
  37644. #endif
  37645. #ifndef WOLFSSL_NOSHA3_512
  37646. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  37647. TEST_SUCCESS);
  37648. ExpectIntEQ(digestSz, 64);
  37649. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  37650. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  37651. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  37652. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  37653. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  37654. digest, digestSz), 0);
  37655. #endif
  37656. #endif
  37657. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  37658. HAVE_FIPS_VERSION <= 2)
  37659. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  37660. TEST_SUCCESS);
  37661. ExpectIntEQ(digestSz, 16);
  37662. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  37663. "\xE4\x98\xDD", digest, digestSz), 0);
  37664. #endif /* !NO_MD5 */
  37665. #endif
  37666. return EXPECT_RESULT();
  37667. }
  37668. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37669. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37670. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37671. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  37672. {
  37673. EXPECT_DECLS;
  37674. static const unsigned char key[] = "simple test key";
  37675. HMAC_CTX* hmac = NULL;
  37676. ENGINE* e = NULL;
  37677. unsigned char hash[WC_MAX_DIGEST_SIZE];
  37678. unsigned int len;
  37679. ExpectNotNull(hmac = HMAC_CTX_new());
  37680. HMAC_CTX_init(hmac);
  37681. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  37682. ((! defined(HAVE_FIPS_VERSION)) || \
  37683. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  37684. /* Get size on object that hasn't had a digest set - MD5. */
  37685. ExpectIntEQ(HMAC_size(hmac), 16);
  37686. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  37687. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  37688. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  37689. #else
  37690. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  37691. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  37692. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  37693. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  37694. #endif
  37695. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  37696. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  37697. /* re-using test key as data to hash */
  37698. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  37699. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  37700. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  37701. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  37702. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  37703. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  37704. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  37705. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  37706. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  37707. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  37708. ExpectIntEQ(len, md_len);
  37709. ExpectIntEQ(HMAC_size(NULL), 0);
  37710. ExpectIntEQ(HMAC_size(hmac), md_len);
  37711. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  37712. HMAC_cleanup(NULL);
  37713. HMAC_cleanup(hmac);
  37714. HMAC_CTX_free(hmac);
  37715. len = 0;
  37716. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  37717. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  37718. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  37719. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  37720. ExpectIntEQ(len, md_len);
  37721. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  37722. /* With data. */
  37723. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  37724. &len));
  37725. /* With NULL data. */
  37726. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  37727. &len));
  37728. /* With zero length data. */
  37729. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  37730. return EXPECT_RESULT();
  37731. }
  37732. #endif
  37733. static int test_wolfSSL_HMAC(void)
  37734. {
  37735. EXPECT_DECLS;
  37736. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37737. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37738. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37739. #ifndef NO_SHA256
  37740. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  37741. TEST_SUCCESS);
  37742. #endif
  37743. #ifdef WOLFSSL_SHA224
  37744. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  37745. TEST_SUCCESS);
  37746. #endif
  37747. #ifdef WOLFSSL_SHA384
  37748. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  37749. TEST_SUCCESS);
  37750. #endif
  37751. #ifdef WOLFSSL_SHA512
  37752. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  37753. TEST_SUCCESS);
  37754. #endif
  37755. #ifdef WOLFSSL_SHA3
  37756. #ifndef WOLFSSL_NOSHA3_224
  37757. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  37758. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  37759. #endif
  37760. #ifndef WOLFSSL_NOSHA3_256
  37761. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  37762. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  37763. #endif
  37764. #ifndef WOLFSSL_NOSHA3_384
  37765. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  37766. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  37767. #endif
  37768. #ifndef WOLFSSL_NOSHA3_512
  37769. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  37770. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  37771. #endif
  37772. #endif
  37773. #ifndef NO_SHA
  37774. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  37775. TEST_SUCCESS);
  37776. #endif
  37777. #endif
  37778. return EXPECT_RESULT();
  37779. }
  37780. static int test_wolfSSL_CMAC(void)
  37781. {
  37782. EXPECT_DECLS;
  37783. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  37784. defined(WOLFSSL_AES_DIRECT)
  37785. int i;
  37786. byte key[AES_256_KEY_SIZE];
  37787. CMAC_CTX* cmacCtx = NULL;
  37788. byte out[AES_BLOCK_SIZE];
  37789. size_t outLen = AES_BLOCK_SIZE;
  37790. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  37791. key[i] = i;
  37792. }
  37793. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37794. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  37795. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37796. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37797. NULL), 1);
  37798. /* re-using test key as data to hash */
  37799. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37800. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  37801. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  37802. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  37803. /* No Update works. */
  37804. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37805. NULL), 1);
  37806. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37807. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37808. NULL), 1);
  37809. /* Test parameters with CMAC_Update. */
  37810. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  37811. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  37812. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  37813. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  37814. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  37815. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  37816. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  37817. /* Test parameters with CMAC_Final. */
  37818. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  37819. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  37820. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  37821. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  37822. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  37823. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  37824. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37825. CMAC_CTX_free(cmacCtx);
  37826. /* Test parameters with CMAC Init. */
  37827. cmacCtx = NULL;
  37828. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37829. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37830. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  37831. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37832. NULL), 0);
  37833. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37834. NULL), 0);
  37835. /* give a key too small for the cipher, verify we get failure */
  37836. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  37837. NULL), 0);
  37838. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  37839. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  37840. /* Only AES-CBC supported. */
  37841. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  37842. NULL), 0);
  37843. #endif
  37844. CMAC_CTX_free(cmacCtx);
  37845. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  37846. cmacCtx = NULL;
  37847. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37848. /* No Init. */
  37849. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  37850. CMAC_CTX_free(cmacCtx);
  37851. /* Test AES-256-CBC */
  37852. cmacCtx = NULL;
  37853. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37854. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  37855. NULL), 1);
  37856. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37857. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37858. CMAC_CTX_free(cmacCtx);
  37859. /* Test AES-192-CBC */
  37860. cmacCtx = NULL;
  37861. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37862. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37863. NULL), 1);
  37864. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37865. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37866. CMAC_CTX_free(cmacCtx);
  37867. cmacCtx = NULL;
  37868. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37869. CMAC_CTX_free(cmacCtx);
  37870. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  37871. return EXPECT_RESULT();
  37872. }
  37873. static int test_wolfSSL_DES(void)
  37874. {
  37875. EXPECT_DECLS;
  37876. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37877. const_DES_cblock myDes;
  37878. DES_cblock iv;
  37879. DES_key_schedule key;
  37880. word32 i = 0;
  37881. DES_LONG dl = 0;
  37882. unsigned char msg[] = "hello wolfssl";
  37883. unsigned char weakKey[][8] = {
  37884. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  37885. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  37886. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  37887. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  37888. };
  37889. unsigned char semiWeakKey[][8] = {
  37890. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  37891. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  37892. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  37893. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  37894. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  37895. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  37896. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  37897. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  37898. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  37899. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  37900. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  37901. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  37902. };
  37903. DES_check_key(1);
  37904. DES_set_key(&myDes, &key);
  37905. /* check, check of odd parity */
  37906. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  37907. myDes[0] = 6; /* set even parity */
  37908. XMEMSET(key, 5, sizeof(DES_key_schedule));
  37909. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  37910. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  37911. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  37912. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  37913. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  37914. /* set odd parity for success case */
  37915. DES_set_odd_parity(&myDes);
  37916. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  37917. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  37918. myDes[3]);
  37919. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  37920. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37921. ExpectIntEQ(key[i], myDes[i]);
  37922. }
  37923. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  37924. /* check weak key */
  37925. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  37926. XMEMSET(key, 5, sizeof(DES_key_schedule));
  37927. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  37928. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  37929. DES_set_key_unchecked(NULL, NULL);
  37930. DES_set_key_unchecked(&myDes, NULL);
  37931. DES_set_key_unchecked(NULL, &key);
  37932. /* compare arrays, should be the same */
  37933. /* now do unchecked copy of a weak key over */
  37934. DES_set_key_unchecked(&myDes, &key);
  37935. /* compare arrays, should be the same */
  37936. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37937. ExpectIntEQ(key[i], myDes[i]);
  37938. }
  37939. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  37940. myDes[7] = 2;
  37941. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  37942. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  37943. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  37944. /* Test all weak keys. */
  37945. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  37946. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  37947. }
  37948. /* Test all semi-weak keys. */
  37949. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  37950. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  37951. }
  37952. /* check DES_key_sched API */
  37953. XMEMSET(key, 1, sizeof(DES_key_schedule));
  37954. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  37955. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  37956. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  37957. /* compare arrays, should be the same */
  37958. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37959. ExpectIntEQ(key[i], myDes[i]);
  37960. }
  37961. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  37962. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  37963. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  37964. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  37965. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  37966. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  37967. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  37968. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  37969. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  37970. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  37971. * DES_cbc_encrypt on the input */
  37972. XMEMSET(iv, 0, sizeof(DES_cblock));
  37973. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  37974. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  37975. ExpectIntEQ(dl, 480052723);
  37976. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  37977. return EXPECT_RESULT();
  37978. }
  37979. static int test_wolfSSL_DES_ncbc(void)
  37980. {
  37981. EXPECT_DECLS;
  37982. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37983. const_DES_cblock myDes;
  37984. DES_cblock iv = {1};
  37985. DES_key_schedule key = {0};
  37986. unsigned char msg[] = "hello wolfssl";
  37987. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  37988. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  37989. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  37990. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  37991. /* partial block test */
  37992. DES_set_key(&key, &myDes);
  37993. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  37994. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  37995. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37996. DES_set_key(&key, &myDes);
  37997. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37998. *((byte*)&iv) = 1;
  37999. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38000. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38001. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38002. /* full block test */
  38003. DES_set_key(&key, &myDes);
  38004. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38005. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38006. *((byte*)&iv) = 1;
  38007. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38008. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38009. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38010. DES_set_key(&key, &myDes);
  38011. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38012. *((byte*)&iv) = 1;
  38013. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38014. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38015. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38016. #endif
  38017. return EXPECT_RESULT();
  38018. }
  38019. static int test_wolfSSL_DES_ecb_encrypt(void)
  38020. {
  38021. EXPECT_DECLS;
  38022. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  38023. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  38024. WOLFSSL_DES_key_schedule key;
  38025. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38026. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  38027. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  38028. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  38029. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  38030. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  38031. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  38032. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  38033. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  38034. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  38035. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  38036. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  38037. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  38038. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  38039. /* Encrypt messages */
  38040. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  38041. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  38042. {
  38043. /* Decrypt messages */
  38044. int ret1 = 0;
  38045. int ret2 = 0;
  38046. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  38047. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  38048. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  38049. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  38050. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  38051. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  38052. }
  38053. #endif
  38054. return EXPECT_RESULT();
  38055. }
  38056. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  38057. {
  38058. EXPECT_DECLS;
  38059. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38060. unsigned char input1[8], input2[8];
  38061. unsigned char output1[8], output2[8];
  38062. unsigned char back1[8], back2[8];
  38063. WOLFSSL_DES_cblock iv1, iv2;
  38064. WOLFSSL_DES_key_schedule key1, key2, key3;
  38065. int i;
  38066. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38067. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  38068. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  38069. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  38070. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  38071. XMEMSET(output1, 0, sizeof(output1));
  38072. XMEMSET(output2, 0, sizeof(output2));
  38073. XMEMSET(back1, 0, sizeof(back1));
  38074. XMEMSET(back2, 0, sizeof(back2));
  38075. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38076. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38077. /* Encrypt messages */
  38078. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  38079. DES_ENCRYPT);
  38080. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  38081. DES_ENCRYPT);
  38082. {
  38083. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38084. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38085. /* Decrypt messages */
  38086. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  38087. &iv1, DES_DECRYPT);
  38088. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  38089. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  38090. &iv2, DES_DECRYPT);
  38091. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  38092. }
  38093. for (i = 0; i < 8; i++) {
  38094. XMEMSET(output1, 0, sizeof(output1));
  38095. XMEMSET(output2, 0, sizeof(output2));
  38096. XMEMSET(back1, 0, sizeof(back1));
  38097. XMEMSET(back2, 0, sizeof(back2));
  38098. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38099. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38100. /* Encrypt partial messages */
  38101. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  38102. &iv1, DES_ENCRYPT);
  38103. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  38104. &iv2, DES_ENCRYPT);
  38105. {
  38106. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38107. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38108. /* Decrypt messages */
  38109. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  38110. &key3, &iv1, DES_DECRYPT);
  38111. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  38112. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  38113. &key3, &iv2, DES_DECRYPT);
  38114. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  38115. }
  38116. }
  38117. #endif
  38118. return EXPECT_RESULT();
  38119. }
  38120. static int test_wolfSSL_AES_encrypt(void)
  38121. {
  38122. EXPECT_DECLS;
  38123. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38124. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38125. AES_KEY enc;
  38126. AES_KEY dec;
  38127. const byte msg[] = {
  38128. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38129. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  38130. };
  38131. const byte exp[] = {
  38132. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  38133. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  38134. };
  38135. const byte key[] = {
  38136. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38137. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38138. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38139. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38140. };
  38141. byte eout[sizeof(msg)];
  38142. byte dout[sizeof(msg)];
  38143. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  38144. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  38145. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  38146. wolfSSL_AES_encrypt(msg, NULL, NULL);
  38147. wolfSSL_AES_encrypt(NULL, eout, NULL);
  38148. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  38149. wolfSSL_AES_encrypt(msg, eout, NULL);
  38150. wolfSSL_AES_encrypt(msg, NULL, &enc);
  38151. wolfSSL_AES_encrypt(NULL, eout, &enc);
  38152. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  38153. wolfSSL_AES_decrypt(eout, NULL, NULL);
  38154. wolfSSL_AES_decrypt(NULL, dout, NULL);
  38155. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  38156. wolfSSL_AES_decrypt(eout, dout, NULL);
  38157. wolfSSL_AES_decrypt(eout, NULL, &dec);
  38158. wolfSSL_AES_decrypt(NULL, dout, &dec);
  38159. wolfSSL_AES_encrypt(msg, eout, &enc);
  38160. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  38161. wolfSSL_AES_decrypt(eout, dout, &dec);
  38162. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  38163. #endif
  38164. return EXPECT_RESULT();
  38165. }
  38166. static int test_wolfSSL_AES_ecb_encrypt(void)
  38167. {
  38168. EXPECT_DECLS;
  38169. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38170. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38171. AES_KEY aes;
  38172. const byte msg[] =
  38173. {
  38174. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38175. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  38176. };
  38177. const byte verify[] =
  38178. {
  38179. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  38180. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  38181. };
  38182. const byte key[] =
  38183. {
  38184. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38185. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38186. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38187. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  38188. };
  38189. byte out[AES_BLOCK_SIZE];
  38190. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  38191. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38192. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  38193. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  38194. #ifdef HAVE_AES_DECRYPT
  38195. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  38196. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38197. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  38198. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  38199. #endif
  38200. /* test bad arguments */
  38201. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  38202. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  38203. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  38204. #endif
  38205. return EXPECT_RESULT();
  38206. }
  38207. static int test_wolfSSL_AES_cbc_encrypt(void)
  38208. {
  38209. EXPECT_DECLS;
  38210. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38211. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38212. AES_KEY aes;
  38213. AES_KEY* aesN = NULL;
  38214. size_t len = 0;
  38215. size_t lenB = 0;
  38216. int keySz0 = 0;
  38217. int keySzN = -1;
  38218. byte out[AES_BLOCK_SIZE] = {0};
  38219. byte* outN = NULL;
  38220. /* Test vectors retrieved from:
  38221. * <begin URL>
  38222. * https://csrc.nist.gov/
  38223. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38224. * documents/aes/KAT_AES.zip
  38225. * </end URL>
  38226. */
  38227. const byte* pt128N = NULL;
  38228. byte* key128N = NULL;
  38229. byte* iv128N = NULL;
  38230. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38231. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38232. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38233. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38234. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38235. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38236. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38237. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38238. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38239. len = sizeof(pt128);
  38240. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38241. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38242. ExpectIntNE(XMEMCMP(b, g, h), i)
  38243. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38244. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38245. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38246. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38247. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38248. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38249. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38250. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38251. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38252. /* Stressing wolfSSL_AES_set_encrypt_key */
  38253. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38254. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38255. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38256. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38257. /* Stressing wolfSSL_AES_set_decrypt_key */
  38258. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38259. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38260. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38261. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38262. #ifdef WOLFSSL_AES_128
  38263. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38264. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38265. RESET_IV(iv128tmp, iv128);
  38266. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38267. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38268. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38269. wc_AesFree((Aes*)&aes);
  38270. #ifdef HAVE_AES_DECRYPT
  38271. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38272. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38273. RESET_IV(iv128tmp, iv128);
  38274. len = sizeof(ct128);
  38275. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38276. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38277. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38278. wc_AesFree((Aes*)&aes);
  38279. #endif
  38280. #endif /* WOLFSSL_AES_128 */
  38281. #ifdef WOLFSSL_AES_192
  38282. {
  38283. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38284. * Appendix F.2.3 */
  38285. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38286. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38287. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38288. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38289. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38290. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38291. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38292. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38293. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38294. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38295. len = sizeof(pt192);
  38296. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38297. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38298. RESET_IV(iv192tmp, iv192);
  38299. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38300. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38301. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38302. wc_AesFree((Aes*)&aes);
  38303. #ifdef HAVE_AES_DECRYPT
  38304. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38305. len = sizeof(ct192);
  38306. RESET_IV(iv192tmp, iv192);
  38307. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38308. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38309. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38310. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38311. wc_AesFree((Aes*)&aes);
  38312. #endif
  38313. }
  38314. #endif /* WOLFSSL_AES_192 */
  38315. #ifdef WOLFSSL_AES_256
  38316. {
  38317. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38318. * Appendix F.2.5 */
  38319. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38320. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38321. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38322. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38323. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38324. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38325. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38326. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38327. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38328. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38329. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38330. len = sizeof(pt256);
  38331. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38332. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38333. RESET_IV(iv256tmp, iv256);
  38334. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38335. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38336. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38337. wc_AesFree((Aes*)&aes);
  38338. #ifdef HAVE_AES_DECRYPT
  38339. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38340. len = sizeof(ct256);
  38341. RESET_IV(iv256tmp, iv256);
  38342. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38343. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38344. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  38345. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  38346. wc_AesFree((Aes*)&aes);
  38347. #endif
  38348. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  38349. !defined(HAVE_SELFTEST)
  38350. {
  38351. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  38352. byte wrapPlain[sizeof(key256)] = { 0 };
  38353. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  38354. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  38355. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38356. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38357. 15), WOLFSSL_FAILURE);
  38358. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38359. sizeof(key256)), sizeof(wrapCipher));
  38360. wc_AesFree((Aes*)&aes);
  38361. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  38362. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38363. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38364. 23), WOLFSSL_FAILURE);
  38365. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38366. sizeof(wrapCipher)), sizeof(wrapPlain));
  38367. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38368. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  38369. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  38370. wc_AesFree((Aes*)&aes);
  38371. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  38372. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38373. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  38374. sizeof(key256)), sizeof(wrapCipher));
  38375. wc_AesFree((Aes*)&aes);
  38376. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  38377. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38378. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  38379. sizeof(wrapCipher)), sizeof(wrapPlain));
  38380. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38381. wc_AesFree((Aes*)&aes);
  38382. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  38383. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  38384. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  38385. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  38386. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  38387. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  38388. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  38389. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  38390. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  38391. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  38392. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  38393. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  38394. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  38395. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  38396. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  38397. 0);
  38398. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  38399. 0);
  38400. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  38401. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  38402. }
  38403. #endif /* HAVE_AES_KEYWRAP */
  38404. }
  38405. #endif /* WOLFSSL_AES_256 */
  38406. #endif
  38407. return EXPECT_RESULT();
  38408. }
  38409. static int test_wolfSSL_AES_cfb128_encrypt(void)
  38410. {
  38411. EXPECT_DECLS;
  38412. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  38413. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38414. AES_KEY aesEnc;
  38415. AES_KEY aesDec;
  38416. const byte msg[] = {
  38417. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38418. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  38419. };
  38420. const byte exp[] = {
  38421. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  38422. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  38423. };
  38424. const byte key[] = {
  38425. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38426. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38427. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38428. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38429. };
  38430. const byte ivData[] = {
  38431. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38432. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38433. };
  38434. byte out[AES_BLOCK_SIZE];
  38435. byte iv[AES_BLOCK_SIZE];
  38436. word32 i;
  38437. int num;
  38438. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  38439. XMEMCPY(iv, ivData, sizeof(iv));
  38440. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38441. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  38442. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  38443. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38444. #ifdef HAVE_AES_DECRYPT
  38445. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  38446. XMEMCPY(iv, ivData, sizeof(iv));
  38447. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38448. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  38449. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  38450. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38451. #endif
  38452. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  38453. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  38454. XMEMCPY(iv, ivData, sizeof(iv));
  38455. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38456. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  38457. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  38458. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  38459. if (i == 0) {
  38460. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38461. }
  38462. else {
  38463. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38464. }
  38465. #ifdef HAVE_AES_DECRYPT
  38466. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  38467. XMEMCPY(iv, ivData, sizeof(iv));
  38468. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38469. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  38470. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  38471. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  38472. if (i == 0) {
  38473. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38474. }
  38475. else {
  38476. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38477. }
  38478. #endif
  38479. }
  38480. if (EXPECT_SUCCESS()) {
  38481. /* test bad arguments */
  38482. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  38483. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  38484. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  38485. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  38486. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  38487. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  38488. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  38489. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  38490. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  38491. }
  38492. #endif
  38493. return EXPECT_RESULT();
  38494. }
  38495. static int test_wolfSSL_CRYPTO_cts128(void)
  38496. {
  38497. EXPECT_DECLS;
  38498. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38499. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38500. byte tmp[64]; /* Largest vector size */
  38501. /* Test vectors taken form RFC3962 Appendix B */
  38502. const testVector vects[] = {
  38503. {
  38504. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38505. "\x20",
  38506. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  38507. "\x97",
  38508. 17, 17
  38509. },
  38510. {
  38511. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38512. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  38513. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  38514. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  38515. 31, 31
  38516. },
  38517. {
  38518. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38519. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38520. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38521. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38522. 32, 32
  38523. },
  38524. {
  38525. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38526. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38527. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38528. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38529. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38530. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38531. 47, 47
  38532. },
  38533. {
  38534. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38535. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38536. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38537. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38538. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38539. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38540. 48, 48
  38541. },
  38542. {
  38543. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38544. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38545. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38546. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38547. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38548. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38549. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38550. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38551. 64, 64
  38552. }
  38553. };
  38554. byte keyBytes[AES_128_KEY_SIZE] = {
  38555. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38556. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38557. };
  38558. size_t i;
  38559. AES_KEY encKey;
  38560. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38561. XMEMSET(tmp, 0, sizeof(tmp));
  38562. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38563. AES_KEY decKey;
  38564. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38565. &encKey), 0);
  38566. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38567. &decKey), 0);
  38568. XMEMSET(iv, 0, sizeof(iv));
  38569. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38570. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38571. vects[i].outLen);
  38572. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38573. XMEMSET(iv, 0, sizeof(iv));
  38574. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38575. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38576. vects[i].inLen);
  38577. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38578. }
  38579. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  38580. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  38581. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  38582. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  38583. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  38584. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  38585. (cbc128_f)AES_cbc_encrypt), 0);
  38586. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  38587. (cbc128_f)AES_cbc_encrypt), 0);
  38588. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  38589. (cbc128_f)AES_cbc_encrypt), 0);
  38590. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  38591. (cbc128_f)AES_cbc_encrypt), 0);
  38592. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  38593. (cbc128_f)AES_cbc_encrypt), 0);
  38594. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  38595. /* Length too small. */
  38596. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  38597. (cbc128_f)AES_cbc_encrypt), 0);
  38598. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  38599. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  38600. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  38601. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  38602. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  38603. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  38604. (cbc128_f)AES_cbc_encrypt), 0);
  38605. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  38606. (cbc128_f)AES_cbc_encrypt), 0);
  38607. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  38608. (cbc128_f)AES_cbc_encrypt), 0);
  38609. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  38610. (cbc128_f)AES_cbc_encrypt), 0);
  38611. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  38612. (cbc128_f)AES_cbc_encrypt), 0);
  38613. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  38614. /* Length too small. */
  38615. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  38616. (cbc128_f)AES_cbc_encrypt), 0);
  38617. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38618. return EXPECT_RESULT();
  38619. }
  38620. static int test_wolfSSL_RC4(void)
  38621. {
  38622. EXPECT_DECLS;
  38623. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  38624. WOLFSSL_RC4_KEY rc4Key;
  38625. unsigned char key[] = {
  38626. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38627. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38628. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38629. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38630. };
  38631. unsigned char data[] = {
  38632. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38633. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38634. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38635. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38636. };
  38637. unsigned char enc[sizeof(data)];
  38638. unsigned char dec[sizeof(data)];
  38639. word32 i;
  38640. word32 j;
  38641. wolfSSL_RC4_set_key(NULL, -1, NULL);
  38642. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  38643. wolfSSL_RC4_set_key(NULL, 0, NULL);
  38644. wolfSSL_RC4_set_key(NULL, -1, key);
  38645. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  38646. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  38647. wolfSSL_RC4_set_key(NULL, 0, key);
  38648. wolfSSL_RC4(NULL, 0, NULL, NULL);
  38649. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  38650. wolfSSL_RC4(NULL, 0, data, NULL);
  38651. wolfSSL_RC4(NULL, 0, NULL, enc);
  38652. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  38653. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  38654. wolfSSL_RC4(NULL, 0, data, enc);
  38655. ExpectIntEQ(1, 1);
  38656. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  38657. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  38658. XMEMSET(enc, 0, sizeof(enc));
  38659. XMEMSET(dec, 0, sizeof(dec));
  38660. /* Encrypt */
  38661. wolfSSL_RC4_set_key(&rc4Key, i, key);
  38662. wolfSSL_RC4(&rc4Key, j, data, enc);
  38663. /* Decrypt */
  38664. wolfSSL_RC4_set_key(&rc4Key, i, key);
  38665. wolfSSL_RC4(&rc4Key, j, enc, dec);
  38666. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  38667. }
  38668. }
  38669. #endif
  38670. return EXPECT_RESULT();
  38671. }
  38672. static int test_wolfSSL_OBJ(void)
  38673. {
  38674. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  38675. * mode
  38676. */
  38677. EXPECT_DECLS;
  38678. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  38679. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  38680. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  38681. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  38682. ASN1_OBJECT *obj = NULL;
  38683. ASN1_OBJECT *obj2 = NULL;
  38684. char buf[50];
  38685. XFILE fp = XBADFILE;
  38686. X509 *x509 = NULL;
  38687. X509_NAME *x509Name = NULL;
  38688. X509_NAME_ENTRY *x509NameEntry = NULL;
  38689. ASN1_OBJECT *asn1Name = NULL;
  38690. int numNames = 0;
  38691. BIO *bio = NULL;
  38692. int nid;
  38693. int i, j;
  38694. const char *f[] = {
  38695. #ifndef NO_RSA
  38696. "./certs/ca-cert.der",
  38697. #endif
  38698. #ifdef HAVE_ECC
  38699. "./certs/ca-ecc-cert.der",
  38700. "./certs/ca-ecc384-cert.der",
  38701. #endif
  38702. NULL};
  38703. ASN1_OBJECT *field_name_obj = NULL;
  38704. int lastpos = -1;
  38705. int tmp = -1;
  38706. ASN1_STRING *asn1 = NULL;
  38707. unsigned char *buf_dyn = NULL;
  38708. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  38709. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  38710. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  38711. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  38712. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  38713. ASN1_OBJECT_free(obj);
  38714. obj = NULL;
  38715. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  38716. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  38717. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  38718. #ifdef WOLFSSL_CERT_EXT
  38719. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  38720. #endif
  38721. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  38722. ExpectNotNull(obj2 = OBJ_dup(obj));
  38723. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  38724. ASN1_OBJECT_free(obj);
  38725. obj = NULL;
  38726. ASN1_OBJECT_free(obj2);
  38727. obj2 = NULL;
  38728. for (i = 0; f[i] != NULL; i++)
  38729. {
  38730. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  38731. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  38732. if (fp != XBADFILE) {
  38733. XFCLOSE(fp);
  38734. fp = XBADFILE;
  38735. }
  38736. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  38737. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  38738. /* Get the Common Name by using OBJ_txt2obj */
  38739. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  38740. do
  38741. {
  38742. lastpos = tmp;
  38743. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  38744. } while (tmp > -1);
  38745. ExpectIntNE(lastpos, -1);
  38746. ASN1_OBJECT_free(field_name_obj);
  38747. field_name_obj = NULL;
  38748. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  38749. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  38750. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  38751. /*
  38752. * All Common Names should be www.wolfssl.com
  38753. * This makes testing easier as we can test for the expected value.
  38754. */
  38755. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  38756. OPENSSL_free(buf_dyn);
  38757. buf_dyn = NULL;
  38758. bio = BIO_new(BIO_s_mem());
  38759. ExpectTrue(bio != NULL);
  38760. for (j = 0; j < numNames; j++)
  38761. {
  38762. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  38763. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  38764. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  38765. }
  38766. BIO_free(bio);
  38767. bio = NULL;
  38768. X509_free(x509);
  38769. x509 = NULL;
  38770. }
  38771. #ifdef HAVE_PKCS12
  38772. {
  38773. PKCS12 *p12 = NULL;
  38774. int boolRet;
  38775. EVP_PKEY *pkey = NULL;
  38776. const char *p12_f[] = {
  38777. #if !defined(NO_DES3) && !defined(NO_RSA)
  38778. "./certs/test-servercert.p12",
  38779. #endif
  38780. NULL};
  38781. for (i = 0; p12_f[i] != NULL; i++)
  38782. {
  38783. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  38784. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  38785. if (fp != XBADFILE) {
  38786. XFCLOSE(fp);
  38787. fp = XBADFILE;
  38788. }
  38789. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  38790. &pkey, &x509, NULL)) > 0);
  38791. wc_PKCS12_free(p12);
  38792. p12 = NULL;
  38793. EVP_PKEY_free(pkey);
  38794. x509Name = X509_get_issuer_name(x509);
  38795. ExpectNotNull(x509Name);
  38796. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  38797. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  38798. for (j = 0; j < numNames; j++)
  38799. {
  38800. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  38801. ExpectNotNull(asn1Name =
  38802. X509_NAME_ENTRY_get_object(x509NameEntry));
  38803. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  38804. }
  38805. BIO_free(bio);
  38806. bio = NULL;
  38807. X509_free(x509);
  38808. x509 = NULL;
  38809. }
  38810. }
  38811. #endif /* HAVE_PKCS12 */
  38812. #endif
  38813. return EXPECT_RESULT();
  38814. }
  38815. static int test_wolfSSL_OBJ_cmp(void)
  38816. {
  38817. EXPECT_DECLS;
  38818. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  38819. ASN1_OBJECT *obj = NULL;
  38820. ASN1_OBJECT *obj2 = NULL;
  38821. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  38822. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  38823. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  38824. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  38825. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  38826. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  38827. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  38828. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  38829. ASN1_OBJECT_free(obj);
  38830. ASN1_OBJECT_free(obj2);
  38831. #endif
  38832. return EXPECT_RESULT();
  38833. }
  38834. static int test_wolfSSL_OBJ_txt2nid(void)
  38835. {
  38836. EXPECT_DECLS;
  38837. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  38838. defined(WOLFSSL_APACHE_HTTPD)
  38839. int i;
  38840. static const struct {
  38841. const char* sn;
  38842. const char* ln;
  38843. const char* oid;
  38844. int nid;
  38845. } testVals[] = {
  38846. #ifdef WOLFSSL_APACHE_HTTPD
  38847. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  38848. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  38849. NID_id_on_dnsSRV },
  38850. { "msUPN", "Microsoft User Principal Name",
  38851. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  38852. #endif
  38853. { NULL, NULL, NULL, NID_undef }
  38854. };
  38855. /* Invalid cases */
  38856. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  38857. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  38858. /* Valid cases */
  38859. for (i = 0; testVals[i].sn != NULL; i++) {
  38860. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  38861. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  38862. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  38863. }
  38864. #endif
  38865. return EXPECT_RESULT();
  38866. }
  38867. static int test_wolfSSL_OBJ_txt2obj(void)
  38868. {
  38869. EXPECT_DECLS;
  38870. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  38871. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  38872. int i;
  38873. char buf[50];
  38874. ASN1_OBJECT* obj = NULL;
  38875. static const struct {
  38876. const char* oidStr;
  38877. const char* sn;
  38878. const char* ln;
  38879. } objs_list[] = {
  38880. #if defined(WOLFSSL_APACHE_HTTPD)
  38881. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  38882. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  38883. #endif
  38884. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  38885. { NULL, NULL, NULL }
  38886. };
  38887. static const struct {
  38888. const char* numeric;
  38889. const char* name;
  38890. } objs_named[] = {
  38891. /* In dictionary but not in normal list. */
  38892. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  38893. /* Made up OID. */
  38894. { "1.3.5.7", "1.3.5.7" },
  38895. { NULL, NULL }
  38896. };
  38897. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  38898. ASN1_OBJECT_free(obj);
  38899. obj = NULL;
  38900. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  38901. ASN1_OBJECT_free(obj);
  38902. obj = NULL;
  38903. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  38904. /* Test numerical value of oid (oidStr) */
  38905. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  38906. /* Convert object back to text to confirm oid is correct */
  38907. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38908. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38909. ASN1_OBJECT_free(obj);
  38910. obj = NULL;
  38911. XMEMSET(buf, 0, sizeof(buf));
  38912. /* Test short name (sn) */
  38913. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  38914. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  38915. /* Convert object back to text to confirm oid is correct */
  38916. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38917. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38918. ASN1_OBJECT_free(obj);
  38919. obj = NULL;
  38920. XMEMSET(buf, 0, sizeof(buf));
  38921. /* Test long name (ln) - should fail when no_name = 1 */
  38922. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  38923. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  38924. /* Convert object back to text to confirm oid is correct */
  38925. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38926. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38927. ASN1_OBJECT_free(obj);
  38928. obj = NULL;
  38929. XMEMSET(buf, 0, sizeof(buf));
  38930. }
  38931. for (i = 0; objs_named[i].numeric != NULL; i++) {
  38932. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  38933. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  38934. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  38935. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38936. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  38937. ASN1_OBJECT_free(obj);
  38938. obj = NULL;
  38939. }
  38940. #endif
  38941. return EXPECT_RESULT();
  38942. }
  38943. static int test_wolfSSL_PEM_write_bio_X509(void)
  38944. {
  38945. EXPECT_DECLS;
  38946. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  38947. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  38948. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  38949. !defined(NO_FILESYSTEM)
  38950. /* This test contains the hard coded expected
  38951. * lengths. Update if necessary */
  38952. XFILE fp = XBADFILE;
  38953. WOLFSSL_EVP_PKEY *priv = NULL;
  38954. BIO* input = NULL;
  38955. BIO* output = NULL;
  38956. X509* x509a = NULL;
  38957. X509* x509b = NULL;
  38958. ASN1_TIME* notBeforeA = NULL;
  38959. ASN1_TIME* notAfterA = NULL;
  38960. #ifndef NO_ASN_TIME
  38961. ASN1_TIME* notBeforeB = NULL;
  38962. ASN1_TIME* notAfterB = NULL;
  38963. #endif
  38964. int expectedLen;
  38965. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  38966. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  38967. if (fp != XBADFILE) {
  38968. XFCLOSE(fp);
  38969. fp = XBADFILE;
  38970. }
  38971. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  38972. "rb"));
  38973. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  38974. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  38975. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38976. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  38977. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  38978. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  38979. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38980. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38981. /* compare length against expected */
  38982. expectedLen = 2000;
  38983. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38984. #ifndef NO_ASN_TIME
  38985. /* read exported X509 PEM back into struct, sanity check on export,
  38986. * make sure notBefore/notAfter are the same and certs are identical. */
  38987. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38988. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  38989. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  38990. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  38991. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  38992. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38993. X509_free(x509b);
  38994. x509b = NULL;
  38995. #endif
  38996. /* Reset output buffer */
  38997. BIO_free(output);
  38998. output = NULL;
  38999. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39000. /* Test forcing the AKID to be generated just from KeyIdentifier */
  39001. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  39002. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  39003. x509a->authKeyId = x509a->authKeyIdSrc;
  39004. x509a->authKeyIdSrc = NULL;
  39005. x509a->authKeyIdSrcSz = 0;
  39006. }
  39007. /* Resign to re-generate the der */
  39008. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  39009. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39010. /* Check that we generate a smaller output since the AKID will
  39011. * only contain the KeyIdentifier without any additional
  39012. * information */
  39013. /* Here we copy the validity struct from the original */
  39014. expectedLen = 1688;
  39015. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39016. /* Reset buffers and x509 */
  39017. BIO_free(input);
  39018. input = NULL;
  39019. BIO_free(output);
  39020. output = NULL;
  39021. X509_free(x509a);
  39022. x509a = NULL;
  39023. /* test CA and basicConstSet values are encoded when
  39024. * the cert is a CA */
  39025. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  39026. /* read PEM into X509 struct */
  39027. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39028. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39029. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39030. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39031. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  39032. * values are maintained and certs are identical.*/
  39033. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39034. ExpectIntEQ(x509b->isCa, 1);
  39035. ExpectIntEQ(x509b->basicConstSet, 1);
  39036. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39037. X509_free(x509a);
  39038. x509a = NULL;
  39039. X509_free(x509b);
  39040. x509b = NULL;
  39041. BIO_free(input);
  39042. input = NULL;
  39043. BIO_free(output);
  39044. output = NULL;
  39045. /* test CA and basicConstSet values are encoded when
  39046. * the cert is not CA */
  39047. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  39048. /* read PEM into X509 struct */
  39049. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39050. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39051. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39052. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39053. /* read exported X509 PEM back into struct, ensure isCa and
  39054. * basicConstSet values are maintained and certs are identical */
  39055. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39056. ExpectIntEQ(x509b->isCa, 0);
  39057. ExpectIntEQ(x509b->basicConstSet, 1);
  39058. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39059. wolfSSL_EVP_PKEY_free(priv);
  39060. X509_free(x509a);
  39061. X509_free(x509b);
  39062. BIO_free(input);
  39063. BIO_free(output);
  39064. #endif
  39065. return EXPECT_RESULT();
  39066. }
  39067. static int test_wolfSSL_X509_NAME_ENTRY(void)
  39068. {
  39069. EXPECT_DECLS;
  39070. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  39071. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  39072. X509* x509 = NULL;
  39073. #ifndef NO_BIO
  39074. BIO* bio = NULL;
  39075. #endif
  39076. X509_NAME* nm = NULL;
  39077. X509_NAME_ENTRY* entry = NULL;
  39078. unsigned char cn[] = "another name to add";
  39079. #ifdef OPENSSL_ALL
  39080. int i;
  39081. int names_len = 0;
  39082. #endif
  39083. ExpectNotNull(x509 =
  39084. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39085. #ifndef NO_BIO
  39086. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39087. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  39088. #endif
  39089. #ifdef WOLFSSL_CERT_REQ
  39090. {
  39091. X509_REQ* req = NULL;
  39092. #ifndef NO_BIO
  39093. BIO* bReq = NULL;
  39094. #endif
  39095. ExpectNotNull(req =
  39096. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39097. #ifndef NO_BIO
  39098. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  39099. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  39100. BIO_free(bReq);
  39101. #endif
  39102. X509_free(req);
  39103. }
  39104. #endif
  39105. ExpectNotNull(nm = X509_get_subject_name(x509));
  39106. /* Test add entry */
  39107. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  39108. 0x0c, cn, (int)sizeof(cn)));
  39109. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39110. #ifdef WOLFSSL_CERT_EXT
  39111. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  39112. (byte*)"support@wolfssl.com", 19, -1,
  39113. 1), WOLFSSL_SUCCESS);
  39114. #endif
  39115. X509_NAME_ENTRY_free(entry);
  39116. entry = NULL;
  39117. #ifdef WOLFSSL_CERT_REQ
  39118. {
  39119. unsigned char srv_pkcs9p[] = "Server";
  39120. unsigned char fvrtDrnk[] = "tequila";
  39121. unsigned char* der = NULL;
  39122. char* subject = NULL;
  39123. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  39124. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  39125. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  39126. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  39127. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  39128. ExpectNotNull(der);
  39129. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  39130. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  39131. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  39132. #ifdef DEBUG_WOLFSSL
  39133. if (subject != NULL) {
  39134. fprintf(stderr, "\n\t%s\n", subject);
  39135. }
  39136. #endif
  39137. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  39138. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39139. }
  39140. #endif
  39141. /* Test add entry by text */
  39142. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  39143. 0x0c, cn, (int)sizeof(cn)));
  39144. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  39145. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  39146. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  39147. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  39148. #endif
  39149. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39150. X509_NAME_ENTRY_free(entry);
  39151. entry = NULL;
  39152. /* Test add entry by NID */
  39153. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  39154. cn, -1, -1, 0), SSL_SUCCESS);
  39155. #ifdef OPENSSL_ALL
  39156. /* stack of name entry */
  39157. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  39158. for (i = 0; i < names_len; i++) {
  39159. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  39160. }
  39161. #endif
  39162. #ifndef NO_BIO
  39163. BIO_free(bio);
  39164. #endif
  39165. X509_free(x509); /* free's nm */
  39166. #endif
  39167. return EXPECT_RESULT();
  39168. }
  39169. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39170. static int test_GENERAL_NAME_set0_othername(void) {
  39171. EXPECT_DECLS;
  39172. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39173. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39174. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39175. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39176. defined(WOLFSSL_FPKI)
  39177. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39178. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39179. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39180. const char * cert_fname = "./certs/server-cert.der";
  39181. const char * key_fname = "./certs/server-key.der";
  39182. X509* x509 = NULL;
  39183. GENERAL_NAME* gn = NULL;
  39184. GENERAL_NAMES* gns = NULL;
  39185. ASN1_OBJECT* upn_oid = NULL;
  39186. ASN1_UTF8STRING *utf8str = NULL;
  39187. ASN1_TYPE *value = NULL;
  39188. X509_EXTENSION * ext = NULL;
  39189. byte* pt = NULL;
  39190. byte der[4096];
  39191. int derSz = 0;
  39192. EVP_PKEY* priv = NULL;
  39193. XFILE f = XBADFILE;
  39194. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  39195. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  39196. if (f != XBADFILE) {
  39197. XFCLOSE(f);
  39198. f = XBADFILE;
  39199. }
  39200. ExpectNotNull(gn = GENERAL_NAME_new());
  39201. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39202. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39203. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39204. ExpectNotNull(value = ASN1_TYPE_new());
  39205. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39206. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  39207. wolfSSL_ASN1_STRING_free(utf8str);
  39208. }
  39209. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39210. if (EXPECT_FAIL()) {
  39211. ASN1_TYPE_free(value);
  39212. }
  39213. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39214. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39215. if (EXPECT_FAIL()) {
  39216. GENERAL_NAME_free(gn);
  39217. gn = NULL;
  39218. }
  39219. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39220. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  39221. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39222. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39223. if (f != XBADFILE) {
  39224. XFCLOSE(f);
  39225. f = XBADFILE;
  39226. }
  39227. pt = der;
  39228. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39229. (const unsigned char**)&pt, derSz));
  39230. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  39231. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39232. gns = NULL;
  39233. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39234. NULL));
  39235. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  39236. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  39237. ExpectIntEQ(gn->type, 0);
  39238. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39239. ASN1_OBJECT_free(upn_oid);
  39240. X509_EXTENSION_free(ext);
  39241. X509_free(x509);
  39242. EVP_PKEY_free(priv);
  39243. #endif
  39244. return EXPECT_RESULT();
  39245. }
  39246. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39247. static int test_othername_and_SID_ext(void) {
  39248. EXPECT_DECLS;
  39249. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39250. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39251. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39252. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39253. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  39254. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39255. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39256. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39257. const char* csr_fname = "./certs/csr.signed.der";
  39258. const char* key_fname = "./certs/server-key.der";
  39259. byte der[4096];
  39260. int derSz = 0;
  39261. X509_REQ* x509 = NULL;
  39262. STACK_OF(X509_EXTENSION) *exts = NULL;
  39263. X509_EXTENSION * san_ext = NULL;
  39264. X509_EXTENSION * ext = NULL;
  39265. GENERAL_NAME* gn = NULL;
  39266. GENERAL_NAMES* gns = NULL;
  39267. ASN1_OBJECT* upn_oid = NULL;
  39268. ASN1_UTF8STRING *utf8str = NULL;
  39269. ASN1_TYPE *value = NULL;
  39270. ASN1_STRING *extval = NULL;
  39271. /* SID extension. SID data format explained here:
  39272. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  39273. */
  39274. uint8_t SidExtension[] = {
  39275. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  39276. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  39277. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  39278. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  39279. uint8_t expectedAltName[] = {
  39280. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  39281. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  39282. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  39283. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  39284. X509_EXTENSION *sid_ext = NULL;
  39285. ASN1_OBJECT* sid_oid = NULL;
  39286. ASN1_OCTET_STRING *sid_data = NULL;
  39287. EVP_PKEY* priv = NULL;
  39288. XFILE f = XBADFILE;
  39289. byte* pt = NULL;
  39290. BIO* bio = NULL;
  39291. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  39292. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  39293. if (f != XBADFILE) {
  39294. XFCLOSE(f);
  39295. f = XBADFILE;
  39296. }
  39297. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  39298. ExpectNotNull(gn = GENERAL_NAME_new());
  39299. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39300. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39301. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39302. ExpectNotNull(value = ASN1_TYPE_new());
  39303. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39304. if (EXPECT_FAIL()) {
  39305. ASN1_UTF8STRING_free(utf8str);
  39306. }
  39307. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39308. if (EXPECT_FAIL()) {
  39309. ASN1_TYPE_free(value);
  39310. GENERAL_NAME_free(gn);
  39311. gn = NULL;
  39312. }
  39313. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39314. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39315. if (EXPECT_FAIL()) {
  39316. GENERAL_NAME_free(gn);
  39317. }
  39318. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39319. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  39320. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  39321. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  39322. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  39323. sid_data));
  39324. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  39325. /* Ensure an empty stack doesn't raise an error. */
  39326. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39327. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  39328. if (EXPECT_FAIL()) {
  39329. X509_EXTENSION_free(san_ext);
  39330. }
  39331. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  39332. if (EXPECT_FAIL()) {
  39333. X509_EXTENSION_free(sid_ext);
  39334. }
  39335. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39336. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39337. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39338. if (f != XBADFILE)
  39339. XFCLOSE(f);
  39340. pt = der;
  39341. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39342. (const unsigned char**)&pt, derSz));
  39343. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  39344. pt = der;
  39345. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  39346. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39347. gns = NULL;
  39348. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  39349. exts = NULL;
  39350. ASN1_OBJECT_free(upn_oid);
  39351. ASN1_OBJECT_free(sid_oid);
  39352. ASN1_OCTET_STRING_free(sid_data);
  39353. X509_REQ_free(x509);
  39354. x509 = NULL;
  39355. EVP_PKEY_free(priv);
  39356. /* At this point everything used to generate what is in der is cleaned up.
  39357. * We now read back from der to confirm the extensions were inserted
  39358. * correctly. */
  39359. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  39360. ExpectNotNull(bio);
  39361. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  39362. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  39363. ExpectNotNull(x509);
  39364. BIO_free(bio);
  39365. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  39366. x509));
  39367. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  39368. /* Check the SID extension. */
  39369. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  39370. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  39371. ExpectIntEQ(extval->length, sizeof(SidExtension));
  39372. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  39373. /* Check the AltNames extension. */
  39374. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  39375. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  39376. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  39377. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  39378. 0);
  39379. /* Cleanup */
  39380. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39381. NULL));
  39382. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  39383. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  39384. ExpectIntEQ(gn->type, 0);
  39385. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39386. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  39387. X509_REQ_free(x509);
  39388. #endif
  39389. return EXPECT_RESULT();
  39390. }
  39391. static int test_wolfSSL_X509_set_name(void)
  39392. {
  39393. EXPECT_DECLS;
  39394. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39395. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  39396. X509* x509 = NULL;
  39397. X509_NAME* name = NULL;
  39398. ExpectNotNull(name = X509_NAME_new());
  39399. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  39400. (byte*)"wolfssl.com", 11, 0, 1),
  39401. WOLFSSL_SUCCESS);
  39402. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  39403. (byte*)"support@wolfssl.com", 19, -1,
  39404. 1), WOLFSSL_SUCCESS);
  39405. ExpectNotNull(x509 = X509_new());
  39406. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  39407. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  39408. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  39409. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  39410. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  39411. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  39412. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  39413. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  39414. X509_free(x509);
  39415. X509_NAME_free(name);
  39416. #endif /* OPENSSL_ALL && !NO_CERTS */
  39417. return EXPECT_RESULT();
  39418. }
  39419. static int test_wolfSSL_X509_set_notAfter(void)
  39420. {
  39421. EXPECT_DECLS;
  39422. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  39423. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  39424. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  39425. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  39426. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  39427. /* Generalized time will overflow time_t if not long */
  39428. X509* x = NULL;
  39429. BIO* bio = NULL;
  39430. ASN1_TIME *asn_time = NULL;
  39431. ASN1_TIME *time_check = NULL;
  39432. const int year = 365*24*60*60;
  39433. const int day = 24*60*60;
  39434. const int hour = 60*60;
  39435. const int mini = 60;
  39436. int offset_day;
  39437. unsigned char buf[25];
  39438. time_t t;
  39439. /*
  39440. * Setup asn_time. APACHE HTTPD uses time(NULL)
  39441. */
  39442. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  39443. offset_day = 7;
  39444. /*
  39445. * Free these.
  39446. */
  39447. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  39448. ExpectNotNull(asn_time);
  39449. ExpectNotNull(x = X509_new());
  39450. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39451. /*
  39452. * Tests
  39453. */
  39454. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  39455. /* time_check is simply (ANS1_TIME*)x->notAfter */
  39456. ExpectNotNull(time_check = X509_get_notAfter(x));
  39457. /* ANS1_TIME_check validates by checking if argument can be parsed */
  39458. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  39459. /* Convert to human readable format and compare to intended date */
  39460. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  39461. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  39462. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  39463. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  39464. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  39465. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  39466. /*
  39467. * Cleanup
  39468. */
  39469. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39470. X509_free(x);
  39471. BIO_free(bio);
  39472. #endif
  39473. return EXPECT_RESULT();
  39474. }
  39475. static int test_wolfSSL_X509_set_notBefore(void)
  39476. {
  39477. EXPECT_DECLS;
  39478. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  39479. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  39480. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  39481. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  39482. X509* x = NULL;
  39483. BIO* bio = NULL;
  39484. ASN1_TIME *asn_time = NULL;
  39485. ASN1_TIME *time_check = NULL;
  39486. const int year = 365*24*60*60;
  39487. const int day = 24*60*60;
  39488. const int hour = 60*60;
  39489. const int mini = 60;
  39490. int offset_day;
  39491. unsigned char buf[25];
  39492. time_t t;
  39493. /*
  39494. * Setup asn_time. APACHE HTTPD uses time(NULL)
  39495. */
  39496. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  39497. offset_day = 7;
  39498. /*
  39499. * Free these.
  39500. */
  39501. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  39502. ExpectNotNull(asn_time);
  39503. ExpectNotNull(x = X509_new());
  39504. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39505. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  39506. /*
  39507. * Main Tests
  39508. */
  39509. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  39510. /* time_check == (ANS1_TIME*)x->notBefore */
  39511. ExpectNotNull(time_check = X509_get_notBefore(x));
  39512. /* ANS1_TIME_check validates by checking if argument can be parsed */
  39513. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  39514. /* Convert to human readable format and compare to intended date */
  39515. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  39516. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  39517. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  39518. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  39519. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  39520. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  39521. /*
  39522. * Cleanup
  39523. */
  39524. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39525. X509_free(x);
  39526. BIO_free(bio);
  39527. #endif
  39528. return EXPECT_RESULT();
  39529. }
  39530. static int test_wolfSSL_X509_set_version(void)
  39531. {
  39532. EXPECT_DECLS;
  39533. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  39534. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  39535. X509* x509 = NULL;
  39536. long v = 2L;
  39537. long maxInt = INT_MAX;
  39538. ExpectNotNull(x509 = X509_new());
  39539. /* These should pass. */
  39540. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  39541. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  39542. /* Fail Case: When v(long) is greater than x509->version(int). */
  39543. v = maxInt+1;
  39544. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  39545. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  39546. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  39547. /* Cleanup */
  39548. X509_free(x509);
  39549. #endif
  39550. return EXPECT_RESULT();
  39551. }
  39552. #ifndef NO_BIO
  39553. static int test_wolfSSL_BIO_gets(void)
  39554. {
  39555. EXPECT_DECLS;
  39556. #if defined(OPENSSL_EXTRA)
  39557. BIO* bio = NULL;
  39558. BIO* bio2 = NULL;
  39559. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  39560. char emp[] = "";
  39561. char bio_buffer[20];
  39562. int bufferSz = 20;
  39563. #ifdef OPENSSL_ALL
  39564. BUF_MEM* emp_bm = NULL;
  39565. BUF_MEM* msg_bm = NULL;
  39566. #endif
  39567. /* try with bad args */
  39568. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  39569. #ifdef OPENSSL_ALL
  39570. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  39571. #endif
  39572. /* try with real msg */
  39573. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  39574. XMEMSET(bio_buffer, 0, bufferSz);
  39575. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  39576. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  39577. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  39578. ExpectFalse(bio2 != BIO_next(bio));
  39579. /* make buffer filled with no terminating characters */
  39580. XMEMSET(bio_buffer, 1, bufferSz);
  39581. /* BIO_gets reads a line of data */
  39582. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39583. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39584. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39585. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39586. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39587. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39588. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39589. #ifdef OPENSSL_ALL
  39590. /* test setting the mem_buf manually */
  39591. BIO_free(bio);
  39592. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  39593. ExpectNotNull(emp_bm = BUF_MEM_new());
  39594. ExpectNotNull(msg_bm = BUF_MEM_new());
  39595. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  39596. if (EXPECT_SUCCESS()) {
  39597. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  39598. msg_bm->data = NULL;
  39599. }
  39600. /* emp size is 1 for terminator */
  39601. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  39602. if (EXPECT_SUCCESS()) {
  39603. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  39604. emp_bm->data = emp;
  39605. msg_bm->data = msg;
  39606. }
  39607. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  39608. /* check reading an empty string */
  39609. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  39610. ExpectStrEQ(emp, bio_buffer);
  39611. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39612. /* BIO_gets reads a line of data */
  39613. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  39614. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39615. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39616. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39617. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39618. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39619. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39620. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39621. if (EXPECT_SUCCESS())
  39622. emp_bm->data = NULL;
  39623. BUF_MEM_free(emp_bm);
  39624. if (EXPECT_SUCCESS())
  39625. msg_bm->data = NULL;
  39626. BUF_MEM_free(msg_bm);
  39627. #endif
  39628. /* check not null terminated string */
  39629. BIO_free(bio);
  39630. bio = NULL;
  39631. msg[0] = 0x33;
  39632. msg[1] = 0x33;
  39633. msg[2] = 0x33;
  39634. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  39635. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  39636. ExpectIntEQ(bio_buffer[0], msg[0]);
  39637. ExpectIntEQ(bio_buffer[1], msg[1]);
  39638. ExpectIntNE(bio_buffer[2], msg[2]);
  39639. BIO_free(bio);
  39640. bio = NULL;
  39641. msg[3] = 0x33;
  39642. bio_buffer[3] = 0x33;
  39643. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  39644. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  39645. ExpectIntEQ(bio_buffer[0], msg[0]);
  39646. ExpectIntEQ(bio_buffer[1], msg[1]);
  39647. ExpectIntEQ(bio_buffer[2], msg[2]);
  39648. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  39649. /* check reading an empty string */
  39650. BIO_free(bio);
  39651. bio = NULL;
  39652. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  39653. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  39654. ExpectStrEQ(emp, bio_buffer);
  39655. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39656. /* check error cases */
  39657. BIO_free(bio);
  39658. bio = NULL;
  39659. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  39660. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39661. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  39662. #if !defined(NO_FILESYSTEM)
  39663. {
  39664. BIO* f_bio = NULL;
  39665. XFILE f = XBADFILE;
  39666. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  39667. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  39668. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  39669. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  39670. if (EXPECT_FAIL() && (f != XBADFILE)) {
  39671. XFCLOSE(f);
  39672. }
  39673. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  39674. BIO_free(f_bio);
  39675. f_bio = NULL;
  39676. }
  39677. #endif /* NO_FILESYSTEM */
  39678. BIO_free(bio);
  39679. bio = NULL;
  39680. BIO_free(bio2);
  39681. bio2 = NULL;
  39682. /* try with type BIO */
  39683. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  39684. sizeof(msg));
  39685. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  39686. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  39687. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  39688. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  39689. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  39690. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  39691. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  39692. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39693. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39694. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39695. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39696. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39697. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39698. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39699. BIO_free(bio);
  39700. bio = NULL;
  39701. BIO_free(bio2);
  39702. bio2 = NULL;
  39703. /* check reading an empty string */
  39704. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  39705. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  39706. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39707. ExpectStrEQ(emp, bio_buffer);
  39708. BIO_free(bio);
  39709. bio = NULL;
  39710. #endif
  39711. return EXPECT_RESULT();
  39712. }
  39713. static int test_wolfSSL_BIO_puts(void)
  39714. {
  39715. EXPECT_DECLS;
  39716. #if defined(OPENSSL_EXTRA)
  39717. BIO* bio = NULL;
  39718. char input[] = "hello\0world\n.....ok\n\0";
  39719. char output[128];
  39720. XMEMSET(output, 0, sizeof(output));
  39721. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39722. ExpectIntEQ(BIO_puts(bio, input), 5);
  39723. ExpectIntEQ(BIO_pending(bio), 5);
  39724. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  39725. ExpectIntEQ(BIO_pending(bio), 19);
  39726. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  39727. ExpectStrEQ(output, "helloworld\n");
  39728. ExpectIntEQ(BIO_pending(bio), 8);
  39729. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  39730. ExpectStrEQ(output, ".....ok\n");
  39731. ExpectIntEQ(BIO_pending(bio), 0);
  39732. ExpectIntEQ(BIO_puts(bio, ""), -1);
  39733. BIO_free(bio);
  39734. #endif
  39735. return EXPECT_RESULT();
  39736. }
  39737. static int test_wolfSSL_BIO_dump(void)
  39738. {
  39739. EXPECT_DECLS;
  39740. #if defined(OPENSSL_EXTRA)
  39741. BIO* bio;
  39742. static const unsigned char data[] = {
  39743. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  39744. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  39745. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  39746. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  39747. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  39748. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  39749. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  39750. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  39751. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  39752. 0xB4
  39753. };
  39754. /* Generated with OpenSSL. */
  39755. static const char expected[] =
  39756. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  39757. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  39758. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  39759. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  39760. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  39761. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  39762. static const char expectedAll[] =
  39763. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  39764. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  39765. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  39766. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  39767. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  39768. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  39769. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  39770. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  39771. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  39772. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  39773. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  39774. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  39775. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  39776. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  39777. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  39778. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  39779. char output[16 * 80];
  39780. int i;
  39781. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39782. /* Example key dumped. */
  39783. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  39784. sizeof(expected) - 1);
  39785. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  39786. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  39787. /* Try every possible value for a character. */
  39788. for (i = 0; i < 256; i++)
  39789. output[i] = i;
  39790. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  39791. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  39792. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  39793. BIO_free(bio);
  39794. #endif
  39795. return EXPECT_RESULT();
  39796. }
  39797. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39798. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  39799. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  39800. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  39801. {
  39802. (void)ssl;
  39803. (void)buf;
  39804. (void)sz;
  39805. (void)ctx;
  39806. return WOLFSSL_CBIO_ERR_WANT_READ;
  39807. }
  39808. #endif
  39809. static int test_wolfSSL_BIO_should_retry(void)
  39810. {
  39811. EXPECT_DECLS;
  39812. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39813. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  39814. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  39815. tcp_ready ready;
  39816. func_args server_args;
  39817. THREAD_TYPE serverThread;
  39818. SOCKET_T sockfd = 0;
  39819. WOLFSSL_CTX* ctx = NULL;
  39820. WOLFSSL* ssl = NULL;
  39821. char msg[64] = "hello wolfssl!";
  39822. char reply[1024];
  39823. int msgSz = (int)XSTRLEN(msg);
  39824. int ret;
  39825. BIO* bio = NULL;
  39826. XMEMSET(&server_args, 0, sizeof(func_args));
  39827. #ifdef WOLFSSL_TIRTOS
  39828. fdOpenSession(Task_self());
  39829. #endif
  39830. StartTCP();
  39831. InitTcpReady(&ready);
  39832. #if defined(USE_WINDOWS_API)
  39833. /* use RNG to get random port if using windows */
  39834. ready.port = GetRandomPort();
  39835. #endif
  39836. server_args.signal = &ready;
  39837. start_thread(test_server_nofail, &server_args, &serverThread);
  39838. wait_tcp_ready(&server_args);
  39839. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39840. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  39841. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  39842. #endif
  39843. ExpectIntEQ(WOLFSSL_SUCCESS,
  39844. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39845. ExpectIntEQ(WOLFSSL_SUCCESS,
  39846. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39847. ExpectIntEQ(WOLFSSL_SUCCESS,
  39848. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39849. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  39850. /* force retry */
  39851. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  39852. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  39853. ExpectNotNull(ssl);
  39854. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39855. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  39856. if (EXPECT_FAIL()) {
  39857. wolfSSL_free(ssl);
  39858. ssl = NULL;
  39859. }
  39860. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  39861. ExpectIntNE(BIO_should_retry(bio), 0);
  39862. ExpectIntEQ(BIO_should_read(bio), 0);
  39863. ExpectIntEQ(BIO_should_write(bio), 0);
  39864. /* now perform successful connection */
  39865. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  39866. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  39867. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  39868. ret = wolfSSL_get_error(ssl, -1);
  39869. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  39870. ExpectIntNE(BIO_should_retry(bio), 0);
  39871. if (ret == WOLFSSL_ERROR_WANT_READ)
  39872. ExpectIntEQ(BIO_should_read(bio), 1);
  39873. else
  39874. ExpectIntEQ(BIO_should_read(bio), 0);
  39875. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  39876. ExpectIntEQ(BIO_should_write(bio), 1);
  39877. else
  39878. ExpectIntEQ(BIO_should_write(bio), 0);
  39879. }
  39880. else {
  39881. ExpectIntEQ(BIO_should_retry(bio), 0);
  39882. ExpectIntEQ(BIO_should_read(bio), 0);
  39883. ExpectIntEQ(BIO_should_write(bio), 0);
  39884. }
  39885. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  39886. XSTRLEN("I hear you fa shizzle!")), 0);
  39887. BIO_free(bio);
  39888. wolfSSL_CTX_free(ctx);
  39889. CloseSocket(sockfd);
  39890. join_thread(serverThread);
  39891. FreeTcpReady(&ready);
  39892. #ifdef WOLFSSL_TIRTOS
  39893. fdOpenSession(Task_self());
  39894. #endif
  39895. #endif
  39896. return EXPECT_RESULT();
  39897. }
  39898. static int test_wolfSSL_BIO_connect(void)
  39899. {
  39900. EXPECT_DECLS;
  39901. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39902. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  39903. tcp_ready ready;
  39904. func_args server_args;
  39905. THREAD_TYPE serverThread;
  39906. BIO *tcpBio = NULL;
  39907. BIO *sslBio = NULL;
  39908. SSL_CTX* ctx = NULL;
  39909. SSL *ssl = NULL;
  39910. SSL *sslPtr;
  39911. char msg[] = "hello wolfssl!";
  39912. char reply[30];
  39913. char buff[10] = {0};
  39914. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39915. ExpectIntEQ(WOLFSSL_SUCCESS,
  39916. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39917. ExpectIntEQ(WOLFSSL_SUCCESS,
  39918. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39919. ExpectIntEQ(WOLFSSL_SUCCESS,
  39920. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39921. /* Setup server */
  39922. XMEMSET(&server_args, 0, sizeof(func_args));
  39923. StartTCP();
  39924. InitTcpReady(&ready);
  39925. #if defined(USE_WINDOWS_API)
  39926. /* use RNG to get random port if using windows */
  39927. ready.port = GetRandomPort();
  39928. #endif
  39929. server_args.signal = &ready;
  39930. start_thread(test_server_nofail, &server_args, &serverThread);
  39931. wait_tcp_ready(&server_args);
  39932. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39933. /* Start the test proper */
  39934. /* Setup the TCP BIO */
  39935. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  39936. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  39937. /* Setup the SSL object */
  39938. ExpectNotNull(ssl = SSL_new(ctx));
  39939. SSL_set_connect_state(ssl);
  39940. /* Setup the SSL BIO */
  39941. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  39942. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  39943. if (EXPECT_FAIL()) {
  39944. wolfSSL_free(ssl);
  39945. }
  39946. /* Verify that BIO_get_ssl works. */
  39947. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  39948. ExpectPtrEq(ssl, sslPtr);
  39949. /* Link BIO's so that sslBio uses tcpBio for IO */
  39950. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  39951. /* Do TCP connect */
  39952. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39953. /* Do TLS handshake */
  39954. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39955. /* Test writing */
  39956. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39957. /* Expect length of default wolfSSL reply */
  39958. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39959. /* Clean it all up */
  39960. BIO_free_all(sslBio);
  39961. /* Server clean up */
  39962. join_thread(serverThread);
  39963. FreeTcpReady(&ready);
  39964. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  39965. * after. */
  39966. XMEMSET(&server_args, 0, sizeof(func_args));
  39967. StartTCP();
  39968. InitTcpReady(&ready);
  39969. #if defined(USE_WINDOWS_API)
  39970. /* use RNG to get random port if using windows */
  39971. ready.port = GetRandomPort();
  39972. #endif
  39973. server_args.signal = &ready;
  39974. start_thread(test_server_nofail, &server_args, &serverThread);
  39975. wait_tcp_ready(&server_args);
  39976. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39977. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  39978. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  39979. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  39980. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39981. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39982. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39983. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39984. /* Attempt to close the TLS connection gracefully. */
  39985. BIO_ssl_shutdown(sslBio);
  39986. BIO_free_all(sslBio);
  39987. join_thread(serverThread);
  39988. FreeTcpReady(&ready);
  39989. SSL_CTX_free(ctx);
  39990. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39991. wc_ecc_fp_free(); /* free per thread cache */
  39992. #endif
  39993. #endif
  39994. return EXPECT_RESULT();
  39995. }
  39996. static int test_wolfSSL_BIO_tls(void)
  39997. {
  39998. EXPECT_DECLS;
  39999. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  40000. SSL_CTX* ctx = NULL;
  40001. SSL *ssl = NULL;
  40002. BIO *readBio = NULL;
  40003. BIO *writeBio = NULL;
  40004. int ret;
  40005. int err = 0;
  40006. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40007. ExpectNotNull(ssl = SSL_new(ctx));
  40008. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  40009. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  40010. /* Qt reads data from write-bio,
  40011. * then writes the read data into plain packet.
  40012. * Qt reads data from plain packet,
  40013. * then writes the read data into read-bio.
  40014. */
  40015. SSL_set_bio(ssl, readBio, writeBio);
  40016. do {
  40017. #ifdef WOLFSSL_ASYNC_CRYPT
  40018. if (err == WC_PENDING_E) {
  40019. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40020. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40021. }
  40022. #endif
  40023. ret = SSL_connect(ssl);
  40024. err = SSL_get_error(ssl, 0);
  40025. } while (err == WC_PENDING_E);
  40026. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  40027. /* in this use case, should return WANT READ
  40028. * so that Qt will read the data from plain packet for next state.
  40029. */
  40030. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  40031. SSL_free(ssl);
  40032. SSL_CTX_free(ctx);
  40033. #endif
  40034. return EXPECT_RESULT();
  40035. }
  40036. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40037. defined(HAVE_HTTP_CLIENT)
  40038. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  40039. {
  40040. BIO* clientBio;
  40041. SSL* sslClient;
  40042. SSL_CTX* ctx;
  40043. char connectAddr[20]; /* IP + port */;
  40044. (void)args;
  40045. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  40046. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  40047. AssertIntEQ(BIO_do_connect(clientBio), 1);
  40048. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40049. AssertNotNull(sslClient = SSL_new(ctx));
  40050. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  40051. SSL_set_bio(sslClient, clientBio, clientBio);
  40052. AssertIntEQ(SSL_connect(sslClient), 1);
  40053. SSL_free(sslClient);
  40054. SSL_CTX_free(ctx);
  40055. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40056. wc_ecc_fp_free(); /* free per thread cache */
  40057. #endif
  40058. WOLFSSL_RETURN_FROM_THREAD(0);
  40059. }
  40060. #endif
  40061. static int test_wolfSSL_BIO_accept(void)
  40062. {
  40063. EXPECT_DECLS;
  40064. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40065. defined(HAVE_HTTP_CLIENT)
  40066. BIO* serverBindBio = NULL;
  40067. BIO* serverAcceptBio = NULL;
  40068. SSL* sslServer = NULL;
  40069. SSL_CTX* ctx = NULL;
  40070. func_args args;
  40071. THREAD_TYPE thread;
  40072. char port[10]; /* 10 bytes should be enough to store the string
  40073. * representation of the port */
  40074. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  40075. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  40076. /* First BIO_do_accept binds the port */
  40077. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40078. XMEMSET(&args, 0, sizeof(func_args));
  40079. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  40080. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40081. /* Let's plug it into SSL to test */
  40082. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40083. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  40084. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40085. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  40086. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40087. ExpectNotNull(sslServer = SSL_new(ctx));
  40088. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  40089. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  40090. ExpectIntEQ(SSL_accept(sslServer), 1);
  40091. join_thread(thread);
  40092. BIO_free(serverBindBio);
  40093. SSL_free(sslServer);
  40094. SSL_CTX_free(ctx);
  40095. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40096. wc_ecc_fp_free(); /* free per thread cache */
  40097. #endif
  40098. #endif
  40099. return EXPECT_RESULT();
  40100. }
  40101. static int test_wolfSSL_BIO_write(void)
  40102. {
  40103. EXPECT_DECLS;
  40104. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  40105. BIO* bio = NULL;
  40106. BIO* bio64 = NULL;
  40107. BIO* bio_mem = NULL;
  40108. BIO* ptr = NULL;
  40109. int sz;
  40110. char msg[] = "conversion test";
  40111. char out[40];
  40112. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  40113. void* bufPtr = NULL;
  40114. BUF_MEM* buf = NULL;
  40115. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40116. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  40117. if (EXPECT_FAIL()) {
  40118. BIO_free(bio64);
  40119. }
  40120. /* now should convert to base64 then write to memory */
  40121. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40122. BIO_flush(bio);
  40123. /* test BIO chain */
  40124. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  40125. ExpectNotNull(buf);
  40126. ExpectIntEQ(buf->length, 25);
  40127. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  40128. ExpectPtrEq(buf->data, bufPtr);
  40129. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  40130. sz = sizeof(out);
  40131. XMEMSET(out, 0, sz);
  40132. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  40133. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40134. /* write then read should return the same message */
  40135. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40136. sz = sizeof(out);
  40137. XMEMSET(out, 0, sz);
  40138. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  40139. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40140. /* now try encoding with no line ending */
  40141. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  40142. #ifdef HAVE_EX_DATA
  40143. BIO_set_ex_data(bio64, 0, (void*) "data");
  40144. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  40145. #endif
  40146. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40147. BIO_flush(bio);
  40148. sz = sizeof(out);
  40149. XMEMSET(out, 0, sz);
  40150. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  40151. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40152. BIO_free_all(bio); /* frees bio64 also */
  40153. bio = NULL;
  40154. /* test with more than one bio64 in list */
  40155. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40156. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  40157. if (EXPECT_FAIL()) {
  40158. BIO_free(bio64);
  40159. bio64 = NULL;
  40160. }
  40161. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  40162. ExpectNotNull(BIO_push(bio64, bio_mem));
  40163. if (EXPECT_FAIL()) {
  40164. BIO_free(bio_mem);
  40165. }
  40166. /* now should convert to base64 when stored and then decode with read */
  40167. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  40168. BIO_flush(bio);
  40169. sz = sizeof(out);
  40170. XMEMSET(out, 0, sz);
  40171. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  40172. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  40173. BIO_clear_flags(bio64, ~0);
  40174. BIO_set_retry_read(bio);
  40175. BIO_free_all(bio); /* frees bio64s also */
  40176. bio = NULL;
  40177. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  40178. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40179. BIO_free(bio);
  40180. #endif
  40181. return EXPECT_RESULT();
  40182. }
  40183. static int test_wolfSSL_BIO_printf(void)
  40184. {
  40185. EXPECT_DECLS;
  40186. #if defined(OPENSSL_ALL)
  40187. BIO* bio = NULL;
  40188. int sz = 7;
  40189. char msg[] = "TLS 1.3 for the world";
  40190. char out[60];
  40191. char expected[] = "TLS 1.3 for the world : sz = 7";
  40192. XMEMSET(out, 0, sizeof(out));
  40193. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40194. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  40195. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  40196. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  40197. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  40198. BIO_free(bio);
  40199. #endif
  40200. return EXPECT_RESULT();
  40201. }
  40202. static int test_wolfSSL_BIO_f_md(void)
  40203. {
  40204. EXPECT_DECLS;
  40205. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  40206. BIO* bio = NULL;
  40207. BIO* mem = NULL;
  40208. char msg[] = "message to hash";
  40209. char out[60];
  40210. EVP_MD_CTX* ctx = NULL;
  40211. const unsigned char testKey[] =
  40212. {
  40213. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40214. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40215. 0x0b, 0x0b, 0x0b, 0x0b
  40216. };
  40217. const char testData[] = "Hi There";
  40218. const unsigned char testResult[] =
  40219. {
  40220. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  40221. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  40222. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  40223. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  40224. };
  40225. const unsigned char expectedHash[] =
  40226. {
  40227. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  40228. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  40229. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  40230. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  40231. };
  40232. const unsigned char emptyHash[] =
  40233. {
  40234. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  40235. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  40236. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  40237. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  40238. };
  40239. unsigned char check[sizeof(testResult) + 1];
  40240. size_t checkSz = -1;
  40241. EVP_PKEY* key = NULL;
  40242. XMEMSET(out, 0, sizeof(out));
  40243. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40244. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40245. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  40246. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  40247. /* should not be able to write/read yet since just digest wrapper and no
  40248. * data is passing through the bio */
  40249. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  40250. ExpectIntEQ(BIO_pending(bio), 0);
  40251. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  40252. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  40253. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40254. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  40255. BIO_reset(bio);
  40256. /* append BIO mem to bio in order to read/write */
  40257. ExpectNotNull(bio = BIO_push(bio, mem));
  40258. XMEMSET(out, 0, sizeof(out));
  40259. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  40260. ExpectIntEQ(BIO_pending(bio), 16);
  40261. /* this just reads the message and does not hash it (gets calls final) */
  40262. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  40263. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40264. /* create a message digest using BIO */
  40265. XMEMSET(out, 0, sizeof(out));
  40266. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  40267. ExpectIntEQ(BIO_pending(mem), 16);
  40268. ExpectIntEQ(BIO_pending(bio), 16);
  40269. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40270. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  40271. BIO_free(bio);
  40272. bio = NULL;
  40273. BIO_free(mem);
  40274. mem = NULL;
  40275. /* test with HMAC */
  40276. XMEMSET(out, 0, sizeof(out));
  40277. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40278. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40279. BIO_get_md_ctx(bio, &ctx);
  40280. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  40281. (int)sizeof(testKey)));
  40282. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  40283. ExpectNotNull(bio = BIO_push(bio, mem));
  40284. BIO_write(bio, testData, (int)strlen(testData));
  40285. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  40286. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  40287. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  40288. EVP_PKEY_free(key);
  40289. BIO_free(bio);
  40290. BIO_free(mem);
  40291. #endif
  40292. return EXPECT_RESULT();
  40293. }
  40294. static int test_wolfSSL_BIO_up_ref(void)
  40295. {
  40296. EXPECT_DECLS;
  40297. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40298. BIO* bio = NULL;
  40299. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40300. ExpectIntEQ(BIO_up_ref(NULL), 0);
  40301. ExpectIntEQ(BIO_up_ref(bio), 1);
  40302. BIO_free(bio);
  40303. ExpectIntEQ(BIO_up_ref(bio), 1);
  40304. BIO_free(bio);
  40305. BIO_free(bio);
  40306. #endif
  40307. return EXPECT_RESULT();
  40308. }
  40309. static int test_wolfSSL_BIO_reset(void)
  40310. {
  40311. EXPECT_DECLS;
  40312. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40313. BIO* bio = NULL;
  40314. byte buf[16];
  40315. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  40316. (word32)XSTRLEN("secure your data")));
  40317. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  40318. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  40319. XMEMSET(buf, 0, 16);
  40320. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  40321. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  40322. /* You cannot write to MEM BIO with read-only mode. */
  40323. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  40324. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  40325. XMEMSET(buf, 0, 16);
  40326. ExpectIntEQ(BIO_reset(bio), 0);
  40327. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  40328. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  40329. BIO_free(bio);
  40330. #endif
  40331. return EXPECT_RESULT();
  40332. }
  40333. #endif /* !NO_BIO */
  40334. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40335. /* test that the callback arg is correct */
  40336. static int certCbArg = 0;
  40337. static int certCb(WOLFSSL* ssl, void* arg)
  40338. {
  40339. if (ssl == NULL || arg != &certCbArg)
  40340. return 0;
  40341. if (wolfSSL_is_server(ssl)) {
  40342. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  40343. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40344. return 0;
  40345. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  40346. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40347. return 0;
  40348. }
  40349. else {
  40350. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  40351. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40352. return 0;
  40353. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  40354. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40355. return 0;
  40356. }
  40357. return 1;
  40358. }
  40359. static int certSetupCb(WOLFSSL_CTX* ctx)
  40360. {
  40361. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  40362. return TEST_SUCCESS;
  40363. }
  40364. /**
  40365. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  40366. * to stop certificate and key loading
  40367. */
  40368. static int certClearCb(WOLFSSL* ssl)
  40369. {
  40370. /* Clear the loaded certs to force the callbacks to set them up */
  40371. SSL_certs_clear(ssl);
  40372. return TEST_SUCCESS;
  40373. }
  40374. #endif
  40375. static int test_wolfSSL_cert_cb(void)
  40376. {
  40377. EXPECT_DECLS;
  40378. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40379. test_ssl_cbf func_cb_client;
  40380. test_ssl_cbf func_cb_server;
  40381. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  40382. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  40383. func_cb_client.ctx_ready = certSetupCb;
  40384. func_cb_client.ssl_ready = certClearCb;
  40385. func_cb_server.ctx_ready = certSetupCb;
  40386. func_cb_server.ssl_ready = certClearCb;
  40387. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  40388. &func_cb_server, NULL), TEST_SUCCESS);
  40389. #endif
  40390. return EXPECT_RESULT();
  40391. }
  40392. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40393. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  40394. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  40395. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  40396. {
  40397. EXPECT_DECLS;
  40398. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  40399. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  40400. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  40401. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  40402. return EXPECT_RESULT();
  40403. }
  40404. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  40405. {
  40406. const byte* suites = NULL;
  40407. word16 suiteSz = 0;
  40408. const byte* hashSigAlgo = NULL;
  40409. word16 hashSigAlgoSz = 0;
  40410. word16 idx = 0;
  40411. int haveRSA = 0;
  40412. int haveECC = 0;
  40413. (void)arg;
  40414. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  40415. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  40416. return 0;
  40417. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  40418. hashSigAlgoSz == 0)
  40419. return 0;
  40420. for (idx = 0; idx < suiteSz; idx += 2) {
  40421. WOLFSSL_CIPHERSUITE_INFO info =
  40422. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  40423. if (info.rsaAuth)
  40424. haveRSA = 1;
  40425. else if (info.eccAuth)
  40426. haveECC = 1;
  40427. }
  40428. if (hashSigAlgoSz > 0) {
  40429. /* sigalgs extension takes precedence over ciphersuites */
  40430. haveRSA = 0;
  40431. haveECC = 0;
  40432. }
  40433. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  40434. int hashAlgo = 0;
  40435. int sigAlgo = 0;
  40436. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  40437. &hashAlgo, &sigAlgo) != 0)
  40438. return 0;
  40439. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  40440. haveRSA = 1;
  40441. else if (sigAlgo == ECDSAk)
  40442. haveECC = 1;
  40443. }
  40444. if (haveRSA) {
  40445. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  40446. != WOLFSSL_SUCCESS)
  40447. return 0;
  40448. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  40449. != WOLFSSL_SUCCESS)
  40450. return 0;
  40451. }
  40452. else if (haveECC) {
  40453. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  40454. != WOLFSSL_SUCCESS)
  40455. return 0;
  40456. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  40457. != WOLFSSL_SUCCESS)
  40458. return 0;
  40459. }
  40460. return 1;
  40461. }
  40462. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  40463. {
  40464. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  40465. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  40466. return TEST_SUCCESS;
  40467. }
  40468. #endif
  40469. /* Testing dynamic ciphers offered by client */
  40470. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  40471. {
  40472. EXPECT_DECLS;
  40473. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40474. test_ssl_cbf func_cb_client;
  40475. test_ssl_cbf func_cb_server;
  40476. struct {
  40477. method_provider client_meth;
  40478. const char* client_ciphers;
  40479. const char* client_sigalgs;
  40480. const char* client_ca;
  40481. method_provider server_meth;
  40482. } test_params[] = {
  40483. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  40484. #ifdef WOLFSSL_TLS13
  40485. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  40486. {wolfTLSv1_3_client_method,
  40487. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  40488. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  40489. #endif
  40490. #ifdef HAVE_ECC
  40491. {wolfTLSv1_3_client_method,
  40492. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  40493. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  40494. #endif
  40495. #endif
  40496. #ifndef WOLFSSL_NO_TLS12
  40497. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  40498. {wolfTLSv1_2_client_method,
  40499. "DHE-RSA-AES128-GCM-SHA256",
  40500. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  40501. #endif
  40502. #ifdef HAVE_ECC
  40503. {wolfTLSv1_2_client_method,
  40504. "ECDHE-ECDSA-AES128-GCM-SHA256",
  40505. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  40506. #endif
  40507. #endif
  40508. #endif
  40509. };
  40510. size_t i;
  40511. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  40512. if (testCount > 0) {
  40513. for (i = 0; i < testCount; i++) {
  40514. printf("\tTesting %s ciphers with %s sigalgs\n",
  40515. test_params[i].client_ciphers,
  40516. test_params[i].client_sigalgs);
  40517. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  40518. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  40519. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  40520. test_params[i].client_ciphers;
  40521. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  40522. test_params[i].client_sigalgs;
  40523. func_cb_client.method = test_params[i].client_meth;
  40524. func_cb_client.caPemFile = test_params[i].client_ca;
  40525. func_cb_client.ctx_ready =
  40526. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  40527. func_cb_server.ctx_ready =
  40528. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  40529. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  40530. func_cb_server.method = test_params[i].server_meth;
  40531. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  40532. &func_cb_server, NULL), TEST_SUCCESS);
  40533. }
  40534. }
  40535. #endif
  40536. return EXPECT_RESULT();
  40537. }
  40538. static int test_wolfSSL_ciphersuite_auth(void)
  40539. {
  40540. EXPECT_DECLS;
  40541. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  40542. WOLFSSL_CIPHERSUITE_INFO info;
  40543. (void)info;
  40544. #ifndef WOLFSSL_NO_TLS12
  40545. #ifdef HAVE_CHACHA
  40546. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40547. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  40548. ExpectIntEQ(info.rsaAuth, 1);
  40549. ExpectIntEQ(info.eccAuth, 0);
  40550. ExpectIntEQ(info.eccStatic, 0);
  40551. ExpectIntEQ(info.psk, 0);
  40552. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40553. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  40554. ExpectIntEQ(info.rsaAuth, 0);
  40555. ExpectIntEQ(info.eccAuth, 1);
  40556. ExpectIntEQ(info.eccStatic, 0);
  40557. ExpectIntEQ(info.psk, 0);
  40558. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40559. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  40560. ExpectIntEQ(info.rsaAuth, 0);
  40561. ExpectIntEQ(info.eccAuth, 0);
  40562. ExpectIntEQ(info.eccStatic, 0);
  40563. ExpectIntEQ(info.psk, 1);
  40564. #endif
  40565. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  40566. #ifndef NO_RSA
  40567. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40568. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  40569. ExpectIntEQ(info.rsaAuth, 1);
  40570. ExpectIntEQ(info.eccAuth, 0);
  40571. ExpectIntEQ(info.eccStatic, 0);
  40572. ExpectIntEQ(info.psk, 0);
  40573. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40574. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  40575. ExpectIntEQ(info.rsaAuth, 1);
  40576. ExpectIntEQ(info.eccAuth, 0);
  40577. ExpectIntEQ(info.eccStatic, 1);
  40578. ExpectIntEQ(info.psk, 0);
  40579. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40580. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  40581. ExpectIntEQ(info.rsaAuth, 1);
  40582. ExpectIntEQ(info.eccAuth, 0);
  40583. ExpectIntEQ(info.eccStatic, 1);
  40584. ExpectIntEQ(info.psk, 0);
  40585. #endif
  40586. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40587. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  40588. ExpectIntEQ(info.rsaAuth, 0);
  40589. ExpectIntEQ(info.eccAuth, 1);
  40590. ExpectIntEQ(info.eccStatic, 0);
  40591. ExpectIntEQ(info.psk, 0);
  40592. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40593. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  40594. ExpectIntEQ(info.rsaAuth, 0);
  40595. ExpectIntEQ(info.eccAuth, 1);
  40596. ExpectIntEQ(info.eccStatic, 1);
  40597. ExpectIntEQ(info.psk, 0);
  40598. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  40599. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  40600. ExpectIntEQ(info.rsaAuth, 0);
  40601. ExpectIntEQ(info.eccAuth, 0);
  40602. ExpectIntEQ(info.eccStatic, 0);
  40603. ExpectIntEQ(info.psk, 1);
  40604. #endif
  40605. #endif
  40606. #ifdef WOLFSSL_TLS13
  40607. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  40608. TLS_AES_128_GCM_SHA256);
  40609. ExpectIntEQ(info.rsaAuth, 0);
  40610. ExpectIntEQ(info.eccAuth, 0);
  40611. ExpectIntEQ(info.eccStatic, 0);
  40612. ExpectIntEQ(info.psk, 0);
  40613. #endif
  40614. #endif
  40615. return EXPECT_RESULT();
  40616. }
  40617. static int test_wolfSSL_sigalg_info(void)
  40618. {
  40619. EXPECT_DECLS;
  40620. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  40621. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  40622. word16 len = 0;
  40623. word16 idx = 0;
  40624. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  40625. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  40626. for (idx = 0; idx < len; idx += 2) {
  40627. int hashAlgo = 0;
  40628. int sigAlgo = 0;
  40629. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  40630. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  40631. ExpectIntNE(hashAlgo, 0);
  40632. ExpectIntNE(sigAlgo, 0);
  40633. }
  40634. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  40635. 0xFFFFFFFF, &len);
  40636. for (idx = 0; idx < len; idx += 2) {
  40637. int hashAlgo = 0;
  40638. int sigAlgo = 0;
  40639. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  40640. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  40641. ExpectIntNE(hashAlgo, 0);
  40642. }
  40643. #endif
  40644. return EXPECT_RESULT();
  40645. }
  40646. static int test_wolfSSL_SESSION(void)
  40647. {
  40648. EXPECT_DECLS;
  40649. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40650. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40651. !defined(NO_SESSION_CACHE)
  40652. WOLFSSL* ssl = NULL;
  40653. WOLFSSL_CTX* ctx = NULL;
  40654. WOLFSSL_SESSION* sess = NULL;
  40655. WOLFSSL_SESSION* sess_copy = NULL;
  40656. #ifdef OPENSSL_EXTRA
  40657. #ifdef HAVE_EXT_CACHE
  40658. unsigned char* sessDer = NULL;
  40659. unsigned char* ptr = NULL;
  40660. int sz = 0;
  40661. #endif
  40662. const unsigned char context[] = "user app context";
  40663. unsigned int contextSz = (unsigned int)sizeof(context);
  40664. #endif
  40665. int ret = 0, err = 0;
  40666. SOCKET_T sockfd;
  40667. tcp_ready ready;
  40668. func_args server_args;
  40669. THREAD_TYPE serverThread;
  40670. char msg[80];
  40671. const char* sendGET = "GET";
  40672. /* TLS v1.3 requires session tickets */
  40673. /* CHACHA and POLY1305 required for myTicketEncCb */
  40674. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  40675. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  40676. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  40677. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  40678. #else
  40679. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40680. #endif
  40681. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  40682. WOLFSSL_FILETYPE_PEM));
  40683. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  40684. WOLFSSL_FILETYPE_PEM));
  40685. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  40686. WOLFSSL_SUCCESS);
  40687. #ifdef WOLFSSL_ENCRYPTED_KEYS
  40688. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  40689. #endif
  40690. #ifdef HAVE_SESSION_TICKET
  40691. /* Use session tickets, for ticket tests below */
  40692. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  40693. #endif
  40694. XMEMSET(&server_args, 0, sizeof(func_args));
  40695. #ifdef WOLFSSL_TIRTOS
  40696. fdOpenSession(Task_self());
  40697. #endif
  40698. StartTCP();
  40699. InitTcpReady(&ready);
  40700. #if defined(USE_WINDOWS_API)
  40701. /* use RNG to get random port if using windows */
  40702. ready.port = GetRandomPort();
  40703. #endif
  40704. server_args.signal = &ready;
  40705. start_thread(test_server_nofail, &server_args, &serverThread);
  40706. wait_tcp_ready(&server_args);
  40707. /* client connection */
  40708. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40709. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  40710. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40711. #ifdef WOLFSSL_ASYNC_CRYPT
  40712. err = 0; /* Reset error */
  40713. #endif
  40714. do {
  40715. #ifdef WOLFSSL_ASYNC_CRYPT
  40716. if (err == WC_PENDING_E) {
  40717. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40718. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40719. }
  40720. #endif
  40721. ret = wolfSSL_connect(ssl);
  40722. err = wolfSSL_get_error(ssl, 0);
  40723. } while (err == WC_PENDING_E);
  40724. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  40725. #ifdef WOLFSSL_ASYNC_CRYPT
  40726. err = 0; /* Reset error */
  40727. #endif
  40728. do {
  40729. #ifdef WOLFSSL_ASYNC_CRYPT
  40730. if (err == WC_PENDING_E) {
  40731. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40732. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40733. }
  40734. #endif
  40735. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  40736. err = wolfSSL_get_error(ssl, 0);
  40737. } while (err == WC_PENDING_E);
  40738. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  40739. #ifdef WOLFSSL_ASYNC_CRYPT
  40740. err = 0; /* Reset error */
  40741. #endif
  40742. do {
  40743. #ifdef WOLFSSL_ASYNC_CRYPT
  40744. if (err == WC_PENDING_E) {
  40745. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40746. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40747. }
  40748. #endif
  40749. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  40750. err = wolfSSL_get_error(ssl, 0);
  40751. } while (err == WC_PENDING_E);
  40752. ExpectIntEQ(ret, 23);
  40753. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  40754. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  40755. #ifdef HAVE_EXT_CACHE
  40756. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  40757. * HAVE_EXT_CACHE we get new objects each time */
  40758. #endif
  40759. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  40760. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  40761. sess = wolfSSL_get_session(ssl);
  40762. #ifdef OPENSSL_EXTRA
  40763. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  40764. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  40765. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  40766. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  40767. #ifdef HAVE_SESSION_TICKET
  40768. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  40769. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  40770. SESSION_TICKET_HINT_DEFAULT);
  40771. #else
  40772. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  40773. #endif
  40774. #else
  40775. (void)sess;
  40776. #endif /* OPENSSL_EXTRA */
  40777. /* Retain copy of the session for later testing */
  40778. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  40779. wolfSSL_shutdown(ssl);
  40780. wolfSSL_free(ssl); ssl = NULL;
  40781. CloseSocket(sockfd);
  40782. join_thread(serverThread);
  40783. FreeTcpReady(&ready);
  40784. #ifdef WOLFSSL_TIRTOS
  40785. fdOpenSession(Task_self());
  40786. #endif
  40787. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  40788. {
  40789. X509 *x509 = NULL;
  40790. char buf[30];
  40791. int bufSz = 0;
  40792. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  40793. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  40794. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  40795. sizeof(buf))), 0);
  40796. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  40797. if (bufSz == 7) {
  40798. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  40799. }
  40800. if (bufSz == 16) {
  40801. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  40802. }
  40803. }
  40804. #endif
  40805. #ifdef HAVE_EXT_CACHE
  40806. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  40807. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  40808. sess_copy = NULL;
  40809. #endif
  40810. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  40811. /* get session from DER and update the timeout */
  40812. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  40813. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  40814. wolfSSL_SESSION_free(sess); sess = NULL;
  40815. sess = NULL;
  40816. ptr = sessDer;
  40817. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  40818. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  40819. (const unsigned char**)&ptr, sz));
  40820. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  40821. sessDer = NULL;
  40822. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  40823. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  40824. #endif
  40825. /* successful set session test */
  40826. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40827. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  40828. #ifdef HAVE_SESSION_TICKET
  40829. /* Test set/get session ticket */
  40830. {
  40831. const char* ticket = "This is a session ticket";
  40832. char buf[64] = {0};
  40833. word32 bufSz = (word32)sizeof(buf);
  40834. ExpectIntEQ(SSL_SUCCESS,
  40835. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  40836. (word32)XSTRLEN(ticket)));
  40837. ExpectIntEQ(SSL_SUCCESS,
  40838. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  40839. ExpectStrEQ(ticket, buf);
  40840. }
  40841. #endif
  40842. #ifdef OPENSSL_EXTRA
  40843. /* session timeout case */
  40844. /* make the session to be expired */
  40845. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  40846. XSLEEP_MS(1200);
  40847. /* SSL_set_session should reject specified session but return success
  40848. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  40849. */
  40850. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40851. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  40852. #else
  40853. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  40854. #endif
  40855. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  40856. #ifdef WOLFSSL_SESSION_ID_CTX
  40857. /* fail case with miss match session context IDs (use compatibility API) */
  40858. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  40859. SSL_SUCCESS);
  40860. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  40861. wolfSSL_free(ssl); ssl = NULL;
  40862. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  40863. SSL_FAILURE);
  40864. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  40865. SSL_SUCCESS);
  40866. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40867. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  40868. #endif
  40869. #endif /* OPENSSL_EXTRA */
  40870. wolfSSL_free(ssl);
  40871. wolfSSL_SESSION_free(sess);
  40872. wolfSSL_CTX_free(ctx);
  40873. #endif
  40874. return EXPECT_RESULT();
  40875. }
  40876. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40877. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40878. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  40879. !defined(WOLFSSL_NO_TLS12)
  40880. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  40881. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  40882. {
  40883. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  40884. /* returns previous timeout value */
  40885. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  40886. #else
  40887. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  40888. #endif
  40889. }
  40890. /* set the session to timeout in a second */
  40891. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  40892. {
  40893. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  40894. }
  40895. /* store the client side session from the first successful connection */
  40896. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  40897. {
  40898. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  40899. NULL); /* ref count 1 */
  40900. }
  40901. /* wait till session is expired then set it in the WOLFSSL struct for use */
  40902. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  40903. {
  40904. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  40905. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40906. WOLFSSL_SUCCESS);
  40907. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  40908. }
  40909. /* set expired session in the WOLFSSL struct for use */
  40910. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  40911. {
  40912. XSLEEP_MS(1200); /* wait a second for session to expire */
  40913. /* set the expired session, call to set session fails but continuing on
  40914. after failure should be handled here */
  40915. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40916. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40917. WOLFSSL_SUCCESS);
  40918. #else
  40919. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40920. WOLFSSL_SUCCESS);
  40921. #endif
  40922. }
  40923. /* check that the expired session was not reused */
  40924. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  40925. {
  40926. /* since the session has expired it should not have been reused */
  40927. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  40928. }
  40929. #endif
  40930. static int test_wolfSSL_SESSION_expire_downgrade(void)
  40931. {
  40932. EXPECT_DECLS;
  40933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40934. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40935. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  40936. !defined(WOLFSSL_NO_TLS12)
  40937. WOLFSSL_CTX* ctx = NULL;
  40938. callback_functions server_cbf, client_cbf;
  40939. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  40940. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  40941. /* force server side to use TLS 1.2 */
  40942. server_cbf.ctx = ctx;
  40943. server_cbf.method = wolfTLSv1_2_server_method;
  40944. client_cbf.method = wolfSSLv23_client_method;
  40945. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40946. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  40947. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  40948. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40949. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40950. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40951. /* set the previously created session and wait till expired */
  40952. server_cbf.ctx = ctx;
  40953. client_cbf.method = wolfSSLv23_client_method;
  40954. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40955. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  40956. client_cbf.on_result =
  40957. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  40958. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40959. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40960. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40961. /* set the previously created expired session */
  40962. server_cbf.ctx = ctx;
  40963. client_cbf.method = wolfSSLv23_client_method;
  40964. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40965. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  40966. client_cbf.on_result =
  40967. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  40968. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40969. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40970. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40971. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  40972. wolfSSL_CTX_free(ctx);
  40973. #endif
  40974. return EXPECT_RESULT();
  40975. }
  40976. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  40977. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  40978. static int clientSessRemCountMalloc = 0;
  40979. static int serverSessRemCountMalloc = 0;
  40980. static int clientSessRemCountFree = 0;
  40981. static int serverSessRemCountFree = 0;
  40982. static WOLFSSL_CTX* serverSessCtx = NULL;
  40983. static WOLFSSL_SESSION* serverSess = NULL;
  40984. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40985. !defined(NO_SESSION_CACHE_REF)
  40986. static WOLFSSL_CTX* clientSessCtx = NULL;
  40987. static WOLFSSL_SESSION* clientSess = NULL;
  40988. #endif
  40989. static int serverSessRemIdx = 3;
  40990. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  40991. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  40992. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  40993. {
  40994. int* side;
  40995. (void)ctx;
  40996. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  40997. if (side != NULL) {
  40998. if (*side == WOLFSSL_CLIENT_END)
  40999. clientSessRemCountFree++;
  41000. else
  41001. serverSessRemCountFree++;
  41002. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  41003. }
  41004. }
  41005. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  41006. {
  41007. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  41008. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  41009. !defined(NO_SESSION_CACHE_REF)
  41010. {
  41011. EXPECT_DECLS;
  41012. /* Allow downgrade, set min version, and disable TLS 1.3.
  41013. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  41014. * reference to the session cache. But with WOLFSSL_TLS13 and without
  41015. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  41016. * the session in the cache. In this case we need to downgrade to
  41017. * previous versions to just use the legacy session ID field. */
  41018. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  41019. SSL_SUCCESS);
  41020. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  41021. SSL_SUCCESS);
  41022. return EXPECT_RESULT();
  41023. }
  41024. #else
  41025. return TEST_SUCCESS;
  41026. #endif
  41027. }
  41028. static int SessRemSslSetupCb(WOLFSSL* ssl)
  41029. {
  41030. EXPECT_DECLS;
  41031. int* side;
  41032. if (SSL_is_server(ssl)) {
  41033. side = &sessRemCtx_Server;
  41034. serverSessRemCountMalloc++;
  41035. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  41036. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  41037. SSL_SUCCESS);
  41038. }
  41039. else {
  41040. side = &sessRemCtx_Client;
  41041. clientSessRemCountMalloc++;
  41042. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41043. !defined(NO_SESSION_CACHE_REF)
  41044. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  41045. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  41046. SSL_SUCCESS);
  41047. #endif
  41048. }
  41049. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  41050. serverSessRemIdx, side), SSL_SUCCESS);
  41051. return EXPECT_RESULT();
  41052. }
  41053. #endif
  41054. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  41055. {
  41056. EXPECT_DECLS;
  41057. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41058. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41059. /* Check that the remove callback gets called for external data in a
  41060. * session object */
  41061. test_ssl_cbf func_cb;
  41062. XMEMSET(&func_cb, 0, sizeof(func_cb));
  41063. func_cb.ctx_ready = SessRemCtxSetupCb;
  41064. func_cb.on_result = SessRemSslSetupCb;
  41065. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  41066. NULL), TEST_SUCCESS);
  41067. /* Both should have been allocated */
  41068. ExpectIntEQ(clientSessRemCountMalloc, 1);
  41069. ExpectIntEQ(serverSessRemCountMalloc, 1);
  41070. /* This should not be called yet. Session wasn't evicted from cache yet. */
  41071. ExpectIntEQ(clientSessRemCountFree, 0);
  41072. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41073. !defined(NO_SESSION_CACHE_REF)
  41074. /* Force a cache lookup */
  41075. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41076. /* Force a cache update */
  41077. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  41078. /* This should set the timeout to 0 and call the remove callback from within
  41079. * the session cache. */
  41080. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  41081. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41082. ExpectIntEQ(clientSessRemCountFree, 1);
  41083. #endif
  41084. /* Server session is in the cache so ex_data isn't free'd with the SSL
  41085. * object */
  41086. ExpectIntEQ(serverSessRemCountFree, 0);
  41087. /* Force a cache lookup */
  41088. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41089. /* Force a cache update */
  41090. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  41091. /* This should set the timeout to 0 and call the remove callback from within
  41092. * the session cache. */
  41093. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  41094. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41095. ExpectIntEQ(serverSessRemCountFree, 1);
  41096. /* Need to free the references that we kept */
  41097. SSL_CTX_free(serverSessCtx);
  41098. SSL_SESSION_free(serverSess);
  41099. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41100. !defined(NO_SESSION_CACHE_REF)
  41101. SSL_CTX_free(clientSessCtx);
  41102. SSL_SESSION_free(clientSess);
  41103. #endif
  41104. #endif
  41105. return EXPECT_RESULT();
  41106. }
  41107. static int test_wolfSSL_ticket_keys(void)
  41108. {
  41109. EXPECT_DECLS;
  41110. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  41111. !defined(NO_WOLFSSL_SERVER)
  41112. WOLFSSL_CTX* ctx = NULL;
  41113. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  41114. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  41115. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  41116. WOLFSSL_FAILURE);
  41117. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  41118. WOLFSSL_FAILURE);
  41119. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  41120. WOLFSSL_FAILURE);
  41121. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  41122. WOLFSSL_FAILURE);
  41123. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41124. WOLFSSL_FAILURE);
  41125. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41126. WOLFSSL_FAILURE);
  41127. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41128. WOLFSSL_FAILURE);
  41129. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  41130. WOLFSSL_FAILURE);
  41131. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  41132. WOLFSSL_FAILURE);
  41133. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  41134. WOLFSSL_FAILURE);
  41135. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  41136. WOLFSSL_FAILURE);
  41137. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41138. WOLFSSL_FAILURE);
  41139. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41140. WOLFSSL_FAILURE);
  41141. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41142. WOLFSSL_FAILURE);
  41143. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41144. WOLFSSL_SUCCESS);
  41145. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41146. WOLFSSL_SUCCESS);
  41147. wolfSSL_CTX_free(ctx);
  41148. #endif
  41149. return EXPECT_RESULT();
  41150. }
  41151. #ifndef NO_BIO
  41152. static int test_wolfSSL_d2i_PUBKEY(void)
  41153. {
  41154. EXPECT_DECLS;
  41155. #if defined(OPENSSL_EXTRA)
  41156. BIO* bio = NULL;
  41157. EVP_PKEY* pkey = NULL;
  41158. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41159. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  41160. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  41161. /* RSA PUBKEY test */
  41162. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  41163. sizeof_client_keypub_der_2048), 0);
  41164. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41165. EVP_PKEY_free(pkey);
  41166. pkey = NULL;
  41167. #endif
  41168. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  41169. /* ECC PUBKEY test */
  41170. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  41171. sizeof_ecc_clikeypub_der_256), 0);
  41172. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41173. EVP_PKEY_free(pkey);
  41174. pkey = NULL;
  41175. #endif
  41176. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  41177. /* DSA PUBKEY test */
  41178. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  41179. sizeof_dsa_pub_key_der_2048), 0);
  41180. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41181. EVP_PKEY_free(pkey);
  41182. pkey = NULL;
  41183. #endif
  41184. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  41185. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  41186. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  41187. (HAVE_FIPS_VERSION > 2))
  41188. /* DH PUBKEY test */
  41189. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  41190. sizeof_dh_pub_key_der_2048), 0);
  41191. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41192. EVP_PKEY_free(pkey);
  41193. pkey = NULL;
  41194. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41195. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  41196. BIO_free(bio);
  41197. (void)pkey;
  41198. #endif
  41199. return EXPECT_RESULT();
  41200. }
  41201. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  41202. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  41203. {
  41204. EXPECT_DECLS;
  41205. BIO* bio = NULL;
  41206. EVP_PKEY* pkey = NULL;
  41207. WOLFSSL_CTX* ctx = NULL;
  41208. #if defined(WOLFSSL_KEY_GEN)
  41209. unsigned char buff[4096];
  41210. unsigned char* bufPtr = buff;
  41211. #endif
  41212. /* test creating new EVP_PKEY with bad arg */
  41213. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  41214. /* test loading RSA key using BIO */
  41215. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41216. {
  41217. XFILE file = XBADFILE;
  41218. const char* fname = "./certs/server-key.der";
  41219. size_t sz = 0;
  41220. byte* buf = NULL;
  41221. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41222. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41223. ExpectTrue((sz = XFTELL(file)) != 0);
  41224. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41225. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41226. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41227. if (file != XBADFILE) {
  41228. XFCLOSE(file);
  41229. }
  41230. /* Test using BIO new mem and loading DER private key */
  41231. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41232. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41233. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41234. BIO_free(bio);
  41235. bio = NULL;
  41236. EVP_PKEY_free(pkey);
  41237. pkey = NULL;
  41238. }
  41239. #endif
  41240. /* test loading ECC key using BIO */
  41241. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  41242. {
  41243. XFILE file = XBADFILE;
  41244. const char* fname = "./certs/ecc-key.der";
  41245. size_t sz = 0;
  41246. byte* buf = NULL;
  41247. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41248. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41249. ExpectTrue((sz = XFTELL(file)) != 0);
  41250. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41251. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41252. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41253. if (file != XBADFILE)
  41254. XFCLOSE(file);
  41255. /* Test using BIO new mem and loading DER private key */
  41256. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41257. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41258. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41259. BIO_free(bio);
  41260. bio = NULL;
  41261. EVP_PKEY_free(pkey);
  41262. pkey = NULL;
  41263. }
  41264. #endif
  41265. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41266. #ifndef NO_WOLFSSL_SERVER
  41267. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41268. #else
  41269. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  41270. #endif
  41271. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  41272. {
  41273. RSA* rsa = NULL;
  41274. /* Tests bad parameters */
  41275. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  41276. /* RSA not set yet, expecting to fail*/
  41277. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  41278. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  41279. /* set RSA using bio*/
  41280. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  41281. sizeof_client_key_der_2048), 0);
  41282. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  41283. ExpectNotNull(rsa);
  41284. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  41285. /* i2d RSAprivate key tests */
  41286. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  41287. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  41288. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  41289. sizeof_client_key_der_2048);
  41290. bufPtr -= sizeof_client_key_der_2048;
  41291. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  41292. sizeof_client_key_der_2048), 0);
  41293. bufPtr = NULL;
  41294. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  41295. sizeof_client_key_der_2048);
  41296. ExpectNotNull(bufPtr);
  41297. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  41298. sizeof_client_key_der_2048), 0);
  41299. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  41300. RSA_free(rsa);
  41301. rsa = RSA_new();
  41302. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  41303. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  41304. RSA_free(rsa);
  41305. }
  41306. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  41307. SSL_CTX_free(ctx);
  41308. ctx = NULL;
  41309. BIO_free(bio);
  41310. bio = NULL;
  41311. return EXPECT_RESULT();
  41312. }
  41313. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  41314. #endif /* !NO_BIO */
  41315. static int test_wolfSSL_sk_GENERAL_NAME(void)
  41316. {
  41317. EXPECT_DECLS;
  41318. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41319. !defined(NO_RSA)
  41320. X509* x509 = NULL;
  41321. GENERAL_NAME* gn = NULL;
  41322. unsigned char buf[4096];
  41323. const unsigned char* bufPt = NULL;
  41324. int bytes = 0;
  41325. int i;
  41326. int j;
  41327. XFILE f = XBADFILE;
  41328. STACK_OF(GENERAL_NAME)* sk = NULL;
  41329. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  41330. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41331. if (f != XBADFILE)
  41332. XFCLOSE(f);
  41333. for (j = 0; j < 2; ++j) {
  41334. bufPt = buf;
  41335. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41336. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  41337. NID_subject_alt_name, NULL, NULL));
  41338. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  41339. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  41340. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  41341. if (gn != NULL) {
  41342. switch (gn->type) {
  41343. case GEN_DNS:
  41344. fprintf(stderr, "found type GEN_DNS\n");
  41345. break;
  41346. case GEN_EMAIL:
  41347. fprintf(stderr, "found type GEN_EMAIL\n");
  41348. break;
  41349. case GEN_URI:
  41350. fprintf(stderr, "found type GEN_URI\n");
  41351. break;
  41352. }
  41353. }
  41354. }
  41355. X509_free(x509);
  41356. x509 = NULL;
  41357. if (j == 0) {
  41358. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  41359. }
  41360. else {
  41361. /*
  41362. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  41363. * it was supposed to. This is a regression test for that bug.
  41364. */
  41365. GENERAL_NAMES_free(sk);
  41366. }
  41367. sk = NULL;
  41368. }
  41369. #endif
  41370. return EXPECT_RESULT();
  41371. }
  41372. static int test_wolfSSL_GENERAL_NAME_print(void)
  41373. {
  41374. EXPECT_DECLS;
  41375. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  41376. X509* x509 = NULL;
  41377. GENERAL_NAME* gn = NULL;
  41378. unsigned char buf[4096];
  41379. const unsigned char* bufPt = NULL;
  41380. int bytes;
  41381. XFILE f = XBADFILE;
  41382. STACK_OF(GENERAL_NAME)* sk = NULL;
  41383. BIO* out = NULL;
  41384. unsigned char outbuf[128];
  41385. X509_EXTENSION* ext = NULL;
  41386. AUTHORITY_INFO_ACCESS* aia = NULL;
  41387. ACCESS_DESCRIPTION* ad = NULL;
  41388. ASN1_IA5STRING *dnsname = NULL;
  41389. const unsigned char v4Addr[] = {192,168,53,1};
  41390. const unsigned char v6Addr[] =
  41391. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  41392. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  41393. const unsigned char email[] =
  41394. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  41395. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  41396. const char* dnsStr = "DNS:example.com";
  41397. const char* uriStr = "URI:http://127.0.0.1:22220";
  41398. const char* v4addStr = "IP Address:192.168.53.1";
  41399. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  41400. const char* emailStr = "email:info@wolfssl.com";
  41401. const char* othrStr = "othername:<unsupported>";
  41402. const char* x400Str = "X400Name:<unsupported>";
  41403. const char* ediStr = "EdiPartyName:<unsupported>";
  41404. /* BIO to output */
  41405. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  41406. /* test for NULL param */
  41407. gn = NULL;
  41408. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  41409. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  41410. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  41411. /* test for GEN_DNS */
  41412. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  41413. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41414. if (f != XBADFILE) {
  41415. XFCLOSE(f);
  41416. f = XBADFILE;
  41417. }
  41418. bufPt = buf;
  41419. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41420. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  41421. NID_subject_alt_name, NULL, NULL));
  41422. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  41423. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41424. XMEMSET(outbuf, 0, sizeof(outbuf));
  41425. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41426. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  41427. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  41428. gn = NULL;
  41429. sk = NULL;
  41430. X509_free(x509);
  41431. x509 = NULL;
  41432. /* Lets test for setting as well. */
  41433. ExpectNotNull(gn = GENERAL_NAME_new());
  41434. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  41435. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  41436. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  41437. dnsname = NULL;
  41438. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41439. XMEMSET(outbuf, 0, sizeof(outbuf));
  41440. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41441. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  41442. GENERAL_NAME_free(gn);
  41443. /* test for GEN_URI */
  41444. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  41445. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  41446. if (f != XBADFILE) {
  41447. XFCLOSE(f);
  41448. f = XBADFILE;
  41449. }
  41450. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  41451. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  41452. ext));
  41453. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  41454. if (ad != NULL) {
  41455. gn = ad->location;
  41456. }
  41457. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41458. gn = NULL;
  41459. XMEMSET(outbuf,0,sizeof(outbuf));
  41460. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41461. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  41462. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  41463. aia = NULL;
  41464. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  41465. ExpectNotNull(aia);
  41466. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  41467. aia = NULL;
  41468. X509_free(x509);
  41469. x509 = NULL;
  41470. /* test for GEN_IPADD */
  41471. /* ip v4 address */
  41472. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41473. if (gn != NULL) {
  41474. gn->type = GEN_IPADD;
  41475. if (gn->d.iPAddress != NULL) {
  41476. gn->d.iPAddress->length = sizeof(v4Addr);
  41477. }
  41478. }
  41479. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  41480. sizeof(v4Addr)), 1);
  41481. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41482. XMEMSET(outbuf,0,sizeof(outbuf));
  41483. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41484. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  41485. GENERAL_NAME_free(gn);
  41486. gn = NULL;
  41487. /* ip v6 address */
  41488. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41489. if (gn != NULL) {
  41490. gn->type = GEN_IPADD;
  41491. if (gn->d.iPAddress != NULL) {
  41492. gn->d.iPAddress->length = sizeof(v6Addr);
  41493. }
  41494. }
  41495. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  41496. sizeof(v6Addr)), 1);
  41497. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41498. XMEMSET(outbuf,0,sizeof(outbuf));
  41499. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41500. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  41501. GENERAL_NAME_free(gn);
  41502. gn = NULL;
  41503. /* test for GEN_EMAIL */
  41504. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41505. if (gn != NULL) {
  41506. gn->type = GEN_EMAIL;
  41507. if (gn->d.rfc822Name != NULL) {
  41508. gn->d.rfc822Name->length = sizeof(email);
  41509. }
  41510. }
  41511. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  41512. 1);
  41513. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41514. XMEMSET(outbuf,0,sizeof(outbuf));
  41515. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41516. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  41517. GENERAL_NAME_free(gn);
  41518. gn = NULL;
  41519. /* test for GEN_OTHERNAME */
  41520. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41521. if (gn != NULL) {
  41522. gn->type = GEN_OTHERNAME;
  41523. }
  41524. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41525. XMEMSET(outbuf,0,sizeof(outbuf));
  41526. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41527. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  41528. GENERAL_NAME_free(gn);
  41529. gn = NULL;
  41530. /* test for GEN_X400 */
  41531. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41532. if (gn != NULL) {
  41533. gn->type = GEN_X400;
  41534. }
  41535. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41536. XMEMSET(outbuf,0,sizeof(outbuf));
  41537. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41538. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  41539. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  41540. if (gn != NULL) {
  41541. gn->type = GEN_IA5;
  41542. }
  41543. GENERAL_NAME_free(gn);
  41544. gn = NULL;
  41545. /* test for GEN_EDIPARTY */
  41546. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41547. if (gn != NULL) {
  41548. gn->type = GEN_EDIPARTY;
  41549. }
  41550. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41551. XMEMSET(outbuf,0,sizeof(outbuf));
  41552. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41553. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  41554. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  41555. if (gn != NULL) {
  41556. gn->type = GEN_IA5;
  41557. }
  41558. GENERAL_NAME_free(gn);
  41559. gn = NULL;
  41560. BIO_free(out);
  41561. #endif /* OPENSSL_ALL */
  41562. return EXPECT_RESULT();
  41563. }
  41564. static int test_wolfSSL_sk_DIST_POINT(void)
  41565. {
  41566. EXPECT_DECLS;
  41567. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41568. !defined(NO_RSA)
  41569. X509* x509 = NULL;
  41570. unsigned char buf[4096];
  41571. const unsigned char* bufPt;
  41572. int bytes = 0;
  41573. int i = 0;
  41574. int j = 0;
  41575. XFILE f = XBADFILE;
  41576. DIST_POINT* dp = NULL;
  41577. DIST_POINT_NAME* dpn = NULL;
  41578. GENERAL_NAME* gn = NULL;
  41579. ASN1_IA5STRING* uri = NULL;
  41580. STACK_OF(DIST_POINT)* dps = NULL;
  41581. STACK_OF(GENERAL_NAME)* gns = NULL;
  41582. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  41583. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  41584. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41585. if (f != XBADFILE)
  41586. XFCLOSE(f);
  41587. bufPt = buf;
  41588. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41589. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  41590. NID_crl_distribution_points, NULL, NULL));
  41591. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  41592. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  41593. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  41594. ExpectNotNull(dpn = dp->distpoint);
  41595. /* this should be type 0, fullname */
  41596. ExpectIntEQ(dpn->type, 0);
  41597. ExpectNotNull(gns = dp->distpoint->name.fullname);
  41598. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  41599. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  41600. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  41601. ExpectIntEQ(gn->type, GEN_URI);
  41602. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  41603. ExpectNotNull(uri->data);
  41604. ExpectIntGT(uri->length, 0);
  41605. }
  41606. }
  41607. X509_free(x509);
  41608. CRL_DIST_POINTS_free(dps);
  41609. #endif
  41610. return EXPECT_RESULT();
  41611. }
  41612. static int test_wolfSSL_verify_mode(void)
  41613. {
  41614. EXPECT_DECLS;
  41615. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  41616. WOLFSSL* ssl = NULL;
  41617. WOLFSSL_CTX* ctx = NULL;
  41618. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41619. ExpectNotNull(ssl = SSL_new(ctx));
  41620. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41621. SSL_free(ssl);
  41622. ssl = NULL;
  41623. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41624. ExpectNotNull(ssl = SSL_new(ctx));
  41625. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41626. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  41627. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  41628. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  41629. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  41630. SSL_free(ssl);
  41631. ssl = NULL;
  41632. wolfSSL_CTX_set_verify(ctx,
  41633. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  41634. ExpectNotNull(ssl = SSL_new(ctx));
  41635. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41636. ExpectIntEQ(SSL_get_verify_mode(ssl),
  41637. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41638. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  41639. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  41640. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41641. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  41642. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  41643. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  41644. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  41645. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41646. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  41647. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  41648. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  41649. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  41650. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  41651. #endif
  41652. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  41653. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41654. SSL_free(ssl);
  41655. SSL_CTX_free(ctx);
  41656. #endif
  41657. return EXPECT_RESULT();
  41658. }
  41659. static int test_wolfSSL_verify_depth(void)
  41660. {
  41661. EXPECT_DECLS;
  41662. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41663. WOLFSSL* ssl = NULL;
  41664. WOLFSSL_CTX* ctx = NULL;
  41665. long depth;
  41666. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41667. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  41668. ExpectNotNull(ssl = SSL_new(ctx));
  41669. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  41670. SSL_free(ssl);
  41671. ssl = NULL;
  41672. SSL_CTX_set_verify_depth(ctx, -1);
  41673. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  41674. SSL_CTX_set_verify_depth(ctx, 2);
  41675. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  41676. ExpectNotNull(ssl = SSL_new(ctx));
  41677. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  41678. SSL_free(ssl);
  41679. SSL_CTX_free(ctx);
  41680. #endif
  41681. return EXPECT_RESULT();
  41682. }
  41683. static int test_wolfSSL_verify_result(void)
  41684. {
  41685. EXPECT_DECLS;
  41686. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  41687. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  41688. WOLFSSL* ssl = NULL;
  41689. WOLFSSL_CTX* ctx = NULL;
  41690. long result = 0xDEADBEEF;
  41691. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  41692. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41693. ExpectNotNull(ssl = SSL_new(ctx));
  41694. wolfSSL_set_verify_result(ssl, result);
  41695. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  41696. SSL_free(ssl);
  41697. SSL_CTX_free(ctx);
  41698. #endif
  41699. return EXPECT_RESULT();
  41700. }
  41701. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41702. static void sslMsgCb(int w, int version, int type, const void* buf,
  41703. size_t sz, SSL* ssl, void* arg)
  41704. {
  41705. int i;
  41706. unsigned char* pt = (unsigned char*)buf;
  41707. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  41708. (w)?"Writing":"Reading", (int)sz, version, type);
  41709. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  41710. fprintf(stderr, "\n");
  41711. (void)ssl;
  41712. (void)arg;
  41713. }
  41714. #endif /* OPENSSL_EXTRA */
  41715. static int test_wolfSSL_msg_callback(void)
  41716. {
  41717. EXPECT_DECLS;
  41718. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41719. WOLFSSL* ssl = NULL;
  41720. WOLFSSL_CTX* ctx = NULL;
  41721. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41722. ExpectNotNull(ssl = SSL_new(ctx));
  41723. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  41724. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  41725. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  41726. SSL_free(ssl);
  41727. SSL_CTX_free(ctx);
  41728. #endif
  41729. return EXPECT_RESULT();
  41730. }
  41731. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  41732. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  41733. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  41734. static void binary_dump(void *ptr, int size)
  41735. {
  41736. #ifdef WOLFSSL_EVP_PRINT
  41737. int i = 0;
  41738. unsigned char *p = (unsigned char *) ptr;
  41739. fprintf(stderr, "{");
  41740. while ((p != NULL) && (i < size)) {
  41741. if ((i % 8) == 0) {
  41742. fprintf(stderr, "\n");
  41743. fprintf(stderr, " ");
  41744. }
  41745. fprintf(stderr, "0x%02x, ", p[i]);
  41746. i++;
  41747. }
  41748. fprintf(stderr, "\n};\n");
  41749. #else
  41750. (void) ptr;
  41751. (void) size;
  41752. #endif
  41753. }
  41754. static int last_val = 0x0f;
  41755. static int check_result(unsigned char *data, int len)
  41756. {
  41757. int i;
  41758. for ( ; len; ) {
  41759. last_val = (last_val + 1) % 16;
  41760. for (i = 0; i < 16; len--, i++, data++)
  41761. if (*data != last_val) {
  41762. return -1;
  41763. }
  41764. }
  41765. return 0;
  41766. }
  41767. static int r_offset;
  41768. static int w_offset;
  41769. static void init_offset(void)
  41770. {
  41771. r_offset = 0;
  41772. w_offset = 0;
  41773. }
  41774. static void get_record(unsigned char *data, unsigned char *buf, int len)
  41775. {
  41776. XMEMCPY(buf, data+r_offset, len);
  41777. r_offset += len;
  41778. }
  41779. static void set_record(unsigned char *data, unsigned char *buf, int len)
  41780. {
  41781. XMEMCPY(data+w_offset, buf, len);
  41782. w_offset += len;
  41783. }
  41784. static void set_plain(unsigned char *plain, int rec)
  41785. {
  41786. int i, j;
  41787. unsigned char *p = plain;
  41788. #define BLOCKSZ 16
  41789. for (i=0; i<(rec/BLOCKSZ); i++) {
  41790. for (j=0; j<BLOCKSZ; j++)
  41791. *p++ = (i % 16);
  41792. }
  41793. }
  41794. #endif
  41795. static int test_wolfSSL_EVP_Cipher_extra(void)
  41796. {
  41797. EXPECT_DECLS;
  41798. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  41799. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  41800. /* aes128-cbc, keylen=16, ivlen=16 */
  41801. byte aes128_cbc_key[] = {
  41802. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  41803. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  41804. };
  41805. byte aes128_cbc_iv[] = {
  41806. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  41807. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  41808. };
  41809. /* teset data size table */
  41810. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  41811. int test_drive2[] = {8, 3, 8, 512, 0};
  41812. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  41813. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  41814. int test_drive_len[100];
  41815. int ret = 0;
  41816. EVP_CIPHER_CTX *evp = NULL;
  41817. int ilen = 0;
  41818. int klen = 0;
  41819. int i, j;
  41820. const EVP_CIPHER *type;
  41821. byte *iv;
  41822. byte *key;
  41823. int ivlen;
  41824. int keylen;
  41825. #define RECORDS 16
  41826. #define BUFFSZ 512
  41827. byte plain [BUFFSZ * RECORDS];
  41828. byte cipher[BUFFSZ * RECORDS];
  41829. byte inb[BUFFSZ];
  41830. byte outb[BUFFSZ+16];
  41831. int outl = 0;
  41832. int inl;
  41833. iv = aes128_cbc_iv;
  41834. ivlen = sizeof(aes128_cbc_iv);
  41835. key = aes128_cbc_key;
  41836. keylen = sizeof(aes128_cbc_key);
  41837. type = EVP_aes_128_cbc();
  41838. set_plain(plain, BUFFSZ * RECORDS);
  41839. SSL_library_init();
  41840. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  41841. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41842. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41843. klen = EVP_CIPHER_CTX_key_length(evp);
  41844. if (klen > 0 && keylen != klen) {
  41845. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41846. }
  41847. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41848. if (ilen > 0 && ivlen != ilen) {
  41849. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41850. }
  41851. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41852. for (j = 0; j<RECORDS; j++)
  41853. {
  41854. inl = BUFFSZ;
  41855. get_record(plain, inb, inl);
  41856. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  41857. set_record(cipher, outb, outl);
  41858. }
  41859. for (i = 0; test_drive[i]; i++) {
  41860. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41861. init_offset();
  41862. test_drive_len[i] = 0;
  41863. for (j = 0; test_drive[i][j]; j++)
  41864. {
  41865. inl = test_drive[i][j];
  41866. test_drive_len[i] += inl;
  41867. get_record(plain, inb, inl);
  41868. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  41869. 0);
  41870. /* output to cipher buffer, so that following Dec test can detect
  41871. if any error */
  41872. set_record(cipher, outb, outl);
  41873. }
  41874. EVP_CipherFinal(evp, outb, &outl);
  41875. if (outl > 0)
  41876. set_record(cipher, outb, outl);
  41877. }
  41878. for (i = 0; test_drive[i]; i++) {
  41879. last_val = 0x0f;
  41880. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  41881. init_offset();
  41882. for (j = 0; test_drive[i][j]; j++) {
  41883. inl = test_drive[i][j];
  41884. get_record(cipher, inb, inl);
  41885. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  41886. 0);
  41887. binary_dump(outb, outl);
  41888. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  41889. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  41890. }
  41891. ret = EVP_CipherFinal(evp, outb, &outl);
  41892. binary_dump(outb, outl);
  41893. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  41894. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  41895. ExpectTrue(ret);
  41896. }
  41897. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  41898. EVP_CIPHER_CTX_free(evp);
  41899. evp = NULL;
  41900. /* Do an extra test to verify correct behavior with empty input. */
  41901. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  41902. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41903. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41904. klen = EVP_CIPHER_CTX_key_length(evp);
  41905. if (klen > 0 && keylen != klen) {
  41906. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41907. }
  41908. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41909. if (ilen > 0 && ivlen != ilen) {
  41910. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41911. }
  41912. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41913. /* outl should be set to 0 after passing NULL, 0 for input args. */
  41914. outl = -1;
  41915. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  41916. ExpectIntEQ(outl, 0);
  41917. EVP_CIPHER_CTX_free(evp);
  41918. #endif /* test_EVP_Cipher */
  41919. return EXPECT_RESULT();
  41920. }
  41921. static int test_wolfSSL_PEM_read_DHparams(void)
  41922. {
  41923. EXPECT_DECLS;
  41924. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  41925. !defined(NO_FILESYSTEM)
  41926. DH* dh = NULL;
  41927. XFILE fp = XBADFILE;
  41928. unsigned char derOut[300];
  41929. unsigned char* derOutBuf = derOut;
  41930. int derOutSz = 0;
  41931. unsigned char derExpected[300];
  41932. int derExpectedSz = 0;
  41933. XMEMSET(derOut, 0, sizeof(derOut));
  41934. XMEMSET(derExpected, 0, sizeof(derExpected));
  41935. /* open DH param file, read into DH struct */
  41936. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  41937. /* bad args */
  41938. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  41939. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  41940. /* good args */
  41941. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41942. if (fp != XBADFILE) {
  41943. XFCLOSE(fp);
  41944. fp = XBADFILE;
  41945. }
  41946. /* read in certs/dh2048.der for comparison against exported params */
  41947. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  41948. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  41949. fp), 0);
  41950. if (fp != XBADFILE) {
  41951. XFCLOSE(fp);
  41952. fp = XBADFILE;
  41953. }
  41954. /* export DH back to DER and compare */
  41955. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  41956. ExpectIntEQ(derOutSz, derExpectedSz);
  41957. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  41958. DH_free(dh);
  41959. dh = NULL;
  41960. /* Test parsing with X9.42 header */
  41961. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  41962. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41963. if (fp != XBADFILE)
  41964. XFCLOSE(fp);
  41965. DH_free(dh);
  41966. dh = NULL;
  41967. #endif
  41968. return EXPECT_RESULT();
  41969. }
  41970. static int test_wolfSSL_X509_get_serialNumber(void)
  41971. {
  41972. EXPECT_DECLS;
  41973. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  41974. ASN1_INTEGER* a = NULL;
  41975. BIGNUM* bn = NULL;
  41976. X509* x509 = NULL;
  41977. char *serialHex = NULL;
  41978. byte serial[3];
  41979. int serialSz;
  41980. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41981. SSL_FILETYPE_PEM));
  41982. ExpectNotNull(a = X509_get_serialNumber(x509));
  41983. /* check on value of ASN1 Integer */
  41984. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  41985. a = NULL;
  41986. /* test setting serial number and then retrieving it */
  41987. ExpectNotNull(a = ASN1_INTEGER_new());
  41988. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  41989. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41990. serialSz = sizeof(serial);
  41991. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41992. WOLFSSL_SUCCESS);
  41993. ExpectIntEQ(serialSz, 1);
  41994. ExpectIntEQ(serial[0], 3);
  41995. ASN1_INTEGER_free(a);
  41996. a = NULL;
  41997. /* test setting serial number with 0's in it */
  41998. serial[0] = 0x01;
  41999. serial[1] = 0x00;
  42000. serial[2] = 0x02;
  42001. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  42002. if (a != NULL) {
  42003. a->data[0] = ASN_INTEGER;
  42004. a->data[1] = sizeof(serial);
  42005. XMEMCPY(&a->data[2], serial, sizeof(serial));
  42006. a->length = sizeof(serial) + 2;
  42007. }
  42008. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42009. XMEMSET(serial, 0, sizeof(serial));
  42010. serialSz = sizeof(serial);
  42011. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42012. WOLFSSL_SUCCESS);
  42013. ExpectIntEQ(serialSz, 3);
  42014. ExpectIntEQ(serial[0], 0x01);
  42015. ExpectIntEQ(serial[1], 0x00);
  42016. ExpectIntEQ(serial[2], 0x02);
  42017. ASN1_INTEGER_free(a);
  42018. a = NULL;
  42019. X509_free(x509); /* free's a */
  42020. ExpectNotNull(serialHex = BN_bn2hex(bn));
  42021. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42022. ExpectStrEQ(serialHex, "01");
  42023. #else
  42024. ExpectStrEQ(serialHex, "1");
  42025. #endif
  42026. OPENSSL_free(serialHex);
  42027. ExpectIntEQ(BN_get_word(bn), 1);
  42028. BN_free(bn);
  42029. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  42030. ExpectNotNull(a = ASN1_INTEGER_new());
  42031. if (a != NULL) {
  42032. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  42033. DYNAMIC_TYPE_OPENSSL));
  42034. a->isDynamic = 1;
  42035. ASN1_INTEGER_free(a);
  42036. }
  42037. #endif
  42038. return EXPECT_RESULT();
  42039. }
  42040. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  42041. {
  42042. EXPECT_DECLS;
  42043. #if defined(OPENSSL_EXTRA)
  42044. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  42045. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  42046. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  42047. #endif
  42048. return EXPECT_RESULT();
  42049. }
  42050. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  42051. {
  42052. EXPECT_DECLS;
  42053. #if defined(OPENSSL_EXTRA)
  42054. #define MAX_HEXSTR_BUFSZ 9
  42055. #define NUM_CASES 5
  42056. struct Output {
  42057. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  42058. long ret;
  42059. };
  42060. int i;
  42061. int j;
  42062. const char* inputs[NUM_CASES] = {
  42063. "aabcd1357e",
  42064. "01:12:23:34:a5:b6:c7:d8:e9",
  42065. ":01:02",
  42066. "012",
  42067. ":ab:ac:d"
  42068. };
  42069. struct Output expectedOutputs[NUM_CASES] = {
  42070. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  42071. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  42072. {{0x01, 0x02}, 2},
  42073. {{0x00}, 0},
  42074. {{0x00}, 0}
  42075. };
  42076. long len = 0;
  42077. unsigned char* returnedBuf = NULL;
  42078. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  42079. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  42080. if (returnedBuf == NULL) {
  42081. ExpectIntEQ(expectedOutputs[i].ret, 0);
  42082. continue;
  42083. }
  42084. ExpectIntEQ(expectedOutputs[i].ret, len);
  42085. for (j = 0; j < len; ++j) {
  42086. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  42087. }
  42088. OPENSSL_free(returnedBuf);
  42089. returnedBuf = NULL;
  42090. }
  42091. #endif
  42092. return EXPECT_RESULT();
  42093. }
  42094. static int test_wolfSSL_X509_CA_num(void)
  42095. {
  42096. EXPECT_DECLS;
  42097. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  42098. defined(HAVE_ECC) && !defined(NO_RSA)
  42099. WOLFSSL_X509_STORE *store = NULL;
  42100. WOLFSSL_X509 *x509_1 = NULL;
  42101. WOLFSSL_X509 *x509_2 = NULL;
  42102. int ca_num = 0;
  42103. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  42104. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42105. WOLFSSL_FILETYPE_PEM));
  42106. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  42107. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  42108. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  42109. WOLFSSL_FILETYPE_PEM));
  42110. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  42111. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  42112. wolfSSL_X509_free(x509_1);
  42113. wolfSSL_X509_free(x509_2);
  42114. wolfSSL_X509_STORE_free(store);
  42115. #endif
  42116. return EXPECT_RESULT();
  42117. }
  42118. static int test_wolfSSL_X509_check_ca(void)
  42119. {
  42120. EXPECT_DECLS;
  42121. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42122. WOLFSSL_X509 *x509 = NULL;
  42123. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42124. WOLFSSL_FILETYPE_PEM));
  42125. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  42126. wolfSSL_X509_free(x509);
  42127. #endif
  42128. return EXPECT_RESULT();
  42129. }
  42130. static int test_wolfSSL_X509_check_ip_asc(void)
  42131. {
  42132. EXPECT_DECLS;
  42133. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42134. WOLFSSL_X509 *x509 = NULL;
  42135. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  42136. WOLFSSL_FILETYPE_PEM));
  42137. #if 0
  42138. /* TODO: add cert gen for testing positive case */
  42139. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  42140. #endif
  42141. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  42142. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  42143. wolfSSL_X509_free(x509);
  42144. #endif
  42145. return EXPECT_RESULT();
  42146. }
  42147. static int test_wolfSSL_make_cert(void)
  42148. {
  42149. EXPECT_DECLS;
  42150. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  42151. defined(WOLFSSL_CERT_EXT)
  42152. int ret = 0;
  42153. Cert cert;
  42154. CertName name;
  42155. RsaKey key;
  42156. WC_RNG rng;
  42157. byte der[FOURK_BUF];
  42158. word32 idx = 0;
  42159. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  42160. #ifdef OPENSSL_EXTRA
  42161. const unsigned char* pt = NULL;
  42162. int certSz = 0;
  42163. X509* x509 = NULL;
  42164. X509_NAME* x509name = NULL;
  42165. X509_NAME_ENTRY* entry = NULL;
  42166. ASN1_STRING* entryValue = NULL;
  42167. #endif
  42168. XMEMSET(&name, 0, sizeof(CertName));
  42169. /* set up cert name */
  42170. XMEMCPY(name.country, "US", sizeof("US"));
  42171. name.countryEnc = CTC_PRINTABLE;
  42172. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  42173. name.stateEnc = CTC_UTF8;
  42174. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  42175. name.localityEnc = CTC_UTF8;
  42176. XMEMCPY(name.sur, "Test", sizeof("Test"));
  42177. name.surEnc = CTC_UTF8;
  42178. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  42179. name.orgEnc = CTC_UTF8;
  42180. XMEMCPY(name.unit, "Development", sizeof("Development"));
  42181. name.unitEnc = CTC_UTF8;
  42182. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  42183. name.commonNameEnc = CTC_UTF8;
  42184. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  42185. name.serialDevEnc = CTC_PRINTABLE;
  42186. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  42187. name.userIdEnc = CTC_PRINTABLE;
  42188. #ifdef WOLFSSL_MULTI_ATTRIB
  42189. #if CTC_MAX_ATTRIB > 2
  42190. {
  42191. NameAttrib* n;
  42192. n = &name.name[0];
  42193. n->id = ASN_DOMAIN_COMPONENT;
  42194. n->type = CTC_UTF8;
  42195. n->sz = sizeof("com");
  42196. XMEMCPY(n->value, "com", sizeof("com"));
  42197. n = &name.name[1];
  42198. n->id = ASN_DOMAIN_COMPONENT;
  42199. n->type = CTC_UTF8;
  42200. n->sz = sizeof("wolfssl");
  42201. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  42202. }
  42203. #endif
  42204. #endif /* WOLFSSL_MULTI_ATTRIB */
  42205. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  42206. #ifndef HAVE_FIPS
  42207. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  42208. #else
  42209. ExpectIntEQ(wc_InitRng(&rng), 0);
  42210. #endif
  42211. /* load test RSA key */
  42212. idx = 0;
  42213. #if defined(USE_CERT_BUFFERS_1024)
  42214. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  42215. sizeof_server_key_der_1024), 0);
  42216. #elif defined(USE_CERT_BUFFERS_2048)
  42217. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  42218. sizeof_server_key_der_2048), 0);
  42219. #else
  42220. /* error case, no RSA key loaded, happens later */
  42221. (void)idx;
  42222. #endif
  42223. XMEMSET(&cert, 0 , sizeof(Cert));
  42224. ExpectIntEQ(wc_InitCert(&cert), 0);
  42225. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  42226. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  42227. cert.serialSz = (int)sizeof(mySerial);
  42228. cert.isCA = 1;
  42229. #ifndef NO_SHA256
  42230. cert.sigType = CTC_SHA256wRSA;
  42231. #else
  42232. cert.sigType = CTC_SHAwRSA;
  42233. #endif
  42234. /* add SKID from the Public Key */
  42235. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42236. /* add AKID from the Public Key */
  42237. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42238. ret = 0;
  42239. do {
  42240. #if defined(WOLFSSL_ASYNC_CRYPT)
  42241. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  42242. #endif
  42243. if (ret >= 0) {
  42244. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  42245. }
  42246. } while (ret == WC_PENDING_E);
  42247. ExpectIntGT(ret, 0);
  42248. #ifdef OPENSSL_EXTRA
  42249. /* der holds a certificate with DC's now check X509 parsing of it */
  42250. certSz = ret;
  42251. pt = der;
  42252. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  42253. ExpectNotNull(x509name = X509_get_subject_name(x509));
  42254. #ifdef WOLFSSL_MULTI_ATTRIB
  42255. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42256. -1)), 5);
  42257. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42258. idx)), 6);
  42259. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42260. idx)), -1);
  42261. #endif /* WOLFSSL_MULTI_ATTRIB */
  42262. /* compare DN at index 0 */
  42263. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  42264. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42265. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  42266. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  42267. #ifndef WOLFSSL_MULTI_ATTRIB
  42268. /* compare Serial Number */
  42269. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  42270. -1)), 7);
  42271. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42272. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42273. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  42274. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  42275. #endif
  42276. #ifdef WOLFSSL_MULTI_ATTRIB
  42277. /* get first and second DC and compare result */
  42278. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42279. -1)), 5);
  42280. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42281. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42282. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  42283. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42284. idx)), 6);
  42285. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42286. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42287. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  42288. #endif /* WOLFSSL_MULTI_ATTRIB */
  42289. /* try invalid index locations for regression test and sanity check */
  42290. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  42291. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  42292. X509_free(x509);
  42293. #endif /* OPENSSL_EXTRA */
  42294. wc_FreeRsaKey(&key);
  42295. wc_FreeRng(&rng);
  42296. #endif
  42297. return EXPECT_RESULT();
  42298. }
  42299. static int test_wolfSSL_X509_get_version(void)
  42300. {
  42301. EXPECT_DECLS;
  42302. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42303. WOLFSSL_X509 *x509 = NULL;
  42304. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42305. WOLFSSL_FILETYPE_PEM));
  42306. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  42307. wolfSSL_X509_free(x509);
  42308. #endif
  42309. return EXPECT_RESULT();
  42310. }
  42311. #if defined(OPENSSL_ALL)
  42312. static int test_wolfSSL_sk_CIPHER_description(void)
  42313. {
  42314. EXPECT_DECLS;
  42315. #if !defined(NO_RSA)
  42316. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  42317. int i;
  42318. int numCiphers = 0;
  42319. const SSL_METHOD *method = NULL;
  42320. const SSL_CIPHER *cipher = NULL;
  42321. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  42322. SSL_CTX *ctx = NULL;
  42323. SSL *ssl = NULL;
  42324. char buf[256];
  42325. char test_str[9] = "0000000";
  42326. const char badStr[] = "unknown";
  42327. const char certPath[] = "./certs/client-cert.pem";
  42328. XMEMSET(buf, 0, sizeof(buf));
  42329. ExpectNotNull(method = TLSv1_2_client_method());
  42330. ExpectNotNull(ctx = SSL_CTX_new(method));
  42331. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42332. SSL_CTX_set_verify_depth(ctx, 4);
  42333. SSL_CTX_set_options(ctx, flags);
  42334. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  42335. WOLFSSL_SUCCESS);
  42336. ExpectNotNull(ssl = SSL_new(ctx));
  42337. /* SSL_get_ciphers returns a stack of all configured ciphers
  42338. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  42339. */
  42340. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  42341. /* loop through the amount of supportedCiphers */
  42342. numCiphers = sk_num(supportedCiphers);
  42343. for (i = 0; i < numCiphers; ++i) {
  42344. int j;
  42345. /* sk_value increments "sk->data.cipher->cipherOffset".
  42346. * wolfSSL_sk_CIPHER_description sets the description for
  42347. * the cipher based on the provided offset.
  42348. */
  42349. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  42350. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  42351. }
  42352. /* Search cipher description string for "unknown" descriptor */
  42353. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  42354. int k = 0;
  42355. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  42356. test_str[k] = badStr[k];
  42357. j++;
  42358. k++;
  42359. }
  42360. }
  42361. /* Fail if test_str == badStr == "unknown" */
  42362. ExpectStrNE(test_str,badStr);
  42363. }
  42364. SSL_free(ssl);
  42365. SSL_CTX_free(ctx);
  42366. #endif
  42367. return EXPECT_RESULT();
  42368. }
  42369. static int test_wolfSSL_get_ciphers_compat(void)
  42370. {
  42371. EXPECT_DECLS;
  42372. #if !defined(NO_RSA)
  42373. const SSL_METHOD *method = NULL;
  42374. const char certPath[] = "./certs/client-cert.pem";
  42375. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  42376. SSL_CTX *ctx = NULL;
  42377. WOLFSSL *ssl = NULL;
  42378. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  42379. ExpectNotNull(method = SSLv23_client_method());
  42380. ExpectNotNull(ctx = SSL_CTX_new(method));
  42381. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42382. SSL_CTX_set_verify_depth(ctx, 4);
  42383. SSL_CTX_set_options(ctx, flags);
  42384. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  42385. WOLFSSL_SUCCESS);
  42386. ExpectNotNull(ssl = SSL_new(ctx));
  42387. /* Test Bad NULL input */
  42388. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  42389. /* Test for Good input */
  42390. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  42391. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  42392. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  42393. SSL_free(ssl);
  42394. SSL_CTX_free(ctx);
  42395. #endif
  42396. return EXPECT_RESULT();
  42397. }
  42398. static int test_wolfSSL_X509_PUBKEY_get(void)
  42399. {
  42400. EXPECT_DECLS;
  42401. WOLFSSL_X509_PUBKEY pubkey;
  42402. WOLFSSL_X509_PUBKEY* key;
  42403. WOLFSSL_EVP_PKEY evpkey ;
  42404. WOLFSSL_EVP_PKEY* evpPkey;
  42405. WOLFSSL_EVP_PKEY* retEvpPkey;
  42406. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  42407. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  42408. key = &pubkey;
  42409. evpPkey = &evpkey;
  42410. evpPkey->type = WOLFSSL_SUCCESS;
  42411. key->pkey = evpPkey;
  42412. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  42413. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  42414. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  42415. key->pkey = NULL;
  42416. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  42417. return EXPECT_RESULT();
  42418. }
  42419. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  42420. {
  42421. EXPECT_DECLS;
  42422. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42423. DSA *dsa = NULL;
  42424. DSA *setDsa = NULL;
  42425. EVP_PKEY *pkey = NULL;
  42426. EVP_PKEY *set1Pkey = NULL;
  42427. SHA_CTX sha;
  42428. byte signature[DSA_SIG_SIZE];
  42429. byte hash[WC_SHA_DIGEST_SIZE];
  42430. word32 bytes;
  42431. int answer;
  42432. #ifdef USE_CERT_BUFFERS_1024
  42433. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  42434. int dsaKeySz = sizeof_dsa_key_der_1024;
  42435. byte tmp[ONEK_BUF];
  42436. XMEMSET(tmp, 0, sizeof(tmp));
  42437. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  42438. bytes = dsaKeySz;
  42439. #elif defined(USE_CERT_BUFFERS_2048)
  42440. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  42441. int dsaKeySz = sizeof_dsa_key_der_2048;
  42442. byte tmp[TWOK_BUF];
  42443. XMEMSET(tmp, 0, sizeof(tmp));
  42444. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  42445. bytes = dsaKeySz;
  42446. #else
  42447. byte tmp[TWOK_BUF];
  42448. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  42449. int dsaKeySz;
  42450. XFILE fp = XBADFILE;
  42451. XMEMSET(tmp, 0, sizeof(tmp));
  42452. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  42453. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  42454. if (fp != XBADFILE)
  42455. XFCLOSE(fp);
  42456. #endif /* END USE_CERT_BUFFERS_1024 */
  42457. /* Create hash to later Sign and Verify */
  42458. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  42459. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  42460. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  42461. /* Initialize pkey with der format dsa key */
  42462. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  42463. (long)dsaKeySz));
  42464. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  42465. /* Should Fail: NULL argument */
  42466. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  42467. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  42468. /* Should Pass: Initialized pkey argument */
  42469. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  42470. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  42471. #ifdef USE_CERT_BUFFERS_1024
  42472. ExpectIntEQ(DSA_bits(dsa), 1024);
  42473. #else
  42474. ExpectIntEQ(DSA_bits(dsa), 2048);
  42475. #endif
  42476. /* Sign */
  42477. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  42478. /* Verify. */
  42479. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  42480. WOLFSSL_SUCCESS);
  42481. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  42482. /* Should Fail: set1Pkey not initialized */
  42483. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  42484. /* Initialize set1Pkey */
  42485. set1Pkey = EVP_PKEY_new();
  42486. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  42487. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  42488. WOLFSSL_SUCCESS);
  42489. /* Should Pass: set dsa into set1Pkey */
  42490. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  42491. DSA_free(dsa);
  42492. DSA_free(setDsa);
  42493. EVP_PKEY_free(pkey);
  42494. EVP_PKEY_free(set1Pkey);
  42495. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42496. return EXPECT_RESULT();
  42497. } /* END test_EVP_PKEY_set1_get1_DSA */
  42498. static int test_wolfSSL_DSA_generate_parameters(void)
  42499. {
  42500. EXPECT_DECLS;
  42501. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  42502. !defined(HAVE_FIPS)
  42503. DSA *dsa = NULL;
  42504. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  42505. NULL));
  42506. DSA_free(dsa);
  42507. #endif
  42508. return EXPECT_RESULT();
  42509. }
  42510. static int test_wolfSSL_DSA_SIG(void)
  42511. {
  42512. EXPECT_DECLS;
  42513. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  42514. !defined(HAVE_FIPS)
  42515. DSA *dsa = NULL;
  42516. DSA *dsa2 = NULL;
  42517. DSA_SIG *sig = NULL;
  42518. const BIGNUM *p = NULL;
  42519. const BIGNUM *q = NULL;
  42520. const BIGNUM *g = NULL;
  42521. const BIGNUM *pub = NULL;
  42522. const BIGNUM *priv = NULL;
  42523. BIGNUM *dup_p = NULL;
  42524. BIGNUM *dup_q = NULL;
  42525. BIGNUM *dup_g = NULL;
  42526. BIGNUM *dup_pub = NULL;
  42527. BIGNUM *dup_priv = NULL;
  42528. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  42529. ExpectNotNull(dsa = DSA_new());
  42530. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  42531. NULL), 1);
  42532. ExpectIntEQ(DSA_generate_key(dsa), 1);
  42533. DSA_get0_pqg(dsa, &p, &q, &g);
  42534. DSA_get0_key(dsa, &pub, &priv);
  42535. ExpectNotNull(dup_p = BN_dup(p));
  42536. ExpectNotNull(dup_q = BN_dup(q));
  42537. ExpectNotNull(dup_g = BN_dup(g));
  42538. ExpectNotNull(dup_pub = BN_dup(pub));
  42539. ExpectNotNull(dup_priv = BN_dup(priv));
  42540. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  42541. ExpectNotNull(dsa2 = DSA_new());
  42542. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  42543. if (EXPECT_FAIL()) {
  42544. BN_free(dup_p);
  42545. BN_free(dup_q);
  42546. BN_free(dup_g);
  42547. }
  42548. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  42549. if (EXPECT_FAIL()) {
  42550. BN_free(dup_pub);
  42551. BN_free(dup_priv);
  42552. }
  42553. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  42554. DSA_free(dsa);
  42555. DSA_free(dsa2);
  42556. DSA_SIG_free(sig);
  42557. #endif
  42558. return EXPECT_RESULT();
  42559. }
  42560. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  42561. {
  42562. EXPECT_DECLS;
  42563. #ifdef HAVE_ECC
  42564. WOLFSSL_EC_KEY* ecKey = NULL;
  42565. WOLFSSL_EC_KEY* ecGet1 = NULL;
  42566. EVP_PKEY* pkey = NULL;
  42567. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42568. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42569. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  42570. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42571. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42572. /* Should fail since ecKey is empty */
  42573. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42574. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42575. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42576. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  42577. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  42578. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  42579. wolfSSL_EC_KEY_free(ecKey);
  42580. wolfSSL_EC_KEY_free(ecGet1);
  42581. EVP_PKEY_free(pkey);
  42582. #endif /* HAVE_ECC */
  42583. return EXPECT_RESULT();
  42584. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  42585. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  42586. {
  42587. EXPECT_DECLS;
  42588. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  42589. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42590. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  42591. DH *dh = NULL;
  42592. DH *setDh = NULL;
  42593. EVP_PKEY *pkey = NULL;
  42594. XFILE f = XBADFILE;
  42595. unsigned char buf[4096];
  42596. const unsigned char* pt = buf;
  42597. const char* dh2048 = "./certs/dh2048.der";
  42598. long len = 0;
  42599. int code = -1;
  42600. XMEMSET(buf, 0, sizeof(buf));
  42601. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  42602. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42603. if (f != XBADFILE)
  42604. XFCLOSE(f);
  42605. /* Load dh2048.der into DH with internal format */
  42606. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42607. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  42608. ExpectIntEQ(code, 0);
  42609. code = -1;
  42610. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42611. /* Set DH into PKEY */
  42612. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  42613. /* Get DH from PKEY */
  42614. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  42615. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  42616. ExpectIntEQ(code, 0);
  42617. EVP_PKEY_free(pkey);
  42618. DH_free(setDh);
  42619. setDh = NULL;
  42620. DH_free(dh);
  42621. dh = NULL;
  42622. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  42623. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  42624. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  42625. return EXPECT_RESULT();
  42626. } /* END test_EVP_PKEY_set1_get1_DH */
  42627. static int test_wolfSSL_CTX_ctrl(void)
  42628. {
  42629. EXPECT_DECLS;
  42630. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42631. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42632. char caFile[] = "./certs/client-ca.pem";
  42633. char clientFile[] = "./certs/client-cert.pem";
  42634. SSL_CTX* ctx = NULL;
  42635. X509* x509 = NULL;
  42636. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42637. byte buf[6000];
  42638. char file[] = "./certs/dsaparams.pem";
  42639. XFILE f = XBADFILE;
  42640. int bytes = 0;
  42641. BIO* bio = NULL;
  42642. DSA* dsa = NULL;
  42643. DH* dh = NULL;
  42644. #endif
  42645. #ifdef HAVE_ECC
  42646. WOLFSSL_EC_KEY* ecKey = NULL;
  42647. #endif
  42648. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42649. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  42650. WOLFSSL_FILETYPE_PEM));
  42651. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  42652. if (EXPECT_FAIL()) {
  42653. wolfSSL_X509_free(x509);
  42654. }
  42655. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  42656. WOLFSSL_FILETYPE_PEM));
  42657. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42658. /* Initialize DH */
  42659. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  42660. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  42661. if (f != XBADFILE)
  42662. XFCLOSE(f);
  42663. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  42664. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  42665. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  42666. #endif
  42667. #ifdef HAVE_ECC
  42668. /* Initialize WOLFSSL_EC_KEY */
  42669. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42670. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42671. #endif
  42672. /* additional test of getting EVP_PKEY key size from X509
  42673. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  42674. * allowed with user RSA */
  42675. {
  42676. EVP_PKEY* pkey = NULL;
  42677. #if defined(HAVE_ECC)
  42678. X509* ecX509 = NULL;
  42679. #endif /* HAVE_ECC */
  42680. ExpectNotNull(pkey = X509_get_pubkey(x509));
  42681. /* current RSA key is 2048 bit (256 bytes) */
  42682. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  42683. EVP_PKEY_free(pkey);
  42684. pkey = NULL;
  42685. #if defined(HAVE_ECC)
  42686. #if defined(USE_CERT_BUFFERS_256)
  42687. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  42688. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  42689. SSL_FILETYPE_ASN1));
  42690. #else
  42691. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  42692. cliEccCertFile, SSL_FILETYPE_PEM));
  42693. #endif
  42694. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  42695. /* current ECC key is 256 bit (32 bytes) */
  42696. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  42697. X509_free(ecX509);
  42698. EVP_PKEY_free(pkey);
  42699. #endif /* HAVE_ECC */
  42700. }
  42701. /* Tests should fail with passed in NULL pointer */
  42702. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  42703. SSL_FAILURE);
  42704. #if !defined(NO_DH) && !defined(NO_DSA)
  42705. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  42706. SSL_FAILURE);
  42707. #endif
  42708. #ifdef HAVE_ECC
  42709. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  42710. SSL_FAILURE);
  42711. #endif
  42712. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  42713. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  42714. */
  42715. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  42716. SSL_SUCCESS);
  42717. if (EXPECT_FAIL()) {
  42718. wolfSSL_X509_free(x509);
  42719. }
  42720. /* Test with SSL_CTRL_OPTIONS
  42721. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  42722. */
  42723. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  42724. NULL) == SSL_OP_NO_TLSv1);
  42725. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  42726. /* Test with SSL_CTRL_SET_TMP_DH
  42727. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  42728. */
  42729. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42730. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  42731. SSL_SUCCESS);
  42732. #endif
  42733. /* Test with SSL_CTRL_SET_TMP_ECDH
  42734. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  42735. */
  42736. #ifdef HAVE_ECC
  42737. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  42738. SSL_SUCCESS);
  42739. #endif
  42740. #ifdef WOLFSSL_ENCRYPTED_KEYS
  42741. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  42742. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  42743. #endif
  42744. /* Test for min/max proto */
  42745. #ifndef WOLFSSL_NO_TLS12
  42746. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42747. 0, NULL), SSL_SUCCESS);
  42748. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42749. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42750. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  42751. #endif
  42752. #ifdef WOLFSSL_TLS13
  42753. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42754. 0, NULL), SSL_SUCCESS);
  42755. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42756. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  42757. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  42758. #ifndef WOLFSSL_NO_TLS12
  42759. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42760. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42761. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  42762. #endif
  42763. #endif
  42764. /* Cleanup and Pass */
  42765. #if !defined(NO_DH) && !defined(NO_DSA)
  42766. #ifndef NO_BIO
  42767. BIO_free(bio);
  42768. DSA_free(dsa);
  42769. DH_free(dh);
  42770. dh = NULL;
  42771. #endif
  42772. #endif
  42773. #ifdef HAVE_ECC
  42774. wolfSSL_EC_KEY_free(ecKey);
  42775. #endif
  42776. SSL_CTX_free(ctx);
  42777. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42778. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  42779. return EXPECT_RESULT();
  42780. }
  42781. static int test_wolfSSL_EVP_PKEY_assign(void)
  42782. {
  42783. EXPECT_DECLS;
  42784. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  42785. int type;
  42786. WOLFSSL_EVP_PKEY* pkey = NULL;
  42787. #ifndef NO_RSA
  42788. WOLFSSL_RSA* rsa = NULL;
  42789. #endif
  42790. #ifndef NO_DSA
  42791. WOLFSSL_DSA* dsa = NULL;
  42792. #endif
  42793. #ifdef HAVE_ECC
  42794. WOLFSSL_EC_KEY* ecKey = NULL;
  42795. #endif
  42796. #ifndef NO_RSA
  42797. type = EVP_PKEY_RSA;
  42798. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42799. ExpectNotNull(rsa = wolfSSL_RSA_new());
  42800. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  42801. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42802. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  42803. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  42804. if (EXPECT_FAIL()) {
  42805. wolfSSL_RSA_free(rsa);
  42806. }
  42807. wolfSSL_EVP_PKEY_free(pkey);
  42808. pkey = NULL;
  42809. #endif /* NO_RSA */
  42810. #ifndef NO_DSA
  42811. type = EVP_PKEY_DSA;
  42812. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42813. ExpectNotNull(dsa = wolfSSL_DSA_new());
  42814. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  42815. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42816. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  42817. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  42818. if (EXPECT_FAIL()) {
  42819. wolfSSL_DSA_free(dsa);
  42820. }
  42821. wolfSSL_EVP_PKEY_free(pkey);
  42822. pkey = NULL;
  42823. #endif /* NO_DSA */
  42824. #ifdef HAVE_ECC
  42825. type = EVP_PKEY_EC;
  42826. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42827. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42828. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  42829. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42830. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  42831. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  42832. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42833. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  42834. if (EXPECT_FAIL()) {
  42835. wolfSSL_EC_KEY_free(ecKey);
  42836. }
  42837. wolfSSL_EVP_PKEY_free(pkey);
  42838. pkey = NULL;
  42839. #endif /* HAVE_ECC */
  42840. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  42841. return EXPECT_RESULT();
  42842. }
  42843. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  42844. {
  42845. EXPECT_DECLS;
  42846. #if !defined(NO_DH) && \
  42847. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42848. XFILE f = XBADFILE;
  42849. unsigned char buf[4096];
  42850. const unsigned char* pt = buf;
  42851. const char* params1 = "./certs/dh2048.der";
  42852. long len = 0;
  42853. WOLFSSL_DH* dh = NULL;
  42854. WOLFSSL_EVP_PKEY* pkey = NULL;
  42855. XMEMSET(buf, 0, sizeof(buf));
  42856. /* Load DH parameters DER. */
  42857. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  42858. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42859. if (f != XBADFILE)
  42860. XFCLOSE(f);
  42861. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42862. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  42863. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42864. /* Bad cases */
  42865. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  42866. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  42867. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  42868. /* Good case */
  42869. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  42870. if (EXPECT_FAIL()) {
  42871. wolfSSL_DH_free(dh);
  42872. }
  42873. EVP_PKEY_free(pkey);
  42874. #endif
  42875. return EXPECT_RESULT();
  42876. }
  42877. static int test_wolfSSL_EVP_PKEY_base_id(void)
  42878. {
  42879. EXPECT_DECLS;
  42880. WOLFSSL_EVP_PKEY* pkey = NULL;
  42881. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42882. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  42883. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  42884. EVP_PKEY_free(pkey);
  42885. return EXPECT_RESULT();
  42886. }
  42887. static int test_wolfSSL_EVP_PKEY_id(void)
  42888. {
  42889. EXPECT_DECLS;
  42890. WOLFSSL_EVP_PKEY* pkey = NULL;
  42891. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42892. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  42893. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  42894. EVP_PKEY_free(pkey);
  42895. return EXPECT_RESULT();
  42896. }
  42897. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  42898. {
  42899. EXPECT_DECLS;
  42900. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  42901. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  42902. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  42903. ECC_MIN_KEY_SZ <= 256)
  42904. EVP_PKEY_CTX* ctx = NULL;
  42905. EVP_PKEY* pkey = NULL;
  42906. /* Test error conditions. */
  42907. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  42908. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42909. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  42910. #ifndef NO_RSA
  42911. EVP_PKEY_CTX_free(ctx);
  42912. /* Parameter generation for RSA not supported yet. */
  42913. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  42914. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  42915. #endif
  42916. #ifdef HAVE_ECC
  42917. EVP_PKEY_CTX_free(ctx);
  42918. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42919. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  42920. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  42921. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  42922. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  42923. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  42924. WOLFSSL_SUCCESS);
  42925. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42926. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42927. #endif
  42928. EVP_PKEY_CTX_free(ctx);
  42929. EVP_PKEY_free(pkey);
  42930. #endif
  42931. return EXPECT_RESULT();
  42932. }
  42933. static int test_wolfSSL_EVP_PKEY_keygen(void)
  42934. {
  42935. EXPECT_DECLS;
  42936. WOLFSSL_EVP_PKEY* pkey = NULL;
  42937. EVP_PKEY_CTX* ctx = NULL;
  42938. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42939. WOLFSSL_EVP_PKEY* params = NULL;
  42940. DH* dh = NULL;
  42941. const BIGNUM* pubkey = NULL;
  42942. const BIGNUM* privkey = NULL;
  42943. ASN1_INTEGER* asn1int = NULL;
  42944. unsigned int length = 0;
  42945. byte* derBuffer = NULL;
  42946. #endif
  42947. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42948. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42949. /* Bad cases */
  42950. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  42951. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  42952. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  42953. /* Good case */
  42954. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  42955. EVP_PKEY_CTX_free(ctx);
  42956. ctx = NULL;
  42957. EVP_PKEY_free(pkey);
  42958. pkey = NULL;
  42959. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42960. /* Test DH keygen */
  42961. {
  42962. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  42963. ExpectNotNull(dh = DH_get_2048_256());
  42964. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42965. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  42966. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42967. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42968. DH_free(dh);
  42969. dh = NULL;
  42970. EVP_PKEY_CTX_free(ctx);
  42971. EVP_PKEY_free(params);
  42972. /* try exporting generated key to DER, to verify */
  42973. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  42974. DH_get0_key(dh, &pubkey, &privkey);
  42975. ExpectNotNull(pubkey);
  42976. ExpectNotNull(privkey);
  42977. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  42978. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  42979. ASN1_INTEGER_free(asn1int);
  42980. DH_free(dh);
  42981. dh = NULL;
  42982. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42983. EVP_PKEY_free(pkey);
  42984. }
  42985. #endif
  42986. return EXPECT_RESULT();
  42987. }
  42988. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  42989. {
  42990. EXPECT_DECLS;
  42991. WOLFSSL_EVP_PKEY* pkey = NULL;
  42992. EVP_PKEY_CTX *ctx = NULL;
  42993. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42994. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42995. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42996. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  42997. EVP_PKEY_CTX_free(ctx);
  42998. EVP_PKEY_free(pkey);
  42999. return EXPECT_RESULT();
  43000. }
  43001. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  43002. {
  43003. EXPECT_DECLS;
  43004. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  43005. WOLFSSL_EVP_PKEY* pkey = NULL;
  43006. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43007. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  43008. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  43009. EVP_PKEY_free(pkey);
  43010. #endif
  43011. return EXPECT_RESULT();
  43012. }
  43013. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  43014. {
  43015. EXPECT_DECLS;
  43016. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  43017. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  43018. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  43019. !defined(NO_FILESYSTEM)
  43020. WOLFSSL_EVP_PKEY* params = NULL;
  43021. WOLFSSL_EVP_PKEY* copy = NULL;
  43022. DH* dh = NULL;
  43023. BIGNUM* p1;
  43024. BIGNUM* g1;
  43025. BIGNUM* q1;
  43026. BIGNUM* p2;
  43027. BIGNUM* g2;
  43028. BIGNUM* q2;
  43029. /* create DH with DH_get_2048_256 params */
  43030. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43031. ExpectNotNull(dh = DH_get_2048_256());
  43032. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43033. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  43034. (const BIGNUM**)&q1,
  43035. (const BIGNUM**)&g1);
  43036. DH_free(dh);
  43037. dh = NULL;
  43038. /* create DH with random generated DH params */
  43039. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  43040. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  43041. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  43042. DH_free(dh);
  43043. dh = NULL;
  43044. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  43045. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  43046. ExpectNotNull(dh->p);
  43047. ExpectNotNull(dh->g);
  43048. ExpectNotNull(dh->q);
  43049. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  43050. (const BIGNUM**)&q2,
  43051. (const BIGNUM**)&g2);
  43052. ExpectIntEQ(BN_cmp(p1, p2), 0);
  43053. ExpectIntEQ(BN_cmp(q1, q2), 0);
  43054. ExpectIntEQ(BN_cmp(g1, g2), 0);
  43055. DH_free(dh);
  43056. dh = NULL;
  43057. EVP_PKEY_free(copy);
  43058. EVP_PKEY_free(params);
  43059. #endif
  43060. return EXPECT_RESULT();
  43061. }
  43062. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  43063. {
  43064. EXPECT_DECLS;
  43065. WOLFSSL_EVP_PKEY* pkey = NULL;
  43066. EVP_PKEY_CTX* ctx = NULL;
  43067. int bits = 2048;
  43068. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43069. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43070. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  43071. WOLFSSL_SUCCESS);
  43072. EVP_PKEY_CTX_free(ctx);
  43073. EVP_PKEY_free(pkey);
  43074. return EXPECT_RESULT();
  43075. }
  43076. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  43077. {
  43078. EXPECT_DECLS;
  43079. /* This is large enough to be used for all key sizes */
  43080. byte key[AES_256_KEY_SIZE] = {0};
  43081. byte iv[AES_BLOCK_SIZE] = {0};
  43082. int i;
  43083. int nids[] = {
  43084. #ifdef HAVE_AES_CBC
  43085. NID_aes_128_cbc,
  43086. #endif
  43087. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43088. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43089. #ifdef HAVE_AESGCM
  43090. NID_aes_128_gcm,
  43091. #endif
  43092. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43093. #ifdef WOLFSSL_AES_COUNTER
  43094. NID_aes_128_ctr,
  43095. #endif
  43096. #ifndef NO_DES3
  43097. NID_des_cbc,
  43098. NID_des_ede3_cbc,
  43099. #endif
  43100. };
  43101. int iv_lengths[] = {
  43102. #ifdef HAVE_AES_CBC
  43103. AES_BLOCK_SIZE,
  43104. #endif
  43105. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43106. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43107. #ifdef HAVE_AESGCM
  43108. GCM_NONCE_MID_SZ,
  43109. #endif
  43110. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43111. #ifdef WOLFSSL_AES_COUNTER
  43112. AES_BLOCK_SIZE,
  43113. #endif
  43114. #ifndef NO_DES3
  43115. DES_BLOCK_SIZE,
  43116. DES_BLOCK_SIZE,
  43117. #endif
  43118. };
  43119. int nidsLen = (sizeof(nids)/sizeof(int));
  43120. for (i = 0; i < nidsLen; i++) {
  43121. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43122. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43123. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43124. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43125. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  43126. EVP_CIPHER_CTX_free(ctx);
  43127. }
  43128. return EXPECT_RESULT();
  43129. }
  43130. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  43131. {
  43132. EXPECT_DECLS;
  43133. byte key[AES_256_KEY_SIZE] = {0};
  43134. byte iv[AES_BLOCK_SIZE] = {0};
  43135. int i;
  43136. int nids[] = {
  43137. #ifdef HAVE_AES_CBC
  43138. NID_aes_128_cbc,
  43139. NID_aes_256_cbc,
  43140. #endif
  43141. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43142. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43143. #ifdef HAVE_AESGCM
  43144. NID_aes_128_gcm,
  43145. NID_aes_256_gcm,
  43146. #endif
  43147. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43148. #ifdef WOLFSSL_AES_COUNTER
  43149. NID_aes_128_ctr,
  43150. NID_aes_256_ctr,
  43151. #endif
  43152. #ifndef NO_DES3
  43153. NID_des_cbc,
  43154. NID_des_ede3_cbc,
  43155. #endif
  43156. };
  43157. int key_lengths[] = {
  43158. #ifdef HAVE_AES_CBC
  43159. AES_128_KEY_SIZE,
  43160. AES_256_KEY_SIZE,
  43161. #endif
  43162. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43163. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43164. #ifdef HAVE_AESGCM
  43165. AES_128_KEY_SIZE,
  43166. AES_256_KEY_SIZE,
  43167. #endif
  43168. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43169. #ifdef WOLFSSL_AES_COUNTER
  43170. AES_128_KEY_SIZE,
  43171. AES_256_KEY_SIZE,
  43172. #endif
  43173. #ifndef NO_DES3
  43174. DES_KEY_SIZE,
  43175. DES3_KEY_SIZE,
  43176. #endif
  43177. };
  43178. int nidsLen = (sizeof(nids)/sizeof(int));
  43179. for (i = 0; i < nidsLen; i++) {
  43180. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43181. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43182. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43183. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43184. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  43185. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  43186. WOLFSSL_SUCCESS);
  43187. EVP_CIPHER_CTX_free(ctx);
  43188. }
  43189. return EXPECT_RESULT();
  43190. }
  43191. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  43192. {
  43193. EXPECT_DECLS;
  43194. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  43195. int ivLen, keyLen;
  43196. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43197. #ifdef HAVE_AESGCM
  43198. byte key[AES_128_KEY_SIZE] = {0};
  43199. byte iv[AES_BLOCK_SIZE] = {0};
  43200. const EVP_CIPHER *init = EVP_aes_128_gcm();
  43201. #else
  43202. byte key[DES3_KEY_SIZE] = {0};
  43203. byte iv[DES_BLOCK_SIZE] = {0};
  43204. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43205. #endif
  43206. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43207. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43208. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  43209. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  43210. /* Bad cases */
  43211. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  43212. WOLFSSL_FAILURE);
  43213. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  43214. WOLFSSL_FAILURE);
  43215. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  43216. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  43217. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  43218. WOLFSSL_FAILURE);
  43219. /* Good case */
  43220. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  43221. EVP_CIPHER_CTX_free(ctx);
  43222. #endif
  43223. return EXPECT_RESULT();
  43224. }
  43225. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  43226. {
  43227. EXPECT_DECLS;
  43228. WOLFSSL_ENGINE* e = NULL;
  43229. int id = 0;
  43230. EVP_PKEY_CTX *ctx = NULL;
  43231. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  43232. EVP_PKEY_CTX_free(ctx);
  43233. return EXPECT_RESULT();
  43234. }
  43235. static int test_wolfSSL_EVP_rc4(void)
  43236. {
  43237. EXPECT_DECLS;
  43238. #if !defined(NO_RC4)
  43239. ExpectNotNull(wolfSSL_EVP_rc4());
  43240. #endif
  43241. return EXPECT_RESULT();
  43242. }
  43243. static int test_wolfSSL_EVP_enc_null(void)
  43244. {
  43245. EXPECT_DECLS;
  43246. ExpectNotNull(wolfSSL_EVP_enc_null());
  43247. return EXPECT_RESULT();
  43248. }
  43249. static int test_wolfSSL_EVP_rc2_cbc(void)
  43250. {
  43251. EXPECT_DECLS;
  43252. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  43253. ExpectNull(wolfSSL_EVP_rc2_cbc());
  43254. #endif
  43255. return EXPECT_RESULT();
  43256. }
  43257. static int test_wolfSSL_EVP_mdc2(void)
  43258. {
  43259. EXPECT_DECLS;
  43260. #if !defined(NO_WOLFSSL_STUB)
  43261. ExpectNull(wolfSSL_EVP_mdc2());
  43262. #endif
  43263. return EXPECT_RESULT();
  43264. }
  43265. static int test_wolfSSL_EVP_md4(void)
  43266. {
  43267. EXPECT_DECLS;
  43268. #if !defined(NO_MD4)
  43269. ExpectNotNull(wolfSSL_EVP_md4());
  43270. #endif
  43271. return EXPECT_RESULT();
  43272. }
  43273. static int test_wolfSSL_EVP_aes_256_gcm(void)
  43274. {
  43275. EXPECT_DECLS;
  43276. #ifdef HAVE_AESGCM
  43277. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  43278. #endif
  43279. return EXPECT_RESULT();
  43280. }
  43281. static int test_wolfSSL_EVP_aes_192_gcm(void)
  43282. {
  43283. EXPECT_DECLS;
  43284. #ifdef HAVE_AESGCM
  43285. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  43286. #endif
  43287. return EXPECT_RESULT();
  43288. }
  43289. static int test_wolfSSL_EVP_aes_256_ccm(void)
  43290. {
  43291. EXPECT_DECLS;
  43292. #ifdef HAVE_AESCCM
  43293. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  43294. #endif
  43295. return EXPECT_RESULT();
  43296. }
  43297. static int test_wolfSSL_EVP_aes_192_ccm(void)
  43298. {
  43299. EXPECT_DECLS;
  43300. #ifdef HAVE_AESCCM
  43301. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  43302. #endif
  43303. return EXPECT_RESULT();
  43304. }
  43305. static int test_wolfSSL_EVP_aes_128_ccm(void)
  43306. {
  43307. EXPECT_DECLS;
  43308. #ifdef HAVE_AESCCM
  43309. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  43310. #endif
  43311. return EXPECT_RESULT();
  43312. }
  43313. static int test_wolfSSL_EVP_ripemd160(void)
  43314. {
  43315. EXPECT_DECLS;
  43316. #if !defined(NO_WOLFSSL_STUB)
  43317. ExpectNull(wolfSSL_EVP_ripemd160());
  43318. #endif
  43319. return EXPECT_RESULT();
  43320. }
  43321. static int test_wolfSSL_EVP_get_digestbynid(void)
  43322. {
  43323. EXPECT_DECLS;
  43324. #ifndef NO_MD5
  43325. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  43326. #endif
  43327. #ifndef NO_SHA
  43328. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  43329. #endif
  43330. #ifndef NO_SHA256
  43331. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  43332. #endif
  43333. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  43334. return EXPECT_RESULT();
  43335. }
  43336. static int test_wolfSSL_EVP_MD_nid(void)
  43337. {
  43338. EXPECT_DECLS;
  43339. #ifndef NO_MD5
  43340. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  43341. #endif
  43342. #ifndef NO_SHA
  43343. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  43344. #endif
  43345. #ifndef NO_SHA256
  43346. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  43347. #endif
  43348. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  43349. return EXPECT_RESULT();
  43350. }
  43351. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  43352. {
  43353. EXPECT_DECLS;
  43354. #if defined(HAVE_ECC)
  43355. WOLFSSL_EVP_PKEY* pkey = NULL;
  43356. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  43357. ExpectNotNull(pkey = EVP_PKEY_new());
  43358. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  43359. EVP_PKEY_free(pkey);
  43360. #endif
  43361. return EXPECT_RESULT();
  43362. }
  43363. static int test_wolfSSL_EVP_X_STATE(void)
  43364. {
  43365. EXPECT_DECLS;
  43366. #if !defined(NO_DES3) && !defined(NO_RC4)
  43367. byte key[DES3_KEY_SIZE] = {0};
  43368. byte iv[DES_IV_SIZE] = {0};
  43369. EVP_CIPHER_CTX *ctx = NULL;
  43370. const EVP_CIPHER *init = NULL;
  43371. /* Bad test cases */
  43372. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  43373. ExpectNotNull(init = EVP_des_ede3_cbc());
  43374. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43375. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43376. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  43377. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  43378. EVP_CIPHER_CTX_free(ctx);
  43379. ctx = NULL;
  43380. /* Good test case */
  43381. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  43382. ExpectNotNull(init = wolfSSL_EVP_rc4());
  43383. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43384. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43385. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  43386. EVP_CIPHER_CTX_free(ctx);
  43387. #endif
  43388. return EXPECT_RESULT();
  43389. }
  43390. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  43391. {
  43392. EXPECT_DECLS;
  43393. #if !defined(NO_DES3) && !defined(NO_RC4)
  43394. byte key[DES3_KEY_SIZE] = {0};
  43395. byte iv[DES_IV_SIZE] = {0};
  43396. EVP_CIPHER_CTX *ctx = NULL;
  43397. const EVP_CIPHER *init = NULL;
  43398. /* Bad test cases */
  43399. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  43400. ExpectNotNull(init = EVP_des_ede3_cbc());
  43401. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43402. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43403. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  43404. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  43405. EVP_CIPHER_CTX_free(ctx);
  43406. ctx = NULL;
  43407. /* Good test case */
  43408. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  43409. ExpectNotNull(init = wolfSSL_EVP_rc4());
  43410. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43411. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43412. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  43413. EVP_CIPHER_CTX_free(ctx);
  43414. #endif
  43415. return EXPECT_RESULT();
  43416. }
  43417. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  43418. {
  43419. EXPECT_DECLS;
  43420. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  43421. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  43422. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  43423. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  43424. #ifdef HAVE_AES_CBC
  43425. #ifdef WOLFSSL_AES_128
  43426. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  43427. #endif
  43428. #ifdef WOLFSSL_AES_192
  43429. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  43430. #endif
  43431. #ifdef WOLFSSL_AES_256
  43432. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  43433. #endif
  43434. #endif
  43435. #ifdef HAVE_AESGCM
  43436. #ifdef WOLFSSL_AES_128
  43437. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  43438. #endif
  43439. #ifdef WOLFSSL_AES_192
  43440. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  43441. #endif
  43442. #ifdef WOLFSSL_AES_256
  43443. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  43444. #endif
  43445. #endif
  43446. #ifdef HAVE_AESCCM
  43447. #ifdef WOLFSSL_AES_128
  43448. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  43449. #endif
  43450. #ifdef WOLFSSL_AES_192
  43451. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  43452. #endif
  43453. #ifdef WOLFSSL_AES_256
  43454. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  43455. #endif
  43456. #endif
  43457. #ifdef WOLFSSL_AES_COUNTER
  43458. #ifdef WOLFSSL_AES_128
  43459. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  43460. #endif
  43461. #ifdef WOLFSSL_AES_192
  43462. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  43463. #endif
  43464. #ifdef WOLFSSL_AES_256
  43465. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  43466. #endif
  43467. #endif
  43468. #ifdef HAVE_AES_ECB
  43469. #ifdef WOLFSSL_AES_128
  43470. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  43471. #endif
  43472. #ifdef WOLFSSL_AES_192
  43473. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  43474. #endif
  43475. #ifdef WOLFSSL_AES_256
  43476. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  43477. #endif
  43478. #endif
  43479. #ifdef WOLFSSL_AES_OFB
  43480. #ifdef WOLFSSL_AES_128
  43481. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  43482. #endif
  43483. #ifdef WOLFSSL_AES_192
  43484. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  43485. #endif
  43486. #ifdef WOLFSSL_AES_256
  43487. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  43488. #endif
  43489. #endif
  43490. #ifndef NO_RC4
  43491. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  43492. #endif
  43493. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43494. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  43495. #endif
  43496. #endif
  43497. #ifdef WOLFSSL_SM4_ECB
  43498. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  43499. #endif
  43500. #ifdef WOLFSSL_SM4_CBC
  43501. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  43502. #endif
  43503. #ifdef WOLFSSL_SM4_CTR
  43504. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  43505. #endif
  43506. #ifdef WOLFSSL_SM4_GCM
  43507. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  43508. #endif
  43509. #ifdef WOLFSSL_SM4_CCM
  43510. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  43511. #endif
  43512. return EXPECT_RESULT();
  43513. }
  43514. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  43515. {
  43516. EXPECT_DECLS;
  43517. int nids[] = {
  43518. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43519. #ifdef WOLFSSL_AES_128
  43520. NID_aes_128_cbc,
  43521. #endif
  43522. #ifdef WOLFSSL_AES_192
  43523. NID_aes_192_cbc,
  43524. #endif
  43525. #ifdef WOLFSSL_AES_256
  43526. NID_aes_256_cbc,
  43527. #endif
  43528. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  43529. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43530. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43531. #ifdef HAVE_AESGCM
  43532. #ifdef WOLFSSL_AES_128
  43533. NID_aes_128_gcm,
  43534. #endif
  43535. #ifdef WOLFSSL_AES_192
  43536. NID_aes_192_gcm,
  43537. #endif
  43538. #ifdef WOLFSSL_AES_256
  43539. NID_aes_256_gcm,
  43540. #endif
  43541. #endif /* HAVE_AESGCM */
  43542. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43543. #ifdef WOLFSSL_AES_COUNTER
  43544. #ifdef WOLFSSL_AES_128
  43545. NID_aes_128_ctr,
  43546. #endif
  43547. #ifdef WOLFSSL_AES_192
  43548. NID_aes_192_ctr,
  43549. #endif
  43550. #ifdef WOLFSSL_AES_256
  43551. NID_aes_256_ctr,
  43552. #endif
  43553. #endif
  43554. #ifndef NO_DES3
  43555. NID_des_cbc,
  43556. NID_des_ede3_cbc,
  43557. #endif
  43558. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43559. NID_chacha20_poly1305,
  43560. #endif
  43561. };
  43562. int iv_lengths[] = {
  43563. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43564. #ifdef WOLFSSL_AES_128
  43565. AES_BLOCK_SIZE,
  43566. #endif
  43567. #ifdef WOLFSSL_AES_192
  43568. AES_BLOCK_SIZE,
  43569. #endif
  43570. #ifdef WOLFSSL_AES_256
  43571. AES_BLOCK_SIZE,
  43572. #endif
  43573. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  43574. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43575. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43576. #ifdef HAVE_AESGCM
  43577. #ifdef WOLFSSL_AES_128
  43578. GCM_NONCE_MID_SZ,
  43579. #endif
  43580. #ifdef WOLFSSL_AES_192
  43581. GCM_NONCE_MID_SZ,
  43582. #endif
  43583. #ifdef WOLFSSL_AES_256
  43584. GCM_NONCE_MID_SZ,
  43585. #endif
  43586. #endif /* HAVE_AESGCM */
  43587. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43588. #ifdef WOLFSSL_AES_COUNTER
  43589. #ifdef WOLFSSL_AES_128
  43590. AES_BLOCK_SIZE,
  43591. #endif
  43592. #ifdef WOLFSSL_AES_192
  43593. AES_BLOCK_SIZE,
  43594. #endif
  43595. #ifdef WOLFSSL_AES_256
  43596. AES_BLOCK_SIZE,
  43597. #endif
  43598. #endif
  43599. #ifndef NO_DES3
  43600. DES_BLOCK_SIZE,
  43601. DES_BLOCK_SIZE,
  43602. #endif
  43603. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43604. CHACHA20_POLY1305_AEAD_IV_SIZE,
  43605. #endif
  43606. };
  43607. int i;
  43608. int nidsLen = (sizeof(nids)/sizeof(int));
  43609. for (i = 0; i < nidsLen; i++) {
  43610. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  43611. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  43612. }
  43613. return EXPECT_RESULT();
  43614. }
  43615. static int test_wolfSSL_EVP_SignInit_ex(void)
  43616. {
  43617. EXPECT_DECLS;
  43618. WOLFSSL_EVP_MD_CTX mdCtx;
  43619. WOLFSSL_ENGINE* e = 0;
  43620. const EVP_MD* md = EVP_sha256();
  43621. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43622. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  43623. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43624. return EXPECT_RESULT();
  43625. }
  43626. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  43627. {
  43628. EXPECT_DECLS;
  43629. #if !defined(NO_SHA256)
  43630. WOLFSSL_EVP_MD_CTX mdCtx;
  43631. unsigned int s = 0;
  43632. unsigned char md[WC_SHA256_DIGEST_SIZE];
  43633. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  43634. /* Bad Case */
  43635. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  43636. (HAVE_FIPS_VERSION > 2))
  43637. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43638. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  43639. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43640. #else
  43641. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43642. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  43643. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  43644. #endif
  43645. /* Good Case */
  43646. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43647. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  43648. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  43649. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  43650. #endif
  43651. return EXPECT_RESULT();
  43652. }
  43653. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  43654. {
  43655. EXPECT_DECLS;
  43656. #if defined(OPENSSL_EXTRA)
  43657. EVP_PKEY* pkey = NULL;
  43658. EVP_PKEY_CTX* ctx = NULL;
  43659. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43660. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43661. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  43662. /* void */
  43663. EVP_PKEY_CTX_free(ctx);
  43664. #else
  43665. /* int */
  43666. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  43667. #endif
  43668. EVP_PKEY_free(pkey);
  43669. #endif
  43670. return EXPECT_RESULT();
  43671. }
  43672. static int test_wolfSSL_EVP_PKEY_param_check(void)
  43673. {
  43674. EXPECT_DECLS;
  43675. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  43676. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43677. DH *dh = NULL;
  43678. DH *setDh = NULL;
  43679. EVP_PKEY *pkey = NULL;
  43680. EVP_PKEY_CTX* ctx = NULL;
  43681. FILE* f = NULL;
  43682. unsigned char buf[512];
  43683. const unsigned char* pt = buf;
  43684. const char* dh2048 = "./certs/dh2048.der";
  43685. long len = 0;
  43686. int code = -1;
  43687. XMEMSET(buf, 0, sizeof(buf));
  43688. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  43689. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43690. if (f != XBADFILE)
  43691. XFCLOSE(f);
  43692. /* Load dh2048.der into DH with internal format */
  43693. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  43694. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43695. ExpectIntEQ(code, 0);
  43696. code = -1;
  43697. pkey = wolfSSL_EVP_PKEY_new();
  43698. /* Set DH into PKEY */
  43699. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43700. /* create ctx from pkey */
  43701. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43702. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  43703. /* TODO: more invalid cases */
  43704. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  43705. EVP_PKEY_CTX_free(ctx);
  43706. EVP_PKEY_free(pkey);
  43707. DH_free(setDh);
  43708. setDh = NULL;
  43709. DH_free(dh);
  43710. dh = NULL;
  43711. #endif
  43712. #endif
  43713. return EXPECT_RESULT();
  43714. }
  43715. static int test_wolfSSL_EVP_BytesToKey(void)
  43716. {
  43717. EXPECT_DECLS;
  43718. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43719. byte key[AES_BLOCK_SIZE] = {0};
  43720. byte iv[AES_BLOCK_SIZE] = {0};
  43721. int count = 0;
  43722. const EVP_MD* md = EVP_sha256();
  43723. const EVP_CIPHER *type;
  43724. const unsigned char *salt = (unsigned char *)"salt1234";
  43725. int sz = 5;
  43726. const byte data[] = {
  43727. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43728. 0x72,0x6c,0x64
  43729. };
  43730. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  43731. /* Bad cases */
  43732. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  43733. 0);
  43734. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  43735. 16);
  43736. md = "2";
  43737. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43738. WOLFSSL_FAILURE);
  43739. /* Good case */
  43740. md = EVP_sha256();
  43741. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43742. 16);
  43743. #endif
  43744. return EXPECT_RESULT();
  43745. }
  43746. static int test_evp_cipher_aes_gcm(void)
  43747. {
  43748. EXPECT_DECLS;
  43749. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  43750. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  43751. (HAVE_FIPS_VERSION >= 2)))
  43752. /*
  43753. * This test checks data at various points in the encrypt/decrypt process
  43754. * against known values produced using the same test with OpenSSL. This
  43755. * interop testing is critical for verifying the correctness of our
  43756. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  43757. * a flow supported by OpenSSL that uses the control command
  43758. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  43759. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  43760. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  43761. * wolfSSL OpenSSH clients because there was a bug in this flow that
  43762. * happened to "cancel out" if both sides of the connection had the bug.
  43763. */
  43764. enum {
  43765. NUM_ENCRYPTIONS = 3,
  43766. AAD_SIZE = 4
  43767. };
  43768. byte plainText1[] = {
  43769. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  43770. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  43771. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  43772. };
  43773. byte plainText2[] = {
  43774. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  43775. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  43776. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  43777. };
  43778. byte plainText3[] = {
  43779. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  43780. 0x5b, 0x57, 0x10
  43781. };
  43782. byte* plainTexts[NUM_ENCRYPTIONS] = {
  43783. plainText1,
  43784. plainText2,
  43785. plainText3
  43786. };
  43787. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  43788. sizeof(plainText1),
  43789. sizeof(plainText2),
  43790. sizeof(plainText3)
  43791. };
  43792. byte aad1[AAD_SIZE] = {
  43793. 0x00, 0x00, 0x00, 0x01
  43794. };
  43795. byte aad2[AAD_SIZE] = {
  43796. 0x00, 0x00, 0x00, 0x10
  43797. };
  43798. byte aad3[AAD_SIZE] = {
  43799. 0x00, 0x00, 0x01, 0x00
  43800. };
  43801. byte* aads[NUM_ENCRYPTIONS] = {
  43802. aad1,
  43803. aad2,
  43804. aad3
  43805. };
  43806. const byte iv[GCM_NONCE_MID_SZ] = {
  43807. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  43808. };
  43809. byte currentIv[GCM_NONCE_MID_SZ];
  43810. const byte key[] = {
  43811. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  43812. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  43813. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  43814. };
  43815. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  43816. {
  43817. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43818. 0xEF
  43819. },
  43820. {
  43821. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43822. 0xF0
  43823. },
  43824. {
  43825. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43826. 0xF1
  43827. }
  43828. };
  43829. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  43830. {
  43831. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  43832. 0x3D, 0x32, 0x18, 0x34, 0xA9
  43833. },
  43834. {
  43835. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  43836. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  43837. },
  43838. {
  43839. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  43840. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  43841. }
  43842. };
  43843. const byte expCipherText1[] = {
  43844. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  43845. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  43846. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  43847. };
  43848. const byte expCipherText2[] = {
  43849. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  43850. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  43851. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  43852. };
  43853. const byte expCipherText3[] = {
  43854. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  43855. 0x80, 0x5E, 0x6B,
  43856. };
  43857. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  43858. expCipherText1,
  43859. expCipherText2,
  43860. expCipherText3
  43861. };
  43862. byte* cipherText = NULL;
  43863. byte* calcPlainText = NULL;
  43864. byte tag[AES_BLOCK_SIZE];
  43865. EVP_CIPHER_CTX* encCtx = NULL;
  43866. EVP_CIPHER_CTX* decCtx = NULL;
  43867. int i, j, outl;
  43868. /****************************************************/
  43869. for (i = 0; i < 3; ++i) {
  43870. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  43871. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  43872. /* First iteration, set key before IV. */
  43873. if (i == 0) {
  43874. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  43875. SSL_SUCCESS);
  43876. /*
  43877. * The call to EVP_CipherInit below (with NULL key) should clear the
  43878. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  43879. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  43880. * behavior.
  43881. */
  43882. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43883. (void*)iv), SSL_SUCCESS);
  43884. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  43885. SSL_SUCCESS);
  43886. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43887. currentIv), SSL_FAILURE);
  43888. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  43889. SSL_SUCCESS);
  43890. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  43891. SSL_SUCCESS);
  43892. }
  43893. /* Second iteration, IV before key. */
  43894. else {
  43895. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  43896. SSL_SUCCESS);
  43897. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  43898. SSL_SUCCESS);
  43899. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  43900. SSL_SUCCESS);
  43901. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  43902. SSL_SUCCESS);
  43903. }
  43904. /*
  43905. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  43906. * been issued first.
  43907. */
  43908. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43909. currentIv), SSL_FAILURE);
  43910. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43911. (void*)iv), SSL_SUCCESS);
  43912. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43913. (void*)iv), SSL_SUCCESS);
  43914. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  43915. /*************** Encrypt ***************/
  43916. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43917. currentIv), SSL_SUCCESS);
  43918. /* Check current IV against expected. */
  43919. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43920. /* Add AAD. */
  43921. if (i == 2) {
  43922. /* Test streaming API. */
  43923. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  43924. AAD_SIZE), SSL_SUCCESS);
  43925. }
  43926. else {
  43927. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  43928. AAD_SIZE);
  43929. }
  43930. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43931. DYNAMIC_TYPE_TMP_BUFFER));
  43932. /* Encrypt plaintext. */
  43933. if (i == 2) {
  43934. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  43935. plainTexts[j], plainTextSzs[j]),
  43936. SSL_SUCCESS);
  43937. }
  43938. else {
  43939. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  43940. plainTextSzs[j]), plainTextSzs[j]);
  43941. }
  43942. if (i == 2) {
  43943. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  43944. SSL_SUCCESS);
  43945. }
  43946. else {
  43947. /*
  43948. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  43949. * akin to calling EVP_CipherFinal.
  43950. */
  43951. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  43952. }
  43953. /* Check ciphertext against expected. */
  43954. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  43955. 0);
  43956. /* Get and check tag against expected. */
  43957. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  43958. sizeof(tag), tag), SSL_SUCCESS);
  43959. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  43960. /*************** Decrypt ***************/
  43961. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43962. currentIv), SSL_SUCCESS);
  43963. /* Check current IV against expected. */
  43964. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43965. /* Add AAD. */
  43966. if (i == 2) {
  43967. /* Test streaming API. */
  43968. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  43969. AAD_SIZE), SSL_SUCCESS);
  43970. }
  43971. else {
  43972. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  43973. AAD_SIZE);
  43974. }
  43975. /* Set expected tag. */
  43976. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  43977. sizeof(tag), tag), SSL_SUCCESS);
  43978. /* Decrypt ciphertext. */
  43979. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43980. DYNAMIC_TYPE_TMP_BUFFER));
  43981. if (i == 2) {
  43982. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  43983. cipherText, plainTextSzs[j]),
  43984. SSL_SUCCESS);
  43985. }
  43986. else {
  43987. /* This first EVP_Cipher call will check the tag, too. */
  43988. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  43989. plainTextSzs[j]), plainTextSzs[j]);
  43990. }
  43991. if (i == 2) {
  43992. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  43993. SSL_SUCCESS);
  43994. }
  43995. else {
  43996. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  43997. }
  43998. /* Check plaintext against expected. */
  43999. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  44000. 0);
  44001. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44002. cipherText = NULL;
  44003. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44004. calcPlainText = NULL;
  44005. }
  44006. EVP_CIPHER_CTX_free(encCtx);
  44007. encCtx = NULL;
  44008. EVP_CIPHER_CTX_free(decCtx);
  44009. decCtx = NULL;
  44010. }
  44011. #endif
  44012. return EXPECT_RESULT();
  44013. }
  44014. static int test_wolfSSL_OBJ_ln(void)
  44015. {
  44016. EXPECT_DECLS;
  44017. const int nid_set[] = {
  44018. NID_commonName,
  44019. NID_serialNumber,
  44020. NID_countryName,
  44021. NID_localityName,
  44022. NID_stateOrProvinceName,
  44023. NID_organizationName,
  44024. NID_organizationalUnitName,
  44025. NID_domainComponent,
  44026. NID_businessCategory,
  44027. NID_jurisdictionCountryName,
  44028. NID_jurisdictionStateOrProvinceName,
  44029. NID_emailAddress
  44030. };
  44031. const char* ln_set[] = {
  44032. "commonName",
  44033. "serialNumber",
  44034. "countryName",
  44035. "localityName",
  44036. "stateOrProvinceName",
  44037. "organizationName",
  44038. "organizationalUnitName",
  44039. "domainComponent",
  44040. "businessCategory",
  44041. "jurisdictionCountryName",
  44042. "jurisdictionStateOrProvinceName",
  44043. "emailAddress",
  44044. };
  44045. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  44046. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  44047. #ifdef HAVE_ECC
  44048. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44049. {
  44050. EC_builtin_curve r[27];
  44051. size_t nCurves = sizeof(r) / sizeof(r[0]);
  44052. nCurves = EC_get_builtin_curves(r, nCurves);
  44053. for (i = 0; i < nCurves; i++) {
  44054. /* skip ECC_CURVE_INVALID */
  44055. if (r[i].nid != ECC_CURVE_INVALID) {
  44056. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  44057. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  44058. }
  44059. }
  44060. }
  44061. #endif
  44062. #endif
  44063. for (i = 0; i < maxIdx; i++) {
  44064. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  44065. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  44066. }
  44067. return EXPECT_RESULT();
  44068. }
  44069. static int test_wolfSSL_OBJ_sn(void)
  44070. {
  44071. EXPECT_DECLS;
  44072. int i = 0, maxIdx = 7;
  44073. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  44074. NID_stateOrProvinceName,NID_organizationName,
  44075. NID_organizationalUnitName,NID_emailAddress};
  44076. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  44077. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  44078. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  44079. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  44080. WOLFSSL_EMAIL_ADDR};
  44081. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  44082. for (i = 0; i < maxIdx; i++) {
  44083. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  44084. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  44085. }
  44086. return EXPECT_RESULT();
  44087. }
  44088. #if !defined(NO_BIO)
  44089. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  44090. {
  44091. return lh_strhash(s[3]);
  44092. }
  44093. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  44094. {
  44095. return XSTRCMP(a[3], b[3]);
  44096. }
  44097. #endif
  44098. static int test_wolfSSL_TXT_DB(void)
  44099. {
  44100. EXPECT_DECLS;
  44101. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44102. BIO *bio = NULL;
  44103. TXT_DB *db = NULL;
  44104. const int columns = 6;
  44105. const char *fields[6] = {
  44106. "V",
  44107. "320926161116Z",
  44108. "",
  44109. "12BD",
  44110. "unknown",
  44111. "/CN=rsa doe",
  44112. };
  44113. char** fields_copy = NULL;
  44114. /* Test read */
  44115. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  44116. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  44117. ExpectNotNull(db = TXT_DB_read(bio, columns));
  44118. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  44119. DYNAMIC_TYPE_OPENSSL));
  44120. if (fields_copy != NULL) {
  44121. XMEMCPY(fields_copy, fields, sizeof(fields));
  44122. }
  44123. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  44124. if (EXPECT_FAIL()) {
  44125. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  44126. }
  44127. BIO_free(bio);
  44128. bio = NULL;
  44129. /* Test write */
  44130. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44131. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  44132. BIO_free(bio);
  44133. /* Test index */
  44134. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  44135. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  44136. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44137. fields[3] = "12DA";
  44138. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44139. fields[3] = "FFFF";
  44140. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44141. fields[3] = "";
  44142. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44143. TXT_DB_free(db);
  44144. #endif
  44145. return EXPECT_RESULT();
  44146. }
  44147. static int test_wolfSSL_NCONF(void)
  44148. {
  44149. EXPECT_DECLS;
  44150. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44151. const char* confFile = "./tests/NCONF_test.cnf";
  44152. CONF* conf = NULL;
  44153. long eline = 0;
  44154. long num = 0;
  44155. ExpectNotNull(conf = NCONF_new(NULL));
  44156. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  44157. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  44158. ExpectIntEQ(num, 1234);
  44159. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  44160. ExpectIntEQ(num, 4321);
  44161. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  44162. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  44163. "./test-dir/file1");
  44164. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  44165. "./test-dir/file1:./test-dir/file2:./section1:file2");
  44166. NCONF_free(conf);
  44167. #endif
  44168. return EXPECT_RESULT();
  44169. }
  44170. #endif /* OPENSSL_ALL */
  44171. static int test_wolfSSL_X509V3_EXT_get(void) {
  44172. EXPECT_DECLS;
  44173. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44174. XFILE f = XBADFILE;
  44175. int numOfExt =0;
  44176. int extNid = 0;
  44177. int i = 0;
  44178. WOLFSSL_X509* x509 = NULL;
  44179. WOLFSSL_X509_EXTENSION* ext = NULL;
  44180. const WOLFSSL_v3_ext_method* method = NULL;
  44181. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44182. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44183. if (f != XBADFILE)
  44184. XFCLOSE(f);
  44185. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  44186. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44187. for (i = 0; i < numOfExt; i++) {
  44188. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44189. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  44190. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44191. ExpectIntEQ(method->ext_nid, extNid);
  44192. }
  44193. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  44194. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  44195. wolfSSL_X509_free(x509);
  44196. #endif
  44197. return EXPECT_RESULT();
  44198. }
  44199. static int test_wolfSSL_X509V3_EXT_nconf(void)
  44200. {
  44201. EXPECT_DECLS;
  44202. #ifdef OPENSSL_ALL
  44203. const char *ext_names[] = {
  44204. "subjectKeyIdentifier",
  44205. "authorityKeyIdentifier",
  44206. "subjectAltName",
  44207. "keyUsage",
  44208. "extendedKeyUsage",
  44209. };
  44210. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  44211. int ext_nids[] = {
  44212. NID_subject_key_identifier,
  44213. NID_authority_key_identifier,
  44214. NID_subject_alt_name,
  44215. NID_key_usage,
  44216. NID_ext_key_usage,
  44217. };
  44218. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  44219. const char *ext_values[] = {
  44220. "hash",
  44221. "hash",
  44222. "DNS:example.com, IP:127.0.0.1",
  44223. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  44224. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  44225. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  44226. "OCSPSigning",
  44227. };
  44228. size_t i;
  44229. X509_EXTENSION* ext = NULL;
  44230. X509* x509 = NULL;
  44231. unsigned int keyUsageFlags;
  44232. unsigned int extKeyUsageFlags;
  44233. ExpectNotNull(x509 = X509_new());
  44234. /* keyUsage / extKeyUsage should match string above */
  44235. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44236. | KU_NON_REPUDIATION
  44237. | KU_KEY_ENCIPHERMENT
  44238. | KU_DATA_ENCIPHERMENT
  44239. | KU_KEY_AGREEMENT
  44240. | KU_KEY_CERT_SIGN
  44241. | KU_CRL_SIGN
  44242. | KU_ENCIPHER_ONLY
  44243. | KU_DECIPHER_ONLY;
  44244. extKeyUsageFlags = XKU_SSL_CLIENT
  44245. | XKU_SSL_SERVER
  44246. | XKU_CODE_SIGN
  44247. | XKU_SMIME
  44248. | XKU_TIMESTAMP
  44249. | XKU_OCSP_SIGN;
  44250. for (i = 0; i < ext_names_count; i++) {
  44251. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44252. ext_values[i]));
  44253. X509_EXTENSION_free(ext);
  44254. ext = NULL;
  44255. }
  44256. for (i = 0; i < ext_nids_count; i++) {
  44257. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  44258. ext_values[i]));
  44259. X509_EXTENSION_free(ext);
  44260. ext = NULL;
  44261. }
  44262. /* Test adding extension to X509 */
  44263. for (i = 0; i < ext_nids_count; i++) {
  44264. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44265. ext_values[i]));
  44266. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  44267. if (ext_nids[i] == NID_key_usage) {
  44268. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44269. }
  44270. else if (ext_nids[i] == NID_ext_key_usage) {
  44271. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  44272. }
  44273. X509_EXTENSION_free(ext);
  44274. ext = NULL;
  44275. }
  44276. X509_free(x509);
  44277. #endif
  44278. return EXPECT_RESULT();
  44279. }
  44280. static int test_wolfSSL_X509V3_EXT(void) {
  44281. EXPECT_DECLS;
  44282. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44283. XFILE f = XBADFILE;
  44284. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  44285. char* str = NULL;
  44286. unsigned char* data = NULL;
  44287. const WOLFSSL_v3_ext_method* method = NULL;
  44288. WOLFSSL_X509* x509 = NULL;
  44289. WOLFSSL_X509_EXTENSION* ext = NULL;
  44290. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  44291. WOLFSSL_ASN1_OBJECT *obj = NULL;
  44292. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  44293. WOLFSSL_ASN1_STRING* asn1str = NULL;
  44294. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  44295. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  44296. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  44297. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  44298. WOLFSSL_GENERAL_NAME* gn = NULL;
  44299. /* Check NULL argument */
  44300. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  44301. /* Using OCSP cert with X509V3 extensions */
  44302. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  44303. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44304. if (f != XBADFILE)
  44305. XFCLOSE(f);
  44306. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44307. /* Basic Constraints */
  44308. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44309. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44310. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  44311. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  44312. ExpectIntEQ(bc->ca, 1);
  44313. ExpectNull(bc->pathlen);
  44314. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  44315. bc = NULL;
  44316. i++;
  44317. /* Subject Key Identifier */
  44318. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44319. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44320. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  44321. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  44322. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  44323. asn1str));
  44324. X509_EXTENSION_free(ext2);
  44325. ext2 = NULL;
  44326. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44327. ExpectNotNull(method->i2s);
  44328. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  44329. wolfSSL_ASN1_STRING_free(asn1str);
  44330. asn1str = NULL;
  44331. if (str != NULL) {
  44332. actual = strcmp(str,
  44333. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  44334. }
  44335. ExpectIntEQ(actual, 0);
  44336. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44337. str = NULL;
  44338. i++;
  44339. /* Authority Key Identifier */
  44340. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44341. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44342. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  44343. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  44344. ext));
  44345. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44346. ExpectNotNull(asn1str = aKeyId->keyid);
  44347. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  44348. asn1str));
  44349. asn1str = NULL;
  44350. if (str != NULL) {
  44351. actual = strcmp(str,
  44352. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  44353. }
  44354. ExpectIntEQ(actual, 0);
  44355. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44356. str = NULL;
  44357. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  44358. aKeyId = NULL;
  44359. i++;
  44360. /* Key Usage */
  44361. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44362. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44363. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  44364. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  44365. #if defined(WOLFSSL_QT)
  44366. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  44367. #else
  44368. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  44369. #endif
  44370. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  44371. if (data != NULL) {
  44372. #ifdef BIG_ENDIAN_ORDER
  44373. actual = data[1];
  44374. #else
  44375. actual = data[0];
  44376. #endif
  44377. }
  44378. ExpectIntEQ(actual, expected);
  44379. wolfSSL_ASN1_STRING_free(asn1str);
  44380. asn1str = NULL;
  44381. #if 1
  44382. i++;
  44383. /* Authority Info Access */
  44384. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44385. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44386. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  44387. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  44388. ext));
  44389. #if defined(WOLFSSL_QT)
  44390. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  44391. #else
  44392. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  44393. #endif
  44394. /* URI entry is an ACCESS_DESCRIPTION type */
  44395. #if defined(WOLFSSL_QT)
  44396. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  44397. #else
  44398. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  44399. #endif
  44400. ExpectNotNull(adObj = ad->method);
  44401. /* Make sure nid is OCSP */
  44402. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  44403. /* GENERAL_NAME stores URI as an ASN1_STRING */
  44404. ExpectNotNull(gn = ad->location);
  44405. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  44406. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  44407. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  44408. #if defined(WOLFSSL_QT)
  44409. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  44410. #else
  44411. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  44412. #endif
  44413. if (str != NULL) {
  44414. actual = strcmp(str, "http://127.0.0.1:22220");
  44415. }
  44416. ExpectIntEQ(actual, 0);
  44417. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  44418. aia = NULL;
  44419. #else
  44420. (void) aia; (void) ad; (void) adObj; (void) gn;
  44421. #endif
  44422. wolfSSL_X509_free(x509);
  44423. #endif
  44424. return EXPECT_RESULT();
  44425. }
  44426. static int test_wolfSSL_X509_get_extension_flags(void)
  44427. {
  44428. EXPECT_DECLS;
  44429. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44430. XFILE f = XBADFILE;
  44431. X509* x509 = NULL;
  44432. unsigned int extFlags;
  44433. unsigned int keyUsageFlags;
  44434. unsigned int extKeyUsageFlags;
  44435. /* client-int-cert.pem has the following extension flags. */
  44436. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  44437. /* and the following key usage flags. */
  44438. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44439. | KU_NON_REPUDIATION
  44440. | KU_KEY_ENCIPHERMENT;
  44441. /* and the following extended key usage flags. */
  44442. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  44443. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  44444. XBADFILE);
  44445. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44446. if (f != XBADFILE) {
  44447. XFCLOSE(f);
  44448. f = XBADFILE;
  44449. }
  44450. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  44451. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44452. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  44453. X509_free(x509);
  44454. x509 = NULL;
  44455. /* client-cert-ext.pem has the following extension flags. */
  44456. extFlags = EXFLAG_KUSAGE;
  44457. /* and the following key usage flags. */
  44458. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44459. | KU_KEY_CERT_SIGN
  44460. | KU_CRL_SIGN;
  44461. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  44462. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44463. if (f != XBADFILE)
  44464. XFCLOSE(f);
  44465. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  44466. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44467. X509_free(x509);
  44468. #endif /* OPENSSL_ALL */
  44469. return EXPECT_RESULT();
  44470. }
  44471. static int test_wolfSSL_X509_get_ext(void)
  44472. {
  44473. EXPECT_DECLS;
  44474. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44475. int ret = 0;
  44476. XFILE f = XBADFILE;
  44477. WOLFSSL_X509* x509 = NULL;
  44478. WOLFSSL_X509_EXTENSION* foundExtension;
  44479. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44480. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44481. if (f != XBADFILE)
  44482. XFCLOSE(f);
  44483. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  44484. /* wolfSSL_X509_get_ext() valid input */
  44485. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  44486. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  44487. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  44488. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  44489. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  44490. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  44491. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  44492. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  44493. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  44494. wolfSSL_X509_free(x509);
  44495. #endif
  44496. return EXPECT_RESULT();
  44497. }
  44498. static int test_wolfSSL_X509_get_ext_by_NID(void)
  44499. {
  44500. EXPECT_DECLS;
  44501. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44502. int rc = 0;
  44503. XFILE f = XBADFILE;
  44504. WOLFSSL_X509* x509 = NULL;
  44505. ASN1_OBJECT* obj = NULL;
  44506. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44507. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44508. if (f != XBADFILE)
  44509. XFCLOSE(f);
  44510. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  44511. -1), 0);
  44512. /* Start search from last location (should fail) */
  44513. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  44514. rc), -1);
  44515. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  44516. -2), -1);
  44517. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  44518. -1), -1);
  44519. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  44520. /* NID_ext_key_usage, check also its nid and oid */
  44521. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  44522. -1);
  44523. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  44524. x509, rc)));
  44525. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  44526. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  44527. wolfSSL_X509_free(x509);
  44528. #endif
  44529. return EXPECT_RESULT();
  44530. }
  44531. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  44532. {
  44533. EXPECT_DECLS;
  44534. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44535. int rc = 0;
  44536. XFILE f = XBADFILE;
  44537. WOLFSSL_X509* x509 = NULL;
  44538. WOLFSSL_X509_EXTENSION* ext = NULL;
  44539. WOLFSSL_ASN1_STRING* sanString = NULL;
  44540. byte* sanDer = NULL;
  44541. const byte expectedDer[] = {
  44542. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  44543. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  44544. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44545. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44546. if (f != XBADFILE)
  44547. XFCLOSE(f);
  44548. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  44549. ExpectNotNull(ext = X509_get_ext(x509, rc));
  44550. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  44551. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  44552. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  44553. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  44554. X509_free(x509);
  44555. #endif
  44556. return EXPECT_RESULT();
  44557. }
  44558. static int test_wolfSSL_X509_EXTENSION_new(void)
  44559. {
  44560. EXPECT_DECLS;
  44561. #if defined (OPENSSL_ALL)
  44562. WOLFSSL_X509_EXTENSION* ext = NULL;
  44563. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  44564. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  44565. wolfSSL_X509_EXTENSION_free(ext);
  44566. #endif
  44567. return EXPECT_RESULT();
  44568. }
  44569. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  44570. {
  44571. EXPECT_DECLS;
  44572. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44573. WOLFSSL_X509* x509 = NULL;
  44574. WOLFSSL_X509_EXTENSION* ext = NULL;
  44575. WOLFSSL_ASN1_OBJECT* o = NULL;
  44576. XFILE file = XBADFILE;
  44577. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44578. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44579. if (file != XBADFILE)
  44580. XFCLOSE(file);
  44581. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  44582. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44583. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  44584. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  44585. ExpectIntEQ(o->nid, 128);
  44586. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  44587. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  44588. wolfSSL_X509_free(x509);
  44589. #endif
  44590. return EXPECT_RESULT();
  44591. }
  44592. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  44593. {
  44594. EXPECT_DECLS;
  44595. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44596. WOLFSSL_X509* x509 = NULL;
  44597. WOLFSSL_X509_EXTENSION* ext = NULL;
  44598. WOLFSSL_ASN1_STRING* str = NULL;
  44599. XFILE file = XBADFILE;
  44600. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44601. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44602. if (file != XBADFILE)
  44603. XFCLOSE(file);
  44604. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44605. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  44606. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  44607. wolfSSL_X509_free(x509);
  44608. #endif
  44609. return EXPECT_RESULT();
  44610. }
  44611. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  44612. {
  44613. EXPECT_DECLS;
  44614. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44615. WOLFSSL_X509* x509 = NULL;
  44616. WOLFSSL_X509_EXTENSION* ext = NULL;
  44617. XFILE file = XBADFILE;
  44618. int crit = 0;
  44619. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44620. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44621. if (file != XBADFILE)
  44622. XFCLOSE(file);
  44623. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44624. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  44625. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  44626. wolfSSL_X509_free(x509);
  44627. #endif
  44628. return EXPECT_RESULT();
  44629. }
  44630. static int test_wolfSSL_X509V3_EXT_print(void)
  44631. {
  44632. EXPECT_DECLS;
  44633. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  44634. !defined(NO_RSA)
  44635. {
  44636. XFILE f = XBADFILE;
  44637. WOLFSSL_X509* x509 = NULL;
  44638. X509_EXTENSION * ext = NULL;
  44639. int loc = 0;
  44640. BIO *bio = NULL;
  44641. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  44642. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44643. if (f != XBADFILE)
  44644. fclose(f);
  44645. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  44646. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44647. NID_basic_constraints, -1), -1);
  44648. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44649. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44650. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44651. NID_subject_key_identifier, -1), -1);
  44652. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44653. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44654. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44655. NID_authority_key_identifier, -1), -1);
  44656. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44657. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44658. wolfSSL_BIO_free(bio);
  44659. wolfSSL_X509_free(x509);
  44660. }
  44661. {
  44662. X509 *x509 = NULL;
  44663. BIO *bio = NULL;
  44664. X509_EXTENSION *ext = NULL;
  44665. unsigned int i = 0;
  44666. unsigned int idx = 0;
  44667. /* Some NIDs to test with */
  44668. int nids[] = {
  44669. /* NID_key_usage, currently X509_get_ext returns this as a bit
  44670. * string, which messes up X509V3_EXT_print */
  44671. /* NID_ext_key_usage, */
  44672. NID_subject_alt_name,
  44673. };
  44674. int* n = NULL;
  44675. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  44676. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  44677. WOLFSSL_FILETYPE_PEM));
  44678. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  44679. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  44680. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  44681. * update the index */
  44682. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  44683. ExpectNotNull(ext = X509_get_ext(x509, idx));
  44684. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  44685. ExpectIntGT(fprintf(stderr, "\n"), 0);
  44686. }
  44687. BIO_free(bio);
  44688. X509_free(x509);
  44689. }
  44690. #endif
  44691. return EXPECT_RESULT();
  44692. }
  44693. static int test_wolfSSL_X509_cmp(void)
  44694. {
  44695. EXPECT_DECLS;
  44696. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44697. XFILE file1 = XBADFILE;
  44698. XFILE file2 = XBADFILE;
  44699. WOLFSSL_X509* cert1 = NULL;
  44700. WOLFSSL_X509* cert2 = NULL;
  44701. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44702. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  44703. XBADFILE);
  44704. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  44705. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  44706. if (file1 != XBADFILE)
  44707. fclose(file1);
  44708. if (file2 != XBADFILE)
  44709. fclose(file2);
  44710. /* wolfSSL_X509_cmp() testing matching certs */
  44711. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  44712. /* wolfSSL_X509_cmp() testing mismatched certs */
  44713. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  44714. /* wolfSSL_X509_cmp() testing NULL, valid args */
  44715. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  44716. /* wolfSSL_X509_cmp() testing valid, NULL args */
  44717. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  44718. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  44719. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  44720. wolfSSL_X509_free(cert1);
  44721. wolfSSL_X509_free(cert2);
  44722. #endif
  44723. return EXPECT_RESULT();
  44724. }
  44725. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  44726. {
  44727. EXPECT_DECLS;
  44728. #if defined(OPENSSL_ALL)
  44729. EVP_PKEY* pkey;
  44730. pkey = EVP_PKEY_new();
  44731. ExpectNotNull(pkey);
  44732. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  44733. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44734. EVP_PKEY_free(pkey);
  44735. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44736. EVP_PKEY_free(pkey);
  44737. EVP_PKEY_free(pkey);
  44738. #endif
  44739. return EXPECT_RESULT();
  44740. }
  44741. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  44742. {
  44743. EXPECT_DECLS;
  44744. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44745. EVP_PKEY* pkey = NULL;
  44746. const unsigned char* p;
  44747. unsigned char *der = NULL;
  44748. unsigned char *tmp = NULL;
  44749. int derLen;
  44750. p = client_keypub_der_2048;
  44751. /* Check that key can be successfully decoded. */
  44752. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  44753. sizeof_client_keypub_der_2048));
  44754. /* Check that key can be successfully encoded. */
  44755. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44756. /* Ensure that the encoded version matches the original. */
  44757. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  44758. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44759. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44760. * is advanced. */
  44761. tmp = der;
  44762. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44763. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  44764. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44765. ExpectTrue(der + derLen == tmp);
  44766. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44767. EVP_PKEY_free(pkey);
  44768. #endif
  44769. return EXPECT_RESULT();
  44770. }
  44771. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  44772. {
  44773. EXPECT_DECLS;
  44774. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  44775. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44776. EVP_PKEY* pkey = NULL;
  44777. const unsigned char* p;
  44778. unsigned char *der = NULL;
  44779. unsigned char *tmp = NULL;
  44780. int derLen;
  44781. unsigned char pub_buf[65];
  44782. const int pub_len = 65;
  44783. BN_CTX* ctx;
  44784. EC_GROUP* curve = NULL;
  44785. EC_KEY* ephemeral_key = NULL;
  44786. const EC_POINT* h;
  44787. /* Generate an x963 key pair and get public part into pub_buf */
  44788. ExpectNotNull(ctx = BN_CTX_new());
  44789. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  44790. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  44791. NID_X9_62_prime256v1));
  44792. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  44793. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  44794. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  44795. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  44796. /* Prepare the EVP_PKEY */
  44797. ExpectNotNull(pkey = EVP_PKEY_new());
  44798. p = pub_buf;
  44799. /* Check that key can be successfully decoded. */
  44800. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  44801. pub_len));
  44802. /* Check that key can be successfully encoded. */
  44803. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44804. /* Ensure that the encoded version matches the original. */
  44805. ExpectIntEQ(derLen, pub_len);
  44806. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44807. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44808. * is advanced. */
  44809. tmp = der;
  44810. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44811. ExpectIntEQ(derLen, pub_len);
  44812. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44813. ExpectTrue(der + derLen == tmp);
  44814. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44815. EVP_PKEY_free(pkey);
  44816. EC_KEY_free(ephemeral_key);
  44817. EC_GROUP_free(curve);
  44818. #endif
  44819. return EXPECT_RESULT();
  44820. }
  44821. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  44822. {
  44823. EXPECT_DECLS;
  44824. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  44825. DSA* dsa = NULL;
  44826. byte derIn[] = {
  44827. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  44828. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  44829. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  44830. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  44831. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  44832. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  44833. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  44834. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  44835. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  44836. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  44837. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  44838. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  44839. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  44840. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  44841. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  44842. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  44843. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  44844. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  44845. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  44846. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  44847. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  44848. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  44849. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  44850. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  44851. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  44852. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  44853. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  44854. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  44855. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  44856. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  44857. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  44858. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  44859. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  44860. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  44861. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  44862. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  44863. 0x1e, 0xde, 0xc4
  44864. };
  44865. int derInLen = sizeof(derIn);
  44866. byte* derOut = NULL;
  44867. int derOutLen;
  44868. byte* p = derIn;
  44869. /* Check that params can be successfully decoded. */
  44870. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  44871. /* Check that params can be successfully encoded. */
  44872. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  44873. /* Ensure that the encoded version matches the original. */
  44874. ExpectIntEQ(derInLen, derOutLen);
  44875. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  44876. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44877. DSA_free(dsa);
  44878. #endif
  44879. return EXPECT_RESULT();
  44880. }
  44881. static int test_wolfSSL_i2d_PrivateKey(void)
  44882. {
  44883. EXPECT_DECLS;
  44884. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  44885. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44886. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44887. {
  44888. EVP_PKEY* pkey = NULL;
  44889. const unsigned char* server_key =
  44890. (const unsigned char*)server_key_der_2048;
  44891. unsigned char buf[FOURK_BUF];
  44892. unsigned char* pt = NULL;
  44893. int bufSz = 0;
  44894. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  44895. (long)sizeof_server_key_der_2048));
  44896. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  44897. pt = buf;
  44898. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  44899. ExpectIntNE((pt - buf), 0);
  44900. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  44901. EVP_PKEY_free(pkey);
  44902. }
  44903. #endif
  44904. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44905. {
  44906. EVP_PKEY* pkey = NULL;
  44907. const unsigned char* client_key =
  44908. (const unsigned char*)ecc_clikey_der_256;
  44909. unsigned char buf[FOURK_BUF];
  44910. unsigned char* pt = NULL;
  44911. int bufSz = 0;
  44912. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  44913. (long)sizeof_ecc_clikey_der_256)));
  44914. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  44915. pt = buf;
  44916. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  44917. ExpectIntNE((pt - buf), 0);
  44918. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  44919. EVP_PKEY_free(pkey);
  44920. }
  44921. #endif
  44922. #endif
  44923. return EXPECT_RESULT();
  44924. }
  44925. static int test_wolfSSL_OCSP_id_get0_info(void)
  44926. {
  44927. EXPECT_DECLS;
  44928. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  44929. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44930. X509* cert = NULL;
  44931. X509* issuer = NULL;
  44932. OCSP_CERTID* id = NULL;
  44933. OCSP_CERTID* id2 = NULL;
  44934. ASN1_STRING* name = NULL;
  44935. ASN1_OBJECT* pmd = NULL;
  44936. ASN1_STRING* keyHash = NULL;
  44937. ASN1_INTEGER* serial = NULL;
  44938. ASN1_INTEGER* x509Int = NULL;
  44939. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  44940. SSL_FILETYPE_PEM));
  44941. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  44942. SSL_FILETYPE_PEM));
  44943. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  44944. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  44945. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  44946. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  44947. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  44948. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  44949. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  44950. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  44951. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  44952. ExpectNotNull(serial);
  44953. /* compare serial number to one in cert, should be equal */
  44954. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  44955. ExpectIntEQ(x509Int->length, serial->length);
  44956. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  44957. /* test OCSP_id_cmp */
  44958. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  44959. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  44960. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  44961. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  44962. if (id != NULL) {
  44963. id->issuerHash[0] = ~id->issuerHash[0];
  44964. }
  44965. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  44966. OCSP_CERTID_free(id);
  44967. OCSP_CERTID_free(id2);
  44968. X509_free(cert); /* free's x509Int */
  44969. X509_free(issuer);
  44970. #endif
  44971. return EXPECT_RESULT();
  44972. }
  44973. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  44974. {
  44975. EXPECT_DECLS;
  44976. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44977. WOLFSSL_OCSP_CERTID certId;
  44978. byte* targetBuffer = NULL;
  44979. byte* p;
  44980. /* OCSP CertID bytes taken from PCAP */
  44981. byte rawCertId[] = {
  44982. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44983. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44984. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44985. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44986. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44987. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44988. 0xcf, 0xbc, 0x91
  44989. };
  44990. int ret = 0;
  44991. int i;
  44992. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  44993. certId.rawCertId = rawCertId;
  44994. certId.rawCertIdSize = sizeof(rawCertId);
  44995. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  44996. DYNAMIC_TYPE_TMP_BUFFER));
  44997. p = targetBuffer;
  44998. /* Function returns the size of the encoded data. */
  44999. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  45000. /* If target buffer is not null, function increments targetBuffer to point
  45001. * just past the end of the encoded data. */
  45002. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  45003. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45004. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45005. }
  45006. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45007. targetBuffer = NULL;
  45008. /* If target buffer is null, function allocates memory for a buffer and
  45009. * copies the encoded data into it. targetBuffer then points to the start of
  45010. * this newly allocate buffer. */
  45011. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  45012. sizeof(rawCertId));
  45013. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45014. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45015. }
  45016. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  45017. #endif
  45018. return EXPECT_RESULT();
  45019. }
  45020. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  45021. {
  45022. EXPECT_DECLS;
  45023. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45024. WOLFSSL_OCSP_CERTID* certId;
  45025. WOLFSSL_OCSP_CERTID* certIdGood;
  45026. WOLFSSL_OCSP_CERTID* certIdBad;
  45027. const unsigned char* rawCertIdPtr;
  45028. const unsigned char rawCertId[] = {
  45029. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45030. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45031. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45032. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45033. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45034. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45035. 0xcf, 0xbc, 0x91
  45036. };
  45037. rawCertIdPtr = &rawCertId[0];
  45038. /* If the cert ID is NULL the function should allocate it and copy the
  45039. * data to it. */
  45040. certId = NULL;
  45041. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45042. sizeof(rawCertId)));
  45043. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45044. if (certId != NULL) {
  45045. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45046. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  45047. }
  45048. /* If the cert ID is not NULL the function will just copy the data to it. */
  45049. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  45050. DYNAMIC_TYPE_TMP_BUFFER));
  45051. ExpectNotNull(certId);
  45052. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  45053. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  45054. rawCertIdPtr = &rawCertId[0];
  45055. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45056. sizeof(rawCertId)));
  45057. ExpectPtrEq(certIdGood, certId);
  45058. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45059. if (certId != NULL) {
  45060. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45061. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45062. certId = NULL;
  45063. }
  45064. /* The below tests should fail when passed bad parameters. NULL should
  45065. * always be returned. */
  45066. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  45067. sizeof(rawCertId)));
  45068. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  45069. sizeof(rawCertId)));
  45070. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  45071. #endif
  45072. return EXPECT_RESULT();
  45073. }
  45074. static int test_wolfSSL_OCSP_id_cmp(void)
  45075. {
  45076. EXPECT_DECLS;
  45077. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45078. OCSP_CERTID id1;
  45079. OCSP_CERTID id2;
  45080. XMEMSET(&id1, 0, sizeof(id1));
  45081. XMEMSET(&id2, 0, sizeof(id2));
  45082. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  45083. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45084. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  45085. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  45086. #endif
  45087. return EXPECT_RESULT();
  45088. }
  45089. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  45090. {
  45091. EXPECT_DECLS;
  45092. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45093. WOLFSSL_OCSP_SINGLERESP single;
  45094. const WOLFSSL_OCSP_CERTID* certId;
  45095. XMEMSET(&single, 0, sizeof(single));
  45096. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  45097. ExpectPtrEq(&single, certId);
  45098. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  45099. #endif
  45100. return EXPECT_RESULT();
  45101. }
  45102. static int test_wolfSSL_OCSP_single_get0_status(void)
  45103. {
  45104. EXPECT_DECLS;
  45105. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45106. WOLFSSL_OCSP_SINGLERESP single;
  45107. CertStatus certStatus;
  45108. WOLFSSL_ASN1_TIME* thisDate;
  45109. WOLFSSL_ASN1_TIME* nextDate;
  45110. int ret, i;
  45111. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45112. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  45113. /* Fill the date fields with some dummy data. */
  45114. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  45115. certStatus.thisDateParsed.data[i] = i;
  45116. certStatus.nextDateParsed.data[i] = i;
  45117. }
  45118. certStatus.status = CERT_GOOD;
  45119. single.status = &certStatus;
  45120. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  45121. &nextDate);
  45122. ExpectIntEQ(ret, CERT_GOOD);
  45123. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  45124. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  45125. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  45126. CERT_GOOD);
  45127. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  45128. NULL), CERT_GOOD);
  45129. #endif
  45130. return EXPECT_RESULT();
  45131. }
  45132. static int test_wolfSSL_OCSP_resp_count(void)
  45133. {
  45134. EXPECT_DECLS;
  45135. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45136. WOLFSSL_OCSP_BASICRESP basicResp;
  45137. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45138. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45139. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45140. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45141. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45142. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  45143. basicResp.single = &singleRespOne;
  45144. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  45145. singleRespOne.next = &singleRespTwo;
  45146. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  45147. #endif
  45148. return EXPECT_RESULT();
  45149. }
  45150. static int test_wolfSSL_OCSP_resp_get0(void)
  45151. {
  45152. EXPECT_DECLS;
  45153. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45154. WOLFSSL_OCSP_BASICRESP basicResp;
  45155. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45156. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45157. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45158. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45159. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45160. basicResp.single = &singleRespOne;
  45161. singleRespOne.next = &singleRespTwo;
  45162. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  45163. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  45164. #endif
  45165. return EXPECT_RESULT();
  45166. }
  45167. static int test_wolfSSL_EVP_PKEY_derive(void)
  45168. {
  45169. EXPECT_DECLS;
  45170. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45171. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  45172. EVP_PKEY_CTX *ctx = NULL;
  45173. unsigned char *skey = NULL;
  45174. size_t skeylen;
  45175. EVP_PKEY *pkey = NULL;
  45176. EVP_PKEY *peerkey = NULL;
  45177. const unsigned char* key;
  45178. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  45179. /* DH */
  45180. key = dh_key_der_2048;
  45181. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45182. sizeof_dh_key_der_2048)));
  45183. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  45184. key = dh_key_der_2048;
  45185. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45186. sizeof_dh_key_der_2048)));
  45187. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  45188. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45189. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45190. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45191. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45192. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45193. DYNAMIC_TYPE_OPENSSL));
  45194. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45195. EVP_PKEY_CTX_free(ctx);
  45196. ctx = NULL;
  45197. EVP_PKEY_free(peerkey);
  45198. peerkey = NULL;
  45199. EVP_PKEY_free(pkey);
  45200. pkey = NULL;
  45201. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45202. skey = NULL;
  45203. #endif
  45204. #ifdef HAVE_ECC
  45205. /* ECDH */
  45206. key = ecc_clikey_der_256;
  45207. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  45208. sizeof_ecc_clikey_der_256)));
  45209. key = ecc_clikeypub_der_256;
  45210. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  45211. sizeof_ecc_clikeypub_der_256)));
  45212. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45213. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45214. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45215. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45216. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45217. DYNAMIC_TYPE_OPENSSL));
  45218. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45219. EVP_PKEY_CTX_free(ctx);
  45220. EVP_PKEY_free(peerkey);
  45221. EVP_PKEY_free(pkey);
  45222. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45223. #endif /* HAVE_ECC */
  45224. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  45225. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45226. return EXPECT_RESULT();
  45227. }
  45228. static int test_wolfSSL_EVP_PBE_scrypt(void)
  45229. {
  45230. EXPECT_DECLS;
  45231. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  45232. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  45233. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  45234. int ret;
  45235. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  45236. int pwdlen = sizeof(pwd);
  45237. const byte salt[] = {'N','a','C','l'};
  45238. int saltlen = sizeof(salt);
  45239. byte key[80];
  45240. word64 numOvr32 = (word64)INT32_MAX + 1;
  45241. /* expected derived key for N:16, r:1, p:1 */
  45242. const byte expectedKey[] = {
  45243. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  45244. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  45245. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  45246. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  45247. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  45248. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  45249. 0xE7, 0xE9, 0xC0, 0x9A};
  45250. /* N r p mx key keylen */
  45251. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  45252. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  45253. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  45254. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  45255. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  45256. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  45257. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  45258. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  45259. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  45260. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  45261. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  45262. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  45263. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  45264. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  45265. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  45266. ExpectIntEQ(ret, 1); /* should succeed */
  45267. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  45268. key, 64);
  45269. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  45270. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  45271. key, 64);
  45272. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  45273. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  45274. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  45275. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  45276. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  45277. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  45278. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  45279. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  45280. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  45281. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  45282. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  45283. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  45284. ExpectIntEQ(ret, 1);
  45285. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  45286. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  45287. #endif /* !NO_PWDBASED && !NO_SHA256 */
  45288. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  45289. return EXPECT_RESULT();
  45290. }
  45291. static int test_no_op_functions(void)
  45292. {
  45293. EXPECT_DECLS;
  45294. #if defined(OPENSSL_EXTRA)
  45295. /* this makes sure wolfSSL can compile and run these no-op functions */
  45296. SSL_load_error_strings();
  45297. ENGINE_load_builtin_engines();
  45298. OpenSSL_add_all_ciphers();
  45299. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  45300. #endif
  45301. return EXPECT_RESULT();
  45302. }
  45303. static int test_wolfSSL_CRYPTO_memcmp(void)
  45304. {
  45305. EXPECT_DECLS;
  45306. #ifdef OPENSSL_EXTRA
  45307. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45308. "implementation of TLS/SSL for embedded devices to the cloud.";
  45309. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45310. "implementation of TLS/SSL for embedded devices to the cloud.";
  45311. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45312. "implementation of TLS/SSL for embedded devices to the cloud!";
  45313. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  45314. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  45315. #endif
  45316. return EXPECT_RESULT();
  45317. }
  45318. /*----------------------------------------------------------------------------*
  45319. | wolfCrypt ASN
  45320. *----------------------------------------------------------------------------*/
  45321. static int test_wc_CreateEncryptedPKCS8Key(void)
  45322. {
  45323. EXPECT_DECLS;
  45324. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  45325. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  45326. WC_RNG rng;
  45327. byte* encKey = NULL;
  45328. word32 encKeySz = 0;
  45329. word32 decKeySz = 0;
  45330. const char password[] = "Lorem ipsum dolor sit amet";
  45331. word32 passwordSz = (word32)XSTRLEN(password);
  45332. word32 tradIdx = 0;
  45333. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45334. ExpectIntEQ(wc_InitRng(&rng), 0);
  45335. /* Call with NULL for out buffer to get necessary length. */
  45336. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  45337. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  45338. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  45339. LENGTH_ONLY_E);
  45340. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  45341. DYNAMIC_TYPE_TMP_BUFFER));
  45342. /* Call with the allocated out buffer. */
  45343. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  45344. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  45345. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  45346. 0);
  45347. /* Decrypt the encrypted PKCS8 key we just made. */
  45348. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  45349. passwordSz)), 0);
  45350. /* encKey now holds the decrypted key (decrypted in place). */
  45351. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  45352. /* Check that the decrypted key matches the key prior to encryption. */
  45353. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  45354. sizeof_server_key_der_2048), 0);
  45355. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45356. wc_FreeRng(&rng);
  45357. #endif
  45358. return EXPECT_RESULT();
  45359. }
  45360. static int test_wc_GetPkcs8TraditionalOffset(void)
  45361. {
  45362. EXPECT_DECLS;
  45363. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  45364. int length;
  45365. int derSz = 0;
  45366. word32 inOutIdx;
  45367. const char* path = "./certs/server-keyPkcs8.der";
  45368. XFILE file = XBADFILE;
  45369. byte der[2048];
  45370. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  45371. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  45372. if (file != XBADFILE)
  45373. XFCLOSE(file);
  45374. /* valid case */
  45375. inOutIdx = 0;
  45376. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  45377. 0);
  45378. /* inOutIdx > sz */
  45379. inOutIdx = 4000;
  45380. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  45381. BAD_FUNC_ARG);
  45382. /* null input */
  45383. inOutIdx = 0;
  45384. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  45385. BAD_FUNC_ARG);
  45386. /* invalid input, fill buffer with 1's */
  45387. XMEMSET(der, 1, sizeof(der));
  45388. inOutIdx = 0;
  45389. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  45390. ASN_PARSE_E);
  45391. #endif /* NO_ASN */
  45392. return EXPECT_RESULT();
  45393. }
  45394. static int test_wc_SetSubjectRaw(void)
  45395. {
  45396. EXPECT_DECLS;
  45397. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45398. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45399. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45400. WOLFSSL_X509* x509 = NULL;
  45401. int peerCertSz;
  45402. const byte* peerCertBuf = NULL;
  45403. Cert forgedCert;
  45404. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  45405. WOLFSSL_FILETYPE_ASN1));
  45406. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45407. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  45408. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  45409. wolfSSL_FreeX509(x509);
  45410. #endif
  45411. return EXPECT_RESULT();
  45412. }
  45413. static int test_wc_GetSubjectRaw(void)
  45414. {
  45415. EXPECT_DECLS;
  45416. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45417. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  45418. Cert cert;
  45419. byte *subjectRaw;
  45420. ExpectIntEQ(0, wc_InitCert(&cert));
  45421. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  45422. #endif
  45423. return EXPECT_RESULT();
  45424. }
  45425. static int test_wc_SetIssuerRaw(void)
  45426. {
  45427. EXPECT_DECLS;
  45428. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45429. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45430. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45431. WOLFSSL_X509* x509 = NULL;
  45432. int peerCertSz;
  45433. const byte* peerCertBuf;
  45434. Cert forgedCert;
  45435. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  45436. WOLFSSL_FILETYPE_ASN1));
  45437. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45438. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  45439. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  45440. wolfSSL_FreeX509(x509);
  45441. #endif
  45442. return EXPECT_RESULT();
  45443. }
  45444. static int test_wc_SetIssueBuffer(void)
  45445. {
  45446. EXPECT_DECLS;
  45447. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45448. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45449. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45450. WOLFSSL_X509* x509 = NULL;
  45451. int peerCertSz;
  45452. const byte* peerCertBuf;
  45453. Cert forgedCert;
  45454. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  45455. WOLFSSL_FILETYPE_ASN1));
  45456. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45457. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  45458. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  45459. wolfSSL_FreeX509(x509);
  45460. #endif
  45461. return EXPECT_RESULT();
  45462. }
  45463. /*
  45464. * Testing wc_SetSubjectKeyId
  45465. */
  45466. static int test_wc_SetSubjectKeyId(void)
  45467. {
  45468. EXPECT_DECLS;
  45469. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45470. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  45471. Cert cert;
  45472. const char* file = "certs/ecc-client-keyPub.pem";
  45473. ExpectIntEQ(0, wc_InitCert(&cert));
  45474. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  45475. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  45476. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  45477. #endif
  45478. return EXPECT_RESULT();
  45479. } /* END test_wc_SetSubjectKeyId */
  45480. /*
  45481. * Testing wc_SetSubject
  45482. */
  45483. static int test_wc_SetSubject(void)
  45484. {
  45485. EXPECT_DECLS;
  45486. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45487. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  45488. Cert cert;
  45489. const char* file = "./certs/ca-ecc-cert.pem";
  45490. ExpectIntEQ(0, wc_InitCert(&cert));
  45491. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  45492. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  45493. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  45494. #endif
  45495. return EXPECT_RESULT();
  45496. } /* END test_wc_SetSubject */
  45497. static int test_CheckCertSignature(void)
  45498. {
  45499. EXPECT_DECLS;
  45500. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  45501. WOLFSSL_CERT_MANAGER* cm = NULL;
  45502. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  45503. XFILE fp = XBADFILE;
  45504. byte cert[4096];
  45505. int certSz;
  45506. #endif
  45507. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  45508. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  45509. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  45510. #ifndef NO_RSA
  45511. #ifdef USE_CERT_BUFFERS_1024
  45512. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  45513. sizeof_server_cert_der_1024, NULL, cm));
  45514. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45515. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  45516. WOLFSSL_FILETYPE_ASN1));
  45517. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  45518. sizeof_server_cert_der_1024, NULL, cm));
  45519. #elif defined(USE_CERT_BUFFERS_2048)
  45520. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  45521. sizeof_server_cert_der_2048, NULL, cm));
  45522. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45523. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  45524. WOLFSSL_FILETYPE_ASN1));
  45525. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  45526. sizeof_server_cert_der_2048, NULL, cm));
  45527. #endif
  45528. #endif
  45529. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45530. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  45531. sizeof_serv_ecc_der_256, NULL, cm));
  45532. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45533. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  45534. WOLFSSL_FILETYPE_ASN1));
  45535. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  45536. NULL, cm));
  45537. #endif
  45538. #if !defined(NO_FILESYSTEM)
  45539. wolfSSL_CertManagerFree(cm);
  45540. cm = NULL;
  45541. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  45542. #ifndef NO_RSA
  45543. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  45544. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  45545. if (fp != XBADFILE) {
  45546. XFCLOSE(fp);
  45547. fp = XBADFILE;
  45548. }
  45549. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  45550. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  45551. "./certs/ca-cert.pem", NULL));
  45552. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  45553. #endif
  45554. #ifdef HAVE_ECC
  45555. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  45556. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  45557. if (fp != XBADFILE) {
  45558. XFCLOSE(fp);
  45559. fp = XBADFILE;
  45560. }
  45561. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  45562. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  45563. "./certs/ca-ecc-cert.pem", NULL));
  45564. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  45565. #endif
  45566. #endif
  45567. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  45568. (void)fp;
  45569. (void)cert;
  45570. (void)certSz;
  45571. #endif
  45572. wolfSSL_CertManagerFree(cm);
  45573. #endif
  45574. return EXPECT_RESULT();
  45575. }
  45576. static int test_wc_ParseCert(void)
  45577. {
  45578. EXPECT_DECLS;
  45579. #if !defined(NO_CERTS) && !defined(NO_RSA)
  45580. DecodedCert decodedCert;
  45581. const byte* rawCert = client_cert_der_2048;
  45582. const int rawCertSize = sizeof_client_cert_der_2048;
  45583. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  45584. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45585. #ifndef IGNORE_NAME_CONSTRAINTS
  45586. /* check that the subjects emailAddress was not put in the alt name list */
  45587. ExpectNotNull(decodedCert.subjectEmail);
  45588. ExpectNull(decodedCert.altEmailNames);
  45589. #endif
  45590. wc_FreeDecodedCert(&decodedCert);
  45591. #endif
  45592. return EXPECT_RESULT();
  45593. }
  45594. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  45595. * the API safely errors out on badly-formed ASN input.
  45596. * NOTE: Test not compatible with released FIPS implementations!
  45597. */
  45598. static int test_wc_ParseCert_Error(void)
  45599. {
  45600. EXPECT_DECLS;
  45601. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  45602. (!defined(HAVE_FIPS) || \
  45603. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  45604. DecodedCert decodedCert;
  45605. int i;
  45606. /* Certificate data */
  45607. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  45608. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  45609. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  45610. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  45611. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  45612. /* Test data */
  45613. const struct testStruct {
  45614. const byte* c;
  45615. const int cSz;
  45616. const int expRet;
  45617. } t[] = {
  45618. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  45619. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  45620. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  45621. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  45622. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  45623. };
  45624. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  45625. for (i = 0; i < tSz; i++) {
  45626. WOLFSSL_MSG_EX("i == %d", i);
  45627. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  45628. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  45629. wc_FreeDecodedCert(&decodedCert);
  45630. }
  45631. #endif
  45632. return EXPECT_RESULT();
  45633. }
  45634. static int test_MakeCertWithPathLen(void)
  45635. {
  45636. EXPECT_DECLS;
  45637. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  45638. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  45639. const byte expectedPathLen = 7;
  45640. Cert cert;
  45641. DecodedCert decodedCert;
  45642. byte der[FOURK_BUF];
  45643. int derSize = 0;
  45644. WC_RNG rng;
  45645. ecc_key key;
  45646. int ret;
  45647. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45648. XMEMSET(&key, 0, sizeof(ecc_key));
  45649. XMEMSET(&cert, 0, sizeof(Cert));
  45650. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  45651. ExpectIntEQ(wc_InitRng(&rng), 0);
  45652. ExpectIntEQ(wc_ecc_init(&key), 0);
  45653. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  45654. ExpectIntEQ(wc_InitCert(&cert), 0);
  45655. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  45656. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  45657. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  45658. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  45659. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  45660. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  45661. CTC_NAME_SIZE);
  45662. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  45663. CTC_NAME_SIZE);
  45664. cert.selfSigned = 1;
  45665. cert.isCA = 1;
  45666. cert.pathLen = expectedPathLen;
  45667. cert.pathLenSet = 1;
  45668. cert.sigType = CTC_SHA256wECDSA;
  45669. #ifdef WOLFSSL_CERT_EXT
  45670. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  45671. #endif
  45672. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  45673. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  45674. FOURK_BUF, NULL, &key, &rng), 0);
  45675. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  45676. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45677. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  45678. wc_FreeDecodedCert(&decodedCert);
  45679. ret = wc_ecc_free(&key);
  45680. ExpectIntEQ(ret, 0);
  45681. ret = wc_FreeRng(&rng);
  45682. ExpectIntEQ(ret, 0);
  45683. #endif
  45684. return EXPECT_RESULT();
  45685. }
  45686. static int test_MakeCertWithCaFalse(void)
  45687. {
  45688. EXPECT_DECLS;
  45689. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  45690. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  45691. const byte expectedIsCa = 0;
  45692. Cert cert;
  45693. DecodedCert decodedCert;
  45694. byte der[FOURK_BUF];
  45695. int derSize = 0;
  45696. WC_RNG rng;
  45697. ecc_key key;
  45698. int ret;
  45699. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45700. XMEMSET(&key, 0, sizeof(ecc_key));
  45701. XMEMSET(&cert, 0, sizeof(Cert));
  45702. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  45703. ExpectIntEQ(wc_InitRng(&rng), 0);
  45704. ExpectIntEQ(wc_ecc_init(&key), 0);
  45705. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  45706. ExpectIntEQ(wc_InitCert(&cert), 0);
  45707. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  45708. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  45709. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  45710. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  45711. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  45712. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  45713. CTC_NAME_SIZE);
  45714. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  45715. CTC_NAME_SIZE);
  45716. cert.selfSigned = 1;
  45717. cert.isCA = expectedIsCa;
  45718. cert.isCaSet = 1;
  45719. cert.sigType = CTC_SHA256wECDSA;
  45720. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  45721. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  45722. FOURK_BUF, NULL, &key, &rng), 0);
  45723. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  45724. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45725. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  45726. wc_FreeDecodedCert(&decodedCert);
  45727. ret = wc_ecc_free(&key);
  45728. ExpectIntEQ(ret, 0);
  45729. ret = wc_FreeRng(&rng);
  45730. ExpectIntEQ(ret, 0);
  45731. #endif
  45732. return EXPECT_RESULT();
  45733. }
  45734. /*----------------------------------------------------------------------------*
  45735. | wolfCrypt ECC
  45736. *----------------------------------------------------------------------------*/
  45737. static int test_wc_ecc_get_curve_size_from_name(void)
  45738. {
  45739. EXPECT_DECLS;
  45740. #ifdef HAVE_ECC
  45741. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45742. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  45743. #endif
  45744. /* invalid case */
  45745. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  45746. /* NULL input */
  45747. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  45748. #endif /* HAVE_ECC */
  45749. return EXPECT_RESULT();
  45750. }
  45751. static int test_wc_ecc_get_curve_id_from_name(void)
  45752. {
  45753. EXPECT_DECLS;
  45754. #ifdef HAVE_ECC
  45755. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45756. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  45757. ECC_SECP256R1);
  45758. #endif
  45759. /* invalid case */
  45760. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  45761. /* NULL input */
  45762. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  45763. #endif /* HAVE_ECC */
  45764. return EXPECT_RESULT();
  45765. }
  45766. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  45767. !defined(HAVE_SELFTEST) && \
  45768. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  45769. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  45770. {
  45771. EXPECT_DECLS;
  45772. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45773. ecc_key* key;
  45774. const ecc_set_type* params = NULL;
  45775. int ret;
  45776. #endif
  45777. WOLFSSL_EC_KEY *ecKey = NULL;
  45778. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45779. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  45780. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  45781. if (EXPECT_SUCCESS()) {
  45782. ret = EC_KEY_generate_key(ecKey);
  45783. } else
  45784. ret = 0;
  45785. if (ret == 1) {
  45786. /* normal test */
  45787. key = (ecc_key*)ecKey->internal;
  45788. if (key != NULL) {
  45789. params = key->dp;
  45790. }
  45791. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  45792. ECC_SECP256R1);
  45793. }
  45794. #endif
  45795. /* invalid case, NULL input*/
  45796. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  45797. wolfSSL_EC_KEY_free(ecKey);
  45798. return EXPECT_RESULT();
  45799. }
  45800. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  45801. static int test_wc_ecc_get_curve_id_from_params(void)
  45802. {
  45803. EXPECT_DECLS;
  45804. #ifdef HAVE_ECC
  45805. const byte prime[] =
  45806. {
  45807. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45808. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45809. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45810. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  45811. };
  45812. const byte primeInvalid[] =
  45813. {
  45814. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45815. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45816. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45817. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  45818. };
  45819. const byte Af[] =
  45820. {
  45821. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45822. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45823. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45824. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  45825. };
  45826. const byte Bf[] =
  45827. {
  45828. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  45829. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  45830. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  45831. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  45832. };
  45833. const byte order[] =
  45834. {
  45835. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  45836. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  45837. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  45838. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  45839. };
  45840. const byte Gx[] =
  45841. {
  45842. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  45843. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  45844. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  45845. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  45846. };
  45847. const byte Gy[] =
  45848. {
  45849. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  45850. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  45851. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  45852. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  45853. };
  45854. int cofactor = 1;
  45855. int fieldSize = 256;
  45856. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45857. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  45858. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  45859. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  45860. ECC_SECP256R1);
  45861. #endif
  45862. /* invalid case, fieldSize = 0 */
  45863. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  45864. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45865. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  45866. /* invalid case, NULL prime */
  45867. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  45868. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45869. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  45870. /* invalid case, invalid prime */
  45871. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  45872. primeInvalid, sizeof(primeInvalid),
  45873. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45874. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  45875. #endif
  45876. return EXPECT_RESULT();
  45877. }
  45878. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  45879. {
  45880. EXPECT_DECLS;
  45881. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  45882. WOLFSSL_RSA* rsa = NULL;
  45883. WOLFSSL_EVP_PKEY* pkey = NULL;
  45884. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45885. const char* in = "What is easy to do is easy not to do.";
  45886. size_t inlen = XSTRLEN(in);
  45887. size_t outEncLen = 0;
  45888. byte* outEnc = NULL;
  45889. byte* outDec = NULL;
  45890. size_t outDecLen = 0;
  45891. size_t rsaKeySz = 2048/8; /* Bytes */
  45892. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45893. byte* inTmp = NULL;
  45894. byte* outEncTmp = NULL;
  45895. byte* outDecTmp = NULL;
  45896. #endif
  45897. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45898. DYNAMIC_TYPE_TMP_BUFFER));
  45899. if (outEnc != NULL) {
  45900. XMEMSET(outEnc, 0, rsaKeySz);
  45901. }
  45902. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45903. DYNAMIC_TYPE_TMP_BUFFER));
  45904. if (outDec != NULL) {
  45905. XMEMSET(outDec, 0, rsaKeySz);
  45906. }
  45907. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45908. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45909. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45910. if (EXPECT_FAIL()) {
  45911. RSA_free(rsa);
  45912. }
  45913. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45914. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45915. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45916. WOLFSSL_SUCCESS);
  45917. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  45918. since ctx uses it.*/
  45919. ExpectIntEQ(pkey->ref.count, 2);
  45920. EVP_PKEY_free(pkey);
  45921. ExpectIntEQ(pkey->ref.count, 1);
  45922. /* Encrypt data */
  45923. /* Check that we can get the required output buffer length by passing in a
  45924. * NULL output buffer. */
  45925. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  45926. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45927. ExpectIntEQ(rsaKeySz, outEncLen);
  45928. /* Now do the actual encryption. */
  45929. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  45930. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45931. /* Decrypt data */
  45932. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45933. /* Check that we can get the required output buffer length by passing in a
  45934. * NULL output buffer. */
  45935. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  45936. WOLFSSL_SUCCESS);
  45937. ExpectIntEQ(rsaKeySz, outDecLen);
  45938. /* Now do the actual decryption. */
  45939. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  45940. WOLFSSL_SUCCESS);
  45941. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  45942. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45943. /* The input length must be the same size as the RSA key.*/
  45944. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45945. DYNAMIC_TYPE_TMP_BUFFER));
  45946. if (inTmp != NULL) {
  45947. XMEMSET(inTmp, 9, rsaKeySz);
  45948. }
  45949. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45950. DYNAMIC_TYPE_TMP_BUFFER));
  45951. if (outEncTmp != NULL) {
  45952. XMEMSET(outEncTmp, 0, rsaKeySz);
  45953. }
  45954. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45955. DYNAMIC_TYPE_TMP_BUFFER));
  45956. if (outDecTmp != NULL) {
  45957. XMEMSET(outDecTmp, 0, rsaKeySz);
  45958. }
  45959. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45960. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45961. WOLFSSL_SUCCESS);
  45962. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  45963. WOLFSSL_SUCCESS);
  45964. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45965. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  45966. outEncLen), WOLFSSL_SUCCESS);
  45967. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  45968. #endif
  45969. EVP_PKEY_CTX_free(ctx);
  45970. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45971. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45972. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45973. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45974. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45975. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45976. #endif
  45977. #endif
  45978. return EXPECT_RESULT();
  45979. }
  45980. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45981. !defined(HAVE_SELFTEST)
  45982. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45983. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45984. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45985. #endif
  45986. #endif
  45987. #endif
  45988. #if defined(OPENSSL_EXTRA)
  45989. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45990. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45991. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45992. #endif
  45993. #endif
  45994. #endif
  45995. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45996. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45997. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45998. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45999. #endif
  46000. #endif
  46001. #endif
  46002. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46003. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  46004. {
  46005. EXPECT_DECLS;
  46006. #if defined(OPENSSL_EXTRA)
  46007. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46008. !defined(HAVE_SELFTEST)
  46009. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46010. WOLFSSL_RSA* rsa = NULL;
  46011. #endif
  46012. #endif
  46013. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46014. WOLFSSL_DSA* dsa = NULL;
  46015. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46016. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46017. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46018. WOLFSSL_EC_KEY* ecKey = NULL;
  46019. #endif
  46020. #endif
  46021. WOLFSSL_EVP_PKEY* pkey = NULL;
  46022. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46023. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  46024. const char* in = "What is easy to do is easy not to do.";
  46025. size_t inlen = XSTRLEN(in);
  46026. byte hash[SHA256_DIGEST_LENGTH] = {0};
  46027. byte zero[SHA256_DIGEST_LENGTH] = {0};
  46028. SHA256_CTX c;
  46029. byte* sig = NULL;
  46030. byte* sigVerify = NULL;
  46031. size_t siglen;
  46032. size_t siglenOnlyLen;
  46033. size_t keySz = 2048/8; /* Bytes */
  46034. ExpectNotNull(sig =
  46035. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46036. ExpectNotNull(sigVerify =
  46037. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46038. siglen = keySz;
  46039. ExpectNotNull(XMEMSET(sig, 0, keySz));
  46040. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  46041. /* Generate hash */
  46042. SHA256_Init(&c);
  46043. SHA256_Update(&c, in, inlen);
  46044. SHA256_Final(hash, &c);
  46045. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46046. /* workaround for small stack cache case */
  46047. wc_Sha256Free((wc_Sha256*)&c);
  46048. #endif
  46049. /* Generate key */
  46050. ExpectNotNull(pkey = EVP_PKEY_new());
  46051. switch (keyType) {
  46052. case EVP_PKEY_RSA:
  46053. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46054. !defined(HAVE_SELFTEST)
  46055. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46056. {
  46057. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46058. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46059. }
  46060. #endif
  46061. #endif
  46062. break;
  46063. case EVP_PKEY_DSA:
  46064. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46065. ExpectNotNull(dsa = DSA_new());
  46066. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  46067. NULL, 0, NULL, NULL, NULL), 1);
  46068. ExpectIntEQ(DSA_generate_key(dsa), 1);
  46069. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  46070. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46071. break;
  46072. case EVP_PKEY_EC:
  46073. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46074. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46075. {
  46076. ExpectNotNull(ecKey = EC_KEY_new());
  46077. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  46078. ExpectIntEQ(
  46079. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46080. if (EXPECT_FAIL()) {
  46081. EC_KEY_free(ecKey);
  46082. }
  46083. }
  46084. #endif
  46085. #endif
  46086. break;
  46087. }
  46088. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46089. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46090. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46091. !defined(HAVE_SELFTEST)
  46092. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46093. if (keyType == EVP_PKEY_RSA)
  46094. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46095. WOLFSSL_SUCCESS);
  46096. #endif
  46097. #endif
  46098. /* Check returning only length */
  46099. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  46100. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46101. ExpectIntGT(siglenOnlyLen, 0);
  46102. /* Sign data */
  46103. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  46104. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46105. ExpectIntGE(siglenOnlyLen, siglen);
  46106. /* Verify signature */
  46107. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  46108. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46109. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46110. !defined(HAVE_SELFTEST)
  46111. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46112. if (keyType == EVP_PKEY_RSA)
  46113. ExpectIntEQ(
  46114. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  46115. WOLFSSL_SUCCESS);
  46116. #endif
  46117. #endif
  46118. ExpectIntEQ(EVP_PKEY_verify(
  46119. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  46120. WOLFSSL_SUCCESS);
  46121. ExpectIntEQ(EVP_PKEY_verify(
  46122. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  46123. WOLFSSL_FAILURE);
  46124. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46125. !defined(HAVE_SELFTEST)
  46126. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46127. if (keyType == EVP_PKEY_RSA) {
  46128. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  46129. /* Try RSA sign/verify with no padding. */
  46130. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46131. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46132. WOLFSSL_SUCCESS);
  46133. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46134. siglen), WOLFSSL_SUCCESS);
  46135. ExpectIntGE(siglenOnlyLen, siglen);
  46136. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46137. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46138. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  46139. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46140. siglen), WOLFSSL_SUCCESS);
  46141. #endif
  46142. /* Wrong padding schemes. */
  46143. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46144. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  46145. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46146. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46147. siglen), WOLFSSL_SUCCESS);
  46148. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46149. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46150. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46151. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46152. siglen), WOLFSSL_SUCCESS);
  46153. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46154. WOLFSSL_SUCCESS);
  46155. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46156. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  46157. }
  46158. #endif
  46159. #endif
  46160. /* error cases */
  46161. siglen = keySz; /* Reset because sig size may vary slightly */
  46162. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  46163. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46164. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  46165. WOLFSSL_SUCCESS);
  46166. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  46167. WOLFSSL_SUCCESS);
  46168. EVP_PKEY_free(pkey);
  46169. pkey = NULL;
  46170. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46171. DSA_free(dsa);
  46172. dsa = NULL;
  46173. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46174. EVP_PKEY_CTX_free(ctx_verify);
  46175. ctx_verify = NULL;
  46176. EVP_PKEY_CTX_free(ctx);
  46177. ctx = NULL;
  46178. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46179. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46180. #endif /* OPENSSL_EXTRA */
  46181. return EXPECT_RESULT();
  46182. }
  46183. #endif
  46184. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  46185. {
  46186. EXPECT_DECLS;
  46187. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46188. !defined(HAVE_SELFTEST)
  46189. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46190. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  46191. #endif
  46192. #endif
  46193. return EXPECT_RESULT();
  46194. }
  46195. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  46196. {
  46197. EXPECT_DECLS;
  46198. #if defined(OPENSSL_EXTRA)
  46199. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46200. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  46201. #endif
  46202. #endif
  46203. return EXPECT_RESULT();
  46204. }
  46205. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  46206. {
  46207. EXPECT_DECLS;
  46208. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46209. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46210. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  46211. #endif
  46212. #endif
  46213. return EXPECT_RESULT();
  46214. }
  46215. static int test_EVP_PKEY_rsa(void)
  46216. {
  46217. EXPECT_DECLS;
  46218. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46219. WOLFSSL_RSA* rsa = NULL;
  46220. WOLFSSL_EVP_PKEY* pkey = NULL;
  46221. ExpectNotNull(rsa = wolfSSL_RSA_new());
  46222. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46223. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  46224. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  46225. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46226. if (EXPECT_FAIL()) {
  46227. wolfSSL_RSA_free(rsa);
  46228. }
  46229. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  46230. wolfSSL_EVP_PKEY_free(pkey);
  46231. #endif
  46232. return EXPECT_RESULT();
  46233. }
  46234. static int test_EVP_PKEY_ec(void)
  46235. {
  46236. EXPECT_DECLS;
  46237. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46238. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46239. WOLFSSL_EC_KEY* ecKey = NULL;
  46240. WOLFSSL_EVP_PKEY* pkey = NULL;
  46241. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  46242. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46243. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  46244. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  46245. /* Should fail since ecKey is empty */
  46246. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  46247. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  46248. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46249. if (EXPECT_FAIL()) {
  46250. wolfSSL_EC_KEY_free(ecKey);
  46251. }
  46252. wolfSSL_EVP_PKEY_free(pkey);
  46253. #endif
  46254. #endif
  46255. return EXPECT_RESULT();
  46256. }
  46257. static int test_EVP_PKEY_cmp(void)
  46258. {
  46259. EXPECT_DECLS;
  46260. #if defined(OPENSSL_EXTRA)
  46261. EVP_PKEY *a = NULL;
  46262. EVP_PKEY *b = NULL;
  46263. const unsigned char *in;
  46264. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  46265. in = client_key_der_2048;
  46266. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46267. &in, (long)sizeof_client_key_der_2048));
  46268. in = client_key_der_2048;
  46269. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46270. &in, (long)sizeof_client_key_der_2048));
  46271. /* Test success case RSA */
  46272. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46273. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  46274. #else
  46275. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46276. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46277. EVP_PKEY_free(b);
  46278. b = NULL;
  46279. EVP_PKEY_free(a);
  46280. a = NULL;
  46281. #endif
  46282. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46283. in = ecc_clikey_der_256;
  46284. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46285. &in, (long)sizeof_ecc_clikey_der_256));
  46286. in = ecc_clikey_der_256;
  46287. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46288. &in, (long)sizeof_ecc_clikey_der_256));
  46289. /* Test success case ECC */
  46290. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46291. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  46292. #else
  46293. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46294. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46295. EVP_PKEY_free(b);
  46296. b = NULL;
  46297. EVP_PKEY_free(a);
  46298. a = NULL;
  46299. #endif
  46300. /* Test failure cases */
  46301. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  46302. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46303. in = client_key_der_2048;
  46304. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46305. &in, (long)sizeof_client_key_der_2048));
  46306. in = ecc_clikey_der_256;
  46307. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46308. &in, (long)sizeof_ecc_clikey_der_256));
  46309. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46310. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  46311. #else
  46312. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  46313. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46314. EVP_PKEY_free(b);
  46315. b = NULL;
  46316. EVP_PKEY_free(a);
  46317. a = NULL;
  46318. #endif
  46319. /* invalid or empty failure cases */
  46320. a = EVP_PKEY_new();
  46321. b = EVP_PKEY_new();
  46322. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46323. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  46324. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  46325. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  46326. #ifdef NO_RSA
  46327. /* Type check will fail since RSA is the default EVP key type */
  46328. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  46329. #else
  46330. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46331. #endif
  46332. #else
  46333. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  46334. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  46335. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  46336. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  46337. #endif
  46338. EVP_PKEY_free(b);
  46339. EVP_PKEY_free(a);
  46340. (void)in;
  46341. #endif
  46342. return EXPECT_RESULT();
  46343. }
  46344. static int test_ERR_load_crypto_strings(void)
  46345. {
  46346. #if defined(OPENSSL_ALL)
  46347. ERR_load_crypto_strings();
  46348. return TEST_SUCCESS;
  46349. #else
  46350. return TEST_SKIPPED;
  46351. #endif
  46352. }
  46353. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  46354. static void free_x509(X509* x)
  46355. {
  46356. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  46357. }
  46358. #endif
  46359. static int test_sk_X509(void)
  46360. {
  46361. EXPECT_DECLS;
  46362. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  46363. {
  46364. STACK_OF(X509)* s = NULL;
  46365. ExpectNotNull(s = sk_X509_new_null());
  46366. ExpectIntEQ(sk_X509_num(s), 0);
  46367. sk_X509_pop_free(s, NULL);
  46368. ExpectNotNull(s = sk_X509_new_null());
  46369. ExpectIntEQ(sk_X509_num(s), 0);
  46370. sk_X509_pop_free(s, NULL);
  46371. ExpectNotNull(s = sk_X509_new_null());
  46372. sk_X509_push(s, (X509*)1);
  46373. ExpectIntEQ(sk_X509_num(s), 1);
  46374. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  46375. sk_X509_push(s, (X509*)2);
  46376. ExpectIntEQ(sk_X509_num(s), 2);
  46377. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  46378. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  46379. sk_X509_push(s, (X509*)2);
  46380. sk_X509_pop_free(s, free_x509);
  46381. }
  46382. {
  46383. /* Push a list of 10 X509s onto stack, then verify that
  46384. * value(), push(), shift(), and pop() behave as expected. */
  46385. STACK_OF(X509)* s = NULL;
  46386. X509* xList[10];
  46387. int i = 0;
  46388. const int len = (sizeof(xList) / sizeof(xList[0]));
  46389. for (i = 0; i < len; ++i) {
  46390. xList[i] = NULL;
  46391. ExpectNotNull(xList[i] = X509_new());
  46392. }
  46393. /* test push, pop, and free */
  46394. ExpectNotNull(s = sk_X509_new_null());
  46395. for (i = 0; i < len; ++i) {
  46396. sk_X509_push(s, xList[i]);
  46397. ExpectIntEQ(sk_X509_num(s), i + 1);
  46398. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  46399. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  46400. }
  46401. /* pop returns and removes last pushed on stack, which is index 0
  46402. * in sk_x509_value */
  46403. for (i = 0; i < len; ++i) {
  46404. X509 * x = sk_X509_value(s, 0);
  46405. X509 * y = sk_X509_pop(s);
  46406. X509 * z = xList[len - 1 - i];
  46407. ExpectIntEQ((x == y), 1);
  46408. ExpectIntEQ((x == z), 1);
  46409. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  46410. }
  46411. sk_free(s);
  46412. s = NULL;
  46413. /* test push, shift, and free */
  46414. ExpectNotNull(s = sk_X509_new_null());
  46415. for (i = 0; i < len; ++i) {
  46416. sk_X509_push(s, xList[i]);
  46417. ExpectIntEQ(sk_X509_num(s), i + 1);
  46418. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  46419. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  46420. }
  46421. /* shift returns and removes first pushed on stack, which is index i
  46422. * in sk_x509_value() */
  46423. for (i = 0; i < len; ++i) {
  46424. X509 * x = sk_X509_value(s, len - 1 - i);
  46425. X509 * y = sk_X509_shift(s);
  46426. X509 * z = xList[i];
  46427. ExpectIntEQ((x == y), 1);
  46428. ExpectIntEQ((x == z), 1);
  46429. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  46430. }
  46431. sk_free(s);
  46432. for (i = 0; i < len; ++i)
  46433. X509_free(xList[i]);
  46434. }
  46435. #endif
  46436. return EXPECT_RESULT();
  46437. }
  46438. static int test_sk_X509_CRL(void)
  46439. {
  46440. EXPECT_DECLS;
  46441. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  46442. X509_CRL* crl = NULL;
  46443. XFILE fp = XBADFILE;
  46444. STACK_OF(X509_CRL)* s = NULL;
  46445. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  46446. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46447. NULL, NULL));
  46448. if (fp != XBADFILE)
  46449. XFCLOSE(fp);
  46450. ExpectNotNull(s = sk_X509_CRL_new());
  46451. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  46452. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  46453. if (EXPECT_FAIL()) {
  46454. X509_CRL_free(crl);
  46455. }
  46456. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  46457. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  46458. sk_X509_CRL_free(s);
  46459. #endif
  46460. return EXPECT_RESULT();
  46461. }
  46462. static int test_X509_get_signature_nid(void)
  46463. {
  46464. EXPECT_DECLS;
  46465. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46466. X509* x509 = NULL;
  46467. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  46468. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  46469. SSL_FILETYPE_PEM));
  46470. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  46471. X509_free(x509);
  46472. #endif
  46473. return EXPECT_RESULT();
  46474. }
  46475. static int test_X509_REQ(void)
  46476. {
  46477. EXPECT_DECLS;
  46478. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  46479. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  46480. X509_NAME* name = NULL;
  46481. #ifndef NO_RSA
  46482. X509_NAME* subject = NULL;
  46483. #endif
  46484. #if !defined(NO_RSA) || defined(HAVE_ECC)
  46485. X509_REQ* req = NULL;
  46486. EVP_PKEY* priv = NULL;
  46487. EVP_PKEY* pub = NULL;
  46488. unsigned char* der = NULL;
  46489. int len;
  46490. #endif
  46491. #ifndef NO_RSA
  46492. EVP_MD_CTX *mctx = NULL;
  46493. EVP_PKEY_CTX *pkctx = NULL;
  46494. #ifdef USE_CERT_BUFFERS_1024
  46495. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  46496. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  46497. #elif defined(USE_CERT_BUFFERS_2048)
  46498. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  46499. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  46500. #endif
  46501. #endif
  46502. #ifdef HAVE_ECC
  46503. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  46504. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  46505. #endif
  46506. ExpectNotNull(name = X509_NAME_new());
  46507. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  46508. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  46509. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  46510. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  46511. #ifndef NO_RSA
  46512. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  46513. (long)sizeof_client_key_der_2048));
  46514. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  46515. (long)sizeof_client_keypub_der_2048));
  46516. ExpectNotNull(req = X509_REQ_new());
  46517. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  46518. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  46519. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  46520. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  46521. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  46522. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46523. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  46524. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  46525. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  46526. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46527. len = i2d_X509_REQ(req, &der);
  46528. DEBUG_WRITE_DER(der, len, "req.der");
  46529. #ifdef USE_CERT_BUFFERS_1024
  46530. ExpectIntEQ(len, 381);
  46531. #else
  46532. ExpectIntEQ(len, 643);
  46533. #endif
  46534. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46535. der = NULL;
  46536. mctx = EVP_MD_CTX_new();
  46537. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  46538. WOLFSSL_SUCCESS);
  46539. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  46540. EVP_MD_CTX_free(mctx);
  46541. mctx = NULL;
  46542. X509_REQ_free(NULL);
  46543. X509_REQ_free(req);
  46544. req = NULL;
  46545. /* Test getting the subject from a newly created X509_REQ */
  46546. ExpectNotNull(req = X509_REQ_new());
  46547. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  46548. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  46549. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  46550. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  46551. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  46552. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  46553. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  46554. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  46555. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  46556. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  46557. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  46558. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  46559. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  46560. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46561. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46562. len = i2d_X509_REQ(req, &der);
  46563. DEBUG_WRITE_DER(der, len, "req2.der");
  46564. #ifdef USE_CERT_BUFFERS_1024
  46565. ExpectIntEQ(len, 435);
  46566. #else
  46567. ExpectIntEQ(len, 696);
  46568. #endif
  46569. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46570. der = NULL;
  46571. EVP_PKEY_free(pub);
  46572. pub = NULL;
  46573. EVP_PKEY_free(priv);
  46574. priv = NULL;
  46575. X509_REQ_free(req);
  46576. req = NULL;
  46577. #endif
  46578. #ifdef HAVE_ECC
  46579. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  46580. sizeof_ecc_clikey_der_256));
  46581. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  46582. sizeof_ecc_clikeypub_der_256));
  46583. ExpectNotNull(req = X509_REQ_new());
  46584. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  46585. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46586. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46587. /* Signature is random and may be shorter or longer. */
  46588. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  46589. ExpectIntLE(len, 253);
  46590. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46591. X509_REQ_free(req);
  46592. EVP_PKEY_free(pub);
  46593. EVP_PKEY_free(priv);
  46594. #ifdef FP_ECC
  46595. wc_ecc_fp_free();
  46596. #endif
  46597. #endif /* HAVE_ECC */
  46598. X509_NAME_free(name);
  46599. #endif
  46600. return EXPECT_RESULT();
  46601. }
  46602. static int test_wolfssl_PKCS7(void)
  46603. {
  46604. EXPECT_DECLS;
  46605. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  46606. !defined(NO_RSA)
  46607. PKCS7* pkcs7 = NULL;
  46608. byte data[FOURK_BUF];
  46609. word32 len = sizeof(data);
  46610. const byte* p = data;
  46611. byte content[] = "Test data to encode.";
  46612. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  46613. BIO* bio = NULL;
  46614. byte key[sizeof(client_key_der_2048)];
  46615. word32 keySz = (word32)sizeof(key);
  46616. byte* out = NULL;
  46617. #endif
  46618. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  46619. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  46620. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  46621. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  46622. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46623. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  46624. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  46625. PKCS7_free(pkcs7);
  46626. pkcs7 = NULL;
  46627. /* fail case, without PKCS7_NOVERIFY */
  46628. p = data;
  46629. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46630. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  46631. 0), WOLFSSL_FAILURE);
  46632. PKCS7_free(pkcs7);
  46633. pkcs7 = NULL;
  46634. /* success case, with PKCS7_NOVERIFY */
  46635. p = data;
  46636. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46637. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  46638. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  46639. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  46640. /* test i2d */
  46641. XMEMCPY(key, client_key_der_2048, keySz);
  46642. if (pkcs7 != NULL) {
  46643. pkcs7->privateKey = key;
  46644. pkcs7->privateKeySz = (word32)sizeof(key);
  46645. pkcs7->encryptOID = RSAk;
  46646. #ifdef NO_SHA
  46647. pkcs7->hashOID = SHA256h;
  46648. #else
  46649. pkcs7->hashOID = SHAh;
  46650. #endif
  46651. }
  46652. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46653. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  46654. #ifndef NO_ASN_TIME
  46655. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  46656. #else
  46657. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  46658. #endif
  46659. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46660. BIO_free(bio);
  46661. #endif
  46662. PKCS7_free(NULL);
  46663. PKCS7_free(pkcs7);
  46664. #endif
  46665. return EXPECT_RESULT();
  46666. }
  46667. static int test_wolfSSL_PKCS7_sign(void)
  46668. {
  46669. EXPECT_DECLS;
  46670. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  46671. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46672. PKCS7* p7 = NULL;
  46673. PKCS7* p7Ver = NULL;
  46674. byte* out = NULL;
  46675. byte* tmpPtr = NULL;
  46676. int outLen = 0;
  46677. int flags = 0;
  46678. byte data[] = "Test data to encode.";
  46679. const char* cert = "./certs/server-cert.pem";
  46680. const char* key = "./certs/server-key.pem";
  46681. const char* ca = "./certs/ca-cert.pem";
  46682. WOLFSSL_BIO* certBio = NULL;
  46683. WOLFSSL_BIO* keyBio = NULL;
  46684. WOLFSSL_BIO* caBio = NULL;
  46685. WOLFSSL_BIO* inBio = NULL;
  46686. X509* signCert = NULL;
  46687. EVP_PKEY* signKey = NULL;
  46688. X509* caCert = NULL;
  46689. X509_STORE* store = NULL;
  46690. #ifndef NO_PKCS7_STREAM
  46691. int z;
  46692. int ret;
  46693. #endif /* !NO_PKCS7_STREAM */
  46694. /* read signer cert/key into BIO */
  46695. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  46696. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  46697. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  46698. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  46699. /* read CA cert into store (for verify) */
  46700. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  46701. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  46702. ExpectNotNull(store = X509_STORE_new());
  46703. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  46704. /* data to be signed into BIO */
  46705. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46706. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46707. /* PKCS7_sign, bad args: signer NULL */
  46708. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  46709. /* PKCS7_sign, bad args: signer key NULL */
  46710. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  46711. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  46712. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  46713. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  46714. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  46715. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  46716. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  46717. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  46718. {
  46719. flags = PKCS7_BINARY;
  46720. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46721. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46722. /* verify with d2i_PKCS7 */
  46723. tmpPtr = out;
  46724. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46725. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46726. PKCS7_free(p7Ver);
  46727. p7Ver = NULL;
  46728. /* verify with wc_PKCS7_VerifySignedData */
  46729. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46730. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  46731. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46732. #ifndef NO_PKCS7_STREAM
  46733. /* verify with wc_PKCS7_VerifySignedData streaming */
  46734. wc_PKCS7_Free(p7Ver);
  46735. p7Ver = NULL;
  46736. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46737. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  46738. /* test for streaming */
  46739. ret = -1;
  46740. for (z = 0; z < outLen && ret != 0; z++) {
  46741. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  46742. if (ret < 0){
  46743. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  46744. }
  46745. }
  46746. ExpectIntEQ(ret, 0);
  46747. #endif /* !NO_PKCS7_STREAM */
  46748. /* compare the signer found to expected signer */
  46749. ExpectIntNE(p7Ver->verifyCertSz, 0);
  46750. tmpPtr = NULL;
  46751. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  46752. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  46753. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  46754. tmpPtr = NULL;
  46755. wc_PKCS7_Free(p7Ver);
  46756. p7Ver = NULL;
  46757. ExpectNotNull(out);
  46758. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46759. out = NULL;
  46760. PKCS7_free(p7);
  46761. p7 = NULL;
  46762. }
  46763. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  46764. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  46765. {
  46766. /* re-populate input BIO, may have been consumed */
  46767. BIO_free(inBio);
  46768. inBio = NULL;
  46769. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46770. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46771. flags = PKCS7_BINARY | PKCS7_STREAM;
  46772. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46773. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  46774. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46775. /* PKCS7_final, bad args: PKCS7 null */
  46776. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  46777. /* PKCS7_final, bad args: PKCS7 null */
  46778. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  46779. tmpPtr = out;
  46780. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46781. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46782. PKCS7_free(p7Ver);
  46783. p7Ver = NULL;
  46784. ExpectNotNull(out);
  46785. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46786. out = NULL;
  46787. PKCS7_free(p7);
  46788. p7 = NULL;
  46789. }
  46790. /* TEST SUCCESS: Detached, not streaming, not MIME */
  46791. {
  46792. /* re-populate input BIO, may have been consumed */
  46793. BIO_free(inBio);
  46794. inBio = NULL;
  46795. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46796. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46797. flags = PKCS7_BINARY | PKCS7_DETACHED;
  46798. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46799. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46800. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  46801. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46802. if (p7Ver != NULL) {
  46803. p7Ver->content = data;
  46804. p7Ver->contentSz = sizeof(data);
  46805. }
  46806. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46807. wc_PKCS7_Free(p7Ver);
  46808. p7Ver = NULL;
  46809. #ifndef NO_PKCS7_STREAM
  46810. /* verify with wc_PKCS7_VerifySignedData streaming */
  46811. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46812. if (p7Ver != NULL) {
  46813. p7Ver->content = data;
  46814. p7Ver->contentSz = sizeof(data);
  46815. }
  46816. /* test for streaming */
  46817. ret = -1;
  46818. for (z = 0; z < outLen && ret != 0; z++) {
  46819. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  46820. if (ret < 0){
  46821. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  46822. }
  46823. }
  46824. ExpectIntEQ(ret, 0);
  46825. wc_PKCS7_Free(p7Ver);
  46826. p7Ver = NULL;
  46827. #endif /* !NO_PKCS7_STREAM */
  46828. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  46829. * yet support detached content */
  46830. tmpPtr = out;
  46831. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46832. PKCS7_free(p7Ver);
  46833. p7Ver = NULL;
  46834. ExpectNotNull(out);
  46835. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46836. out = NULL;
  46837. PKCS7_free(p7);
  46838. p7 = NULL;
  46839. }
  46840. /* TEST SUCCESS: Detached, streaming, not MIME */
  46841. {
  46842. /* re-populate input BIO, may have been consumed */
  46843. BIO_free(inBio);
  46844. inBio = NULL;
  46845. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46846. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46847. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  46848. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46849. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  46850. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46851. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  46852. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46853. if (p7Ver != NULL) {
  46854. p7Ver->content = data;
  46855. p7Ver->contentSz = sizeof(data);
  46856. }
  46857. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46858. wc_PKCS7_Free(p7Ver);
  46859. p7Ver = NULL;
  46860. ExpectNotNull(out);
  46861. #ifndef NO_PKCS7_STREAM
  46862. /* verify with wc_PKCS7_VerifySignedData streaming */
  46863. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46864. if (p7Ver != NULL) {
  46865. p7Ver->content = data;
  46866. p7Ver->contentSz = sizeof(data);
  46867. }
  46868. /* test for streaming */
  46869. ret = -1;
  46870. for (z = 0; z < outLen && ret != 0; z++) {
  46871. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  46872. if (ret < 0){
  46873. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  46874. }
  46875. }
  46876. ExpectIntEQ(ret, 0);
  46877. ExpectNotNull(out);
  46878. wc_PKCS7_Free(p7Ver);
  46879. p7Ver = NULL;
  46880. #endif /* !NO_PKCS7_STREAM */
  46881. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46882. PKCS7_free(p7);
  46883. p7 = NULL;
  46884. }
  46885. X509_STORE_free(store);
  46886. X509_free(caCert);
  46887. X509_free(signCert);
  46888. EVP_PKEY_free(signKey);
  46889. BIO_free(inBio);
  46890. BIO_free(keyBio);
  46891. BIO_free(certBio);
  46892. BIO_free(caBio);
  46893. #endif
  46894. return EXPECT_RESULT();
  46895. }
  46896. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  46897. {
  46898. EXPECT_DECLS;
  46899. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  46900. PKCS7_SIGNED* pkcs7 = NULL;
  46901. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  46902. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  46903. PKCS7_SIGNED_free(pkcs7);
  46904. #endif
  46905. return EXPECT_RESULT();
  46906. }
  46907. #ifndef NO_BIO
  46908. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  46909. {
  46910. EXPECT_DECLS;
  46911. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  46912. PKCS7* pkcs7 = NULL;
  46913. BIO* bio = NULL;
  46914. const byte* cert_buf = NULL;
  46915. int ret = 0;
  46916. WC_RNG rng;
  46917. const byte data[] = { /* Hello World */
  46918. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  46919. 0x72,0x6c,0x64
  46920. };
  46921. #ifndef NO_RSA
  46922. #if defined(USE_CERT_BUFFERS_2048)
  46923. byte key[sizeof(client_key_der_2048)];
  46924. byte cert[sizeof(client_cert_der_2048)];
  46925. word32 keySz = (word32)sizeof(key);
  46926. word32 certSz = (word32)sizeof(cert);
  46927. XMEMSET(key, 0, keySz);
  46928. XMEMSET(cert, 0, certSz);
  46929. XMEMCPY(key, client_key_der_2048, keySz);
  46930. XMEMCPY(cert, client_cert_der_2048, certSz);
  46931. #elif defined(USE_CERT_BUFFERS_1024)
  46932. byte key[sizeof_client_key_der_1024];
  46933. byte cert[sizeof(sizeof_client_cert_der_1024)];
  46934. word32 keySz = (word32)sizeof(key);
  46935. word32 certSz = (word32)sizeof(cert);
  46936. XMEMSET(key, 0, keySz);
  46937. XMEMSET(cert, 0, certSz);
  46938. XMEMCPY(key, client_key_der_1024, keySz);
  46939. XMEMCPY(cert, client_cert_der_1024, certSz);
  46940. #else
  46941. unsigned char cert[ONEK_BUF];
  46942. unsigned char key[ONEK_BUF];
  46943. XFILE fp = XBADFILE;
  46944. int certSz;
  46945. int keySz;
  46946. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  46947. XBADFILE);
  46948. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  46949. fp), 0);
  46950. if (fp != XBADFILE) {
  46951. XFCLOSE(fp);
  46952. fp = XBADFILE;
  46953. }
  46954. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  46955. XBADFILE);
  46956. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  46957. 0);
  46958. if (fp != XBADFILE) {
  46959. XFCLOSE(fp);
  46960. fp = XBADFILE;
  46961. }
  46962. #endif
  46963. #elif defined(HAVE_ECC)
  46964. #if defined(USE_CERT_BUFFERS_256)
  46965. unsigned char cert[sizeof(cliecc_cert_der_256)];
  46966. unsigned char key[sizeof(ecc_clikey_der_256)];
  46967. int certSz = (int)sizeof(cert);
  46968. int keySz = (int)sizeof(key);
  46969. XMEMSET(cert, 0, certSz);
  46970. XMEMSET(key, 0, keySz);
  46971. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  46972. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  46973. #else
  46974. unsigned char cert[ONEK_BUF];
  46975. unsigned char key[ONEK_BUF];
  46976. XFILE fp = XBADFILE;
  46977. int certSz, keySz;
  46978. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  46979. XBADFILE);
  46980. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  46981. fp), 0);
  46982. if (fp != XBADFILE) {
  46983. XFCLOSE(fp);
  46984. fp = XBADFILE;
  46985. }
  46986. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  46987. XBADFILE);
  46988. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  46989. 0);
  46990. if (fp != XBADFILE) {
  46991. XFCLOSE(fp);
  46992. fp = XBADFILE;
  46993. }
  46994. #endif
  46995. #else
  46996. #error PKCS7 requires ECC or RSA
  46997. #endif
  46998. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  46999. /* initialize with DER encoded cert */
  47000. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  47001. /* init rng */
  47002. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47003. ExpectIntEQ(wc_InitRng(&rng), 0);
  47004. if (pkcs7 != NULL) {
  47005. pkcs7->rng = &rng;
  47006. pkcs7->content = (byte*)data; /* not used for ex */
  47007. pkcs7->contentSz = (word32)sizeof(data);
  47008. pkcs7->contentOID = SIGNED_DATA;
  47009. pkcs7->privateKey = key;
  47010. pkcs7->privateKeySz = (word32)sizeof(key);
  47011. pkcs7->encryptOID = RSAk;
  47012. #ifdef NO_SHA
  47013. pkcs7->hashOID = SHA256h;
  47014. #else
  47015. pkcs7->hashOID = SHAh;
  47016. #endif
  47017. pkcs7->signedAttribs = NULL;
  47018. pkcs7->signedAttribsSz = 0;
  47019. }
  47020. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47021. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  47022. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  47023. /* Read PKCS#7 PEM from BIO */
  47024. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  47025. ExpectIntGE(ret, 0);
  47026. BIO_free(bio);
  47027. wc_PKCS7_Free(pkcs7);
  47028. wc_FreeRng(&rng);
  47029. #endif
  47030. return EXPECT_RESULT();
  47031. }
  47032. #ifdef HAVE_SMIME
  47033. static int test_wolfSSL_SMIME_read_PKCS7(void)
  47034. {
  47035. EXPECT_DECLS;
  47036. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  47037. !defined(NO_RSA)
  47038. PKCS7* pkcs7 = NULL;
  47039. BIO* bio = NULL;
  47040. BIO* bcont = NULL;
  47041. BIO* out = NULL;
  47042. const byte* outBuf = NULL;
  47043. int outBufLen = 0;
  47044. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  47045. XFILE smimeTestFile = XBADFILE;
  47046. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  47047. XBADFILE);
  47048. /* smime-test.p7s */
  47049. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  47050. ExpectNotNull(bio);
  47051. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47052. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47053. ExpectNotNull(pkcs7);
  47054. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47055. PKCS7_NOVERIFY), SSL_SUCCESS);
  47056. XFCLOSE(smimeTestFile);
  47057. if (bcont) BIO_free(bcont);
  47058. bcont = NULL;
  47059. wolfSSL_PKCS7_free(pkcs7);
  47060. pkcs7 = NULL;
  47061. /* smime-test-multipart.p7s */
  47062. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  47063. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47064. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47065. ExpectNotNull(pkcs7);
  47066. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47067. PKCS7_NOVERIFY), SSL_SUCCESS);
  47068. XFCLOSE(smimeTestFile);
  47069. if (bcont) BIO_free(bcont);
  47070. bcont = NULL;
  47071. wolfSSL_PKCS7_free(pkcs7);
  47072. pkcs7 = NULL;
  47073. /* smime-test-multipart-badsig.p7s */
  47074. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  47075. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47076. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47077. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  47078. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47079. PKCS7_NOVERIFY), SSL_FAILURE);
  47080. XFCLOSE(smimeTestFile);
  47081. if (bcont) BIO_free(bcont);
  47082. bcont = NULL;
  47083. wolfSSL_PKCS7_free(pkcs7);
  47084. pkcs7 = NULL;
  47085. /* smime-test-canon.p7s */
  47086. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47087. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47088. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47089. ExpectNotNull(pkcs7);
  47090. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47091. PKCS7_NOVERIFY), SSL_SUCCESS);
  47092. XFCLOSE(smimeTestFile);
  47093. if (bcont) BIO_free(bcont);
  47094. bcont = NULL;
  47095. wolfSSL_PKCS7_free(pkcs7);
  47096. pkcs7 = NULL;
  47097. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  47098. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47099. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47100. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47101. ExpectNotNull(pkcs7);
  47102. out = wolfSSL_BIO_new(BIO_s_mem());
  47103. ExpectNotNull(out);
  47104. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  47105. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  47106. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  47107. /* Content-Type should not show up at beginning of output buffer */
  47108. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  47109. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  47110. BIO_free(out);
  47111. BIO_free(bio);
  47112. if (bcont) BIO_free(bcont);
  47113. wolfSSL_PKCS7_free(pkcs7);
  47114. #endif
  47115. return EXPECT_RESULT();
  47116. }
  47117. static int test_wolfSSL_SMIME_write_PKCS7(void)
  47118. {
  47119. EXPECT_DECLS;
  47120. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  47121. PKCS7* p7 = NULL;
  47122. PKCS7* p7Ver = NULL;
  47123. int flags = 0;
  47124. byte data[] = "Test data to encode.";
  47125. const char* cert = "./certs/server-cert.pem";
  47126. const char* key = "./certs/server-key.pem";
  47127. const char* ca = "./certs/ca-cert.pem";
  47128. WOLFSSL_BIO* certBio = NULL;
  47129. WOLFSSL_BIO* keyBio = NULL;
  47130. WOLFSSL_BIO* caBio = NULL;
  47131. WOLFSSL_BIO* inBio = NULL;
  47132. WOLFSSL_BIO* outBio = NULL;
  47133. WOLFSSL_BIO* content = NULL;
  47134. X509* signCert = NULL;
  47135. EVP_PKEY* signKey = NULL;
  47136. X509* caCert = NULL;
  47137. X509_STORE* store = NULL;
  47138. /* read signer cert/key into BIO */
  47139. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47140. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47141. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47142. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47143. /* read CA cert into store (for verify) */
  47144. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47145. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47146. ExpectNotNull(store = X509_STORE_new());
  47147. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47148. /* generate and verify SMIME: not detached */
  47149. {
  47150. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47151. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47152. flags = PKCS7_STREAM;
  47153. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47154. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47155. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47156. /* bad arg: out NULL */
  47157. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  47158. /* bad arg: pkcs7 NULL */
  47159. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  47160. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47161. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47162. BIO_free(content);
  47163. content = NULL;
  47164. BIO_free(inBio);
  47165. inBio = NULL;
  47166. BIO_free(outBio);
  47167. outBio = NULL;
  47168. PKCS7_free(p7Ver);
  47169. p7Ver = NULL;
  47170. PKCS7_free(p7);
  47171. p7 = NULL;
  47172. }
  47173. /* generate and verify SMIME: not detached, add Content-Type */
  47174. {
  47175. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47176. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47177. flags = PKCS7_STREAM | PKCS7_TEXT;
  47178. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47179. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47180. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47181. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47182. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47183. BIO_free(content);
  47184. content = NULL;
  47185. BIO_free(inBio);
  47186. inBio = NULL;
  47187. BIO_free(outBio);
  47188. outBio = NULL;
  47189. PKCS7_free(p7Ver);
  47190. p7Ver = NULL;
  47191. PKCS7_free(p7);
  47192. p7 = NULL;
  47193. }
  47194. /* generate and verify SMIME: detached */
  47195. {
  47196. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47197. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47198. flags = PKCS7_DETACHED | PKCS7_STREAM;
  47199. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47200. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47201. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47202. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47203. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47204. BIO_free(content);
  47205. content = NULL;
  47206. BIO_free(inBio);
  47207. inBio = NULL;
  47208. BIO_free(outBio);
  47209. outBio = NULL;
  47210. PKCS7_free(p7Ver);
  47211. p7Ver = NULL;
  47212. PKCS7_free(p7);
  47213. p7 = NULL;
  47214. }
  47215. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  47216. {
  47217. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47218. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47219. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  47220. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47221. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47222. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47223. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47224. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47225. BIO_free(content);
  47226. content = NULL;
  47227. BIO_free(inBio);
  47228. inBio = NULL;
  47229. BIO_free(outBio);
  47230. outBio = NULL;
  47231. PKCS7_free(p7Ver);
  47232. p7Ver = NULL;
  47233. PKCS7_free(p7);
  47234. p7 = NULL;
  47235. }
  47236. X509_STORE_free(store);
  47237. X509_free(caCert);
  47238. X509_free(signCert);
  47239. EVP_PKEY_free(signKey);
  47240. BIO_free(keyBio);
  47241. BIO_free(certBio);
  47242. BIO_free(caBio);
  47243. #endif
  47244. return EXPECT_RESULT();
  47245. }
  47246. #endif /* HAVE_SMIME */
  47247. #endif /* !NO_BIO */
  47248. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  47249. * returns 0) */
  47250. static int test_X509_STORE_No_SSL_CTX(void)
  47251. {
  47252. EXPECT_DECLS;
  47253. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47254. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47255. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47256. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47257. defined(HAVE_CRL) && !defined(NO_RSA)
  47258. X509_STORE * store = NULL;
  47259. X509_STORE_CTX * storeCtx = NULL;
  47260. X509_CRL * crl = NULL;
  47261. X509 * ca = NULL;
  47262. X509 * cert = NULL;
  47263. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47264. const char srvCert[] = "./certs/server-cert.pem";
  47265. const char caCert[] = "./certs/ca-cert.pem";
  47266. const char caDir[] = "./certs/crl/hash_pem";
  47267. XFILE fp = XBADFILE;
  47268. X509_LOOKUP * lookup = NULL;
  47269. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47270. /* Set up store with CA */
  47271. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47272. SSL_FILETYPE_PEM)));
  47273. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47274. /* Add CRL lookup directory to store
  47275. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  47276. * of crl.pem */
  47277. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47278. X509_LOOKUP_hash_dir())));
  47279. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  47280. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  47281. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47282. SSL_SUCCESS);
  47283. /* Add CRL to store NOT containing the verified certificate, which
  47284. * forces use of the CRL lookup directory */
  47285. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  47286. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47287. NULL, NULL));
  47288. if (fp != XBADFILE)
  47289. XFCLOSE(fp);
  47290. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47291. /* Create verification context outside of an SSL session */
  47292. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  47293. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47294. SSL_FILETYPE_PEM)));
  47295. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47296. /* Perform verification, which should NOT indicate CRL missing due to the
  47297. * store CM's X509 store pointer being NULL */
  47298. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  47299. X509_CRL_free(crl);
  47300. X509_STORE_free(store);
  47301. X509_STORE_CTX_free(storeCtx);
  47302. X509_free(cert);
  47303. X509_free(ca);
  47304. #endif
  47305. return EXPECT_RESULT();
  47306. }
  47307. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  47308. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  47309. static int test_X509_LOOKUP_add_dir(void)
  47310. {
  47311. EXPECT_DECLS;
  47312. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47313. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47314. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47315. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47316. defined(HAVE_CRL) && !defined(NO_RSA)
  47317. X509_STORE * store = NULL;
  47318. X509_STORE_CTX * storeCtx = NULL;
  47319. X509_CRL * crl = NULL;
  47320. X509 * ca = NULL;
  47321. X509 * cert = NULL;
  47322. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47323. const char srvCert[] = "./certs/server-cert.pem";
  47324. const char caCert[] = "./certs/ca-cert.pem";
  47325. const char caDir[] = "./certs/crl/hash_der";
  47326. XFILE fp = XBADFILE;
  47327. X509_LOOKUP * lookup = NULL;
  47328. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47329. /* Set up store with CA */
  47330. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47331. SSL_FILETYPE_PEM)));
  47332. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47333. /* Add CRL lookup directory to store.
  47334. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  47335. * of crl.der */
  47336. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47337. X509_LOOKUP_hash_dir())));
  47338. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  47339. SSL_SUCCESS);
  47340. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47341. SSL_SUCCESS);
  47342. /* Add CRL to store NOT containing the verified certificate, which
  47343. * forces use of the CRL lookup directory */
  47344. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  47345. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47346. NULL, NULL));
  47347. if (fp != XBADFILE) {
  47348. XFCLOSE(fp);
  47349. fp = XBADFILE;
  47350. }
  47351. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47352. /* Create verification context outside of an SSL session */
  47353. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  47354. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47355. SSL_FILETYPE_PEM)));
  47356. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47357. /* Perform verification, which should NOT return CRL missing */
  47358. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  47359. X509_CRL_free(crl);
  47360. crl = NULL;
  47361. X509_STORE_free(store);
  47362. store = NULL;
  47363. X509_STORE_CTX_free(storeCtx);
  47364. storeCtx = NULL;
  47365. X509_free(cert);
  47366. cert = NULL;
  47367. X509_free(ca);
  47368. ca = NULL;
  47369. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  47370. * We should get CRL_MISSING at the end, because the lookup
  47371. * dir has only ASN1 CRLs. */
  47372. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47373. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47374. SSL_FILETYPE_PEM)));
  47375. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47376. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47377. X509_LOOKUP_hash_dir())));
  47378. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  47379. SSL_SUCCESS);
  47380. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47381. SSL_SUCCESS);
  47382. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  47383. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47384. NULL, NULL));
  47385. if (fp != XBADFILE) {
  47386. XFCLOSE(fp);
  47387. fp = XBADFILE;
  47388. }
  47389. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47390. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  47391. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47392. SSL_FILETYPE_PEM)));
  47393. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47394. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  47395. * in dir containing only ASN1/DER. */
  47396. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  47397. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  47398. X509_CRL_free(crl);
  47399. X509_STORE_free(store);
  47400. X509_STORE_CTX_free(storeCtx);
  47401. X509_free(cert);
  47402. X509_free(ca);
  47403. #endif
  47404. return EXPECT_RESULT();
  47405. }
  47406. /*----------------------------------------------------------------------------*
  47407. | Certificate Failure Checks
  47408. *----------------------------------------------------------------------------*/
  47409. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  47410. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  47411. #if !defined(NO_RSA) || defined(HAVE_ECC)
  47412. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  47413. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  47414. int type)
  47415. {
  47416. int ret;
  47417. WOLFSSL_CERT_MANAGER* cm = NULL;
  47418. switch (type) {
  47419. case TESTING_RSA:
  47420. #ifdef NO_RSA
  47421. fprintf(stderr, "RSA disabled, skipping test\n");
  47422. return ASN_SIG_CONFIRM_E;
  47423. #else
  47424. break;
  47425. #endif
  47426. case TESTING_ECC:
  47427. #ifndef HAVE_ECC
  47428. fprintf(stderr, "ECC disabled, skipping test\n");
  47429. return ASN_SIG_CONFIRM_E;
  47430. #else
  47431. break;
  47432. #endif
  47433. default:
  47434. fprintf(stderr, "Bad function argument\n");
  47435. return BAD_FUNC_ARG;
  47436. }
  47437. cm = wolfSSL_CertManagerNew();
  47438. if (cm == NULL) {
  47439. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  47440. return -1;
  47441. }
  47442. #ifndef NO_FILESYSTEM
  47443. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  47444. if (ret != WOLFSSL_SUCCESS) {
  47445. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  47446. wolfSSL_CertManagerFree(cm);
  47447. return ret;
  47448. }
  47449. #else
  47450. (void)ca;
  47451. #endif
  47452. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  47453. WOLFSSL_FILETYPE_ASN1);
  47454. /* Let ExpectIntEQ handle return code */
  47455. wolfSSL_CertManagerFree(cm);
  47456. return ret;
  47457. }
  47458. #endif
  47459. #if !defined(NO_FILESYSTEM)
  47460. static int test_RsaSigFailure_cm(void)
  47461. {
  47462. EXPECT_DECLS;
  47463. #ifndef NO_RSA
  47464. const char* ca_cert = "./certs/ca-cert.pem";
  47465. const char* server_cert = "./certs/server-cert.der";
  47466. byte* cert_buf = NULL;
  47467. size_t cert_sz = 0;
  47468. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  47469. if (cert_buf != NULL) {
  47470. /* corrupt DER - invert last byte, which is signature */
  47471. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  47472. /* test bad cert */
  47473. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  47474. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  47475. WOLFSSL_FATAL_ERROR);
  47476. #else
  47477. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  47478. ASN_SIG_CONFIRM_E);
  47479. #endif
  47480. }
  47481. /* load_file() uses malloc. */
  47482. if (cert_buf != NULL) {
  47483. free(cert_buf);
  47484. }
  47485. #endif /* !NO_RSA */
  47486. return EXPECT_RESULT();
  47487. }
  47488. static int test_EccSigFailure_cm(void)
  47489. {
  47490. EXPECT_DECLS;
  47491. #ifdef HAVE_ECC
  47492. /* self-signed ECC cert, so use server cert as CA */
  47493. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  47494. const char* server_cert = "./certs/server-ecc.der";
  47495. byte* cert_buf = NULL;
  47496. size_t cert_sz = 0;
  47497. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  47498. if (cert_buf != NULL) {
  47499. /* corrupt DER - invert last byte, which is signature */
  47500. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  47501. /* test bad cert */
  47502. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  47503. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  47504. WOLFSSL_FATAL_ERROR);
  47505. #else
  47506. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  47507. ASN_SIG_CONFIRM_E);
  47508. #endif
  47509. }
  47510. /* load_file() uses malloc. */
  47511. if (cert_buf != NULL) {
  47512. free(cert_buf);
  47513. }
  47514. #ifdef FP_ECC
  47515. wc_ecc_fp_free();
  47516. #endif
  47517. #endif /* HAVE_ECC */
  47518. return EXPECT_RESULT();
  47519. }
  47520. #endif /* !NO_FILESYSTEM */
  47521. #endif /* NO_CERTS */
  47522. #ifdef WOLFSSL_TLS13
  47523. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  47524. #ifdef WC_SHA384_DIGEST_SIZE
  47525. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  47526. #else
  47527. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  47528. #endif
  47529. #endif
  47530. #ifdef WOLFSSL_EARLY_DATA
  47531. static const char earlyData[] = "Early Data";
  47532. static char earlyDataBuffer[1];
  47533. #endif
  47534. static int test_tls13_apis(void)
  47535. {
  47536. EXPECT_DECLS;
  47537. int ret;
  47538. #ifndef WOLFSSL_NO_TLS12
  47539. #ifndef NO_WOLFSSL_CLIENT
  47540. WOLFSSL_CTX* clientTls12Ctx = NULL;
  47541. WOLFSSL* clientTls12Ssl = NULL;
  47542. #endif
  47543. #ifndef NO_WOLFSSL_SERVER
  47544. WOLFSSL_CTX* serverTls12Ctx = NULL;
  47545. WOLFSSL* serverTls12Ssl = NULL;
  47546. #endif
  47547. #endif
  47548. #ifndef NO_WOLFSSL_CLIENT
  47549. WOLFSSL_CTX* clientCtx = NULL;
  47550. WOLFSSL* clientSsl = NULL;
  47551. #endif
  47552. #ifndef NO_WOLFSSL_SERVER
  47553. WOLFSSL_CTX* serverCtx = NULL;
  47554. WOLFSSL* serverSsl = NULL;
  47555. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47556. const char* ourCert = svrCertFile;
  47557. const char* ourKey = svrKeyFile;
  47558. #endif
  47559. #endif
  47560. int required;
  47561. #ifdef WOLFSSL_EARLY_DATA
  47562. int outSz;
  47563. #endif
  47564. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  47565. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  47566. #ifdef HAVE_PQC
  47567. WOLFSSL_KYBER_LEVEL1
  47568. #else
  47569. WOLFSSL_ECC_SECP256R1
  47570. #endif
  47571. };
  47572. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  47573. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  47574. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  47575. int numGroups = 2;
  47576. #endif
  47577. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47578. char groupList[] =
  47579. #ifndef NO_ECC_SECP
  47580. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  47581. "P-521:"
  47582. #endif
  47583. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  47584. "P-384:"
  47585. #endif
  47586. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  47587. "P-256"
  47588. #ifdef HAVE_PQC
  47589. ":P256_KYBER_LEVEL1"
  47590. #endif
  47591. #endif
  47592. #endif /* !defined(NO_ECC_SECP) */
  47593. #ifdef HAVE_PQC
  47594. ":KYBER_LEVEL1"
  47595. #endif
  47596. "";
  47597. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  47598. (void)ret;
  47599. #ifndef WOLFSSL_NO_TLS12
  47600. #ifndef NO_WOLFSSL_CLIENT
  47601. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  47602. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  47603. #endif
  47604. #ifndef NO_WOLFSSL_SERVER
  47605. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  47606. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47607. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  47608. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  47609. WOLFSSL_FILETYPE_PEM);
  47610. #endif
  47611. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  47612. #endif
  47613. #endif
  47614. #ifndef NO_WOLFSSL_CLIENT
  47615. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  47616. clientSsl = wolfSSL_new(clientCtx);
  47617. #endif
  47618. #ifndef NO_WOLFSSL_SERVER
  47619. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  47620. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47621. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  47622. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  47623. #endif
  47624. serverSsl = wolfSSL_new(serverCtx);
  47625. ExpectNotNull(serverSsl);
  47626. #endif
  47627. #ifdef WOLFSSL_SEND_HRR_COOKIE
  47628. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  47629. #ifndef NO_WOLFSSL_CLIENT
  47630. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  47631. #endif
  47632. #ifndef NO_WOLFSSL_SERVER
  47633. #ifndef WOLFSSL_NO_TLS12
  47634. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  47635. BAD_FUNC_ARG);
  47636. #endif
  47637. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  47638. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  47639. WOLFSSL_SUCCESS);
  47640. #endif
  47641. #endif
  47642. #ifdef HAVE_SUPPORTED_CURVES
  47643. #ifdef HAVE_ECC
  47644. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  47645. BAD_FUNC_ARG);
  47646. #ifndef NO_WOLFSSL_SERVER
  47647. do {
  47648. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  47649. #ifdef WOLFSSL_ASYNC_CRYPT
  47650. if (ret == WC_PENDING_E)
  47651. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  47652. #endif
  47653. }
  47654. while (ret == WC_PENDING_E);
  47655. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47656. #endif
  47657. #ifndef NO_WOLFSSL_CLIENT
  47658. #ifndef WOLFSSL_NO_TLS12
  47659. do {
  47660. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  47661. #ifdef WOLFSSL_ASYNC_CRYPT
  47662. if (ret == WC_PENDING_E)
  47663. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  47664. #endif
  47665. }
  47666. while (ret == WC_PENDING_E);
  47667. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47668. #endif
  47669. do {
  47670. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  47671. #ifdef WOLFSSL_ASYNC_CRYPT
  47672. if (ret == WC_PENDING_E)
  47673. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  47674. #endif
  47675. }
  47676. while (ret == WC_PENDING_E);
  47677. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47678. #endif
  47679. #elif defined(HAVE_CURVE25519)
  47680. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  47681. #ifndef NO_WOLFSSL_SERVER
  47682. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  47683. WOLFSSL_SUCCESS);
  47684. #endif
  47685. #ifndef NO_WOLFSSL_CLIENT
  47686. #ifndef WOLFSSL_NO_TLS12
  47687. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  47688. WOLFSSL_SUCCESS);
  47689. #endif
  47690. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  47691. WOLFSSL_SUCCESS);
  47692. #endif
  47693. #elif defined(HAVE_CURVE448)
  47694. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  47695. #ifndef NO_WOLFSSL_SERVER
  47696. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  47697. WOLFSSL_SUCCESS);
  47698. #endif
  47699. #ifndef NO_WOLFSSL_CLIENT
  47700. #ifndef WOLFSSL_NO_TLS12
  47701. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  47702. WOLFSSL_SUCCESS);
  47703. #endif
  47704. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  47705. WOLFSSL_SUCCESS);
  47706. #endif
  47707. #else
  47708. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  47709. BAD_FUNC_ARG);
  47710. #ifndef NO_WOLFSSL_CLIENT
  47711. #ifndef WOLFSSL_NO_TLS12
  47712. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  47713. NOT_COMPILED_IN);
  47714. #endif
  47715. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  47716. NOT_COMPILED_IN);
  47717. #endif
  47718. #endif
  47719. #if defined(HAVE_PQC)
  47720. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  47721. #ifndef NO_WOLFSSL_SERVER
  47722. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  47723. WOLFSSL_SUCCESS);
  47724. #endif
  47725. #ifndef NO_WOLFSSL_CLIENT
  47726. #ifndef WOLFSSL_NO_TLS12
  47727. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  47728. BAD_FUNC_ARG);
  47729. #endif
  47730. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  47731. WOLFSSL_SUCCESS);
  47732. #endif
  47733. #endif
  47734. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  47735. #ifndef NO_WOLFSSL_SERVER
  47736. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  47737. #endif
  47738. #ifndef NO_WOLFSSL_CLIENT
  47739. #ifndef WOLFSSL_NO_TLS12
  47740. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  47741. #endif
  47742. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  47743. #endif
  47744. #endif /* HAVE_SUPPORTED_CURVES */
  47745. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  47746. #ifndef NO_WOLFSSL_CLIENT
  47747. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  47748. #endif
  47749. #ifndef NO_WOLFSSL_SERVER
  47750. #ifndef WOLFSSL_NO_TLS12
  47751. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  47752. #endif
  47753. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  47754. #endif
  47755. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  47756. #ifndef NO_WOLFSSL_CLIENT
  47757. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  47758. #endif
  47759. #ifndef NO_WOLFSSL_SERVER
  47760. #ifndef WOLFSSL_NO_TLS12
  47761. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  47762. #endif
  47763. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  47764. #endif
  47765. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  47766. #ifndef NO_WOLFSSL_CLIENT
  47767. #ifndef WOLFSSL_NO_TLS12
  47768. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  47769. #endif
  47770. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  47771. #endif
  47772. #ifndef NO_WOLFSSL_SERVER
  47773. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  47774. #endif
  47775. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  47776. #ifndef NO_WOLFSSL_CLIENT
  47777. #ifndef WOLFSSL_NO_TLS12
  47778. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  47779. #endif
  47780. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  47781. #endif
  47782. #ifndef NO_WOLFSSL_SERVER
  47783. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  47784. #endif
  47785. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  47786. #ifndef NO_WOLFSSL_CLIENT
  47787. #ifndef WOLFSSL_NO_TLS12
  47788. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  47789. #endif
  47790. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  47791. #endif
  47792. #ifndef NO_WOLFSSL_SERVER
  47793. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  47794. #endif
  47795. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  47796. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  47797. #ifndef NO_WOLFSSL_CLIENT
  47798. #ifndef WOLFSSL_NO_TLS12
  47799. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  47800. BAD_FUNC_ARG);
  47801. #endif
  47802. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  47803. #endif
  47804. #ifndef NO_WOLFSSL_SERVER
  47805. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  47806. #endif
  47807. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  47808. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  47809. #ifndef NO_WOLFSSL_SERVER
  47810. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  47811. #endif
  47812. #ifndef NO_WOLFSSL_CLIENT
  47813. #ifndef WOLFSSL_NO_TLS12
  47814. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  47815. BAD_FUNC_ARG);
  47816. #endif
  47817. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  47818. #endif
  47819. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  47820. #ifndef NO_WOLFSSL_SERVER
  47821. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  47822. #endif
  47823. #ifndef NO_WOLFSSL_CLIENT
  47824. #ifndef WOLFSSL_NO_TLS12
  47825. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  47826. BAD_FUNC_ARG);
  47827. #endif
  47828. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  47829. #endif
  47830. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  47831. #ifndef NO_WOLFSSL_CLIENT
  47832. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  47833. #endif
  47834. #ifndef NO_WOLFSSL_SERVER
  47835. #ifndef WOLFSSL_NO_TLS12
  47836. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  47837. BAD_FUNC_ARG);
  47838. #endif
  47839. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  47840. #endif
  47841. #endif
  47842. #ifdef HAVE_ECC
  47843. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  47844. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  47845. #ifndef NO_WOLFSSL_SERVER
  47846. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  47847. #endif
  47848. #ifndef NO_WOLFSSL_CLIENT
  47849. #ifndef WOLFSSL_NO_TLS12
  47850. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  47851. #endif
  47852. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  47853. #endif
  47854. #endif
  47855. #ifdef HAVE_SUPPORTED_CURVES
  47856. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  47857. #ifndef NO_WOLFSSL_CLIENT
  47858. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  47859. #endif
  47860. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  47861. #ifndef NO_WOLFSSL_CLIENT
  47862. #ifndef WOLFSSL_NO_TLS12
  47863. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  47864. BAD_FUNC_ARG);
  47865. #endif
  47866. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  47867. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  47868. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  47869. WOLFSSL_SUCCESS);
  47870. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  47871. BAD_FUNC_ARG);
  47872. #endif
  47873. #ifndef NO_WOLFSSL_SERVER
  47874. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  47875. WOLFSSL_SUCCESS);
  47876. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  47877. BAD_FUNC_ARG);
  47878. #endif
  47879. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  47880. #ifndef NO_WOLFSSL_CLIENT
  47881. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  47882. #endif
  47883. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  47884. #ifndef NO_WOLFSSL_CLIENT
  47885. #ifndef WOLFSSL_NO_TLS12
  47886. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  47887. BAD_FUNC_ARG);
  47888. #endif
  47889. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  47890. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  47891. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  47892. WOLFSSL_SUCCESS);
  47893. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  47894. BAD_FUNC_ARG);
  47895. #endif
  47896. #ifndef NO_WOLFSSL_SERVER
  47897. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  47898. WOLFSSL_SUCCESS);
  47899. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  47900. BAD_FUNC_ARG);
  47901. #endif
  47902. #ifdef OPENSSL_EXTRA
  47903. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  47904. #ifndef NO_WOLFSSL_CLIENT
  47905. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  47906. WOLFSSL_FAILURE);
  47907. #endif
  47908. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  47909. WOLFSSL_FAILURE);
  47910. #ifndef NO_WOLFSSL_CLIENT
  47911. #ifndef WOLFSSL_NO_TLS12
  47912. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  47913. WOLFSSL_FAILURE);
  47914. #endif
  47915. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  47916. WOLFSSL_SUCCESS);
  47917. #endif
  47918. #ifndef NO_WOLFSSL_SERVER
  47919. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  47920. WOLFSSL_SUCCESS);
  47921. #endif
  47922. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  47923. #ifndef NO_WOLFSSL_CLIENT
  47924. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  47925. #endif
  47926. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  47927. #ifndef NO_WOLFSSL_CLIENT
  47928. #ifndef WOLFSSL_NO_TLS12
  47929. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  47930. WOLFSSL_FAILURE);
  47931. #endif
  47932. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  47933. WOLFSSL_SUCCESS);
  47934. #endif
  47935. #ifndef NO_WOLFSSL_SERVER
  47936. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  47937. WOLFSSL_SUCCESS);
  47938. #endif
  47939. #endif /* OPENSSL_EXTRA */
  47940. #endif /* HAVE_SUPPORTED_CURVES */
  47941. #endif /* HAVE_ECC */
  47942. #ifdef WOLFSSL_EARLY_DATA
  47943. #ifndef OPENSSL_EXTRA
  47944. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47945. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47946. #else
  47947. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47948. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47949. #endif
  47950. #ifndef NO_WOLFSSL_CLIENT
  47951. #ifndef OPENSSL_EXTRA
  47952. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47953. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47954. #else
  47955. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47956. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47957. #endif
  47958. #endif
  47959. #ifndef NO_WOLFSSL_SERVER
  47960. #ifndef WOLFSSL_NO_TLS12
  47961. #ifndef OPENSSL_EXTRA
  47962. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47963. BAD_FUNC_ARG);
  47964. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47965. #else
  47966. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47967. BAD_FUNC_ARG);
  47968. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47969. #endif
  47970. #endif
  47971. #ifndef OPENSSL_EXTRA
  47972. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47973. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  47974. WOLFSSL_SUCCESS);
  47975. #else
  47976. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  47977. #endif
  47978. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  47979. #else
  47980. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  47981. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  47982. #endif
  47983. #endif
  47984. #ifndef OPENSSL_EXTRA
  47985. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47986. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  47987. #else
  47988. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47989. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  47990. #endif
  47991. #ifndef NO_WOLFSSL_CLIENT
  47992. #ifndef OPENSSL_EXTRA
  47993. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47994. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  47995. #else
  47996. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  47997. #endif
  47998. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  47999. #else
  48000. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48001. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  48002. #endif
  48003. #endif
  48004. #ifndef NO_WOLFSSL_SERVER
  48005. #ifndef WOLFSSL_NO_TLS12
  48006. #ifndef OPENSSL_EXTRA
  48007. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48008. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48009. #else
  48010. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48011. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48012. #endif
  48013. #endif
  48014. #ifndef OPENSSL_EXTRA
  48015. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48016. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  48017. #else
  48018. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  48019. #endif
  48020. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  48021. #else
  48022. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  48023. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  48024. #endif
  48025. #endif
  48026. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  48027. &outSz), BAD_FUNC_ARG);
  48028. #ifndef NO_WOLFSSL_CLIENT
  48029. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  48030. &outSz), BAD_FUNC_ARG);
  48031. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  48032. BAD_FUNC_ARG);
  48033. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48034. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  48035. #endif
  48036. #ifndef NO_WOLFSSL_SERVER
  48037. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  48038. sizeof(earlyData), &outSz), SIDE_ERROR);
  48039. #endif
  48040. #ifndef NO_WOLFSSL_CLIENT
  48041. #ifndef WOLFSSL_NO_TLS12
  48042. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  48043. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  48044. #endif
  48045. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48046. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  48047. #endif
  48048. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  48049. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48050. #ifndef NO_WOLFSSL_SERVER
  48051. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  48052. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48053. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  48054. &outSz), BAD_FUNC_ARG);
  48055. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48056. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  48057. #endif
  48058. #ifndef NO_WOLFSSL_CLIENT
  48059. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  48060. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  48061. #endif
  48062. #ifndef NO_WOLFSSL_SERVER
  48063. #ifndef WOLFSSL_NO_TLS12
  48064. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  48065. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48066. #endif
  48067. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48068. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  48069. #endif
  48070. #endif
  48071. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  48072. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  48073. #endif
  48074. #ifndef NO_WOLFSSL_SERVER
  48075. wolfSSL_free(serverSsl);
  48076. wolfSSL_CTX_free(serverCtx);
  48077. #endif
  48078. #ifndef NO_WOLFSSL_CLIENT
  48079. wolfSSL_free(clientSsl);
  48080. wolfSSL_CTX_free(clientCtx);
  48081. #endif
  48082. #ifndef WOLFSSL_NO_TLS12
  48083. #ifndef NO_WOLFSSL_SERVER
  48084. wolfSSL_free(serverTls12Ssl);
  48085. wolfSSL_CTX_free(serverTls12Ctx);
  48086. #endif
  48087. #ifndef NO_WOLFSSL_CLIENT
  48088. wolfSSL_free(clientTls12Ssl);
  48089. wolfSSL_CTX_free(clientTls12Ctx);
  48090. #endif
  48091. #endif
  48092. return EXPECT_RESULT();
  48093. }
  48094. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48095. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48096. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48097. /* Called when writing. */
  48098. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48099. {
  48100. (void)ssl;
  48101. (void)buf;
  48102. (void)sz;
  48103. (void)ctx;
  48104. /* Force error return from wolfSSL_accept_TLSv13(). */
  48105. return WANT_WRITE;
  48106. }
  48107. /* Called when reading. */
  48108. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48109. {
  48110. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  48111. int len = (int)msg->length;
  48112. (void)ssl;
  48113. (void)sz;
  48114. /* Pass back as much of message as will fit in buffer. */
  48115. if (len > sz)
  48116. len = sz;
  48117. XMEMCPY(buf, msg->buffer, len);
  48118. /* Move over returned data. */
  48119. msg->buffer += len;
  48120. msg->length -= len;
  48121. /* Amount actually copied. */
  48122. return len;
  48123. }
  48124. #endif
  48125. static int test_tls13_cipher_suites(void)
  48126. {
  48127. EXPECT_DECLS;
  48128. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48129. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48130. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48131. WOLFSSL_CTX* ctx = NULL;
  48132. WOLFSSL *ssl = NULL;
  48133. int i;
  48134. byte clientHello[] = {
  48135. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  48136. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  48137. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  48138. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  48139. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  48140. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  48141. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  48142. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  48143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  48145. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  48146. 0x13, 0x01,
  48147. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  48148. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  48149. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  48150. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  48151. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  48152. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  48153. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  48154. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  48155. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  48156. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  48157. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  48158. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  48159. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  48160. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  48161. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  48162. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  48163. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  48164. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  48165. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  48166. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  48167. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  48168. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  48169. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  48170. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  48171. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  48172. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  48173. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  48174. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  48175. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  48176. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  48177. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  48178. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  48179. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  48180. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  48181. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  48182. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48183. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48184. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  48185. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  48186. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  48187. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  48188. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  48189. };
  48190. WOLFSSL_BUFFER_INFO msg;
  48191. /* Offset into ClientHello message data of first cipher suite. */
  48192. const int csOff = 78;
  48193. /* Server cipher list. */
  48194. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  48195. /* Suite list with duplicates. */
  48196. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  48197. "TLS13-AES128-GCM-SHA256:"
  48198. "TLS13-AES256-GCM-SHA384:"
  48199. "TLS13-AES256-GCM-SHA384:"
  48200. "TLS13-AES128-GCM-SHA256";
  48201. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48202. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48203. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48204. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48205. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48206. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  48207. #endif
  48208. /* Set up wolfSSL context. */
  48209. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  48210. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48211. WOLFSSL_FILETYPE_PEM));
  48212. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48213. WOLFSSL_FILETYPE_PEM));
  48214. /* Read from 'msg'. */
  48215. wolfSSL_SetIORecv(ctx, CsRecv);
  48216. /* No where to send to - dummy sender. */
  48217. wolfSSL_SetIOSend(ctx, CsSend);
  48218. /* Test cipher suite list with many copies of a cipher suite. */
  48219. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48220. msg.buffer = clientHello;
  48221. msg.length = (unsigned int)sizeof(clientHello);
  48222. wolfSSL_SetIOReadCtx(ssl, &msg);
  48223. /* Force server to have as many occurrences of same cipher suite as
  48224. * possible. */
  48225. if (ssl != NULL) {
  48226. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  48227. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  48228. for (i = 0; i < suites->suiteSz; i += 2) {
  48229. suites->suites[i + 0] = TLS13_BYTE;
  48230. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  48231. }
  48232. }
  48233. /* Test multiple occurrences of same cipher suite. */
  48234. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48235. wolfSSL_free(ssl);
  48236. ssl = NULL;
  48237. /* Set client order opposite to server order:
  48238. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  48239. clientHello[csOff + 0] = TLS13_BYTE;
  48240. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  48241. clientHello[csOff + 2] = TLS13_BYTE;
  48242. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  48243. /* Test server order negotiation. */
  48244. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48245. msg.buffer = clientHello;
  48246. msg.length = (unsigned int)sizeof(clientHello);
  48247. wolfSSL_SetIOReadCtx(ssl, &msg);
  48248. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48249. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48250. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  48251. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48252. /* Check refined order - server order. */
  48253. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48254. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48255. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48256. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48257. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48258. wolfSSL_free(ssl);
  48259. ssl = NULL;
  48260. /* Test client order negotiation. */
  48261. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48262. msg.buffer = clientHello;
  48263. msg.length = (unsigned int)sizeof(clientHello);
  48264. wolfSSL_SetIOReadCtx(ssl, &msg);
  48265. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48266. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48267. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  48268. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  48269. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48270. /* Check refined order - client order. */
  48271. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48272. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48273. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48274. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48275. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48276. wolfSSL_free(ssl);
  48277. ssl = NULL;
  48278. /* Check duplicate detection is working. */
  48279. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  48280. ExpectIntEQ(ctx->suites->suiteSz, 4);
  48281. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48282. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48283. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48284. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48285. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48286. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  48287. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  48288. ExpectIntEQ(ctx->suites->suiteSz, 4);
  48289. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48290. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48291. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48292. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48293. #endif
  48294. wolfSSL_CTX_free(ctx);
  48295. #endif
  48296. return EXPECT_RESULT();
  48297. }
  48298. #endif
  48299. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  48300. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  48301. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  48302. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  48303. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  48304. const unsigned char* priv, unsigned int privSz,
  48305. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  48306. unsigned char* out, unsigned int* outlen,
  48307. void* ctx)
  48308. {
  48309. int result;
  48310. /* Test fail when context associated with WOLFSSL is NULL */
  48311. if (ctx == NULL) {
  48312. return -1;
  48313. }
  48314. (void)ssl;
  48315. /* return 0 on success */
  48316. PRIVATE_KEY_UNLOCK();
  48317. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  48318. PRIVATE_KEY_LOCK();
  48319. return result;
  48320. }
  48321. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  48322. EXPECT_DECLS;
  48323. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  48324. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  48325. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  48326. WOLFSSL_SUCCESS);
  48327. #endif
  48328. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  48329. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  48330. WOLFSSL_SUCCESS);
  48331. #endif
  48332. return EXPECT_RESULT();
  48333. }
  48334. static int test_dh_ssl_setup(WOLFSSL* ssl)
  48335. {
  48336. EXPECT_DECLS;
  48337. static int dh_test_ctx = 1;
  48338. int ret;
  48339. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  48340. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  48341. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  48342. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  48343. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48344. }
  48345. return EXPECT_RESULT();
  48346. }
  48347. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  48348. {
  48349. EXPECT_DECLS;
  48350. int ret;
  48351. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  48352. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  48353. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  48354. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  48355. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48356. }
  48357. return EXPECT_RESULT();
  48358. }
  48359. #endif
  48360. static int test_DhCallbacks(void)
  48361. {
  48362. EXPECT_DECLS;
  48363. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  48364. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  48365. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  48366. WOLFSSL_CTX *ctx = NULL;
  48367. WOLFSSL *ssl = NULL;
  48368. int test;
  48369. test_ssl_cbf func_cb_client;
  48370. test_ssl_cbf func_cb_server;
  48371. /* Test that DH callback APIs work. */
  48372. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48373. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  48374. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  48375. /* load client ca cert */
  48376. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  48377. WOLFSSL_SUCCESS);
  48378. /* test with NULL arguments */
  48379. wolfSSL_SetDhAgreeCtx(NULL, &test);
  48380. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  48381. /* test success case */
  48382. test = 1;
  48383. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48384. wolfSSL_SetDhAgreeCtx(ssl, &test);
  48385. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  48386. wolfSSL_free(ssl);
  48387. wolfSSL_CTX_free(ctx);
  48388. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  48389. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  48390. /* set callbacks to use DH functions */
  48391. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  48392. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  48393. func_cb_client.method = wolfTLSv1_2_client_method;
  48394. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  48395. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  48396. func_cb_server.method = wolfTLSv1_2_server_method;
  48397. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  48398. &func_cb_server, NULL), TEST_SUCCESS);
  48399. /* Test fail */
  48400. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  48401. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  48402. /* set callbacks to use DH functions */
  48403. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  48404. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  48405. func_cb_client.method = wolfTLSv1_2_client_method;
  48406. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  48407. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  48408. func_cb_server.method = wolfTLSv1_2_server_method;
  48409. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  48410. &func_cb_server, NULL), TEST_FAIL);
  48411. #endif
  48412. return EXPECT_RESULT();
  48413. }
  48414. #endif /* HAVE_PK_CALLBACKS */
  48415. #ifdef HAVE_HASHDRBG
  48416. #ifdef TEST_RESEED_INTERVAL
  48417. static int test_wc_RNG_GenerateBlock_Reseed(void)
  48418. {
  48419. EXPECT_DECLS;
  48420. int i;
  48421. WC_RNG rng;
  48422. byte key[32];
  48423. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48424. ExpectIntEQ(wc_InitRng(&rng), 0);
  48425. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  48426. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  48427. }
  48428. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  48429. return EXPECT_RESULT();
  48430. }
  48431. #endif /* TEST_RESEED_INTERVAL */
  48432. static int test_wc_RNG_GenerateBlock(void)
  48433. {
  48434. EXPECT_DECLS;
  48435. int i;
  48436. WC_RNG rng;
  48437. byte key[32];
  48438. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48439. ExpectIntEQ(wc_InitRng(&rng), 0);
  48440. for (i = 0; i < 10; i++) {
  48441. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  48442. }
  48443. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  48444. return EXPECT_RESULT();
  48445. }
  48446. #endif /* HAVE_HASHDRBG */
  48447. /*
  48448. * Testing get_rand_digit
  48449. */
  48450. static int test_get_rand_digit(void)
  48451. {
  48452. EXPECT_DECLS;
  48453. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  48454. WC_RNG rng;
  48455. mp_digit d;
  48456. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48457. ExpectIntEQ(wc_InitRng(&rng), 0);
  48458. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  48459. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  48460. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  48461. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  48462. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  48463. #endif
  48464. return EXPECT_RESULT();
  48465. } /* End test_get_rand_digit*/
  48466. /*
  48467. * Testing get_digit_count
  48468. */
  48469. static int test_get_digit_count(void)
  48470. {
  48471. EXPECT_DECLS;
  48472. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  48473. mp_int a;
  48474. XMEMSET(&a, 0, sizeof(mp_int));
  48475. ExpectIntEQ(mp_init(&a), 0);
  48476. ExpectIntEQ(get_digit_count(NULL), 0);
  48477. ExpectIntEQ(get_digit_count(&a), 0);
  48478. mp_clear(&a);
  48479. #endif
  48480. return EXPECT_RESULT();
  48481. } /* End test_get_digit_count*/
  48482. /*
  48483. * Testing mp_cond_copy
  48484. */
  48485. static int test_mp_cond_copy(void)
  48486. {
  48487. EXPECT_DECLS;
  48488. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  48489. defined(WOLFSSL_PUBLIC_MP)
  48490. mp_int a;
  48491. mp_int b;
  48492. int copy = 0;
  48493. XMEMSET(&a, 0, sizeof(mp_int));
  48494. XMEMSET(&b, 0, sizeof(mp_int));
  48495. ExpectIntEQ(mp_init(&a), MP_OKAY);
  48496. ExpectIntEQ(mp_init(&b), MP_OKAY);
  48497. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  48498. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  48499. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  48500. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  48501. mp_clear(&a);
  48502. mp_clear(&b);
  48503. #endif
  48504. return EXPECT_RESULT();
  48505. } /* End test_mp_cond_copy*/
  48506. /*
  48507. * Testing mp_rand
  48508. */
  48509. static int test_mp_rand(void)
  48510. {
  48511. EXPECT_DECLS;
  48512. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  48513. mp_int a;
  48514. WC_RNG rng;
  48515. int digits = 1;
  48516. XMEMSET(&a, 0, sizeof(mp_int));
  48517. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48518. ExpectIntEQ(mp_init(&a), MP_OKAY);
  48519. ExpectIntEQ(wc_InitRng(&rng), 0);
  48520. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  48521. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  48522. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  48523. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  48524. mp_clear(&a);
  48525. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  48526. #endif
  48527. return EXPECT_RESULT();
  48528. } /* End test_mp_rand*/
  48529. /*
  48530. * Testing get_digit
  48531. */
  48532. static int test_get_digit(void)
  48533. {
  48534. EXPECT_DECLS;
  48535. #if defined(WOLFSSL_PUBLIC_MP)
  48536. mp_int a;
  48537. int n = 0;
  48538. XMEMSET(&a, 0, sizeof(mp_int));
  48539. ExpectIntEQ(mp_init(&a), MP_OKAY);
  48540. ExpectIntEQ(get_digit(NULL, n), 0);
  48541. ExpectIntEQ(get_digit(&a, n), 0);
  48542. mp_clear(&a);
  48543. #endif
  48544. return EXPECT_RESULT();
  48545. } /* End test_get_digit*/
  48546. /*
  48547. * Testing wc_export_int
  48548. */
  48549. static int test_wc_export_int(void)
  48550. {
  48551. EXPECT_DECLS;
  48552. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  48553. defined(WOLFSSL_PUBLIC_MP)
  48554. mp_int mp;
  48555. byte buf[32];
  48556. word32 keySz = (word32)sizeof(buf);
  48557. word32 len = (word32)sizeof(buf);
  48558. XMEMSET(&mp, 0, sizeof(mp_int));
  48559. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  48560. ExpectIntEQ(mp_set(&mp, 1234), 0);
  48561. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  48562. BAD_FUNC_ARG);
  48563. len = sizeof(buf)-1;
  48564. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  48565. BUFFER_E);
  48566. len = sizeof(buf);
  48567. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  48568. len = 4; /* test input too small */
  48569. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  48570. len = sizeof(buf);
  48571. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  48572. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  48573. ExpectIntEQ(len, 5);
  48574. mp_clear(&mp);
  48575. #endif
  48576. return EXPECT_RESULT();
  48577. } /* End test_wc_export_int*/
  48578. static int test_wc_InitRngNonce(void)
  48579. {
  48580. EXPECT_DECLS;
  48581. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  48582. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  48583. HAVE_FIPS_VERSION >= 2))
  48584. WC_RNG rng;
  48585. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  48586. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  48587. word32 nonceSz = sizeof(nonce);
  48588. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  48589. ExpectIntEQ(wc_FreeRng(&rng), 0);
  48590. #endif
  48591. return EXPECT_RESULT();
  48592. } /* End test_wc_InitRngNonce*/
  48593. /*
  48594. * Testing wc_InitRngNonce_ex
  48595. */
  48596. static int test_wc_InitRngNonce_ex(void)
  48597. {
  48598. EXPECT_DECLS;
  48599. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  48600. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  48601. HAVE_FIPS_VERSION >= 2))
  48602. WC_RNG rng;
  48603. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  48604. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  48605. word32 nonceSz = sizeof(nonce);
  48606. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  48607. 0);
  48608. ExpectIntEQ(wc_FreeRng(&rng), 0);
  48609. #endif
  48610. return EXPECT_RESULT();
  48611. } /* End test_wc_InitRngNonce_ex */
  48612. static int test_wolfSSL_X509_CRL(void)
  48613. {
  48614. EXPECT_DECLS;
  48615. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  48616. X509_CRL *crl = NULL;
  48617. char pem[][100] = {
  48618. "./certs/crl/crl.pem",
  48619. "./certs/crl/crl2.pem",
  48620. "./certs/crl/caEccCrl.pem",
  48621. "./certs/crl/eccCliCRL.pem",
  48622. "./certs/crl/eccSrvCRL.pem",
  48623. ""
  48624. };
  48625. #ifndef NO_BIO
  48626. BIO *bio = NULL;
  48627. #endif
  48628. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  48629. char der[][100] = {
  48630. "./certs/crl/crl.der",
  48631. "./certs/crl/crl2.der",
  48632. ""};
  48633. #endif
  48634. XFILE fp = XBADFILE;
  48635. int i;
  48636. for (i = 0; pem[i][0] != '\0'; i++)
  48637. {
  48638. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  48639. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48640. NULL, NULL));
  48641. ExpectNotNull(crl);
  48642. X509_CRL_free(crl);
  48643. if (fp != XBADFILE) {
  48644. XFCLOSE(fp);
  48645. fp = XBADFILE;
  48646. }
  48647. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  48648. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  48649. NULL));
  48650. if (EXPECT_FAIL()) {
  48651. crl = NULL;
  48652. }
  48653. ExpectNotNull(crl);
  48654. X509_CRL_free(crl);
  48655. crl = NULL;
  48656. if (fp != XBADFILE) {
  48657. XFCLOSE(fp);
  48658. fp = XBADFILE;
  48659. }
  48660. }
  48661. #ifndef NO_BIO
  48662. for (i = 0; pem[i][0] != '\0'; i++)
  48663. {
  48664. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  48665. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  48666. X509_CRL_free(crl);
  48667. crl = NULL;
  48668. BIO_free(bio);
  48669. bio = NULL;
  48670. }
  48671. #endif
  48672. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  48673. for (i = 0; der[i][0] != '\0'; i++) {
  48674. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  48675. ExpectTrue((fp != XBADFILE));
  48676. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  48677. ExpectNotNull(crl);
  48678. X509_CRL_free(crl);
  48679. if (fp != XBADFILE) {
  48680. XFCLOSE(fp);
  48681. fp = XBADFILE;
  48682. }
  48683. fp = XFOPEN(der[i], "rb");
  48684. ExpectTrue((fp != XBADFILE));
  48685. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  48686. if (EXPECT_FAIL()) {
  48687. crl = NULL;
  48688. }
  48689. ExpectNotNull(crl);
  48690. X509_CRL_free(crl);
  48691. crl = NULL;
  48692. if (fp != XBADFILE) {
  48693. XFCLOSE(fp);
  48694. fp = XBADFILE;
  48695. }
  48696. }
  48697. #endif
  48698. #endif
  48699. return EXPECT_RESULT();
  48700. }
  48701. static int test_wolfSSL_X509_load_crl_file(void)
  48702. {
  48703. EXPECT_DECLS;
  48704. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48705. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  48706. int i;
  48707. char pem[][100] = {
  48708. "./certs/crl/crl.pem",
  48709. "./certs/crl/crl2.pem",
  48710. "./certs/crl/caEccCrl.pem",
  48711. "./certs/crl/eccCliCRL.pem",
  48712. "./certs/crl/eccSrvCRL.pem",
  48713. #ifdef WC_RSA_PSS
  48714. "./certs/crl/crl_rsapss.pem",
  48715. #endif
  48716. ""
  48717. };
  48718. char der[][100] = {
  48719. "./certs/crl/crl.der",
  48720. "./certs/crl/crl2.der",
  48721. ""
  48722. };
  48723. WOLFSSL_X509_STORE* store = NULL;
  48724. WOLFSSL_X509_LOOKUP* lookup = NULL;
  48725. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  48726. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  48727. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  48728. X509_FILETYPE_PEM), 1);
  48729. #ifdef WC_RSA_PSS
  48730. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  48731. X509_FILETYPE_PEM), 1);
  48732. #endif
  48733. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  48734. X509_FILETYPE_PEM), 1);
  48735. if (store) {
  48736. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  48737. WOLFSSL_FILETYPE_PEM), 1);
  48738. /* since store hasn't yet known the revoked cert*/
  48739. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48740. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  48741. }
  48742. for (i = 0; pem[i][0] != '\0'; i++) {
  48743. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  48744. 1);
  48745. }
  48746. if (store) {
  48747. /* since store knows crl list */
  48748. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48749. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  48750. CRL_CERT_REVOKED);
  48751. #ifdef WC_RSA_PSS
  48752. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48753. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  48754. CRL_CERT_REVOKED);
  48755. #endif
  48756. }
  48757. /* once feeing store */
  48758. X509_STORE_free(store);
  48759. store = NULL;
  48760. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  48761. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  48762. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  48763. X509_FILETYPE_PEM), 1);
  48764. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  48765. X509_FILETYPE_PEM), 1);
  48766. if (store) {
  48767. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  48768. WOLFSSL_FILETYPE_PEM), 1);
  48769. /* since store hasn't yet known the revoked cert*/
  48770. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48771. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  48772. }
  48773. for (i = 0; der[i][0] != '\0'; i++) {
  48774. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  48775. 1);
  48776. }
  48777. if (store) {
  48778. /* since store knows crl list */
  48779. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48780. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  48781. CRL_CERT_REVOKED);
  48782. }
  48783. /* test for incorrect parameter */
  48784. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  48785. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  48786. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  48787. X509_STORE_free(store);
  48788. store = NULL;
  48789. #endif
  48790. return EXPECT_RESULT();
  48791. }
  48792. static int test_wolfSSL_i2d_X509(void)
  48793. {
  48794. EXPECT_DECLS;
  48795. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  48796. const unsigned char* cert_buf = server_cert_der_2048;
  48797. unsigned char* out = NULL;
  48798. unsigned char* tmp = NULL;
  48799. X509* cert = NULL;
  48800. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  48801. /* Pointer should be advanced */
  48802. ExpectPtrGT(cert_buf, server_cert_der_2048);
  48803. ExpectIntGT(i2d_X509(cert, &out), 0);
  48804. ExpectNotNull(out);
  48805. tmp = out;
  48806. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  48807. ExpectPtrGT(tmp, out);
  48808. if (out != NULL)
  48809. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  48810. X509_free(cert);
  48811. #endif
  48812. return EXPECT_RESULT();
  48813. }
  48814. static int test_wolfSSL_d2i_X509_REQ(void)
  48815. {
  48816. EXPECT_DECLS;
  48817. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  48818. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  48819. !defined(WOLFSSL_SP_MATH)
  48820. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  48821. * were generated by libest
  48822. * ./certs/csr.attr.der contains sample attributes
  48823. * ./certs/csr.ext.der contains sample extensions */
  48824. const char* csrFile = "./certs/csr.signed.der";
  48825. const char* csrPopFile = "./certs/csr.attr.der";
  48826. const char* csrExtFile = "./certs/csr.ext.der";
  48827. /* ./certs/csr.dsa.pem is generated using
  48828. * openssl req -newkey dsa:certs/dsaparams.pem \
  48829. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  48830. * -outform PEM
  48831. * with the passphrase "wolfSSL"
  48832. */
  48833. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48834. const char* csrDsaFile = "./certs/csr.dsa.pem";
  48835. XFILE f = XBADFILE;
  48836. #endif
  48837. BIO* bio = NULL;
  48838. X509* req = NULL;
  48839. EVP_PKEY *pub_key = NULL;
  48840. {
  48841. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  48842. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48843. /*
  48844. * Extract the public key from the CSR
  48845. */
  48846. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48847. /*
  48848. * Verify the signature in the CSR
  48849. */
  48850. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48851. X509_free(req);
  48852. req = NULL;
  48853. BIO_free(bio);
  48854. bio = NULL;
  48855. EVP_PKEY_free(pub_key);
  48856. pub_key = NULL;
  48857. }
  48858. {
  48859. #ifdef OPENSSL_ALL
  48860. X509_ATTRIBUTE* attr = NULL;
  48861. ASN1_TYPE *at = NULL;
  48862. #endif
  48863. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  48864. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48865. /*
  48866. * Extract the public key from the CSR
  48867. */
  48868. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48869. /*
  48870. * Verify the signature in the CSR
  48871. */
  48872. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48873. #ifdef OPENSSL_ALL
  48874. /*
  48875. * Obtain the challenge password from the CSR
  48876. */
  48877. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  48878. -1), 1);
  48879. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  48880. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48881. ExpectNotNull(at->value.asn1_string);
  48882. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  48883. "2xIE+qqp/rhyTXP+");
  48884. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  48885. #endif
  48886. X509_free(req);
  48887. req = NULL;
  48888. BIO_free(bio);
  48889. bio = NULL;
  48890. EVP_PKEY_free(pub_key);
  48891. pub_key = NULL;
  48892. }
  48893. {
  48894. #ifdef OPENSSL_ALL
  48895. X509_ATTRIBUTE* attr = NULL;
  48896. ASN1_TYPE *at = NULL;
  48897. STACK_OF(X509_EXTENSION) *exts = NULL;
  48898. #endif
  48899. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  48900. /* This CSR contains an Extension Request attribute so
  48901. * we test extension parsing in a CSR attribute here. */
  48902. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48903. /*
  48904. * Extract the public key from the CSR
  48905. */
  48906. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48907. /*
  48908. * Verify the signature in the CSR
  48909. */
  48910. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48911. #ifdef OPENSSL_ALL
  48912. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  48913. req));
  48914. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  48915. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  48916. /*
  48917. * Obtain the challenge password from the CSR
  48918. */
  48919. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  48920. -1), 0);
  48921. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  48922. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48923. ExpectNotNull(at->value.asn1_string);
  48924. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  48925. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  48926. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  48927. #endif
  48928. X509_free(req);
  48929. req = NULL;
  48930. BIO_free(bio);
  48931. bio = NULL;
  48932. EVP_PKEY_free(pub_key);
  48933. pub_key = NULL;
  48934. }
  48935. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48936. {
  48937. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  48938. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  48939. /*
  48940. * Extract the public key from the CSR
  48941. */
  48942. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48943. /*
  48944. * Verify the signature in the CSR
  48945. */
  48946. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48947. X509_free(req);
  48948. req = NULL;
  48949. BIO_free(bio);
  48950. /* Run the same test, but with a file pointer instead of a BIO.
  48951. * (PEM_read_X509_REQ)*/
  48952. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  48953. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  48954. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48955. X509_free(req);
  48956. EVP_PKEY_free(pub_key);
  48957. }
  48958. #endif /* !NO_DSA && !HAVE_SELFTEST */
  48959. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  48960. return EXPECT_RESULT();
  48961. }
  48962. static int test_wolfSSL_PEM_read_X509(void)
  48963. {
  48964. EXPECT_DECLS;
  48965. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48966. !defined(NO_RSA)
  48967. X509 *x509 = NULL;
  48968. XFILE fp = XBADFILE;
  48969. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  48970. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  48971. X509_free(x509);
  48972. if (fp != XBADFILE)
  48973. XFCLOSE(fp);
  48974. #endif
  48975. return EXPECT_RESULT();
  48976. }
  48977. static int test_wolfSSL_PEM_read(void)
  48978. {
  48979. EXPECT_DECLS;
  48980. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  48981. const char* filename = "./certs/server-keyEnc.pem";
  48982. XFILE fp = XBADFILE;
  48983. char* name = NULL;
  48984. char* header = NULL;
  48985. byte* data = NULL;
  48986. long len;
  48987. EVP_CIPHER_INFO cipher;
  48988. WOLFSSL_BIO* bio = NULL;
  48989. byte* fileData = NULL;
  48990. size_t fileDataSz = 0;
  48991. byte* out;
  48992. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  48993. /* Fail cases. */
  48994. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  48995. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  48996. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  48997. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  48998. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48999. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49000. ExpectIntGT(XSTRLEN(header), 0);
  49001. ExpectIntGT(len, 0);
  49002. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49003. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49004. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49005. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49006. DYNAMIC_TYPE_TMP_BUFFER));
  49007. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49008. if (fp != XBADFILE) {
  49009. XFCLOSE(fp);
  49010. fp = XBADFILE;
  49011. }
  49012. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49013. /* Fail cases. */
  49014. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  49015. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  49016. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  49017. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  49018. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49019. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49020. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49021. /* Fail cases. */
  49022. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  49023. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  49024. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  49025. #ifndef NO_DES3
  49026. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  49027. #endif
  49028. /* Fail cases. */
  49029. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  49030. (void*)"yassl123"), WOLFSSL_FAILURE);
  49031. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  49032. (void*)"yassl123"), WOLFSSL_FAILURE);
  49033. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  49034. (void*)"yassl123"), WOLFSSL_FAILURE);
  49035. #if !defined(NO_DES3) && !defined(NO_MD5)
  49036. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49037. (void*)"yassl123"), WOLFSSL_SUCCESS);
  49038. #endif
  49039. BIO_free(bio);
  49040. bio = NULL;
  49041. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49042. fileData = NULL;
  49043. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49044. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49045. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49046. name = NULL;
  49047. header = NULL;
  49048. data = NULL;
  49049. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  49050. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49051. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49052. ExpectIntEQ(XSTRLEN(header), 0);
  49053. ExpectIntGT(len, 0);
  49054. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49055. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49056. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49057. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49058. DYNAMIC_TYPE_TMP_BUFFER));
  49059. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49060. if (fp != XBADFILE)
  49061. XFCLOSE(fp);
  49062. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49063. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49064. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49065. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49066. BIO_free(bio);
  49067. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49068. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49069. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49070. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49071. #endif
  49072. return EXPECT_RESULT();
  49073. }
  49074. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  49075. {
  49076. EXPECT_DECLS;
  49077. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49078. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49079. const byte iv[12] = { 0 };
  49080. const byte key[16] = { 0 };
  49081. const byte cleartext[16] = { 0 };
  49082. const byte aad[] = {
  49083. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  49084. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  49085. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  49086. 0x4f
  49087. };
  49088. byte out1Part[16];
  49089. byte outTag1Part[16];
  49090. byte out2Part[16];
  49091. byte outTag2Part[16];
  49092. byte decryptBuf[16];
  49093. int len = 0;
  49094. int tlen;
  49095. EVP_CIPHER_CTX* ctx = NULL;
  49096. /* ENCRYPT */
  49097. /* Send AAD and data in 1 part */
  49098. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49099. tlen = 0;
  49100. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49101. 1);
  49102. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49103. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49104. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  49105. sizeof(cleartext)), 1);
  49106. tlen += len;
  49107. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  49108. tlen += len;
  49109. ExpectIntEQ(tlen, sizeof(cleartext));
  49110. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49111. outTag1Part), 1);
  49112. EVP_CIPHER_CTX_free(ctx);
  49113. ctx = NULL;
  49114. /* DECRYPT */
  49115. /* Send AAD and data in 1 part */
  49116. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49117. tlen = 0;
  49118. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49119. 1);
  49120. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49121. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49122. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  49123. sizeof(cleartext)), 1);
  49124. tlen += len;
  49125. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49126. outTag1Part), 1);
  49127. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  49128. tlen += len;
  49129. ExpectIntEQ(tlen, sizeof(cleartext));
  49130. EVP_CIPHER_CTX_free(ctx);
  49131. ctx = NULL;
  49132. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49133. /* ENCRYPT */
  49134. /* Send AAD and data in 2 parts */
  49135. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49136. tlen = 0;
  49137. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49138. 1);
  49139. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49140. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49141. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49142. 1);
  49143. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  49144. tlen += len;
  49145. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  49146. sizeof(cleartext) - 1), 1);
  49147. tlen += len;
  49148. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  49149. tlen += len;
  49150. ExpectIntEQ(tlen, sizeof(cleartext));
  49151. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49152. outTag2Part), 1);
  49153. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  49154. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  49155. EVP_CIPHER_CTX_free(ctx);
  49156. ctx = NULL;
  49157. /* DECRYPT */
  49158. /* Send AAD and data in 2 parts */
  49159. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49160. tlen = 0;
  49161. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49162. 1);
  49163. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49164. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49165. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49166. 1);
  49167. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  49168. tlen += len;
  49169. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  49170. sizeof(cleartext) - 1), 1);
  49171. tlen += len;
  49172. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49173. outTag1Part), 1);
  49174. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  49175. tlen += len;
  49176. ExpectIntEQ(tlen, sizeof(cleartext));
  49177. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49178. /* Test AAD reuse */
  49179. EVP_CIPHER_CTX_free(ctx);
  49180. #endif
  49181. return EXPECT_RESULT();
  49182. }
  49183. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  49184. {
  49185. EXPECT_DECLS;
  49186. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49187. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49188. /* Zero length plain text */
  49189. byte key[] = {
  49190. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49191. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49193. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49194. }; /* align */
  49195. byte iv[] = {
  49196. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49197. }; /* align */
  49198. byte plaintxt[1];
  49199. int ivSz = 12;
  49200. int plaintxtSz = 0;
  49201. unsigned char tag[16];
  49202. unsigned char tag_kat[] = {
  49203. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  49204. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  49205. };
  49206. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49207. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49208. int ciphertxtSz = 0;
  49209. int decryptedtxtSz = 0;
  49210. int len = 0;
  49211. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49212. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49213. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  49214. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49215. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49216. plaintxtSz));
  49217. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49218. ciphertxtSz += len;
  49219. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  49220. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49221. ExpectIntEQ(0, ciphertxtSz);
  49222. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  49223. EVP_CIPHER_CTX_init(de);
  49224. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  49225. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49226. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49227. decryptedtxtSz = len;
  49228. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  49229. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49230. decryptedtxtSz += len;
  49231. ExpectIntEQ(0, decryptedtxtSz);
  49232. EVP_CIPHER_CTX_free(en);
  49233. EVP_CIPHER_CTX_free(de);
  49234. #endif
  49235. return EXPECT_RESULT();
  49236. }
  49237. static int test_wolfssl_EVP_aes_gcm(void)
  49238. {
  49239. EXPECT_DECLS;
  49240. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49241. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49242. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49243. byte *key = (byte*)"01234567890123456789012345678901";
  49244. /* A 128 bit IV */
  49245. byte *iv = (byte*)"0123456789012345";
  49246. int ivSz = AES_BLOCK_SIZE;
  49247. /* Message to be encrypted */
  49248. byte *plaintxt = (byte*)"for things to change you have to change";
  49249. /* Additional non-confidential data */
  49250. byte *aad = (byte*)"Don't spend major time on minor things.";
  49251. unsigned char tag[AES_BLOCK_SIZE] = {0};
  49252. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49253. int aadSz = (int)XSTRLEN((char*)aad);
  49254. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49255. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49256. int ciphertxtSz = 0;
  49257. int decryptedtxtSz = 0;
  49258. int len = 0;
  49259. int i = 0;
  49260. EVP_CIPHER_CTX en[2];
  49261. EVP_CIPHER_CTX de[2];
  49262. for (i = 0; i < 2; i++) {
  49263. EVP_CIPHER_CTX_init(&en[i]);
  49264. if (i == 0) {
  49265. /* Default uses 96-bits IV length */
  49266. #ifdef WOLFSSL_AES_128
  49267. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  49268. key, iv));
  49269. #elif defined(WOLFSSL_AES_192)
  49270. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  49271. key, iv));
  49272. #elif defined(WOLFSSL_AES_256)
  49273. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  49274. key, iv));
  49275. #endif
  49276. }
  49277. else {
  49278. #ifdef WOLFSSL_AES_128
  49279. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  49280. NULL, NULL));
  49281. #elif defined(WOLFSSL_AES_192)
  49282. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  49283. NULL, NULL));
  49284. #elif defined(WOLFSSL_AES_256)
  49285. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  49286. NULL, NULL));
  49287. #endif
  49288. /* non-default must to set the IV length first */
  49289. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  49290. ivSz, NULL));
  49291. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49292. }
  49293. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49294. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49295. plaintxtSz));
  49296. ciphertxtSz = len;
  49297. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49298. ciphertxtSz += len;
  49299. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  49300. AES_BLOCK_SIZE, tag));
  49301. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49302. EVP_CIPHER_CTX_init(&de[i]);
  49303. if (i == 0) {
  49304. /* Default uses 96-bits IV length */
  49305. #ifdef WOLFSSL_AES_128
  49306. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  49307. key, iv));
  49308. #elif defined(WOLFSSL_AES_192)
  49309. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  49310. key, iv));
  49311. #elif defined(WOLFSSL_AES_256)
  49312. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  49313. key, iv));
  49314. #endif
  49315. }
  49316. else {
  49317. #ifdef WOLFSSL_AES_128
  49318. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  49319. NULL, NULL));
  49320. #elif defined(WOLFSSL_AES_192)
  49321. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  49322. NULL, NULL));
  49323. #elif defined(WOLFSSL_AES_256)
  49324. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  49325. NULL, NULL));
  49326. #endif
  49327. /* non-default must to set the IV length first */
  49328. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  49329. ivSz, NULL));
  49330. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49331. }
  49332. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49333. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49334. ciphertxtSz));
  49335. decryptedtxtSz = len;
  49336. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  49337. AES_BLOCK_SIZE, tag));
  49338. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49339. decryptedtxtSz += len;
  49340. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49341. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49342. /* modify tag*/
  49343. if (i == 0) {
  49344. /* Default uses 96-bits IV length */
  49345. #ifdef WOLFSSL_AES_128
  49346. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  49347. key, iv));
  49348. #elif defined(WOLFSSL_AES_192)
  49349. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  49350. key, iv));
  49351. #elif defined(WOLFSSL_AES_256)
  49352. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  49353. key, iv));
  49354. #endif
  49355. }
  49356. else {
  49357. #ifdef WOLFSSL_AES_128
  49358. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  49359. NULL, NULL));
  49360. #elif defined(WOLFSSL_AES_192)
  49361. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  49362. NULL, NULL));
  49363. #elif defined(WOLFSSL_AES_256)
  49364. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  49365. NULL, NULL));
  49366. #endif
  49367. /* non-default must to set the IV length first */
  49368. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  49369. ivSz, NULL));
  49370. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49371. }
  49372. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49373. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49374. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  49375. AES_BLOCK_SIZE, tag));
  49376. /* fail due to wrong tag */
  49377. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49378. ciphertxtSz));
  49379. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49380. ExpectIntEQ(0, len);
  49381. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49382. }
  49383. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  49384. return EXPECT_RESULT();
  49385. }
  49386. static int test_wolfssl_EVP_aria_gcm(void)
  49387. {
  49388. int res = TEST_SKIPPED;
  49389. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  49390. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49391. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49392. byte *key = (byte*)"01234567890123456789012345678901";
  49393. /* A 128 bit IV */
  49394. byte *iv = (byte*)"0123456789012345";
  49395. int ivSz = ARIA_BLOCK_SIZE;
  49396. /* Message to be encrypted */
  49397. const int plaintxtSz = 40;
  49398. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  49399. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  49400. /* Additional non-confidential data */
  49401. byte *aad = (byte*)"Don't spend major time on minor things.";
  49402. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  49403. int aadSz = (int)XSTRLEN((char*)aad);
  49404. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  49405. byte decryptedtxt[plaintxtSz];
  49406. int ciphertxtSz = 0;
  49407. int decryptedtxtSz = 0;
  49408. int len = 0;
  49409. int i = 0;
  49410. #define TEST_ARIA_GCM_COUNT 6
  49411. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  49412. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  49413. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  49414. EVP_CIPHER_CTX_init(&en[i]);
  49415. switch (i) {
  49416. case 0:
  49417. /* Default uses 96-bits IV length */
  49418. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  49419. break;
  49420. case 1:
  49421. /* Default uses 96-bits IV length */
  49422. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  49423. break;
  49424. case 2:
  49425. /* Default uses 96-bits IV length */
  49426. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  49427. break;
  49428. case 3:
  49429. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  49430. /* non-default must to set the IV length first */
  49431. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49432. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49433. break;
  49434. case 4:
  49435. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  49436. /* non-default must to set the IV length first */
  49437. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49438. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49439. break;
  49440. case 5:
  49441. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  49442. /* non-default must to set the IV length first */
  49443. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49444. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49445. break;
  49446. }
  49447. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  49448. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49449. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  49450. ciphertxtSz = len;
  49451. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49452. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  49453. ciphertxtSz += len;
  49454. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  49455. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49456. EVP_CIPHER_CTX_init(&de[i]);
  49457. switch (i) {
  49458. case 0:
  49459. /* Default uses 96-bits IV length */
  49460. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  49461. break;
  49462. case 1:
  49463. /* Default uses 96-bits IV length */
  49464. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  49465. break;
  49466. case 2:
  49467. /* Default uses 96-bits IV length */
  49468. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  49469. break;
  49470. case 3:
  49471. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  49472. /* non-default must to set the IV length first */
  49473. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49474. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49475. break;
  49476. case 4:
  49477. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  49478. /* non-default must to set the IV length first */
  49479. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49480. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49481. break;
  49482. case 5:
  49483. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  49484. /* non-default must to set the IV length first */
  49485. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49486. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49487. break;
  49488. }
  49489. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  49490. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49491. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  49492. decryptedtxtSz = len;
  49493. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  49494. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49495. decryptedtxtSz += len;
  49496. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  49497. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49498. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  49499. /* modify tag*/
  49500. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49501. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49502. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  49503. /* fail due to wrong tag */
  49504. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  49505. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49506. AssertIntEQ(0, len);
  49507. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49508. }
  49509. res = TEST_RES_CHECK(1);
  49510. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  49511. return res;
  49512. }
  49513. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  49514. {
  49515. EXPECT_DECLS;
  49516. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  49517. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49518. /* Zero length plain text */
  49519. byte key[] = {
  49520. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49521. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49522. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49523. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49524. }; /* align */
  49525. byte iv[] = {
  49526. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49527. }; /* align */
  49528. byte plaintxt[1];
  49529. int ivSz = 12;
  49530. int plaintxtSz = 0;
  49531. unsigned char tag[16];
  49532. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49533. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49534. int ciphertxtSz = 0;
  49535. int decryptedtxtSz = 0;
  49536. int len = 0;
  49537. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49538. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49539. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  49540. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49541. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49542. plaintxtSz));
  49543. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49544. ciphertxtSz += len;
  49545. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  49546. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49547. ExpectIntEQ(0, ciphertxtSz);
  49548. EVP_CIPHER_CTX_init(de);
  49549. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  49550. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49551. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49552. decryptedtxtSz = len;
  49553. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  49554. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49555. decryptedtxtSz += len;
  49556. ExpectIntEQ(0, decryptedtxtSz);
  49557. EVP_CIPHER_CTX_free(en);
  49558. EVP_CIPHER_CTX_free(de);
  49559. #endif
  49560. return EXPECT_RESULT();
  49561. }
  49562. static int test_wolfssl_EVP_aes_ccm(void)
  49563. {
  49564. EXPECT_DECLS;
  49565. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  49566. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49567. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49568. byte *key = (byte*)"01234567890123456789012345678901";
  49569. /* A 128 bit IV */
  49570. byte *iv = (byte*)"0123456789012";
  49571. int ivSz = (int)XSTRLEN((char*)iv);
  49572. /* Message to be encrypted */
  49573. byte *plaintxt = (byte*)"for things to change you have to change";
  49574. /* Additional non-confidential data */
  49575. byte *aad = (byte*)"Don't spend major time on minor things.";
  49576. unsigned char tag[AES_BLOCK_SIZE] = {0};
  49577. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49578. int aadSz = (int)XSTRLEN((char*)aad);
  49579. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49580. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49581. int ciphertxtSz = 0;
  49582. int decryptedtxtSz = 0;
  49583. int len = 0;
  49584. int i = 0;
  49585. int ret;
  49586. EVP_CIPHER_CTX en[2];
  49587. EVP_CIPHER_CTX de[2];
  49588. for (i = 0; i < 2; i++) {
  49589. EVP_CIPHER_CTX_init(&en[i]);
  49590. if (i == 0) {
  49591. /* Default uses 96-bits IV length */
  49592. #ifdef WOLFSSL_AES_128
  49593. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  49594. key, iv));
  49595. #elif defined(WOLFSSL_AES_192)
  49596. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  49597. key, iv));
  49598. #elif defined(WOLFSSL_AES_256)
  49599. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  49600. key, iv));
  49601. #endif
  49602. }
  49603. else {
  49604. #ifdef WOLFSSL_AES_128
  49605. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  49606. NULL, NULL));
  49607. #elif defined(WOLFSSL_AES_192)
  49608. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  49609. NULL, NULL));
  49610. #elif defined(WOLFSSL_AES_256)
  49611. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  49612. NULL, NULL));
  49613. #endif
  49614. /* non-default must to set the IV length first */
  49615. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  49616. ivSz, NULL));
  49617. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49618. }
  49619. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49620. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49621. plaintxtSz));
  49622. ciphertxtSz = len;
  49623. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49624. ciphertxtSz += len;
  49625. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  49626. AES_BLOCK_SIZE, tag));
  49627. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  49628. ExpectIntEQ(ret, 1);
  49629. EVP_CIPHER_CTX_init(&de[i]);
  49630. if (i == 0) {
  49631. /* Default uses 96-bits IV length */
  49632. #ifdef WOLFSSL_AES_128
  49633. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  49634. key, iv));
  49635. #elif defined(WOLFSSL_AES_192)
  49636. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  49637. key, iv));
  49638. #elif defined(WOLFSSL_AES_256)
  49639. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  49640. key, iv));
  49641. #endif
  49642. }
  49643. else {
  49644. #ifdef WOLFSSL_AES_128
  49645. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  49646. NULL, NULL));
  49647. #elif defined(WOLFSSL_AES_192)
  49648. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  49649. NULL, NULL));
  49650. #elif defined(WOLFSSL_AES_256)
  49651. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  49652. NULL, NULL));
  49653. #endif
  49654. /* non-default must to set the IV length first */
  49655. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  49656. ivSz, NULL));
  49657. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49658. }
  49659. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49660. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49661. ciphertxtSz));
  49662. decryptedtxtSz = len;
  49663. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49664. AES_BLOCK_SIZE, tag));
  49665. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49666. decryptedtxtSz += len;
  49667. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49668. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49669. /* modify tag*/
  49670. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49671. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49672. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49673. AES_BLOCK_SIZE, tag));
  49674. /* fail due to wrong tag */
  49675. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49676. ciphertxtSz));
  49677. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49678. ExpectIntEQ(0, len);
  49679. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  49680. ExpectIntEQ(ret, 1);
  49681. }
  49682. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  49683. return EXPECT_RESULT();
  49684. }
  49685. static int test_wolfssl_EVP_chacha20_poly1305(void)
  49686. {
  49687. EXPECT_DECLS;
  49688. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  49689. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  49690. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  49691. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49692. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  49693. byte cipherText[sizeof(plainText)];
  49694. byte decryptedText[sizeof(plainText)];
  49695. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  49696. EVP_CIPHER_CTX* ctx = NULL;
  49697. int outSz;
  49698. /* Encrypt. */
  49699. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49700. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  49701. NULL), WOLFSSL_SUCCESS);
  49702. /* Invalid IV length. */
  49703. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49704. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  49705. /* Valid IV length. */
  49706. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49707. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  49708. /* Invalid tag length. */
  49709. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49710. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  49711. /* Valid tag length. */
  49712. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49713. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  49714. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49715. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  49716. WOLFSSL_SUCCESS);
  49717. ExpectIntEQ(outSz, sizeof(aad));
  49718. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49719. sizeof(plainText)), WOLFSSL_SUCCESS);
  49720. ExpectIntEQ(outSz, sizeof(plainText));
  49721. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49722. ExpectIntEQ(outSz, 0);
  49723. /* Invalid tag length. */
  49724. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  49725. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  49726. /* Valid tag length. */
  49727. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  49728. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  49729. EVP_CIPHER_CTX_free(ctx);
  49730. ctx = NULL;
  49731. /* Decrypt. */
  49732. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49733. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  49734. NULL), WOLFSSL_SUCCESS);
  49735. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49736. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  49737. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49738. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  49739. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49740. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  49741. WOLFSSL_SUCCESS);
  49742. ExpectIntEQ(outSz, sizeof(aad));
  49743. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49744. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49745. ExpectIntEQ(outSz, sizeof(cipherText));
  49746. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49747. WOLFSSL_SUCCESS);
  49748. ExpectIntEQ(outSz, 0);
  49749. EVP_CIPHER_CTX_free(ctx);
  49750. ctx = NULL;
  49751. /* Test partial Inits. CipherInit() allow setting of key and iv
  49752. * in separate calls. */
  49753. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49754. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  49755. key, NULL, 1), WOLFSSL_SUCCESS);
  49756. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49757. WOLFSSL_SUCCESS);
  49758. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  49759. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  49760. ExpectIntEQ(outSz, sizeof(aad));
  49761. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49762. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49763. ExpectIntEQ(outSz, sizeof(cipherText));
  49764. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49765. WOLFSSL_SUCCESS);
  49766. ExpectIntEQ(outSz, 0);
  49767. EVP_CIPHER_CTX_free(ctx);
  49768. #endif
  49769. return EXPECT_RESULT();
  49770. }
  49771. static int test_wolfssl_EVP_chacha20(void)
  49772. {
  49773. EXPECT_DECLS;
  49774. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  49775. byte key[CHACHA_MAX_KEY_SZ];
  49776. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  49777. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49778. byte cipherText[sizeof(plainText)];
  49779. byte decryptedText[sizeof(plainText)];
  49780. EVP_CIPHER_CTX* ctx = NULL;
  49781. int outSz;
  49782. XMEMSET(key, 0, sizeof(key));
  49783. XMEMSET(iv, 0, sizeof(iv));
  49784. /* Encrypt. */
  49785. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49786. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  49787. NULL), WOLFSSL_SUCCESS);
  49788. /* Any tag length must fail - not an AEAD cipher. */
  49789. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49790. 16, NULL), WOLFSSL_FAILURE);
  49791. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49792. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49793. sizeof(plainText)), WOLFSSL_SUCCESS);
  49794. ExpectIntEQ(outSz, sizeof(plainText));
  49795. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49796. ExpectIntEQ(outSz, 0);
  49797. EVP_CIPHER_CTX_free(ctx);
  49798. ctx = NULL;
  49799. /* Decrypt. */
  49800. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49801. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  49802. NULL), WOLFSSL_SUCCESS);
  49803. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49804. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49805. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49806. ExpectIntEQ(outSz, sizeof(cipherText));
  49807. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49808. WOLFSSL_SUCCESS);
  49809. ExpectIntEQ(outSz, 0);
  49810. EVP_CIPHER_CTX_free(ctx);
  49811. ctx = NULL;
  49812. /* Test partial Inits. CipherInit() allow setting of key and iv
  49813. * in separate calls. */
  49814. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49815. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  49816. key, NULL, 1), WOLFSSL_SUCCESS);
  49817. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49818. WOLFSSL_SUCCESS);
  49819. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49820. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49821. ExpectIntEQ(outSz, sizeof(cipherText));
  49822. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49823. WOLFSSL_SUCCESS);
  49824. ExpectIntEQ(outSz, 0);
  49825. EVP_CIPHER_CTX_free(ctx);
  49826. #endif
  49827. return EXPECT_RESULT();
  49828. }
  49829. static int test_wolfssl_EVP_sm4_ecb(void)
  49830. {
  49831. int res = TEST_SKIPPED;
  49832. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  49833. EXPECT_DECLS;
  49834. byte key[SM4_KEY_SIZE];
  49835. byte plainText[SM4_BLOCK_SIZE] = {
  49836. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  49837. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  49838. };
  49839. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49840. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49841. EVP_CIPHER_CTX* ctx;
  49842. int outSz;
  49843. XMEMSET(key, 0, sizeof(key));
  49844. /* Encrypt. */
  49845. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49846. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  49847. WOLFSSL_SUCCESS);
  49848. /* Any tag length must fail - not an AEAD cipher. */
  49849. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49850. WOLFSSL_FAILURE);
  49851. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  49852. WOLFSSL_SUCCESS);
  49853. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49854. sizeof(plainText)), WOLFSSL_SUCCESS);
  49855. ExpectIntEQ(outSz, sizeof(plainText));
  49856. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  49857. WOLFSSL_SUCCESS);
  49858. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  49859. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49860. EVP_CIPHER_CTX_free(ctx);
  49861. /* Decrypt. */
  49862. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49863. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  49864. WOLFSSL_SUCCESS);
  49865. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  49866. WOLFSSL_SUCCESS);
  49867. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49868. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49869. ExpectIntEQ(outSz, sizeof(plainText));
  49870. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49871. WOLFSSL_SUCCESS);
  49872. ExpectIntEQ(outSz, 0);
  49873. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49874. EVP_CIPHER_CTX_free(ctx);
  49875. res = EXPECT_RESULT();
  49876. #endif
  49877. return res;
  49878. }
  49879. static int test_wolfssl_EVP_sm4_cbc(void)
  49880. {
  49881. int res = TEST_SKIPPED;
  49882. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  49883. EXPECT_DECLS;
  49884. byte key[SM4_KEY_SIZE];
  49885. byte iv[SM4_BLOCK_SIZE];
  49886. byte plainText[SM4_BLOCK_SIZE] = {
  49887. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  49888. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  49889. };
  49890. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49891. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49892. EVP_CIPHER_CTX* ctx;
  49893. int outSz;
  49894. XMEMSET(key, 0, sizeof(key));
  49895. XMEMSET(iv, 0, sizeof(iv));
  49896. /* Encrypt. */
  49897. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49898. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  49899. WOLFSSL_SUCCESS);
  49900. /* Any tag length must fail - not an AEAD cipher. */
  49901. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49902. WOLFSSL_FAILURE);
  49903. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49904. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49905. sizeof(plainText)), WOLFSSL_SUCCESS);
  49906. ExpectIntEQ(outSz, sizeof(plainText));
  49907. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  49908. WOLFSSL_SUCCESS);
  49909. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  49910. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49911. EVP_CIPHER_CTX_free(ctx);
  49912. /* Decrypt. */
  49913. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49914. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  49915. WOLFSSL_SUCCESS);
  49916. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49917. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49918. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49919. ExpectIntEQ(outSz, sizeof(plainText));
  49920. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49921. WOLFSSL_SUCCESS);
  49922. ExpectIntEQ(outSz, 0);
  49923. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49924. EVP_CIPHER_CTX_free(ctx);
  49925. /* Test partial Inits. CipherInit() allow setting of key and iv
  49926. * in separate calls. */
  49927. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49928. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  49929. WOLFSSL_SUCCESS);
  49930. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  49931. WOLFSSL_SUCCESS);
  49932. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49933. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49934. ExpectIntEQ(outSz, sizeof(plainText));
  49935. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49936. WOLFSSL_SUCCESS);
  49937. ExpectIntEQ(outSz, 0);
  49938. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49939. EVP_CIPHER_CTX_free(ctx);
  49940. res = EXPECT_RESULT();
  49941. #endif
  49942. return res;
  49943. }
  49944. static int test_wolfssl_EVP_sm4_ctr(void)
  49945. {
  49946. int res = TEST_SKIPPED;
  49947. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  49948. EXPECT_DECLS;
  49949. byte key[SM4_KEY_SIZE];
  49950. byte iv[SM4_BLOCK_SIZE];
  49951. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49952. byte cipherText[sizeof(plainText)];
  49953. byte decryptedText[sizeof(plainText)];
  49954. EVP_CIPHER_CTX* ctx;
  49955. int outSz;
  49956. XMEMSET(key, 0, sizeof(key));
  49957. XMEMSET(iv, 0, sizeof(iv));
  49958. /* Encrypt. */
  49959. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49960. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  49961. WOLFSSL_SUCCESS);
  49962. /* Any tag length must fail - not an AEAD cipher. */
  49963. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49964. WOLFSSL_FAILURE);
  49965. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49966. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49967. sizeof(plainText)), WOLFSSL_SUCCESS);
  49968. ExpectIntEQ(outSz, sizeof(plainText));
  49969. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49970. ExpectIntEQ(outSz, 0);
  49971. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49972. EVP_CIPHER_CTX_free(ctx);
  49973. /* Decrypt. */
  49974. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49975. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  49976. WOLFSSL_SUCCESS);
  49977. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49978. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49979. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49980. ExpectIntEQ(outSz, sizeof(cipherText));
  49981. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49982. WOLFSSL_SUCCESS);
  49983. ExpectIntEQ(outSz, 0);
  49984. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49985. EVP_CIPHER_CTX_free(ctx);
  49986. /* Test partial Inits. CipherInit() allow setting of key and iv
  49987. * in separate calls. */
  49988. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49989. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  49990. WOLFSSL_SUCCESS);
  49991. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49992. WOLFSSL_SUCCESS);
  49993. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49994. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49995. ExpectIntEQ(outSz, sizeof(cipherText));
  49996. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49997. WOLFSSL_SUCCESS);
  49998. ExpectIntEQ(outSz, 0);
  49999. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50000. EVP_CIPHER_CTX_free(ctx);
  50001. res = EXPECT_RESULT();
  50002. #endif
  50003. return res;
  50004. }
  50005. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  50006. {
  50007. int res = TEST_SKIPPED;
  50008. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50009. /* Zero length plain text */
  50010. EXPECT_DECLS;
  50011. byte key[] = {
  50012. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50013. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50014. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50015. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50016. }; /* align */
  50017. byte iv[] = {
  50018. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50019. }; /* align */
  50020. byte plaintxt[1];
  50021. int ivSz = 12;
  50022. int plaintxtSz = 0;
  50023. unsigned char tag[16];
  50024. unsigned char tag_kat[16] = {
  50025. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  50026. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  50027. };
  50028. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50029. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50030. int ciphertxtSz = 0;
  50031. int decryptedtxtSz = 0;
  50032. int len = 0;
  50033. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50034. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50035. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  50036. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50037. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50038. plaintxtSz));
  50039. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50040. ciphertxtSz += len;
  50041. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50042. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50043. ExpectIntEQ(0, ciphertxtSz);
  50044. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50045. EVP_CIPHER_CTX_init(de);
  50046. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  50047. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50048. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50049. decryptedtxtSz = len;
  50050. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50051. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50052. decryptedtxtSz += len;
  50053. ExpectIntEQ(0, decryptedtxtSz);
  50054. EVP_CIPHER_CTX_free(en);
  50055. EVP_CIPHER_CTX_free(de);
  50056. res = EXPECT_RESULT();
  50057. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50058. return res;
  50059. }
  50060. static int test_wolfssl_EVP_sm4_gcm(void)
  50061. {
  50062. int res = TEST_SKIPPED;
  50063. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50064. EXPECT_DECLS;
  50065. byte *key = (byte*)"0123456789012345";
  50066. /* A 128 bit IV */
  50067. byte *iv = (byte*)"0123456789012345";
  50068. int ivSz = SM4_BLOCK_SIZE;
  50069. /* Message to be encrypted */
  50070. byte *plaintxt = (byte*)"for things to change you have to change";
  50071. /* Additional non-confidential data */
  50072. byte *aad = (byte*)"Don't spend major time on minor things.";
  50073. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50074. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50075. int aadSz = (int)XSTRLEN((char*)aad);
  50076. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50077. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50078. int ciphertxtSz = 0;
  50079. int decryptedtxtSz = 0;
  50080. int len = 0;
  50081. int i = 0;
  50082. EVP_CIPHER_CTX en[2];
  50083. EVP_CIPHER_CTX de[2];
  50084. for (i = 0; i < 2; i++) {
  50085. EVP_CIPHER_CTX_init(&en[i]);
  50086. if (i == 0) {
  50087. /* Default uses 96-bits IV length */
  50088. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  50089. iv));
  50090. }
  50091. else {
  50092. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  50093. NULL));
  50094. /* non-default must to set the IV length first */
  50095. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50096. ivSz, NULL));
  50097. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50098. }
  50099. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50100. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50101. plaintxtSz));
  50102. ciphertxtSz = len;
  50103. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50104. ciphertxtSz += len;
  50105. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50106. SM4_BLOCK_SIZE, tag));
  50107. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50108. EVP_CIPHER_CTX_init(&de[i]);
  50109. if (i == 0) {
  50110. /* Default uses 96-bits IV length */
  50111. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  50112. iv));
  50113. }
  50114. else {
  50115. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  50116. NULL));
  50117. /* non-default must to set the IV length first */
  50118. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50119. ivSz, NULL));
  50120. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50121. }
  50122. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50123. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50124. ciphertxtSz));
  50125. decryptedtxtSz = len;
  50126. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50127. SM4_BLOCK_SIZE, tag));
  50128. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50129. decryptedtxtSz += len;
  50130. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50131. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50132. /* modify tag*/
  50133. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50134. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50135. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50136. SM4_BLOCK_SIZE, tag));
  50137. /* fail due to wrong tag */
  50138. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50139. ciphertxtSz));
  50140. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50141. ExpectIntEQ(0, len);
  50142. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50143. }
  50144. res = EXPECT_RESULT();
  50145. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50146. return res;
  50147. }
  50148. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  50149. {
  50150. int res = TEST_SKIPPED;
  50151. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50152. /* Zero length plain text */
  50153. EXPECT_DECLS;
  50154. byte key[] = {
  50155. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50156. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50157. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50158. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50159. }; /* align */
  50160. byte iv[] = {
  50161. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50162. }; /* align */
  50163. byte plaintxt[1];
  50164. int ivSz = 12;
  50165. int plaintxtSz = 0;
  50166. unsigned char tag[16];
  50167. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50168. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50169. int ciphertxtSz = 0;
  50170. int decryptedtxtSz = 0;
  50171. int len = 0;
  50172. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50173. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50174. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  50175. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50176. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50177. plaintxtSz));
  50178. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50179. ciphertxtSz += len;
  50180. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50181. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50182. ExpectIntEQ(0, ciphertxtSz);
  50183. EVP_CIPHER_CTX_init(de);
  50184. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  50185. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50186. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50187. decryptedtxtSz = len;
  50188. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50189. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50190. decryptedtxtSz += len;
  50191. ExpectIntEQ(0, decryptedtxtSz);
  50192. EVP_CIPHER_CTX_free(en);
  50193. EVP_CIPHER_CTX_free(de);
  50194. res = EXPECT_RESULT();
  50195. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50196. return res;
  50197. }
  50198. static int test_wolfssl_EVP_sm4_ccm(void)
  50199. {
  50200. int res = TEST_SKIPPED;
  50201. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50202. EXPECT_DECLS;
  50203. byte *key = (byte*)"0123456789012345";
  50204. byte *iv = (byte*)"0123456789012";
  50205. int ivSz = (int)XSTRLEN((char*)iv);
  50206. /* Message to be encrypted */
  50207. byte *plaintxt = (byte*)"for things to change you have to change";
  50208. /* Additional non-confidential data */
  50209. byte *aad = (byte*)"Don't spend major time on minor things.";
  50210. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50211. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50212. int aadSz = (int)XSTRLEN((char*)aad);
  50213. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50214. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50215. int ciphertxtSz = 0;
  50216. int decryptedtxtSz = 0;
  50217. int len = 0;
  50218. int i = 0;
  50219. EVP_CIPHER_CTX en[2];
  50220. EVP_CIPHER_CTX de[2];
  50221. for (i = 0; i < 2; i++) {
  50222. EVP_CIPHER_CTX_init(&en[i]);
  50223. if (i == 0) {
  50224. /* Default uses 96-bits IV length */
  50225. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  50226. iv));
  50227. }
  50228. else {
  50229. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  50230. NULL));
  50231. /* non-default must to set the IV length first */
  50232. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50233. ivSz, NULL));
  50234. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50235. }
  50236. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50237. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50238. plaintxtSz));
  50239. ciphertxtSz = len;
  50240. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50241. ciphertxtSz += len;
  50242. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50243. SM4_BLOCK_SIZE, tag));
  50244. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50245. EVP_CIPHER_CTX_init(&de[i]);
  50246. if (i == 0) {
  50247. /* Default uses 96-bits IV length */
  50248. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  50249. iv));
  50250. }
  50251. else {
  50252. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  50253. NULL));
  50254. /* non-default must to set the IV length first */
  50255. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50256. ivSz, NULL));
  50257. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50258. }
  50259. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50260. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50261. ciphertxtSz));
  50262. decryptedtxtSz = len;
  50263. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50264. SM4_BLOCK_SIZE, tag));
  50265. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50266. decryptedtxtSz += len;
  50267. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50268. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50269. /* modify tag*/
  50270. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50271. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50272. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50273. SM4_BLOCK_SIZE, tag));
  50274. /* fail due to wrong tag */
  50275. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50276. ciphertxtSz));
  50277. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50278. ExpectIntEQ(0, len);
  50279. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50280. }
  50281. res = EXPECT_RESULT();
  50282. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50283. return res;
  50284. }
  50285. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  50286. {
  50287. EXPECT_DECLS;
  50288. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  50289. EVP_PKEY_CTX* ctx = NULL;
  50290. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  50291. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  50292. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  50293. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  50294. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  50295. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  50296. byte outKey[34];
  50297. size_t outKeySz = sizeof(outKey);
  50298. /* These expected outputs were gathered by running the same test below using
  50299. * OpenSSL. */
  50300. const byte extractAndExpand[] = {
  50301. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  50302. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  50303. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  50304. };
  50305. const byte extractOnly[] = {
  50306. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  50307. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  50308. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  50309. };
  50310. const byte expandOnly[] = {
  50311. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  50312. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  50313. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  50314. };
  50315. const byte extractAndExpandAddInfo[] = {
  50316. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  50317. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  50318. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  50319. };
  50320. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  50321. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  50322. /* NULL ctx. */
  50323. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  50324. /* NULL md. */
  50325. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  50326. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  50327. /* NULL ctx. */
  50328. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  50329. WOLFSSL_FAILURE);
  50330. /* NULL salt is ok. */
  50331. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  50332. WOLFSSL_SUCCESS);
  50333. /* Salt length <= 0. */
  50334. /* Length 0 salt is ok. */
  50335. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  50336. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  50337. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  50338. WOLFSSL_SUCCESS);
  50339. /* NULL ctx. */
  50340. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  50341. WOLFSSL_FAILURE);
  50342. /* NULL key. */
  50343. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  50344. WOLFSSL_FAILURE);
  50345. /* Key length <= 0 */
  50346. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  50347. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  50348. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  50349. WOLFSSL_SUCCESS);
  50350. /* NULL ctx. */
  50351. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  50352. WOLFSSL_FAILURE);
  50353. /* NULL info is ok. */
  50354. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  50355. WOLFSSL_SUCCESS);
  50356. /* Info length <= 0 */
  50357. /* Length 0 info is ok. */
  50358. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  50359. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  50360. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  50361. WOLFSSL_SUCCESS);
  50362. /* NULL ctx. */
  50363. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  50364. WOLFSSL_FAILURE);
  50365. /* Extract and expand (default). */
  50366. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  50367. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  50368. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  50369. /* Extract only. */
  50370. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  50371. WOLFSSL_SUCCESS);
  50372. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  50373. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  50374. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  50375. outKeySz = sizeof(outKey);
  50376. /* Expand only. */
  50377. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  50378. WOLFSSL_SUCCESS);
  50379. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  50380. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  50381. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  50382. outKeySz = sizeof(outKey);
  50383. /* Extract and expand with appended additional info. */
  50384. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  50385. WOLFSSL_SUCCESS);
  50386. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  50387. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  50388. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  50389. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  50390. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  50391. EVP_PKEY_CTX_free(ctx);
  50392. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  50393. return EXPECT_RESULT();
  50394. }
  50395. #ifndef NO_BIO
  50396. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  50397. {
  50398. EXPECT_DECLS;
  50399. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  50400. BIO* bio = NULL;
  50401. X509_INFO* info = NULL;
  50402. STACK_OF(X509_INFO)* sk = NULL;
  50403. char* subject = NULL;
  50404. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  50405. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  50406. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  50407. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  50408. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  50409. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  50410. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  50411. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  50412. /* using dereference to maintain testing for Apache port*/
  50413. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  50414. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  50415. 0, 0));
  50416. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  50417. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  50418. X509_INFO_free(info);
  50419. info = NULL;
  50420. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  50421. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  50422. 0, 0));
  50423. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  50424. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  50425. X509_INFO_free(info);
  50426. ExpectNull(info = sk_X509_INFO_pop(sk));
  50427. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  50428. BIO_free(bio);
  50429. #endif
  50430. return EXPECT_RESULT();
  50431. }
  50432. #endif /* !NO_BIO */
  50433. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  50434. {
  50435. EXPECT_DECLS;
  50436. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  50437. X509 *x509 = NULL;
  50438. X509_NAME* name = NULL;
  50439. int idx = 0;
  50440. X509_NAME_ENTRY *ne = NULL;
  50441. ASN1_OBJECT *object = NULL;
  50442. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  50443. WOLFSSL_FILETYPE_PEM));
  50444. ExpectNotNull(name = X509_get_subject_name(x509));
  50445. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  50446. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  50447. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  50448. X509_free(x509);
  50449. #endif
  50450. return EXPECT_RESULT();
  50451. }
  50452. static int test_wolfSSL_X509_STORE_get1_certs(void)
  50453. {
  50454. EXPECT_DECLS;
  50455. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  50456. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  50457. X509_STORE_CTX *storeCtx = NULL;
  50458. X509_STORE *store = NULL;
  50459. X509 *caX509 = NULL;
  50460. X509 *svrX509 = NULL;
  50461. X509_NAME *subject = NULL;
  50462. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  50463. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  50464. SSL_FILETYPE_PEM));
  50465. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  50466. SSL_FILETYPE_PEM)));
  50467. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  50468. ExpectNotNull(store = X509_STORE_new());
  50469. ExpectNotNull(subject = X509_get_subject_name(caX509));
  50470. /* Errors */
  50471. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  50472. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  50473. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  50474. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  50475. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  50476. SSL_SUCCESS);
  50477. /* Should find the cert */
  50478. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  50479. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  50480. sk_X509_pop_free(certs, NULL);
  50481. certs = NULL;
  50482. /* Should not find the cert */
  50483. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  50484. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  50485. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  50486. sk_X509_pop_free(certs, NULL);
  50487. certs = NULL;
  50488. X509_STORE_free(store);
  50489. X509_STORE_CTX_free(storeCtx);
  50490. X509_free(svrX509);
  50491. X509_free(caX509);
  50492. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  50493. return EXPECT_RESULT();
  50494. }
  50495. static int test_wolfSSL_dup_CA_list(void)
  50496. {
  50497. int res = TEST_SKIPPED;
  50498. #if defined(OPENSSL_ALL)
  50499. EXPECT_DECLS;
  50500. STACK_OF(X509_NAME) *originalStack = NULL;
  50501. STACK_OF(X509_NAME) *copyStack = NULL;
  50502. int originalCount = 0;
  50503. int copyCount = 0;
  50504. X509_NAME *name = NULL;
  50505. int i;
  50506. originalStack = sk_X509_NAME_new_null();
  50507. ExpectNotNull(originalStack);
  50508. for (i = 0; i < 3; i++) {
  50509. name = X509_NAME_new();
  50510. ExpectNotNull(name);
  50511. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  50512. }
  50513. copyStack = SSL_dup_CA_list(originalStack);
  50514. ExpectNotNull(copyStack);
  50515. originalCount = sk_X509_NAME_num(originalStack);
  50516. copyCount = sk_X509_NAME_num(copyStack);
  50517. AssertIntEQ(originalCount, copyCount);
  50518. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  50519. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  50520. originalStack = NULL;
  50521. copyStack = NULL;
  50522. res = EXPECT_RESULT();
  50523. #endif /* OPENSSL_ALL */
  50524. return res;
  50525. }
  50526. static int test_ForceZero(void)
  50527. {
  50528. EXPECT_DECLS;
  50529. unsigned char data[32];
  50530. unsigned int i, j, len;
  50531. /* Test case with 0 length */
  50532. ForceZero(data, 0);
  50533. /* Test ForceZero */
  50534. for (i = 0; i < sizeof(data); i++) {
  50535. for (len = 1; len < sizeof(data) - i; len++) {
  50536. for (j = 0; j < sizeof(data); j++)
  50537. data[j] = j + 1;
  50538. ForceZero(data + i, len);
  50539. for (j = 0; j < sizeof(data); j++) {
  50540. if (j < i || j >= i + len) {
  50541. ExpectIntNE(data[j], 0x00);
  50542. }
  50543. else {
  50544. ExpectIntEQ(data[j], 0x00);
  50545. }
  50546. }
  50547. }
  50548. }
  50549. return EXPECT_RESULT();
  50550. }
  50551. #ifndef NO_BIO
  50552. static int test_wolfSSL_X509_print(void)
  50553. {
  50554. EXPECT_DECLS;
  50555. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50556. !defined(NO_RSA) && defined(XSNPRINTF)
  50557. X509 *x509 = NULL;
  50558. BIO *bio = NULL;
  50559. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  50560. const X509_ALGOR *cert_sig_alg = NULL;
  50561. #endif
  50562. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  50563. WOLFSSL_FILETYPE_PEM));
  50564. /* print to memory */
  50565. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  50566. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  50567. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  50568. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  50569. /* Will print IP address subject alt name. */
  50570. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  50571. #elif defined(NO_ASN_TIME)
  50572. /* Will print IP address subject alt name but not Validity. */
  50573. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  50574. #else
  50575. /* Will print IP address subject alt name. */
  50576. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  50577. #endif
  50578. #elif defined(NO_ASN_TIME)
  50579. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  50580. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  50581. #else
  50582. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  50583. #endif
  50584. BIO_free(bio);
  50585. bio = NULL;
  50586. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50587. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  50588. /* Print signature */
  50589. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  50590. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  50591. #endif
  50592. /* print to stderr */
  50593. #if !defined(NO_WOLFSSL_DIR)
  50594. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  50595. #endif
  50596. /* print again */
  50597. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  50598. X509_free(x509);
  50599. BIO_free(bio);
  50600. #endif
  50601. return EXPECT_RESULT();
  50602. }
  50603. static int test_wolfSSL_X509_CRL_print(void)
  50604. {
  50605. EXPECT_DECLS;
  50606. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  50607. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  50608. X509_CRL* crl = NULL;
  50609. BIO *bio = NULL;
  50610. XFILE fp = XBADFILE;
  50611. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  50612. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50613. NULL, NULL));
  50614. if (fp != XBADFILE)
  50615. XFCLOSE(fp);
  50616. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  50617. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  50618. X509_CRL_free(crl);
  50619. BIO_free(bio);
  50620. #endif
  50621. return EXPECT_RESULT();
  50622. }
  50623. static int test_wolfSSL_BIO_get_len(void)
  50624. {
  50625. EXPECT_DECLS;
  50626. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  50627. BIO *bio = NULL;
  50628. const char txt[] = "Some example text to push to the BIO.";
  50629. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  50630. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50631. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  50632. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  50633. BIO_free(bio);
  50634. bio = NULL;
  50635. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50636. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  50637. BIO_free(bio);
  50638. #endif
  50639. return EXPECT_RESULT();
  50640. }
  50641. #endif /* !NO_BIO */
  50642. static int test_wolfSSL_RSA(void)
  50643. {
  50644. EXPECT_DECLS;
  50645. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  50646. RSA* rsa = NULL;
  50647. const BIGNUM *n = NULL;
  50648. const BIGNUM *e = NULL;
  50649. const BIGNUM *d = NULL;
  50650. const BIGNUM *p = NULL;
  50651. const BIGNUM *q = NULL;
  50652. const BIGNUM *dmp1 = NULL;
  50653. const BIGNUM *dmq1 = NULL;
  50654. const BIGNUM *iqmp = NULL;
  50655. ExpectNotNull(rsa = RSA_new());
  50656. ExpectIntEQ(RSA_size(NULL), 0);
  50657. ExpectIntEQ(RSA_size(rsa), 0);
  50658. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  50659. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  50660. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  50661. #ifdef WOLFSSL_RSA_KEY_CHECK
  50662. ExpectIntEQ(RSA_check_key(rsa), 0);
  50663. #endif
  50664. RSA_free(rsa);
  50665. rsa = NULL;
  50666. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50667. ExpectIntEQ(RSA_size(rsa), 256);
  50668. #ifdef WOLFSSL_RSA_KEY_CHECK
  50669. ExpectIntEQ(RSA_check_key(NULL), 0);
  50670. ExpectIntEQ(RSA_check_key(rsa), 1);
  50671. #endif
  50672. /* sanity check */
  50673. ExpectIntEQ(RSA_bits(NULL), 0);
  50674. /* key */
  50675. ExpectIntEQ(RSA_bits(rsa), 2048);
  50676. RSA_get0_key(rsa, &n, &e, &d);
  50677. ExpectPtrEq(rsa->n, n);
  50678. ExpectPtrEq(rsa->e, e);
  50679. ExpectPtrEq(rsa->d, d);
  50680. n = NULL;
  50681. e = NULL;
  50682. d = NULL;
  50683. ExpectNotNull(n = BN_new());
  50684. ExpectNotNull(e = BN_new());
  50685. ExpectNotNull(d = BN_new());
  50686. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  50687. if (EXPECT_FAIL()) {
  50688. BN_free((BIGNUM*)n);
  50689. BN_free((BIGNUM*)e);
  50690. BN_free((BIGNUM*)d);
  50691. }
  50692. ExpectPtrEq(rsa->n, n);
  50693. ExpectPtrEq(rsa->e, e);
  50694. ExpectPtrEq(rsa->d, d);
  50695. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  50696. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  50697. /* crt_params */
  50698. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  50699. ExpectPtrEq(rsa->dmp1, dmp1);
  50700. ExpectPtrEq(rsa->dmq1, dmq1);
  50701. ExpectPtrEq(rsa->iqmp, iqmp);
  50702. dmp1 = NULL;
  50703. dmq1 = NULL;
  50704. iqmp = NULL;
  50705. ExpectNotNull(dmp1 = BN_new());
  50706. ExpectNotNull(dmq1 = BN_new());
  50707. ExpectNotNull(iqmp = BN_new());
  50708. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  50709. (BIGNUM*)iqmp), 1);
  50710. if (EXPECT_FAIL()) {
  50711. BN_free((BIGNUM*)dmp1);
  50712. BN_free((BIGNUM*)dmq1);
  50713. BN_free((BIGNUM*)iqmp);
  50714. }
  50715. ExpectPtrEq(rsa->dmp1, dmp1);
  50716. ExpectPtrEq(rsa->dmq1, dmq1);
  50717. ExpectPtrEq(rsa->iqmp, iqmp);
  50718. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  50719. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  50720. (BIGNUM*)iqmp), 0);
  50721. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  50722. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  50723. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  50724. ExpectNull(dmp1);
  50725. ExpectNull(dmq1);
  50726. ExpectNull(iqmp);
  50727. /* factors */
  50728. RSA_get0_factors(rsa, NULL, NULL);
  50729. RSA_get0_factors(rsa, &p, &q);
  50730. ExpectPtrEq(rsa->p, p);
  50731. ExpectPtrEq(rsa->q, q);
  50732. p = NULL;
  50733. q = NULL;
  50734. ExpectNotNull(p = BN_new());
  50735. ExpectNotNull(q = BN_new());
  50736. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  50737. if (EXPECT_FAIL()) {
  50738. BN_free((BIGNUM*)p);
  50739. BN_free((BIGNUM*)q);
  50740. }
  50741. ExpectPtrEq(rsa->p, p);
  50742. ExpectPtrEq(rsa->q, q);
  50743. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  50744. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  50745. RSA_get0_factors(NULL, NULL, NULL);
  50746. RSA_get0_factors(NULL, &p, &q);
  50747. ExpectNull(p);
  50748. ExpectNull(q);
  50749. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  50750. ExpectIntEQ(RSA_bits(rsa), 21);
  50751. RSA_free(rsa);
  50752. rsa = NULL;
  50753. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  50754. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  50755. ExpectIntEQ(RSA_size(rsa), 384);
  50756. ExpectIntEQ(RSA_bits(rsa), 3072);
  50757. RSA_free(rsa);
  50758. rsa = NULL;
  50759. #endif
  50760. /* remove for now with odd key size until adjusting rsa key size check with
  50761. wc_MakeRsaKey()
  50762. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  50763. RSA_free(rsa);
  50764. rsa = NULL;
  50765. */
  50766. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  50767. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  50768. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  50769. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  50770. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  50771. {
  50772. byte buff[FOURK_BUF];
  50773. byte der[FOURK_BUF];
  50774. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  50775. XFILE f = XBADFILE;
  50776. int bytes = 0;
  50777. /* test loading encrypted RSA private pem w/o password */
  50778. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  50779. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  50780. if (f != XBADFILE)
  50781. XFCLOSE(f);
  50782. XMEMSET(der, 0, sizeof(der));
  50783. /* test that error value is returned with no password */
  50784. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  50785. 0);
  50786. }
  50787. #endif
  50788. #endif
  50789. return EXPECT_RESULT();
  50790. }
  50791. static int test_wolfSSL_RSA_DER(void)
  50792. {
  50793. EXPECT_DECLS;
  50794. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  50795. RSA *rsa = NULL;
  50796. int i;
  50797. const unsigned char *buff = NULL;
  50798. unsigned char *newBuff = NULL;
  50799. struct tbl_s
  50800. {
  50801. const unsigned char *der;
  50802. int sz;
  50803. } tbl[] = {
  50804. #ifdef USE_CERT_BUFFERS_1024
  50805. {client_key_der_1024, sizeof_client_key_der_1024},
  50806. {server_key_der_1024, sizeof_server_key_der_1024},
  50807. #endif
  50808. #ifdef USE_CERT_BUFFERS_2048
  50809. {client_key_der_2048, sizeof_client_key_der_2048},
  50810. {server_key_der_2048, sizeof_server_key_der_2048},
  50811. #endif
  50812. {NULL, 0}
  50813. };
  50814. /* Public Key DER */
  50815. struct tbl_s pub[] = {
  50816. #ifdef USE_CERT_BUFFERS_1024
  50817. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  50818. #endif
  50819. #ifdef USE_CERT_BUFFERS_2048
  50820. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  50821. #endif
  50822. {NULL, 0}
  50823. };
  50824. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  50825. buff = pub[0].der;
  50826. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  50827. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  50828. buff = tbl[0].der;
  50829. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  50830. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  50831. rsa = RSA_new();
  50832. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  50833. RSA_free(rsa);
  50834. rsa = NULL;
  50835. for (i = 0; tbl[i].der != NULL; i++)
  50836. {
  50837. /* Passing in pointer results in pointer moving. */
  50838. buff = tbl[i].der;
  50839. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  50840. ExpectNotNull(rsa);
  50841. RSA_free(rsa);
  50842. rsa = NULL;
  50843. }
  50844. for (i = 0; tbl[i].der != NULL; i++)
  50845. {
  50846. /* Passing in pointer results in pointer moving. */
  50847. buff = tbl[i].der;
  50848. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  50849. ExpectNotNull(rsa);
  50850. RSA_free(rsa);
  50851. rsa = NULL;
  50852. }
  50853. for (i = 0; pub[i].der != NULL; i++)
  50854. {
  50855. buff = pub[i].der;
  50856. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  50857. ExpectNotNull(rsa);
  50858. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  50859. newBuff = NULL;
  50860. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  50861. ExpectNotNull(newBuff);
  50862. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  50863. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50864. RSA_free(rsa);
  50865. rsa = NULL;
  50866. }
  50867. #endif
  50868. return EXPECT_RESULT();
  50869. }
  50870. static int test_wolfSSL_RSA_print(void)
  50871. {
  50872. EXPECT_DECLS;
  50873. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50874. !defined(NO_STDIO_FILESYSTEM) && \
  50875. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  50876. !defined(NO_BIO) && defined(XFPRINTF)
  50877. BIO *bio = NULL;
  50878. WOLFSSL_RSA* rsa = NULL;
  50879. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50880. ExpectNotNull(rsa = RSA_new());
  50881. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  50882. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  50883. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  50884. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  50885. /* Some very large number of indent spaces. */
  50886. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  50887. /* RSA is empty. */
  50888. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  50889. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  50890. RSA_free(rsa);
  50891. rsa = NULL;
  50892. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50893. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  50894. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  50895. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  50896. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  50897. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  50898. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  50899. BIO_free(bio);
  50900. RSA_free(rsa);
  50901. #endif
  50902. return EXPECT_RESULT();
  50903. }
  50904. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  50905. {
  50906. EXPECT_DECLS;
  50907. #ifndef NO_RSA
  50908. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50909. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  50910. RSA *rsa = NULL;
  50911. const unsigned char *derBuf = client_key_der_2048;
  50912. unsigned char em[256] = {0}; /* len = 2048/8 */
  50913. /* Random data simulating a hash */
  50914. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  50915. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  50916. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  50917. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  50918. };
  50919. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  50920. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  50921. RSA_PSS_SALTLEN_DIGEST), 0);
  50922. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  50923. RSA_PSS_SALTLEN_DIGEST), 0);
  50924. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  50925. RSA_PSS_SALTLEN_DIGEST), 0);
  50926. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  50927. RSA_PSS_SALTLEN_DIGEST), 0);
  50928. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  50929. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  50930. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50931. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  50932. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50933. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  50934. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50935. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  50936. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50937. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50938. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50939. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  50940. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50941. RSA_PSS_SALTLEN_DIGEST), 1);
  50942. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50943. RSA_PSS_SALTLEN_DIGEST), 1);
  50944. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50945. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50946. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50947. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50948. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50949. RSA_PSS_SALTLEN_MAX), 1);
  50950. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50951. RSA_PSS_SALTLEN_MAX), 1);
  50952. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  50953. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  50954. RSA_free(rsa);
  50955. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  50956. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50957. #endif
  50958. return EXPECT_RESULT();
  50959. }
  50960. static int test_wolfSSL_RSA_sign_sha3(void)
  50961. {
  50962. EXPECT_DECLS;
  50963. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  50964. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50965. RSA* rsa = NULL;
  50966. const unsigned char *derBuf = client_key_der_2048;
  50967. unsigned char sigRet[256] = {0};
  50968. unsigned int sigLen = sizeof(sigRet);
  50969. /* Random data simulating a hash */
  50970. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  50971. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  50972. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  50973. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  50974. };
  50975. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  50976. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  50977. rsa), 1);
  50978. RSA_free(rsa);
  50979. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50980. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  50981. return EXPECT_RESULT();
  50982. }
  50983. static int test_wolfSSL_RSA_get0_key(void)
  50984. {
  50985. EXPECT_DECLS;
  50986. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50987. RSA *rsa = NULL;
  50988. const BIGNUM* n = NULL;
  50989. const BIGNUM* e = NULL;
  50990. const BIGNUM* d = NULL;
  50991. const unsigned char* der;
  50992. int derSz;
  50993. #ifdef USE_CERT_BUFFERS_1024
  50994. der = client_key_der_1024;
  50995. derSz = sizeof_client_key_der_1024;
  50996. #elif defined(USE_CERT_BUFFERS_2048)
  50997. der = client_key_der_2048;
  50998. derSz = sizeof_client_key_der_2048;
  50999. #else
  51000. der = NULL;
  51001. derSz = 0;
  51002. #endif
  51003. if (der != NULL) {
  51004. RSA_get0_key(NULL, NULL, NULL, NULL);
  51005. RSA_get0_key(rsa, NULL, NULL, NULL);
  51006. RSA_get0_key(NULL, &n, &e, &d);
  51007. ExpectNull(n);
  51008. ExpectNull(e);
  51009. ExpectNull(d);
  51010. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  51011. ExpectNotNull(rsa);
  51012. RSA_get0_key(rsa, NULL, NULL, NULL);
  51013. RSA_get0_key(rsa, &n, NULL, NULL);
  51014. ExpectNotNull(n);
  51015. RSA_get0_key(rsa, NULL, &e, NULL);
  51016. ExpectNotNull(e);
  51017. RSA_get0_key(rsa, NULL, NULL, &d);
  51018. ExpectNotNull(d);
  51019. RSA_get0_key(rsa, &n, &e, &d);
  51020. ExpectNotNull(n);
  51021. ExpectNotNull(e);
  51022. ExpectNotNull(d);
  51023. RSA_free(rsa);
  51024. }
  51025. #endif
  51026. return EXPECT_RESULT();
  51027. }
  51028. static int test_wolfSSL_RSA_meth(void)
  51029. {
  51030. EXPECT_DECLS;
  51031. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51032. RSA *rsa = NULL;
  51033. RSA_METHOD *rsa_meth = NULL;
  51034. #ifdef WOLFSSL_KEY_GEN
  51035. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51036. RSA_free(rsa);
  51037. rsa = NULL;
  51038. #else
  51039. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51040. #endif
  51041. ExpectNotNull(RSA_get_default_method());
  51042. wolfSSL_RSA_meth_free(NULL);
  51043. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  51044. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  51045. RSA_METHOD_FLAG_NO_CHECK));
  51046. #ifndef NO_WOLFSSL_STUB
  51047. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  51048. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  51049. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  51050. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  51051. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  51052. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  51053. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  51054. #endif
  51055. ExpectIntEQ(RSA_flags(NULL), 0);
  51056. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51057. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51058. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  51059. ExpectNotNull(rsa = RSA_new());
  51060. /* No method set. */
  51061. ExpectIntEQ(RSA_flags(rsa), 0);
  51062. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51063. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51064. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51065. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  51066. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  51067. if (EXPECT_FAIL()) {
  51068. wolfSSL_RSA_meth_free(rsa_meth);
  51069. }
  51070. ExpectNull(RSA_get_method(NULL));
  51071. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  51072. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  51073. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51074. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51075. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  51076. RSA_METHOD_FLAG_NO_CHECK);
  51077. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51078. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51079. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  51080. /* rsa_meth is freed here */
  51081. RSA_free(rsa);
  51082. #endif
  51083. return EXPECT_RESULT();
  51084. }
  51085. static int test_wolfSSL_RSA_verify(void)
  51086. {
  51087. EXPECT_DECLS;
  51088. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  51089. #ifndef NO_BIO
  51090. XFILE fp = XBADFILE;
  51091. RSA *pKey = NULL;
  51092. RSA *pubKey = NULL;
  51093. X509 *cert = NULL;
  51094. const char *text = "Hello wolfSSL !";
  51095. unsigned char hash[SHA256_DIGEST_LENGTH];
  51096. unsigned char signature[2048/8];
  51097. unsigned int signatureLength;
  51098. byte *buf = NULL;
  51099. BIO *bio = NULL;
  51100. SHA256_CTX c;
  51101. EVP_PKEY *evpPkey = NULL;
  51102. EVP_PKEY *evpPubkey = NULL;
  51103. size_t sz;
  51104. /* generate hash */
  51105. SHA256_Init(&c);
  51106. SHA256_Update(&c, text, strlen(text));
  51107. SHA256_Final(hash, &c);
  51108. #ifdef WOLFSSL_SMALL_STACK_CACHE
  51109. /* workaround for small stack cache case */
  51110. wc_Sha256Free((wc_Sha256*)&c);
  51111. #endif
  51112. /* read privete key file */
  51113. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  51114. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  51115. ExpectTrue((sz = XFTELL(fp)) > 0);
  51116. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  51117. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  51118. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  51119. if (fp != XBADFILE) {
  51120. XFCLOSE(fp);
  51121. fp = XBADFILE;
  51122. }
  51123. /* read private key and sign hash data */
  51124. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  51125. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  51126. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  51127. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  51128. signature, &signatureLength, pKey), SSL_SUCCESS);
  51129. /* read public key and verify signed data */
  51130. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  51131. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  51132. if (fp != XBADFILE)
  51133. XFCLOSE(fp);
  51134. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  51135. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  51136. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51137. signatureLength, pubKey), SSL_SUCCESS);
  51138. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  51139. signatureLength, NULL), SSL_FAILURE);
  51140. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  51141. signatureLength, pubKey), SSL_FAILURE);
  51142. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  51143. signatureLength, pubKey), SSL_FAILURE);
  51144. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51145. signatureLength, NULL), SSL_FAILURE);
  51146. RSA_free(pKey);
  51147. EVP_PKEY_free(evpPkey);
  51148. RSA_free(pubKey);
  51149. EVP_PKEY_free(evpPubkey);
  51150. X509_free(cert);
  51151. BIO_free(bio);
  51152. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  51153. #endif
  51154. #endif
  51155. return EXPECT_RESULT();
  51156. }
  51157. static int test_wolfSSL_RSA_sign(void)
  51158. {
  51159. EXPECT_DECLS;
  51160. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51161. RSA *rsa;
  51162. unsigned char hash[SHA256_DIGEST_LENGTH];
  51163. #ifdef USE_CERT_BUFFERS_1024
  51164. const unsigned char* privDer = client_key_der_1024;
  51165. size_t privDerSz = sizeof_client_key_der_1024;
  51166. const unsigned char* pubDer = client_keypub_der_1024;
  51167. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51168. unsigned char signature[1024/8];
  51169. #else
  51170. const unsigned char* privDer = client_key_der_2048;
  51171. size_t privDerSz = sizeof_client_key_der_2048;
  51172. const unsigned char* pubDer = client_keypub_der_2048;
  51173. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51174. unsigned char signature[2048/8];
  51175. #endif
  51176. unsigned int signatureLen;
  51177. const unsigned char* der;
  51178. XMEMSET(hash, 0, sizeof(hash));
  51179. der = privDer;
  51180. rsa = NULL;
  51181. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51182. /* Invalid parameters. */
  51183. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  51184. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  51185. &signatureLen, rsa), 0);
  51186. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  51187. &signatureLen, rsa), 0);
  51188. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  51189. &signatureLen, rsa), 0);
  51190. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51191. NULL, rsa), 0);
  51192. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51193. &signatureLen, NULL), 0);
  51194. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51195. &signatureLen, rsa), 1);
  51196. RSA_free(rsa);
  51197. der = pubDer;
  51198. rsa = NULL;
  51199. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51200. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51201. signatureLen, rsa), 1);
  51202. RSA_free(rsa);
  51203. #endif
  51204. return EXPECT_RESULT();
  51205. }
  51206. static int test_wolfSSL_RSA_sign_ex(void)
  51207. {
  51208. EXPECT_DECLS;
  51209. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51210. RSA *rsa = NULL;
  51211. unsigned char hash[SHA256_DIGEST_LENGTH];
  51212. #ifdef USE_CERT_BUFFERS_1024
  51213. const unsigned char* privDer = client_key_der_1024;
  51214. size_t privDerSz = sizeof_client_key_der_1024;
  51215. const unsigned char* pubDer = client_keypub_der_1024;
  51216. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51217. unsigned char signature[1024/8];
  51218. #else
  51219. const unsigned char* privDer = client_key_der_2048;
  51220. size_t privDerSz = sizeof_client_key_der_2048;
  51221. const unsigned char* pubDer = client_keypub_der_2048;
  51222. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51223. unsigned char signature[2048/8];
  51224. #endif
  51225. unsigned int signatureLen;
  51226. const unsigned char* der;
  51227. unsigned char encodedHash[51];
  51228. unsigned int encodedHashLen;
  51229. const unsigned char expEncHash[] = {
  51230. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  51231. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  51232. 0x00, 0x04, 0x20,
  51233. /* Hash data */
  51234. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51235. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51236. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51237. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51238. };
  51239. XMEMSET(hash, 0, sizeof(hash));
  51240. ExpectNotNull(rsa = wolfSSL_RSA_new());
  51241. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51242. &signatureLen, rsa, 1), 0);
  51243. wolfSSL_RSA_free(rsa);
  51244. der = privDer;
  51245. rsa = NULL;
  51246. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51247. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  51248. -1), 0);
  51249. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  51250. signature, &signatureLen, rsa, 1), 0);
  51251. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  51252. &signatureLen, rsa, 1), 0);
  51253. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  51254. &signatureLen, rsa, 1), 0);
  51255. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51256. NULL, rsa, 1), 0);
  51257. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51258. &signatureLen, NULL, 1), 0);
  51259. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51260. &signatureLen, rsa, -1), 0);
  51261. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  51262. &signatureLen, rsa, 0), 0);
  51263. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  51264. &signatureLen, rsa, 0), 0);
  51265. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51266. NULL, rsa, 0), 0);
  51267. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51268. &signatureLen, rsa, 1), 1);
  51269. /* Test returning encoded hash. */
  51270. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  51271. &encodedHashLen, rsa, 0), 1);
  51272. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  51273. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  51274. RSA_free(rsa);
  51275. der = pubDer;
  51276. rsa = NULL;
  51277. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51278. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51279. signatureLen, rsa), 1);
  51280. RSA_free(rsa);
  51281. #endif
  51282. return EXPECT_RESULT();
  51283. }
  51284. static int test_wolfSSL_RSA_public_decrypt(void)
  51285. {
  51286. EXPECT_DECLS;
  51287. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51288. RSA *rsa;
  51289. unsigned char msg[SHA256_DIGEST_LENGTH];
  51290. #ifdef USE_CERT_BUFFERS_1024
  51291. const unsigned char* pubDer = client_keypub_der_1024;
  51292. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51293. unsigned char decMsg[1024/8];
  51294. const unsigned char encMsg[] = {
  51295. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  51296. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  51297. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  51298. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  51299. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  51300. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  51301. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  51302. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  51303. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  51304. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  51305. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  51306. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  51307. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  51308. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  51309. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  51310. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  51311. };
  51312. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  51313. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  51314. defined(WC_RSA_NO_PADDING)
  51315. const unsigned char encMsgNoPad[] = {
  51316. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  51317. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  51318. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  51319. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  51320. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  51321. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  51322. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  51323. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  51324. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  51325. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  51326. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  51327. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  51328. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  51329. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  51330. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  51331. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  51332. };
  51333. #endif
  51334. #else
  51335. const unsigned char* pubDer = client_keypub_der_2048;
  51336. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51337. unsigned char decMsg[2048/8];
  51338. const unsigned char encMsg[] = {
  51339. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  51340. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  51341. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  51342. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  51343. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  51344. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  51345. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  51346. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  51347. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  51348. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  51349. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  51350. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  51351. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  51352. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  51353. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  51354. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  51355. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  51356. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  51357. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  51358. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  51359. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  51360. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  51361. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  51362. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  51363. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  51364. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  51365. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  51366. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  51367. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  51368. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  51369. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  51370. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  51371. };
  51372. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  51373. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  51374. defined(WC_RSA_NO_PADDING)
  51375. const unsigned char encMsgNoPad[] = {
  51376. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  51377. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  51378. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  51379. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  51380. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  51381. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  51382. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  51383. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  51384. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  51385. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  51386. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  51387. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  51388. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  51389. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  51390. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  51391. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  51392. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  51393. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  51394. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  51395. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  51396. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  51397. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  51398. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  51399. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  51400. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  51401. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  51402. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  51403. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  51404. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  51405. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  51406. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  51407. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  51408. };
  51409. #endif
  51410. #endif
  51411. const unsigned char* der;
  51412. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  51413. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  51414. defined(WC_RSA_NO_PADDING)
  51415. int i;
  51416. #endif
  51417. XMEMSET(msg, 0, sizeof(msg));
  51418. der = pubDer;
  51419. rsa = NULL;
  51420. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51421. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  51422. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  51423. RSA_PKCS1_PADDING), -1);
  51424. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  51425. RSA_PKCS1_PADDING), -1);
  51426. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  51427. RSA_PKCS1_PADDING), -1);
  51428. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  51429. RSA_PKCS1_PADDING), -1);
  51430. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  51431. RSA_PKCS1_PSS_PADDING), -1);
  51432. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  51433. RSA_PKCS1_PADDING), 32);
  51434. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  51435. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  51436. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  51437. defined(WC_RSA_NO_PADDING)
  51438. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  51439. rsa, RSA_NO_PADDING), sizeof(decMsg));
  51440. /* Zeros before actual data. */
  51441. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  51442. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  51443. }
  51444. /* Check actual data. */
  51445. XMEMSET(msg, 0x01, sizeof(msg));
  51446. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  51447. #endif
  51448. RSA_free(rsa);
  51449. #endif
  51450. return EXPECT_RESULT();
  51451. }
  51452. static int test_wolfSSL_RSA_private_encrypt(void)
  51453. {
  51454. EXPECT_DECLS;
  51455. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51456. RSA *rsa;
  51457. unsigned char msg[SHA256_DIGEST_LENGTH];
  51458. #ifdef USE_CERT_BUFFERS_1024
  51459. const unsigned char* privDer = client_key_der_1024;
  51460. size_t privDerSz = sizeof_client_key_der_1024;
  51461. unsigned char encMsg[1024/8];
  51462. const unsigned char expEncMsg[] = {
  51463. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  51464. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  51465. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  51466. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  51467. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  51468. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  51469. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  51470. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  51471. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  51472. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  51473. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  51474. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  51475. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  51476. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  51477. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  51478. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  51479. };
  51480. #ifdef WC_RSA_NO_PADDING
  51481. const unsigned char expEncMsgNoPad[] = {
  51482. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  51483. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  51484. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  51485. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  51486. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  51487. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  51488. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  51489. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  51490. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  51491. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  51492. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  51493. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  51494. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  51495. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  51496. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  51497. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  51498. };
  51499. #endif
  51500. #else
  51501. const unsigned char* privDer = client_key_der_2048;
  51502. size_t privDerSz = sizeof_client_key_der_2048;
  51503. unsigned char encMsg[2048/8];
  51504. const unsigned char expEncMsg[] = {
  51505. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  51506. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  51507. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  51508. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  51509. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  51510. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  51511. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  51512. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  51513. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  51514. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  51515. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  51516. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  51517. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  51518. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  51519. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  51520. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  51521. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  51522. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  51523. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  51524. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  51525. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  51526. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  51527. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  51528. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  51529. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  51530. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  51531. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  51532. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  51533. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  51534. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  51535. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  51536. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  51537. };
  51538. #ifdef WC_RSA_NO_PADDING
  51539. const unsigned char expEncMsgNoPad[] = {
  51540. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  51541. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  51542. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  51543. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  51544. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  51545. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  51546. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  51547. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  51548. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  51549. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  51550. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  51551. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  51552. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  51553. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  51554. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  51555. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  51556. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  51557. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  51558. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  51559. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  51560. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  51561. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  51562. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  51563. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  51564. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  51565. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  51566. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  51567. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  51568. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  51569. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  51570. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  51571. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  51572. };
  51573. #endif
  51574. #endif
  51575. const unsigned char* der;
  51576. XMEMSET(msg, 0x00, sizeof(msg));
  51577. der = privDer;
  51578. rsa = NULL;
  51579. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51580. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  51581. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  51582. -1);
  51583. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  51584. RSA_PKCS1_PADDING), -1);
  51585. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  51586. RSA_PKCS1_PADDING), -1);
  51587. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  51588. RSA_PKCS1_PADDING), -1);
  51589. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51590. RSA_PKCS1_PSS_PADDING), -1);
  51591. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51592. RSA_PKCS1_PADDING), sizeof(encMsg));
  51593. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  51594. #ifdef WC_RSA_NO_PADDING
  51595. /* Non-zero message. */
  51596. XMEMSET(msg, 0x01, sizeof(msg));
  51597. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51598. RSA_NO_PADDING), sizeof(encMsg));
  51599. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  51600. #endif
  51601. RSA_free(rsa);
  51602. #endif
  51603. return EXPECT_RESULT();
  51604. }
  51605. static int test_wolfSSL_RSA_public_encrypt(void)
  51606. {
  51607. EXPECT_DECLS;
  51608. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51609. RSA* rsa = NULL;
  51610. const unsigned char msg[2048/8] = { 0 };
  51611. unsigned char encMsg[2048/8];
  51612. ExpectNotNull(rsa = RSA_new());
  51613. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  51614. RSA_PKCS1_PADDING), -1);
  51615. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  51616. RSA_PKCS1_PADDING), -1);
  51617. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  51618. RSA_PKCS1_PADDING), -1);
  51619. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  51620. RSA_PKCS1_PADDING), -1);
  51621. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  51622. RSA_PKCS1_PSS_PADDING), -1);
  51623. /* Empty RSA key. */
  51624. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  51625. RSA_PKCS1_PADDING), -1);
  51626. RSA_free(rsa);
  51627. #endif
  51628. return EXPECT_RESULT();
  51629. }
  51630. static int test_wolfSSL_RSA_private_decrypt(void)
  51631. {
  51632. EXPECT_DECLS;
  51633. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51634. RSA* rsa = NULL;
  51635. unsigned char msg[2048/8];
  51636. const unsigned char encMsg[2048/8] = { 0 };
  51637. ExpectNotNull(rsa = RSA_new());
  51638. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  51639. RSA_PKCS1_PADDING), -1);
  51640. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  51641. RSA_PKCS1_PADDING), -1);
  51642. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  51643. RSA_PKCS1_PADDING), -1);
  51644. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  51645. RSA_PKCS1_PADDING), -1);
  51646. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  51647. RSA_PKCS1_PSS_PADDING), -1);
  51648. /* Empty RSA key. */
  51649. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  51650. RSA_PKCS1_PADDING), -1);
  51651. RSA_free(rsa);
  51652. #endif
  51653. return EXPECT_RESULT();
  51654. }
  51655. static int test_wolfSSL_RSA_GenAdd(void)
  51656. {
  51657. EXPECT_DECLS;
  51658. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51659. RSA *rsa;
  51660. #ifdef USE_CERT_BUFFERS_1024
  51661. const unsigned char* privDer = client_key_der_1024;
  51662. size_t privDerSz = sizeof_client_key_der_1024;
  51663. const unsigned char* pubDer = client_keypub_der_1024;
  51664. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51665. #else
  51666. const unsigned char* privDer = client_key_der_2048;
  51667. size_t privDerSz = sizeof_client_key_der_2048;
  51668. const unsigned char* pubDer = client_keypub_der_2048;
  51669. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51670. #endif
  51671. const unsigned char* der;
  51672. der = privDer;
  51673. rsa = NULL;
  51674. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51675. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  51676. #ifndef RSA_LOW_MEM
  51677. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  51678. #else
  51679. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  51680. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  51681. #endif
  51682. RSA_free(rsa);
  51683. der = pubDer;
  51684. rsa = NULL;
  51685. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51686. /* Need private values. */
  51687. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  51688. RSA_free(rsa);
  51689. #endif
  51690. return EXPECT_RESULT();
  51691. }
  51692. static int test_wolfSSL_RSA_blinding_on(void)
  51693. {
  51694. EXPECT_DECLS;
  51695. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  51696. RSA *rsa;
  51697. WOLFSSL_BN_CTX *bnCtx = NULL;
  51698. #ifdef USE_CERT_BUFFERS_1024
  51699. const unsigned char* privDer = client_key_der_1024;
  51700. size_t privDerSz = sizeof_client_key_der_1024;
  51701. #else
  51702. const unsigned char* privDer = client_key_der_2048;
  51703. size_t privDerSz = sizeof_client_key_der_2048;
  51704. #endif
  51705. const unsigned char* der;
  51706. der = privDer;
  51707. rsa = NULL;
  51708. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51709. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  51710. /* Does nothing so all parameters are valid. */
  51711. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  51712. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  51713. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  51714. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  51715. wolfSSL_BN_CTX_free(bnCtx);
  51716. RSA_free(rsa);
  51717. #endif
  51718. return EXPECT_RESULT();
  51719. }
  51720. static int test_wolfSSL_RSA_ex_data(void)
  51721. {
  51722. EXPECT_DECLS;
  51723. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51724. RSA* rsa = NULL;
  51725. unsigned char data[1];
  51726. ExpectNotNull(rsa = RSA_new());
  51727. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  51728. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  51729. #ifdef MAX_EX_DATA
  51730. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  51731. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  51732. #endif
  51733. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  51734. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  51735. #ifdef HAVE_EX_DATA
  51736. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  51737. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  51738. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  51739. #else
  51740. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  51741. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  51742. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  51743. #endif
  51744. RSA_free(rsa);
  51745. #endif /* !NO_RSA && OPENSSL_EXTRA */
  51746. return EXPECT_RESULT();
  51747. }
  51748. static int test_wolfSSL_RSA_LoadDer(void)
  51749. {
  51750. EXPECT_DECLS;
  51751. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  51752. defined(OPENSSL_EXTRA_X509_SMALL))
  51753. RSA *rsa = NULL;
  51754. #ifdef USE_CERT_BUFFERS_1024
  51755. const unsigned char* privDer = client_key_der_1024;
  51756. size_t privDerSz = sizeof_client_key_der_1024;
  51757. #else
  51758. const unsigned char* privDer = client_key_der_2048;
  51759. size_t privDerSz = sizeof_client_key_der_2048;
  51760. #endif
  51761. ExpectNotNull(rsa = RSA_new());
  51762. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  51763. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  51764. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  51765. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  51766. RSA_free(rsa);
  51767. #endif /* !NO_RSA && OPENSSL_EXTRA */
  51768. return EXPECT_RESULT();
  51769. }
  51770. /* Local API. */
  51771. static int test_wolfSSL_RSA_To_Der(void)
  51772. {
  51773. EXPECT_DECLS;
  51774. #ifdef WOLFSSL_TEST_STATIC_BUILD
  51775. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51776. RSA* rsa;
  51777. #ifdef USE_CERT_BUFFERS_1024
  51778. const unsigned char* privDer = client_key_der_1024;
  51779. size_t privDerSz = sizeof_client_key_der_1024;
  51780. const unsigned char* pubDer = client_keypub_der_1024;
  51781. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51782. unsigned char out[sizeof(client_key_der_1024)];
  51783. #else
  51784. const unsigned char* privDer = client_key_der_2048;
  51785. size_t privDerSz = sizeof_client_key_der_2048;
  51786. const unsigned char* pubDer = client_keypub_der_2048;
  51787. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51788. unsigned char out[sizeof(client_key_der_2048)];
  51789. #endif
  51790. const unsigned char* der;
  51791. unsigned char* outDer = NULL;
  51792. der = privDer;
  51793. rsa = NULL;
  51794. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51795. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51796. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  51797. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  51798. outDer = out;
  51799. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  51800. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  51801. outDer = NULL;
  51802. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  51803. ExpectNotNull(outDer);
  51804. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  51805. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  51806. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  51807. outDer = out;
  51808. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  51809. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  51810. RSA_free(rsa);
  51811. ExpectNotNull(rsa = RSA_new());
  51812. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51813. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  51814. RSA_free(rsa);
  51815. der = pubDer;
  51816. rsa = NULL;
  51817. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51818. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51819. RSA_free(rsa);
  51820. #endif
  51821. #endif
  51822. return EXPECT_RESULT();
  51823. }
  51824. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  51825. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  51826. {
  51827. EXPECT_DECLS;
  51828. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  51829. XFILE file = XBADFILE;
  51830. const char* fname = "./certs/server-keyPub.pem";
  51831. RSA *rsa = NULL;
  51832. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  51833. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  51834. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  51835. ExpectIntEQ(RSA_size(rsa), 256);
  51836. RSA_free(rsa);
  51837. if (file != XBADFILE)
  51838. XFCLOSE(file);
  51839. #endif
  51840. return EXPECT_RESULT();
  51841. }
  51842. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  51843. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  51844. {
  51845. EXPECT_DECLS;
  51846. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51847. defined(WOLFSSL_KEY_GEN)
  51848. RSA* rsa = NULL;
  51849. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  51850. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  51851. /* Valid but stub so returns 0. */
  51852. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  51853. #endif
  51854. return EXPECT_RESULT();
  51855. }
  51856. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  51857. {
  51858. EXPECT_DECLS;
  51859. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  51860. (defined(WOLFSSL_PEM_TO_DER) || \
  51861. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  51862. RSA* rsa = NULL;
  51863. #ifdef USE_CERT_BUFFERS_1024
  51864. const unsigned char* privDer = client_key_der_1024;
  51865. size_t privDerSz = sizeof_client_key_der_1024;
  51866. #else
  51867. const unsigned char* privDer = client_key_der_2048;
  51868. size_t privDerSz = sizeof_client_key_der_2048;
  51869. #endif
  51870. const unsigned char* der;
  51871. #ifndef NO_AES
  51872. unsigned char passwd[] = "password";
  51873. #endif
  51874. ExpectNotNull(rsa = RSA_new());
  51875. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  51876. NULL, NULL), 0);
  51877. RSA_free(rsa);
  51878. der = privDer;
  51879. rsa = NULL;
  51880. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51881. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  51882. NULL, NULL), 0);
  51883. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  51884. NULL, NULL), 0);
  51885. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  51886. NULL, NULL), 1);
  51887. #ifndef NO_AES
  51888. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  51889. NULL, 0, NULL, NULL), 1);
  51890. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  51891. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  51892. #endif
  51893. RSA_free(rsa);
  51894. #endif
  51895. return EXPECT_RESULT();
  51896. }
  51897. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  51898. {
  51899. EXPECT_DECLS;
  51900. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  51901. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  51902. RSA* rsa = NULL;
  51903. #ifdef USE_CERT_BUFFERS_1024
  51904. const unsigned char* privDer = client_key_der_1024;
  51905. size_t privDerSz = sizeof_client_key_der_1024;
  51906. #else
  51907. const unsigned char* privDer = client_key_der_2048;
  51908. size_t privDerSz = sizeof_client_key_der_2048;
  51909. #endif
  51910. const unsigned char* der;
  51911. #ifndef NO_AES
  51912. unsigned char passwd[] = "password";
  51913. #endif
  51914. unsigned char* pem = NULL;
  51915. int plen;
  51916. ExpectNotNull(rsa = RSA_new());
  51917. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51918. &plen), 0);
  51919. RSA_free(rsa);
  51920. der = privDer;
  51921. rsa = NULL;
  51922. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51923. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  51924. &plen), 0);
  51925. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  51926. &plen), 0);
  51927. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51928. NULL), 0);
  51929. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51930. &plen), 1);
  51931. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51932. pem = NULL;
  51933. #ifndef NO_AES
  51934. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  51935. NULL, 0, &pem, &plen), 1);
  51936. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51937. pem = NULL;
  51938. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  51939. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  51940. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51941. #endif
  51942. RSA_free(rsa);
  51943. #endif
  51944. return EXPECT_RESULT();
  51945. }
  51946. static int test_wolfSSL_DH(void)
  51947. {
  51948. EXPECT_DECLS;
  51949. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51950. DH *dh = NULL;
  51951. BIGNUM* p;
  51952. BIGNUM* q;
  51953. BIGNUM* g;
  51954. BIGNUM* pub = NULL;
  51955. BIGNUM* priv = NULL;
  51956. #if defined(OPENSSL_ALL)
  51957. #if !defined(HAVE_FIPS) || \
  51958. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  51959. FILE* f = NULL;
  51960. unsigned char buf[268];
  51961. const unsigned char* pt = buf;
  51962. long len = 0;
  51963. dh = NULL;
  51964. XMEMSET(buf, 0, sizeof(buf));
  51965. /* Test 2048 bit parameters */
  51966. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  51967. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51968. if (f != XBADFILE)
  51969. XFCLOSE(f);
  51970. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  51971. ExpectNotNull(dh->p);
  51972. ExpectNotNull(dh->g);
  51973. ExpectTrue(pt == buf);
  51974. ExpectIntEQ(DH_generate_key(dh), 1);
  51975. ExpectIntEQ(DH_generate_key(dh), 1);
  51976. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  51977. ExpectNotNull(pub = BN_new());
  51978. ExpectIntEQ(BN_set_word(pub, 1), 1);
  51979. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  51980. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  51981. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  51982. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  51983. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  51984. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  51985. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  51986. BN_free(pub);
  51987. pub = NULL;
  51988. DH_get0_pqg(dh, (const BIGNUM**)&p,
  51989. (const BIGNUM**)&q,
  51990. (const BIGNUM**)&g);
  51991. ExpectPtrEq(p, dh->p);
  51992. ExpectPtrEq(q, dh->q);
  51993. ExpectPtrEq(g, dh->g);
  51994. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  51995. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  51996. ExpectPtrEq(pub, dh->pub_key);
  51997. ExpectPtrEq(priv, dh->priv_key);
  51998. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  51999. ExpectPtrEq(pub, dh->pub_key);
  52000. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  52001. ExpectPtrEq(priv, dh->priv_key);
  52002. pub = NULL;
  52003. priv = NULL;
  52004. ExpectNotNull(pub = BN_new());
  52005. ExpectNotNull(priv = BN_new());
  52006. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  52007. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52008. if (EXPECT_FAIL()) {
  52009. BN_free(pub);
  52010. BN_free(priv);
  52011. }
  52012. pub = NULL;
  52013. priv = NULL;
  52014. ExpectNotNull(pub = BN_new());
  52015. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  52016. if (EXPECT_FAIL()) {
  52017. BN_free(pub);
  52018. }
  52019. ExpectNotNull(priv = BN_new());
  52020. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  52021. if (EXPECT_FAIL()) {
  52022. BN_free(priv);
  52023. }
  52024. ExpectPtrEq(pub, dh->pub_key);
  52025. ExpectPtrEq(priv, dh->priv_key);
  52026. pub = NULL;
  52027. priv = NULL;
  52028. DH_free(dh);
  52029. dh = NULL;
  52030. ExpectNotNull(dh = DH_new());
  52031. p = NULL;
  52032. ExpectNotNull(p = BN_new());
  52033. ExpectIntEQ(BN_set_word(p, 1), 1);
  52034. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52035. ExpectNotNull(pub = BN_new());
  52036. ExpectNotNull(priv = BN_new());
  52037. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52038. if (EXPECT_FAIL()) {
  52039. BN_free(pub);
  52040. BN_free(priv);
  52041. }
  52042. pub = NULL;
  52043. priv = NULL;
  52044. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52045. BN_free(p);
  52046. p = NULL;
  52047. DH_free(dh);
  52048. dh = NULL;
  52049. #ifdef WOLFSSL_KEY_GEN
  52050. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  52051. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  52052. DH_free(dh);
  52053. dh = NULL;
  52054. #endif
  52055. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  52056. #endif /* OPENSSL_ALL */
  52057. (void)dh;
  52058. (void)p;
  52059. (void)q;
  52060. (void)g;
  52061. (void)pub;
  52062. (void)priv;
  52063. ExpectNotNull(dh = wolfSSL_DH_new());
  52064. /* invalid parameters test */
  52065. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  52066. (const BIGNUM**)&q,
  52067. (const BIGNUM**)&g);
  52068. DH_get0_pqg(dh, NULL,
  52069. (const BIGNUM**)&q,
  52070. (const BIGNUM**)&g);
  52071. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  52072. DH_get0_pqg(dh, NULL, NULL, NULL);
  52073. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52074. (const BIGNUM**)&q,
  52075. (const BIGNUM**)&g);
  52076. ExpectPtrEq(p, NULL);
  52077. ExpectPtrEq(q, NULL);
  52078. ExpectPtrEq(g, NULL);
  52079. DH_free(dh);
  52080. dh = NULL;
  52081. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  52082. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  52083. #if defined(OPENSSL_ALL) || \
  52084. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52085. dh = wolfSSL_DH_new();
  52086. ExpectNotNull(dh);
  52087. p = wolfSSL_BN_new();
  52088. ExpectNotNull(p);
  52089. ExpectIntEQ(BN_set_word(p, 11), 1);
  52090. g = wolfSSL_BN_new();
  52091. ExpectNotNull(g);
  52092. ExpectIntEQ(BN_set_word(g, 2), 1);
  52093. q = wolfSSL_BN_new();
  52094. ExpectNotNull(q);
  52095. ExpectIntEQ(BN_set_word(q, 5), 1);
  52096. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  52097. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  52098. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  52099. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  52100. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  52101. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  52102. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  52103. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  52104. /* Don't need q. */
  52105. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52106. if (EXPECT_FAIL()) {
  52107. BN_free(p);
  52108. BN_free(g);
  52109. }
  52110. p = NULL;
  52111. g = NULL;
  52112. /* Setting again will free the p and g. */
  52113. wolfSSL_BN_free(q);
  52114. q = NULL;
  52115. DH_free(dh);
  52116. dh = NULL;
  52117. dh = wolfSSL_DH_new();
  52118. ExpectNotNull(dh);
  52119. p = wolfSSL_BN_new();
  52120. ExpectNotNull(p);
  52121. ExpectIntEQ(BN_set_word(p, 11), 1);
  52122. g = wolfSSL_BN_new();
  52123. ExpectNotNull(g);
  52124. ExpectIntEQ(BN_set_word(g, 2), 1);
  52125. q = wolfSSL_BN_new();
  52126. ExpectNotNull(q);
  52127. ExpectIntEQ(BN_set_word(q, 5), 1);
  52128. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  52129. /* p, q and g are now owned by dh - don't free. */
  52130. if (EXPECT_FAIL()) {
  52131. BN_free(p);
  52132. BN_free(q);
  52133. BN_free(g);
  52134. }
  52135. p = NULL;
  52136. q = NULL;
  52137. g = NULL;
  52138. p = wolfSSL_BN_new();
  52139. ExpectNotNull(p);
  52140. ExpectIntEQ(BN_set_word(p, 11), 1);
  52141. g = wolfSSL_BN_new();
  52142. ExpectNotNull(g);
  52143. ExpectIntEQ(BN_set_word(g, 2), 1);
  52144. q = wolfSSL_BN_new();
  52145. ExpectNotNull(q);
  52146. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  52147. if (EXPECT_FAIL()) {
  52148. BN_free(p);
  52149. }
  52150. p = NULL;
  52151. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  52152. if (EXPECT_FAIL()) {
  52153. BN_free(q);
  52154. }
  52155. q = NULL;
  52156. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  52157. if (EXPECT_FAIL()) {
  52158. BN_free(g);
  52159. }
  52160. g = NULL;
  52161. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  52162. /* p, q and g are now owned by dh - don't free. */
  52163. DH_free(dh);
  52164. dh = NULL;
  52165. ExpectIntEQ(DH_generate_key(NULL), 0);
  52166. ExpectNotNull(dh = DH_new());
  52167. ExpectIntEQ(DH_generate_key(dh), 0);
  52168. p = wolfSSL_BN_new();
  52169. ExpectNotNull(p);
  52170. ExpectIntEQ(BN_set_word(p, 0), 1);
  52171. g = wolfSSL_BN_new();
  52172. ExpectNotNull(g);
  52173. ExpectIntEQ(BN_set_word(g, 2), 1);
  52174. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52175. if (EXPECT_FAIL()) {
  52176. BN_free(p);
  52177. BN_free(g);
  52178. }
  52179. p = NULL;
  52180. g = NULL;
  52181. ExpectIntEQ(DH_generate_key(dh), 0);
  52182. DH_free(dh);
  52183. dh = NULL;
  52184. #endif
  52185. #endif
  52186. /* Test DH_up_ref() */
  52187. dh = wolfSSL_DH_new();
  52188. ExpectNotNull(dh);
  52189. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  52190. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  52191. DH_free(dh); /* decrease ref count */
  52192. DH_free(dh); /* free WOLFSSL_DH */
  52193. dh = NULL;
  52194. q = NULL;
  52195. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  52196. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  52197. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  52198. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  52199. #ifdef HAVE_FFDHE_2048
  52200. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52201. DH_free(dh);
  52202. dh = NULL;
  52203. q = NULL;
  52204. #endif
  52205. #ifdef HAVE_FFDHE_3072
  52206. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  52207. DH_free(dh);
  52208. dh = NULL;
  52209. q = NULL;
  52210. #endif
  52211. #ifdef HAVE_FFDHE_4096
  52212. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  52213. DH_free(dh);
  52214. dh = NULL;
  52215. q = NULL;
  52216. #endif
  52217. #else
  52218. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52219. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  52220. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  52221. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  52222. #endif /* OPENSSL_EXTRA && !NO_DH */
  52223. return EXPECT_RESULT();
  52224. }
  52225. static int test_wolfSSL_DH_dup(void)
  52226. {
  52227. EXPECT_DECLS;
  52228. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  52229. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  52230. defined(OPENSSL_EXTRA)
  52231. DH *dh = NULL;
  52232. DH *dhDup = NULL;
  52233. ExpectNotNull(dh = wolfSSL_DH_new());
  52234. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  52235. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  52236. #if defined(OPENSSL_ALL) || \
  52237. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52238. {
  52239. WOLFSSL_BIGNUM* p = NULL;
  52240. WOLFSSL_BIGNUM* g = NULL;
  52241. ExpectNotNull(p = wolfSSL_BN_new());
  52242. ExpectNotNull(g = wolfSSL_BN_new());
  52243. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  52244. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  52245. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52246. if (EXPECT_FAIL()) {
  52247. wolfSSL_BN_free(p);
  52248. wolfSSL_BN_free(g);
  52249. }
  52250. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  52251. wolfSSL_DH_free(dhDup);
  52252. }
  52253. #endif
  52254. wolfSSL_DH_free(dh);
  52255. #endif
  52256. #endif
  52257. return EXPECT_RESULT();
  52258. }
  52259. static int test_wolfSSL_DH_check(void)
  52260. {
  52261. EXPECT_DECLS;
  52262. #ifdef OPENSSL_ALL
  52263. #ifndef NO_DH
  52264. #ifndef NO_BIO
  52265. #ifndef NO_DSA
  52266. byte buf[6000];
  52267. char file[] = "./certs/dsaparams.pem";
  52268. XFILE f = XBADFILE;
  52269. int bytes;
  52270. BIO* bio = NULL;
  52271. DSA* dsa = NULL;
  52272. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  52273. static const byte dh2048[] = {
  52274. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  52275. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  52276. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  52277. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  52278. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  52279. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  52280. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  52281. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  52282. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  52283. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  52284. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  52285. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  52286. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  52287. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  52288. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  52289. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  52290. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  52291. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  52292. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  52293. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  52294. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  52295. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  52296. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  52297. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  52298. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  52299. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  52300. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  52301. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  52302. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  52303. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  52304. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  52305. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  52306. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  52307. 0x93, 0x02, 0x01, 0x02
  52308. };
  52309. const byte* params;
  52310. #endif
  52311. DH* dh = NULL;
  52312. WOLFSSL_BIGNUM* p = NULL;
  52313. WOLFSSL_BIGNUM* g = NULL;
  52314. WOLFSSL_BIGNUM* pTmp = NULL;
  52315. WOLFSSL_BIGNUM* gTmp = NULL;
  52316. int codes = -1;
  52317. #ifndef NO_DSA
  52318. /* Initialize DH */
  52319. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  52320. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  52321. if (f != XBADFILE)
  52322. XFCLOSE(f);
  52323. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  52324. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  52325. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  52326. ExpectNotNull(dh);
  52327. BIO_free(bio);
  52328. DSA_free(dsa);
  52329. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  52330. params = dh2048;
  52331. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  52332. (long)sizeof(dh2048)));
  52333. #else
  52334. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  52335. #endif
  52336. /* Test assumed to be valid dh.
  52337. * Should return WOLFSSL_SUCCESS
  52338. * codes should be 0
  52339. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  52340. */
  52341. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52342. ExpectIntEQ(codes, 0);
  52343. /* Test NULL dh: expected BAD_FUNC_ARG */
  52344. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  52345. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  52346. if (dh != NULL) {
  52347. pTmp = dh->p;
  52348. dh->p = NULL;
  52349. }
  52350. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52351. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  52352. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  52353. /* set dh->p back to normal so it won't fail on next tests */
  52354. if (dh != NULL) {
  52355. dh->p = pTmp;
  52356. pTmp = NULL;
  52357. }
  52358. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  52359. if (dh != NULL) {
  52360. gTmp = dh->g;
  52361. dh->g = NULL;
  52362. }
  52363. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52364. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  52365. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  52366. if (dh != NULL) {
  52367. dh->g = gTmp;
  52368. gTmp = NULL;
  52369. }
  52370. /* Cleanup */
  52371. DH_free(dh);
  52372. dh = NULL;
  52373. dh = DH_new();
  52374. ExpectNotNull(dh);
  52375. /* Check empty DH. */
  52376. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52377. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  52378. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  52379. /* Check non-prime valued p. */
  52380. ExpectNotNull(p = BN_new());
  52381. ExpectIntEQ(BN_set_word(p, 4), 1);
  52382. ExpectNotNull(g = BN_new());
  52383. ExpectIntEQ(BN_set_word(g, 2), 1);
  52384. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  52385. if (EXPECT_FAIL()) {
  52386. wolfSSL_BN_free(p);
  52387. wolfSSL_BN_free(g);
  52388. }
  52389. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52390. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  52391. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  52392. DH_free(dh);
  52393. dh = NULL;
  52394. #endif
  52395. #endif /* !NO_DH && !NO_DSA */
  52396. #endif
  52397. return EXPECT_RESULT();
  52398. }
  52399. static int test_wolfSSL_DH_prime(void)
  52400. {
  52401. EXPECT_DECLS;
  52402. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52403. WOLFSSL_BIGNUM* bn = NULL;
  52404. #if WOLFSSL_MAX_BN_BITS >= 768
  52405. WOLFSSL_BIGNUM* bn2 = NULL;
  52406. #endif
  52407. bn = wolfSSL_DH_768_prime(NULL);
  52408. #if WOLFSSL_MAX_BN_BITS >= 768
  52409. ExpectNotNull(bn);
  52410. bn2 = wolfSSL_DH_768_prime(bn);
  52411. ExpectNotNull(bn2);
  52412. ExpectTrue(bn == bn2);
  52413. wolfSSL_BN_free(bn);
  52414. bn = NULL;
  52415. #else
  52416. ExpectNull(bn);
  52417. #endif
  52418. bn = wolfSSL_DH_1024_prime(NULL);
  52419. #if WOLFSSL_MAX_BN_BITS >= 1024
  52420. ExpectNotNull(bn);
  52421. wolfSSL_BN_free(bn);
  52422. bn = NULL;
  52423. #else
  52424. ExpectNull(bn);
  52425. #endif
  52426. bn = wolfSSL_DH_2048_prime(NULL);
  52427. #if WOLFSSL_MAX_BN_BITS >= 2048
  52428. ExpectNotNull(bn);
  52429. wolfSSL_BN_free(bn);
  52430. bn = NULL;
  52431. #else
  52432. ExpectNull(bn);
  52433. #endif
  52434. bn = wolfSSL_DH_3072_prime(NULL);
  52435. #if WOLFSSL_MAX_BN_BITS >= 3072
  52436. ExpectNotNull(bn);
  52437. wolfSSL_BN_free(bn);
  52438. bn = NULL;
  52439. #else
  52440. ExpectNull(bn);
  52441. #endif
  52442. bn = wolfSSL_DH_4096_prime(NULL);
  52443. #if WOLFSSL_MAX_BN_BITS >= 4096
  52444. ExpectNotNull(bn);
  52445. wolfSSL_BN_free(bn);
  52446. bn = NULL;
  52447. #else
  52448. ExpectNull(bn);
  52449. #endif
  52450. bn = wolfSSL_DH_6144_prime(NULL);
  52451. #if WOLFSSL_MAX_BN_BITS >= 6144
  52452. ExpectNotNull(bn);
  52453. wolfSSL_BN_free(bn);
  52454. bn = NULL;
  52455. #else
  52456. ExpectNull(bn);
  52457. #endif
  52458. bn = wolfSSL_DH_8192_prime(NULL);
  52459. #if WOLFSSL_MAX_BN_BITS >= 8192
  52460. ExpectNotNull(bn);
  52461. wolfSSL_BN_free(bn);
  52462. bn = NULL;
  52463. #else
  52464. ExpectNull(bn);
  52465. #endif
  52466. #endif
  52467. return EXPECT_RESULT();
  52468. }
  52469. static int test_wolfSSL_DH_1536_prime(void)
  52470. {
  52471. EXPECT_DECLS;
  52472. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52473. BIGNUM* bn = NULL;
  52474. unsigned char bits[200];
  52475. int sz = 192; /* known binary size */
  52476. const byte expected[] = {
  52477. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  52478. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  52479. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  52480. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  52481. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  52482. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  52483. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  52484. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  52485. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  52486. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  52487. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  52488. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  52489. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  52490. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  52491. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  52492. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  52493. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  52494. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  52495. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  52496. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  52497. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  52498. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  52499. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  52500. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  52501. };
  52502. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  52503. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  52504. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  52505. BN_free(bn);
  52506. #endif
  52507. return EXPECT_RESULT();
  52508. }
  52509. static int test_wolfSSL_DH_get_2048_256(void)
  52510. {
  52511. EXPECT_DECLS;
  52512. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52513. WOLFSSL_DH* dh = NULL;
  52514. const WOLFSSL_BIGNUM* pBn;
  52515. const WOLFSSL_BIGNUM* gBn;
  52516. const WOLFSSL_BIGNUM* qBn;
  52517. const byte pExpected[] = {
  52518. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  52519. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  52520. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  52521. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  52522. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  52523. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  52524. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  52525. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  52526. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  52527. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  52528. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  52529. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  52530. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  52531. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  52532. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  52533. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  52534. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  52535. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  52536. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  52537. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  52538. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  52539. 0x1E, 0x1A, 0x15, 0x97
  52540. };
  52541. const byte gExpected[] = {
  52542. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  52543. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  52544. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  52545. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  52546. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  52547. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  52548. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  52549. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  52550. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  52551. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  52552. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  52553. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  52554. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  52555. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  52556. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  52557. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  52558. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  52559. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  52560. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  52561. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  52562. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  52563. 0x6C, 0xC4, 0x16, 0x59
  52564. };
  52565. const byte qExpected[] = {
  52566. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  52567. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  52568. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  52569. };
  52570. int pSz = 0;
  52571. int qSz = 0;
  52572. int gSz = 0;
  52573. byte* pReturned = NULL;
  52574. byte* qReturned = NULL;
  52575. byte* gReturned = NULL;
  52576. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  52577. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  52578. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  52579. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  52580. DYNAMIC_TYPE_TMP_BUFFER));
  52581. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  52582. ExpectIntEQ(pSz, sizeof(pExpected));
  52583. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  52584. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  52585. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  52586. DYNAMIC_TYPE_TMP_BUFFER));
  52587. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  52588. ExpectIntEQ(qSz, sizeof(qExpected));
  52589. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  52590. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  52591. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  52592. DYNAMIC_TYPE_TMP_BUFFER));
  52593. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  52594. ExpectIntEQ(gSz, sizeof(gExpected));
  52595. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  52596. wolfSSL_DH_free(dh);
  52597. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52598. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52599. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52600. #endif
  52601. return EXPECT_RESULT();
  52602. }
  52603. static int test_wolfSSL_PEM_write_DHparams(void)
  52604. {
  52605. EXPECT_DECLS;
  52606. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  52607. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  52608. DH* dh = NULL;
  52609. BIO* bio = NULL;
  52610. XFILE fp = XBADFILE;
  52611. byte pem[2048];
  52612. int pemSz = 0;
  52613. const char expected[] =
  52614. "-----BEGIN DH PARAMETERS-----\n"
  52615. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  52616. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  52617. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  52618. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  52619. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  52620. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  52621. "-----END DH PARAMETERS-----\n";
  52622. const char badPem[] =
  52623. "-----BEGIN DH PARAMETERS-----\n"
  52624. "-----END DH PARAMETERS-----\n";
  52625. const char emptySeqPem[] =
  52626. "-----BEGIN DH PARAMETERS-----\n"
  52627. "MAA=\n"
  52628. "-----END DH PARAMETERS-----\n";
  52629. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  52630. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  52631. if (fp != XBADFILE) {
  52632. XFCLOSE(fp);
  52633. fp = XBADFILE;
  52634. }
  52635. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  52636. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52637. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52638. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  52639. (int)sizeof(badPem));
  52640. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52641. BIO_free(bio);
  52642. bio = NULL;
  52643. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52644. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52645. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  52646. (int)sizeof(emptySeqPem));
  52647. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52648. BIO_free(bio);
  52649. bio = NULL;
  52650. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52651. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  52652. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52653. BIO_free(bio);
  52654. bio = NULL;
  52655. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  52656. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  52657. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  52658. DH_free(dh);
  52659. dh = NULL;
  52660. dh = wolfSSL_DH_new();
  52661. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  52662. if (fp != XBADFILE) {
  52663. XFCLOSE(fp);
  52664. fp = XBADFILE;
  52665. }
  52666. wolfSSL_DH_free(dh);
  52667. dh = NULL;
  52668. /* check results */
  52669. XMEMSET(pem, 0, sizeof(pem));
  52670. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  52671. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  52672. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  52673. if (fp != XBADFILE)
  52674. XFCLOSE(fp);
  52675. #endif
  52676. return EXPECT_RESULT();
  52677. }
  52678. static int test_wolfSSL_d2i_DHparams(void)
  52679. {
  52680. EXPECT_DECLS;
  52681. #ifdef OPENSSL_ALL
  52682. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  52683. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52684. XFILE f = XBADFILE;
  52685. unsigned char buf[4096];
  52686. const unsigned char* pt = buf;
  52687. #ifdef HAVE_FFDHE_2048
  52688. const char* params1 = "./certs/dh2048.der";
  52689. #endif
  52690. #ifdef HAVE_FFDHE_3072
  52691. const char* params2 = "./certs/dh3072.der";
  52692. #endif
  52693. long len = 0;
  52694. WOLFSSL_DH* dh = NULL;
  52695. XMEMSET(buf, 0, sizeof(buf));
  52696. /* Test 2048 bit parameters */
  52697. #ifdef HAVE_FFDHE_2048
  52698. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  52699. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52700. if (f != XBADFILE) {
  52701. XFCLOSE(f);
  52702. f = XBADFILE;
  52703. }
  52704. /* Valid case */
  52705. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52706. ExpectNotNull(dh->p);
  52707. ExpectNotNull(dh->g);
  52708. ExpectTrue(pt == buf);
  52709. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  52710. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  52711. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  52712. /* Invalid cases */
  52713. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  52714. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  52715. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  52716. DH_free(dh);
  52717. dh = NULL;
  52718. *buf = 0;
  52719. pt = buf;
  52720. #endif /* HAVE_FFDHE_2048 */
  52721. /* Test 3072 bit parameters */
  52722. #ifdef HAVE_FFDHE_3072
  52723. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  52724. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52725. if (f != XBADFILE) {
  52726. XFCLOSE(f);
  52727. f = XBADFILE;
  52728. }
  52729. /* Valid case */
  52730. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  52731. ExpectNotNull(dh->p);
  52732. ExpectNotNull(dh->g);
  52733. ExpectTrue(pt != buf);
  52734. ExpectIntEQ(DH_generate_key(dh), 1);
  52735. /* Invalid cases */
  52736. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  52737. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  52738. DH_free(dh);
  52739. dh = NULL;
  52740. #endif /* HAVE_FFDHE_3072 */
  52741. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52742. #endif /* !NO_DH */
  52743. #endif
  52744. return EXPECT_RESULT();
  52745. }
  52746. static int test_wolfSSL_DH_LoadDer(void)
  52747. {
  52748. EXPECT_DECLS;
  52749. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  52750. defined(OPENSSL_EXTRA)
  52751. static const byte dh2048[] = {
  52752. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  52753. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  52754. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  52755. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  52756. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  52757. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  52758. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  52759. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  52760. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  52761. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  52762. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  52763. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  52764. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  52765. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  52766. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  52767. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  52768. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  52769. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  52770. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  52771. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  52772. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  52773. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  52774. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  52775. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  52776. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  52777. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  52778. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  52779. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  52780. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  52781. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  52782. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  52783. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  52784. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  52785. 0x93, 0x02, 0x01, 0x02
  52786. };
  52787. WOLFSSL_DH* dh = NULL;
  52788. ExpectNotNull(dh = wolfSSL_DH_new());
  52789. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  52790. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  52791. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  52792. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  52793. wolfSSL_DH_free(dh);
  52794. #endif
  52795. return EXPECT_RESULT();
  52796. }
  52797. static int test_wolfSSL_i2d_DHparams(void)
  52798. {
  52799. EXPECT_DECLS;
  52800. #ifdef OPENSSL_ALL
  52801. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  52802. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52803. XFILE f = XBADFILE;
  52804. unsigned char buf[4096];
  52805. const unsigned char* pt;
  52806. unsigned char* pt2;
  52807. #ifdef HAVE_FFDHE_2048
  52808. const char* params1 = "./certs/dh2048.der";
  52809. #endif
  52810. #ifdef HAVE_FFDHE_3072
  52811. const char* params2 = "./certs/dh3072.der";
  52812. #endif
  52813. long len = 0;
  52814. WOLFSSL_DH* dh = NULL;
  52815. /* Test 2048 bit parameters */
  52816. #ifdef HAVE_FFDHE_2048
  52817. pt = buf;
  52818. pt2 = buf;
  52819. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  52820. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52821. if (f != XBADFILE) {
  52822. XFCLOSE(f);
  52823. f = XBADFILE;
  52824. }
  52825. /* Valid case */
  52826. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52827. ExpectTrue(pt == buf);
  52828. ExpectIntEQ(DH_generate_key(dh), 1);
  52829. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  52830. /* Invalid case */
  52831. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  52832. /* Return length only */
  52833. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  52834. DH_free(dh);
  52835. dh = NULL;
  52836. *buf = 0;
  52837. #endif
  52838. /* Test 3072 bit parameters */
  52839. #ifdef HAVE_FFDHE_3072
  52840. pt = buf;
  52841. pt2 = buf;
  52842. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  52843. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52844. if (f != XBADFILE) {
  52845. XFCLOSE(f);
  52846. f = XBADFILE;
  52847. }
  52848. /* Valid case */
  52849. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52850. ExpectTrue(pt == buf);
  52851. ExpectIntEQ(DH_generate_key(dh), 1);
  52852. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  52853. /* Invalid case */
  52854. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  52855. /* Return length only */
  52856. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  52857. DH_free(dh);
  52858. dh = NULL;
  52859. #endif
  52860. dh = DH_new();
  52861. ExpectNotNull(dh);
  52862. pt2 = buf;
  52863. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  52864. DH_free(dh);
  52865. dh = NULL;
  52866. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52867. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  52868. #endif
  52869. return EXPECT_RESULT();
  52870. }
  52871. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  52872. /*----------------------------------------------------------------------------*
  52873. | EC
  52874. *----------------------------------------------------------------------------*/
  52875. static int test_wolfSSL_EC_GROUP(void)
  52876. {
  52877. EXPECT_DECLS;
  52878. #ifdef OPENSSL_EXTRA
  52879. EC_GROUP *group = NULL;
  52880. EC_GROUP *group2 = NULL;
  52881. EC_GROUP *group3 = NULL;
  52882. #ifndef HAVE_ECC_BRAINPOOL
  52883. EC_GROUP *group4 = NULL;
  52884. #endif
  52885. WOLFSSL_BIGNUM* order = NULL;
  52886. int group_bits;
  52887. int i;
  52888. static const int knownEccNids[] = {
  52889. NID_X9_62_prime192v1,
  52890. NID_X9_62_prime192v2,
  52891. NID_X9_62_prime192v3,
  52892. NID_X9_62_prime239v1,
  52893. NID_X9_62_prime239v2,
  52894. NID_X9_62_prime239v3,
  52895. NID_X9_62_prime256v1,
  52896. NID_secp112r1,
  52897. NID_secp112r2,
  52898. NID_secp128r1,
  52899. NID_secp128r2,
  52900. NID_secp160r1,
  52901. NID_secp160r2,
  52902. NID_secp224r1,
  52903. NID_secp384r1,
  52904. NID_secp521r1,
  52905. NID_secp160k1,
  52906. NID_secp192k1,
  52907. NID_secp224k1,
  52908. NID_secp256k1,
  52909. NID_brainpoolP160r1,
  52910. NID_brainpoolP192r1,
  52911. NID_brainpoolP224r1,
  52912. NID_brainpoolP256r1,
  52913. NID_brainpoolP320r1,
  52914. NID_brainpoolP384r1,
  52915. NID_brainpoolP512r1,
  52916. };
  52917. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  52918. static const int knownEccEnums[] = {
  52919. ECC_SECP192R1,
  52920. ECC_PRIME192V2,
  52921. ECC_PRIME192V3,
  52922. ECC_PRIME239V1,
  52923. ECC_PRIME239V2,
  52924. ECC_PRIME239V3,
  52925. ECC_SECP256R1,
  52926. ECC_SECP112R1,
  52927. ECC_SECP112R2,
  52928. ECC_SECP128R1,
  52929. ECC_SECP128R2,
  52930. ECC_SECP160R1,
  52931. ECC_SECP160R2,
  52932. ECC_SECP224R1,
  52933. ECC_SECP384R1,
  52934. ECC_SECP521R1,
  52935. ECC_SECP160K1,
  52936. ECC_SECP192K1,
  52937. ECC_SECP224K1,
  52938. ECC_SECP256K1,
  52939. ECC_BRAINPOOLP160R1,
  52940. ECC_BRAINPOOLP192R1,
  52941. ECC_BRAINPOOLP224R1,
  52942. ECC_BRAINPOOLP256R1,
  52943. ECC_BRAINPOOLP320R1,
  52944. ECC_BRAINPOOLP384R1,
  52945. ECC_BRAINPOOLP512R1,
  52946. };
  52947. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  52948. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52949. ExpectNotNull(group2 = EC_GROUP_dup(group));
  52950. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  52951. #ifndef HAVE_ECC_BRAINPOOL
  52952. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  52953. NID_brainpoolP256r1));
  52954. #endif
  52955. ExpectNull(EC_GROUP_dup(NULL));
  52956. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  52957. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  52958. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  52959. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  52960. #ifndef HAVE_ECC_BRAINPOOL
  52961. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  52962. #endif
  52963. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  52964. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  52965. ExpectNotNull(order = BN_new());
  52966. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  52967. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  52968. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  52969. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  52970. wolfSSL_BN_free(order);
  52971. ExpectNotNull(EC_GROUP_method_of(group));
  52972. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  52973. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  52974. NID_X9_62_prime_field);
  52975. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  52976. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  52977. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  52978. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  52979. #ifndef NO_WOLFSSL_STUB
  52980. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  52981. #endif
  52982. #ifndef HAVE_ECC_BRAINPOOL
  52983. EC_GROUP_free(group4);
  52984. #endif
  52985. EC_GROUP_free(group3);
  52986. EC_GROUP_free(group2);
  52987. EC_GROUP_free(group);
  52988. for (i = 0; i < knowEccNidsLen; i++) {
  52989. group = NULL;
  52990. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  52991. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  52992. EC_GROUP_free(group);
  52993. }
  52994. for (i = 0; i < knowEccEnumsLen; i++) {
  52995. group = NULL;
  52996. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  52997. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  52998. EC_GROUP_free(group);
  52999. }
  53000. #endif
  53001. return EXPECT_RESULT();
  53002. }
  53003. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  53004. {
  53005. EXPECT_DECLS;
  53006. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53007. EC_GROUP *group = NULL;
  53008. BIO* bio = NULL;
  53009. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53010. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53011. EC_GROUP *ret = NULL;
  53012. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  53013. "BgUrgQQAIg==\n"
  53014. "-----END EC PARAMETERS-----";
  53015. #endif
  53016. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  53017. "MAA=\n"
  53018. "-----END EC PARAMETERS-----";
  53019. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  53020. "BgA=\n"
  53021. "-----END EC PARAMETERS-----";
  53022. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  53023. "BgE=\n"
  53024. "-----END EC PARAMETERS-----";
  53025. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  53026. "BgE*\n"
  53027. "-----END EC PARAMETERS-----";
  53028. /* Test that first parameter, bio, being NULL fails. */
  53029. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  53030. /* Test that reading named parameters works. */
  53031. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  53032. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  53033. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53034. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53035. BIO_free(bio);
  53036. bio = NULL;
  53037. EC_GROUP_free(group);
  53038. group = NULL;
  53039. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53040. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53041. /* Test that reusing group works. */
  53042. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53043. sizeof(ec_nc_p384)));
  53044. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53045. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53046. BIO_free(bio);
  53047. bio = NULL;
  53048. EC_GROUP_free(group);
  53049. group = NULL;
  53050. /* Test that returning through group works. */
  53051. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53052. sizeof(ec_nc_p384)));
  53053. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53054. ExpectIntEQ(group == ret, 1);
  53055. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53056. BIO_free(bio);
  53057. bio = NULL;
  53058. EC_GROUP_free(group);
  53059. group = NULL;
  53060. #endif
  53061. /* Test 0x30, 0x00 (not and object id) fails. */
  53062. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  53063. sizeof(ec_nc_bad_1)));
  53064. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53065. BIO_free(bio);
  53066. bio = NULL;
  53067. /* Test 0x06, 0x00 (empty object id) fails. */
  53068. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  53069. sizeof(ec_nc_bad_2)));
  53070. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53071. BIO_free(bio);
  53072. bio = NULL;
  53073. /* Test 0x06, 0x01 (badly formed object id) fails. */
  53074. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  53075. sizeof(ec_nc_bad_3)));
  53076. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53077. BIO_free(bio);
  53078. bio = NULL;
  53079. /* Test invalid PEM encoding - invalid character. */
  53080. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  53081. sizeof(ec_nc_bad_4)));
  53082. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53083. BIO_free(bio);
  53084. #endif
  53085. return EXPECT_RESULT();
  53086. }
  53087. static int test_wolfSSL_EC_POINT(void)
  53088. {
  53089. EXPECT_DECLS;
  53090. #if !defined(WOLFSSL_SP_MATH) && \
  53091. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  53092. #ifdef OPENSSL_EXTRA
  53093. BN_CTX* ctx = NULL;
  53094. EC_GROUP* group = NULL;
  53095. #ifndef HAVE_ECC_BRAINPOOL
  53096. EC_GROUP* group2 = NULL;
  53097. #endif
  53098. EC_POINT* Gxy = NULL;
  53099. EC_POINT* new_point = NULL;
  53100. EC_POINT* set_point = NULL;
  53101. EC_POINT* infinity = NULL;
  53102. BIGNUM* k = NULL;
  53103. BIGNUM* Gx = NULL;
  53104. BIGNUM* Gy = NULL;
  53105. BIGNUM* Gz = NULL;
  53106. BIGNUM* X = NULL;
  53107. BIGNUM* Y = NULL;
  53108. BIGNUM* set_point_bn = NULL;
  53109. char* hexStr = NULL;
  53110. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  53111. "17AF381913FF7A96314EA47055EA0FD0";
  53112. /* NISTP256R1 Gx/Gy */
  53113. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  53114. "77037D812DEB33A0F4A13945D898C296";
  53115. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53116. "2BCE33576B315ECECBB6406837BF51F5";
  53117. #ifndef HAVE_SELFTEST
  53118. EC_POINT *tmp = NULL;
  53119. size_t bin_len;
  53120. unsigned int blen = 0;
  53121. unsigned char* buf = NULL;
  53122. unsigned char bufInf[1] = { 0x00 };
  53123. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  53124. "77037D812DEB33A0F4A13945D898C296"
  53125. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53126. "2BCE33576B315ECECBB6406837BF51F5";
  53127. const unsigned char binUncompG[] = {
  53128. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53129. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53130. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53131. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53132. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53133. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53134. };
  53135. const unsigned char binUncompGBad[] = {
  53136. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53137. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53138. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53139. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53140. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53141. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53142. };
  53143. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  53144. "77037D812DEB33A0F4A13945D898C296";
  53145. #ifdef HAVE_COMP_KEY
  53146. const unsigned char binCompG[] = {
  53147. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53148. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53149. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53150. };
  53151. #endif
  53152. #endif
  53153. ExpectNotNull(ctx = BN_CTX_new());
  53154. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53155. #ifndef HAVE_ECC_BRAINPOOL
  53156. /* Used to make groups curve_idx == -1. */
  53157. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  53158. #endif
  53159. ExpectNull(EC_POINT_new(NULL));
  53160. ExpectNotNull(Gxy = EC_POINT_new(group));
  53161. ExpectNotNull(new_point = EC_POINT_new(group));
  53162. ExpectNotNull(set_point = EC_POINT_new(group));
  53163. ExpectNotNull(X = BN_new());
  53164. ExpectNotNull(Y = BN_new());
  53165. ExpectNotNull(set_point_bn = BN_new());
  53166. ExpectNotNull(infinity = EC_POINT_new(group));
  53167. /* load test values */
  53168. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  53169. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  53170. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  53171. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  53172. /* populate coordinates for input point */
  53173. if (Gxy != NULL) {
  53174. Gxy->X = Gx;
  53175. Gxy->Y = Gy;
  53176. Gxy->Z = Gz;
  53177. }
  53178. /* Test handling of NULL point. */
  53179. EC_POINT_clear_free(NULL);
  53180. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53181. NULL, NULL, ctx), 0);
  53182. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53183. NULL, NULL, ctx), 0);
  53184. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53185. NULL, NULL, ctx), 0);
  53186. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53187. X, NULL, ctx), 0);
  53188. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53189. NULL, Y, ctx), 0);
  53190. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53191. X, Y, ctx), 0);
  53192. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53193. X, Y, ctx), 0);
  53194. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53195. NULL, Y, ctx), 0);
  53196. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53197. X, NULL, ctx), 0);
  53198. /* Getting point at infinity returns an error. */
  53199. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  53200. X, Y, ctx), 0);
  53201. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53202. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53203. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53204. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  53205. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  53206. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  53207. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  53208. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  53209. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  53210. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  53211. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  53212. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  53213. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  53214. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  53215. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  53216. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  53217. /* perform point multiplication */
  53218. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  53219. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53220. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53221. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53222. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  53223. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53224. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53225. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53226. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  53227. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53228. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53229. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53230. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  53231. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  53232. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  53233. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  53234. /* Set point to something. */
  53235. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  53236. #else
  53237. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  53238. ctx), 1);
  53239. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53240. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53241. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53242. #endif
  53243. /* check if point X coordinate is zero */
  53244. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53245. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  53246. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  53247. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  53248. #endif
  53249. /* extract the coordinates from point */
  53250. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  53251. ctx), WOLFSSL_SUCCESS);
  53252. /* check if point X coordinate is zero */
  53253. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  53254. /* set the same X and Y points in another object */
  53255. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  53256. ctx), WOLFSSL_SUCCESS);
  53257. /* compare points as they should be the same */
  53258. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  53259. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  53260. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  53261. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  53262. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  53263. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  53264. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  53265. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  53266. /* Test copying */
  53267. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  53268. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  53269. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  53270. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  53271. /* Test inverting */
  53272. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  53273. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  53274. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  53275. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  53276. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53277. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53278. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53279. {
  53280. EC_POINT* orig_point = NULL;
  53281. ExpectNotNull(orig_point = EC_POINT_new(group));
  53282. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  53283. 1);
  53284. /* new_point should be set_point inverted so adding it will revert
  53285. * the point back to set_point */
  53286. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  53287. NULL), 1);
  53288. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  53289. EC_POINT_free(orig_point);
  53290. }
  53291. #endif
  53292. /* Test getting affine converts from projective. */
  53293. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  53294. /* Force non-affine coordinates */
  53295. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  53296. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  53297. if (new_point != NULL) {
  53298. new_point->inSet = 0;
  53299. }
  53300. /* extract the coordinates from point */
  53301. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  53302. ctx), WOLFSSL_SUCCESS);
  53303. /* check if point ordinates have changed. */
  53304. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  53305. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  53306. /* Test check for infinity */
  53307. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  53308. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  53309. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  53310. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  53311. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  53312. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  53313. #else
  53314. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  53315. #endif
  53316. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  53317. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  53318. /* check bn2hex */
  53319. hexStr = BN_bn2hex(k);
  53320. ExpectStrEQ(hexStr, kTest);
  53321. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  53322. defined(XFPRINTF)
  53323. BN_print_fp(stderr, k);
  53324. fprintf(stderr, "\n");
  53325. #endif
  53326. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53327. hexStr = BN_bn2hex(Gx);
  53328. ExpectStrEQ(hexStr, kGx);
  53329. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  53330. defined(XFPRINTF)
  53331. BN_print_fp(stderr, Gx);
  53332. fprintf(stderr, "\n");
  53333. #endif
  53334. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53335. hexStr = BN_bn2hex(Gy);
  53336. ExpectStrEQ(hexStr, kGy);
  53337. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  53338. defined(XFPRINTF)
  53339. BN_print_fp(stderr, Gy);
  53340. fprintf(stderr, "\n");
  53341. #endif
  53342. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53343. #ifndef HAVE_SELFTEST
  53344. /* Test point to hex */
  53345. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  53346. ctx));
  53347. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53348. ctx));
  53349. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  53350. ctx));
  53351. #ifndef HAVE_ECC_BRAINPOOL
  53352. /* Group not supported in wolfCrypt. */
  53353. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53354. ctx));
  53355. #endif
  53356. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  53357. ExpectNotNull(hexStr);
  53358. ExpectStrEQ(hexStr, uncompG);
  53359. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53360. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  53361. ExpectNotNull(hexStr);
  53362. ExpectStrEQ(hexStr, compG);
  53363. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53364. /* Test point to oct */
  53365. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  53366. NULL, 0, ctx), 0);
  53367. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53368. NULL, 0, ctx), 0);
  53369. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  53370. NULL, 0, ctx), 0);
  53371. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53372. NULL, 0, ctx);
  53373. ExpectIntEQ(bin_len, sizeof(binUncompG));
  53374. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  53375. DYNAMIC_TYPE_ECC));
  53376. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53377. buf, bin_len, ctx), bin_len);
  53378. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  53379. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  53380. /* Infinity (x=0, y=0) encodes as '0x00'. */
  53381. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  53382. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  53383. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  53384. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  53385. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  53386. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  53387. ExpectIntEQ(bufInf[0], 0);
  53388. wolfSSL_EC_POINT_dump(NULL, NULL);
  53389. /* Test point i2d */
  53390. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  53391. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  53392. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  53393. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  53394. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  53395. ExpectIntEQ(blen, sizeof(binUncompG));
  53396. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  53397. blen -= 1;
  53398. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  53399. blen += 1;
  53400. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  53401. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  53402. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  53403. #ifdef HAVE_COMP_KEY
  53404. /* Test point to oct compressed */
  53405. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  53406. 0, ctx);
  53407. ExpectIntEQ(bin_len, sizeof(binCompG));
  53408. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  53409. DYNAMIC_TYPE_ECC));
  53410. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  53411. bin_len, ctx), bin_len);
  53412. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  53413. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  53414. #endif
  53415. /* Test point BN */
  53416. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  53417. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  53418. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  53419. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  53420. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  53421. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  53422. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  53423. /* Test oct to point */
  53424. ExpectNotNull(tmp = EC_POINT_new(group));
  53425. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  53426. ctx), 0);
  53427. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  53428. ctx), 0);
  53429. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  53430. ctx), 0);
  53431. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  53432. sizeof(binUncompGBad), ctx), 0);
  53433. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  53434. ctx), 1);
  53435. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  53436. EC_POINT_free(tmp);
  53437. tmp = NULL;
  53438. /* Test setting BN ordinates. */
  53439. ExpectNotNull(tmp = EC_POINT_new(group));
  53440. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  53441. NULL, ctx), 0);
  53442. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  53443. NULL, ctx), 0);
  53444. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  53445. NULL, ctx), 0);
  53446. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  53447. NULL, ctx), 0);
  53448. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  53449. Gy, ctx), 0);
  53450. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  53451. ctx), 0);
  53452. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  53453. ctx), 0);
  53454. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  53455. Gy, ctx), 0);
  53456. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  53457. NULL, ctx), 0);
  53458. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  53459. ctx), 1);
  53460. EC_POINT_free(tmp);
  53461. tmp = NULL;
  53462. /* Test point d2i */
  53463. ExpectNotNull(tmp = EC_POINT_new(group));
  53464. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  53465. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  53466. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  53467. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  53468. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  53469. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  53470. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  53471. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  53472. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  53473. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  53474. EC_POINT_free(tmp);
  53475. tmp = NULL;
  53476. #ifdef HAVE_COMP_KEY
  53477. /* Test oct compressed to point */
  53478. ExpectNotNull(tmp = EC_POINT_new(group));
  53479. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  53480. 1);
  53481. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  53482. EC_POINT_free(tmp);
  53483. tmp = NULL;
  53484. /* Test point d2i - compressed */
  53485. ExpectNotNull(tmp = EC_POINT_new(group));
  53486. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  53487. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  53488. EC_POINT_free(tmp);
  53489. tmp = NULL;
  53490. #endif
  53491. #endif
  53492. /* test BN_mod_add */
  53493. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  53494. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  53495. 1);
  53496. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  53497. /* cleanup */
  53498. BN_free(X);
  53499. BN_free(Y);
  53500. BN_free(k);
  53501. BN_free(set_point_bn);
  53502. EC_POINT_free(infinity);
  53503. EC_POINT_free(new_point);
  53504. EC_POINT_free(set_point);
  53505. EC_POINT_clear_free(Gxy);
  53506. #ifndef HAVE_ECC_BRAINPOOL
  53507. EC_GROUP_free(group2);
  53508. #endif
  53509. EC_GROUP_free(group);
  53510. BN_CTX_free(ctx);
  53511. #endif
  53512. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  53513. return EXPECT_RESULT();
  53514. }
  53515. static int test_wolfSSL_SPAKE(void)
  53516. {
  53517. EXPECT_DECLS;
  53518. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  53519. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  53520. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53521. BIGNUM* x = NULL; /* kdc priv */
  53522. BIGNUM* y = NULL; /* client priv */
  53523. BIGNUM* w = NULL; /* shared value */
  53524. byte M_bytes[] = {
  53525. /* uncompressed */
  53526. 0x04,
  53527. /* x */
  53528. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  53529. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  53530. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  53531. /* y */
  53532. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  53533. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  53534. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  53535. };
  53536. EC_POINT* M = NULL; /* shared value */
  53537. byte N_bytes[] = {
  53538. /* uncompressed */
  53539. 0x04,
  53540. /* x */
  53541. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  53542. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  53543. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  53544. /* y */
  53545. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  53546. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  53547. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  53548. };
  53549. EC_POINT* N = NULL; /* shared value */
  53550. EC_POINT* T = NULL; /* kdc pub */
  53551. EC_POINT* tmp1 = NULL; /* kdc pub */
  53552. EC_POINT* tmp2 = NULL; /* kdc pub */
  53553. EC_POINT* S = NULL; /* client pub */
  53554. EC_POINT* client_secret = NULL;
  53555. EC_POINT* kdc_secret = NULL;
  53556. EC_GROUP* group = NULL;
  53557. BN_CTX* bn_ctx = NULL;
  53558. /* Values taken from a test run of Kerberos 5 */
  53559. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53560. ExpectNotNull(bn_ctx = BN_CTX_new());
  53561. ExpectNotNull(M = EC_POINT_new(group));
  53562. ExpectNotNull(N = EC_POINT_new(group));
  53563. ExpectNotNull(T = EC_POINT_new(group));
  53564. ExpectNotNull(tmp1 = EC_POINT_new(group));
  53565. ExpectNotNull(tmp2 = EC_POINT_new(group));
  53566. ExpectNotNull(S = EC_POINT_new(group));
  53567. ExpectNotNull(client_secret = EC_POINT_new(group));
  53568. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  53569. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  53570. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  53571. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  53572. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  53573. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  53574. "506AB395478F0AAB647752CF117B36250"), 1);
  53575. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  53576. 1);
  53577. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  53578. 1);
  53579. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  53580. /* kdc */
  53581. /* T=x*P+w*M */
  53582. /* All in one function call */
  53583. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  53584. /* Spread into separate calls */
  53585. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  53586. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  53587. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  53588. 1);
  53589. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  53590. /* client */
  53591. /* S=y*P+w*N */
  53592. /* All in one function call */
  53593. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  53594. /* Spread into separate calls */
  53595. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  53596. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  53597. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  53598. 1);
  53599. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  53600. /* K=y*(T-w*M) */
  53601. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  53602. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  53603. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  53604. 1);
  53605. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  53606. bn_ctx), 1);
  53607. /* kdc */
  53608. /* K=x*(S-w*N) */
  53609. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  53610. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  53611. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  53612. 1);
  53613. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  53614. 1);
  53615. /* kdc_secret == client_secret */
  53616. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  53617. BN_free(x);
  53618. BN_free(y);
  53619. BN_free(w);
  53620. EC_POINT_free(M);
  53621. EC_POINT_free(N);
  53622. EC_POINT_free(T);
  53623. EC_POINT_free(tmp1);
  53624. EC_POINT_free(tmp2);
  53625. EC_POINT_free(S);
  53626. EC_POINT_free(client_secret);
  53627. EC_POINT_free(kdc_secret);
  53628. EC_GROUP_free(group);
  53629. BN_CTX_free(bn_ctx);
  53630. #endif
  53631. return EXPECT_RESULT();
  53632. }
  53633. static int test_wolfSSL_EC_KEY_generate(void)
  53634. {
  53635. EXPECT_DECLS;
  53636. #ifdef OPENSSL_EXTRA
  53637. WOLFSSL_EC_KEY* key = NULL;
  53638. #ifndef HAVE_ECC_BRAINPOOL
  53639. WOLFSSL_EC_GROUP* group = NULL;
  53640. #endif
  53641. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53642. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  53643. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  53644. wolfSSL_EC_KEY_free(key);
  53645. key = NULL;
  53646. #ifndef HAVE_ECC_BRAINPOOL
  53647. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  53648. NID_brainpoolP256r1));
  53649. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  53650. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  53651. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  53652. wolfSSL_EC_KEY_free(key);
  53653. wolfSSL_EC_GROUP_free(group);
  53654. #endif
  53655. #endif
  53656. return EXPECT_RESULT();
  53657. }
  53658. static int test_EC_i2d(void)
  53659. {
  53660. EXPECT_DECLS;
  53661. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  53662. EC_KEY *key = NULL;
  53663. EC_KEY *copy = NULL;
  53664. int len = 0;
  53665. unsigned char *buf = NULL;
  53666. unsigned char *p = NULL;
  53667. const unsigned char *tmp = NULL;
  53668. const unsigned char octBad[] = {
  53669. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53670. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53671. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53672. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53673. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53674. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53675. };
  53676. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53677. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  53678. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  53679. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53680. DYNAMIC_TYPE_TMP_BUFFER));
  53681. p = buf;
  53682. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  53683. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  53684. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  53685. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  53686. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  53687. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  53688. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  53689. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  53690. tmp = buf;
  53691. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  53692. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  53693. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  53694. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  53695. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  53696. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  53697. tmp = buf;
  53698. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  53699. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  53700. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  53701. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  53702. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  53703. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  53704. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  53705. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  53706. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  53707. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  53708. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  53709. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  53710. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  53711. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53712. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  53713. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53714. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  53715. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53716. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  53717. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53718. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  53719. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  53720. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  53721. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  53722. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  53723. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  53724. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53725. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53726. buf = NULL;
  53727. buf = NULL;
  53728. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  53729. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53730. DYNAMIC_TYPE_TMP_BUFFER));
  53731. p = buf;
  53732. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  53733. p = NULL;
  53734. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  53735. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53736. p = NULL;
  53737. /* Bad point is also an invalid private key. */
  53738. tmp = octBad;
  53739. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  53740. tmp = buf;
  53741. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  53742. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53743. buf = NULL;
  53744. buf = NULL;
  53745. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  53746. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53747. DYNAMIC_TYPE_TMP_BUFFER));
  53748. p = buf;
  53749. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  53750. p = NULL;
  53751. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  53752. tmp = buf;
  53753. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  53754. tmp = octBad;
  53755. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  53756. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  53757. ExpectIntEQ(EC_KEY_check_key(key), 1);
  53758. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  53759. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  53760. EC_KEY_free(key);
  53761. EC_KEY_free(copy);
  53762. #endif
  53763. return EXPECT_RESULT();
  53764. }
  53765. static int test_wolfSSL_EC_curve(void)
  53766. {
  53767. EXPECT_DECLS;
  53768. #if defined(OPENSSL_EXTRA)
  53769. int nid = NID_secp160k1;
  53770. const char* nid_name = NULL;
  53771. ExpectNull(EC_curve_nid2nist(NID_sha256));
  53772. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  53773. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  53774. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  53775. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  53776. #endif
  53777. return EXPECT_RESULT();
  53778. }
  53779. static int test_wolfSSL_EC_KEY_dup(void)
  53780. {
  53781. EXPECT_DECLS;
  53782. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  53783. WOLFSSL_EC_KEY* ecKey = NULL;
  53784. WOLFSSL_EC_KEY* dupKey = NULL;
  53785. ecc_key* srcKey = NULL;
  53786. ecc_key* destKey = NULL;
  53787. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53788. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53789. /* Valid cases */
  53790. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53791. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  53792. /* Compare pubkey */
  53793. if (ecKey != NULL) {
  53794. srcKey = (ecc_key*)ecKey->internal;
  53795. }
  53796. if (dupKey != NULL) {
  53797. destKey = (ecc_key*)dupKey->internal;
  53798. }
  53799. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  53800. /* compare EC_GROUP */
  53801. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  53802. /* compare EC_POINT */
  53803. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  53804. dupKey->pub_key, NULL), MP_EQ);
  53805. /* compare BIGNUM */
  53806. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  53807. wolfSSL_EC_KEY_free(dupKey);
  53808. dupKey = NULL;
  53809. /* Invalid cases */
  53810. /* NULL key */
  53811. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  53812. /* NULL ecc_key */
  53813. if (ecKey != NULL) {
  53814. wc_ecc_free((ecc_key*)ecKey->internal);
  53815. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  53816. ecKey->internal = NULL; /* Set ecc_key to NULL */
  53817. }
  53818. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53819. wolfSSL_EC_KEY_free(ecKey);
  53820. ecKey = NULL;
  53821. wolfSSL_EC_KEY_free(dupKey);
  53822. dupKey = NULL;
  53823. /* NULL Group */
  53824. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53825. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53826. if (ecKey != NULL) {
  53827. wolfSSL_EC_GROUP_free(ecKey->group);
  53828. ecKey->group = NULL; /* Set group to NULL */
  53829. }
  53830. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53831. wolfSSL_EC_KEY_free(ecKey);
  53832. ecKey = NULL;
  53833. wolfSSL_EC_KEY_free(dupKey);
  53834. dupKey = NULL;
  53835. /* NULL public key */
  53836. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53837. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53838. if (ecKey != NULL) {
  53839. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  53840. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  53841. }
  53842. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53843. if (ecKey != NULL) {
  53844. wolfSSL_EC_POINT_free(ecKey->pub_key);
  53845. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  53846. }
  53847. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53848. wolfSSL_EC_KEY_free(ecKey);
  53849. ecKey = NULL;
  53850. wolfSSL_EC_KEY_free(dupKey);
  53851. dupKey = NULL;
  53852. /* NULL private key */
  53853. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53854. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53855. if (ecKey != NULL) {
  53856. wolfSSL_BN_free(ecKey->priv_key);
  53857. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  53858. }
  53859. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53860. wolfSSL_EC_KEY_free(ecKey);
  53861. ecKey = NULL;
  53862. wolfSSL_EC_KEY_free(dupKey);
  53863. dupKey = NULL;
  53864. /* Test EC_KEY_up_ref */
  53865. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53866. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  53867. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  53868. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  53869. /* reference count doesn't follow duplicate */
  53870. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53871. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  53872. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  53873. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  53874. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  53875. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  53876. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  53877. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  53878. #endif
  53879. return EXPECT_RESULT();
  53880. }
  53881. static int test_wolfSSL_EC_KEY_set_group(void)
  53882. {
  53883. EXPECT_DECLS;
  53884. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  53885. defined(OPENSSL_EXTRA)
  53886. EC_KEY *key = NULL;
  53887. EC_GROUP *group = NULL;
  53888. const EC_GROUP *group2 = NULL;
  53889. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53890. ExpectNotNull(key = EC_KEY_new());
  53891. ExpectNull(EC_KEY_get0_group(NULL));
  53892. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  53893. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  53894. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  53895. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  53896. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  53897. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  53898. EC_GROUP_free(group);
  53899. EC_KEY_free(key);
  53900. #endif
  53901. return EXPECT_RESULT();
  53902. }
  53903. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  53904. {
  53905. EXPECT_DECLS;
  53906. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53907. BIO* bio = NULL;
  53908. EC_KEY* key = NULL;
  53909. /* Error condition: NULL key. */
  53910. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  53911. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  53912. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  53913. /* Conversion form defaults to uncompressed. */
  53914. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53915. #ifdef HAVE_COMP_KEY
  53916. /* Explicitly set to compressed. */
  53917. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  53918. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  53919. #else
  53920. /* Will still work just won't change anything. */
  53921. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  53922. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53923. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  53924. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53925. #endif
  53926. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  53927. BIO_free(bio);
  53928. EC_KEY_free(key);
  53929. #endif
  53930. return EXPECT_RESULT();
  53931. }
  53932. static int test_wolfSSL_EC_KEY_private_key(void)
  53933. {
  53934. EXPECT_DECLS;
  53935. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53936. WOLFSSL_EC_KEY* key = NULL;
  53937. WOLFSSL_BIGNUM* priv = NULL;
  53938. WOLFSSL_BIGNUM* priv2 = NULL;
  53939. WOLFSSL_BIGNUM* bn;
  53940. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53941. ExpectNotNull(priv = wolfSSL_BN_new());
  53942. ExpectNotNull(priv2 = wolfSSL_BN_new());
  53943. ExpectIntNE(BN_set_word(priv, 2), 0);
  53944. ExpectIntNE(BN_set_word(priv2, 2), 0);
  53945. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  53946. /* No private key set. */
  53947. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  53948. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  53949. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  53950. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  53951. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  53952. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53953. ExpectPtrNE(bn, priv);
  53954. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  53955. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53956. ExpectPtrNE(bn, priv2);
  53957. wolfSSL_BN_free(priv2);
  53958. wolfSSL_BN_free(priv);
  53959. wolfSSL_EC_KEY_free(key);
  53960. #endif
  53961. return EXPECT_RESULT();
  53962. }
  53963. static int test_wolfSSL_EC_KEY_public_key(void)
  53964. {
  53965. EXPECT_DECLS;
  53966. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53967. WOLFSSL_EC_KEY* key = NULL;
  53968. WOLFSSL_EC_POINT* pub = NULL;
  53969. WOLFSSL_EC_POINT* point = NULL;
  53970. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53971. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  53972. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  53973. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  53974. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  53975. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  53976. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  53977. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  53978. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  53979. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  53980. ExpectPtrEq(point, pub);
  53981. wolfSSL_EC_KEY_free(key);
  53982. #endif
  53983. return EXPECT_RESULT();
  53984. }
  53985. static int test_wolfSSL_EC_KEY_print_fp(void)
  53986. {
  53987. EXPECT_DECLS;
  53988. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  53989. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  53990. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  53991. !defined(NO_STDIO_FILESYSTEM)
  53992. EC_KEY* key = NULL;
  53993. /* Bad file pointer. */
  53994. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  53995. /* NULL key. */
  53996. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  53997. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  53998. /* Negative indent. */
  53999. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  54000. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54001. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54002. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54003. wolfSSL_EC_KEY_free(key);
  54004. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  54005. NID_X9_62_prime256v1)));
  54006. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54007. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54008. wolfSSL_EC_KEY_free(key);
  54009. #endif
  54010. return EXPECT_RESULT();
  54011. }
  54012. static int test_wolfSSL_EC_get_builtin_curves(void)
  54013. {
  54014. EXPECT_DECLS;
  54015. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54016. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54017. EC_builtin_curve* curves = NULL;
  54018. size_t crv_len = 0;
  54019. size_t i = 0;
  54020. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  54021. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  54022. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  54023. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  54024. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  54025. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  54026. if (curves[i].comment != NULL) {
  54027. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  54028. }
  54029. }
  54030. if (crv_len > 1) {
  54031. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  54032. }
  54033. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54034. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54035. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  54036. return EXPECT_RESULT();
  54037. }
  54038. static int test_wolfSSL_ECDSA_SIG(void)
  54039. {
  54040. EXPECT_DECLS;
  54041. #ifdef OPENSSL_EXTRA
  54042. WOLFSSL_ECDSA_SIG* sig = NULL;
  54043. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  54044. WOLFSSL_BIGNUM* r = NULL;
  54045. WOLFSSL_BIGNUM* s = NULL;
  54046. const WOLFSSL_BIGNUM* r2 = NULL;
  54047. const WOLFSSL_BIGNUM* s2 = NULL;
  54048. const unsigned char* cp = NULL;
  54049. unsigned char* p = NULL;
  54050. unsigned char outSig[8];
  54051. unsigned char sigData[8] =
  54052. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54053. unsigned char sigDataBad[8] =
  54054. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54055. wolfSSL_ECDSA_SIG_free(NULL);
  54056. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  54057. ExpectNotNull(r = wolfSSL_BN_new());
  54058. ExpectNotNull(s = wolfSSL_BN_new());
  54059. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  54060. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  54061. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  54062. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  54063. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  54064. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54065. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  54066. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  54067. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  54068. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  54069. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  54070. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  54071. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  54072. r2 = NULL;
  54073. s2 = NULL;
  54074. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54075. ExpectNull(r2);
  54076. ExpectNull(s2);
  54077. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  54078. if (EXPECT_FAIL()) {
  54079. wolfSSL_BN_free(r);
  54080. wolfSSL_BN_free(s);
  54081. }
  54082. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  54083. ExpectPtrEq(r2, r);
  54084. ExpectPtrEq(s2, s);
  54085. r2 = NULL;
  54086. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  54087. ExpectPtrEq(r2, r);
  54088. s2 = NULL;
  54089. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  54090. ExpectPtrEq(s2, s);
  54091. /* r and s are freed when sig is freed. */
  54092. wolfSSL_ECDSA_SIG_free(sig);
  54093. sig = NULL;
  54094. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  54095. cp = sigDataBad;
  54096. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  54097. cp = sigData;
  54098. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  54099. ExpectIntEQ((cp == sigData + 8), 1);
  54100. cp = sigData;
  54101. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  54102. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  54103. ExpectIntEQ((sig == sig2), 1);
  54104. cp = outSig;
  54105. p = outSig;
  54106. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  54107. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  54108. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  54109. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  54110. ExpectIntEQ((p == outSig + 8), 1);
  54111. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  54112. wolfSSL_ECDSA_SIG_free(sig);
  54113. #endif
  54114. return EXPECT_RESULT();
  54115. }
  54116. static int test_ECDSA_size_sign(void)
  54117. {
  54118. EXPECT_DECLS;
  54119. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  54120. EC_KEY* key = NULL;
  54121. ECDSA_SIG* ecdsaSig = NULL;
  54122. int id;
  54123. byte hash[WC_MAX_DIGEST_SIZE];
  54124. byte hash2[WC_MAX_DIGEST_SIZE];
  54125. byte sig[ECC_MAX_SIG_SIZE];
  54126. unsigned int sigSz = sizeof(sig);
  54127. XMEMSET(hash, 123, sizeof(hash));
  54128. XMEMSET(hash2, 234, sizeof(hash2));
  54129. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  54130. ExpectIntEQ(id, ECC_SECP256R1);
  54131. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54132. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54133. ExpectIntGE(ECDSA_size(NULL), 0);
  54134. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  54135. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  54136. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  54137. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  54138. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  54139. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  54140. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  54141. ExpectIntGE(ECDSA_size(key), sigSz);
  54142. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  54143. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  54144. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  54145. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  54146. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  54147. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  54148. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  54149. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  54150. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  54151. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  54152. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  54153. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  54154. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  54155. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  54156. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  54157. ECDSA_SIG_free(ecdsaSig);
  54158. EC_KEY_free(key);
  54159. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  54160. return EXPECT_RESULT();
  54161. }
  54162. static int test_ECDH_compute_key(void)
  54163. {
  54164. EXPECT_DECLS;
  54165. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54166. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54167. EC_KEY* key1 = NULL;
  54168. EC_KEY* key2 = NULL;
  54169. EC_POINT* pub1 = NULL;
  54170. EC_POINT* pub2 = NULL;
  54171. byte secret1[32];
  54172. byte secret2[32];
  54173. int i;
  54174. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54175. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  54176. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  54177. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54178. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  54179. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  54180. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  54181. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  54182. 0);
  54183. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  54184. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  54185. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  54186. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  54187. 0);
  54188. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  54189. 0);
  54190. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  54191. NULL), 0);
  54192. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  54193. sizeof(secret1));
  54194. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  54195. sizeof(secret2));
  54196. for (i = 0; i < (int)sizeof(secret1); i++) {
  54197. ExpectIntEQ(secret1[i], secret2[i]);
  54198. }
  54199. EC_KEY_free(key2);
  54200. EC_KEY_free(key1);
  54201. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  54202. * !WOLF_CRYPTO_CB_ONLY_ECC */
  54203. return EXPECT_RESULT();
  54204. }
  54205. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  54206. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  54207. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  54208. !defined(NO_ASN_TIME)
  54209. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  54210. int expectedDerSz)
  54211. {
  54212. EXPECT_DECLS;
  54213. X509* x509 = NULL;
  54214. BIGNUM* serial_number = NULL;
  54215. X509_NAME* name = NULL;
  54216. time_t epoch_off = 0;
  54217. ASN1_INTEGER* asn1_serial_number;
  54218. long not_before, not_after;
  54219. int derSz;
  54220. ExpectNotNull(x509 = X509_new());
  54221. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  54222. ExpectNotNull(serial_number = BN_new());
  54223. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  54224. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  54225. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  54226. /* version 3 */
  54227. ExpectIntNE(X509_set_version(x509, 2L), 0);
  54228. ExpectNotNull(name = X509_NAME_new());
  54229. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  54230. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  54231. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  54232. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  54233. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  54234. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  54235. not_before = (long)wc_Time(NULL);
  54236. not_after = not_before + (365 * 24 * 60 * 60);
  54237. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  54238. &epoch_off));
  54239. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  54240. &epoch_off));
  54241. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  54242. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  54243. ExpectIntGE(derSz, expectedDerSz);
  54244. BN_free(serial_number);
  54245. X509_NAME_free(name);
  54246. X509_free(x509);
  54247. return EXPECT_RESULT();
  54248. }
  54249. #endif
  54250. static int test_openssl_generate_key_and_cert(void)
  54251. {
  54252. EXPECT_DECLS;
  54253. #if defined(OPENSSL_EXTRA)
  54254. int expectedDerSz;
  54255. EVP_PKEY* pkey = NULL;
  54256. #ifdef HAVE_ECC
  54257. EC_KEY* ec_key = NULL;
  54258. #endif
  54259. #if !defined(NO_RSA)
  54260. int key_length = 2048;
  54261. BIGNUM* exponent = NULL;
  54262. RSA* rsa = NULL;
  54263. ExpectNotNull(pkey = EVP_PKEY_new());
  54264. ExpectNotNull(exponent = BN_new());
  54265. ExpectNotNull(rsa = RSA_new());
  54266. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  54267. #ifndef WOLFSSL_KEY_GEN
  54268. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  54269. #if defined(USE_CERT_BUFFERS_1024)
  54270. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  54271. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  54272. key_length = 1024;
  54273. #elif defined(USE_CERT_BUFFERS_2048)
  54274. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  54275. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  54276. #else
  54277. RSA_free(rsa);
  54278. rsa = NULL;
  54279. #endif
  54280. #else
  54281. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  54282. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  54283. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  54284. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  54285. #endif
  54286. if (rsa) {
  54287. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  54288. if (EXPECT_FAIL()) {
  54289. RSA_free(rsa);
  54290. }
  54291. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  54292. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  54293. expectedDerSz = 743;
  54294. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  54295. expectedDerSz), TEST_SUCCESS);
  54296. #endif
  54297. }
  54298. EVP_PKEY_free(pkey);
  54299. pkey = NULL;
  54300. BN_free(exponent);
  54301. #endif /* !NO_RSA */
  54302. #ifdef HAVE_ECC
  54303. ExpectNotNull(pkey = EVP_PKEY_new());
  54304. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54305. #ifndef NO_WOLFSSL_STUB
  54306. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  54307. #endif
  54308. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  54309. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  54310. if (EXPECT_FAIL()) {
  54311. EC_KEY_free(ec_key);
  54312. }
  54313. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  54314. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  54315. expectedDerSz = 344;
  54316. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  54317. TEST_SUCCESS);
  54318. #endif
  54319. EVP_PKEY_free(pkey);
  54320. #endif /* HAVE_ECC */
  54321. (void)pkey;
  54322. (void)expectedDerSz;
  54323. #endif /* OPENSSL_EXTRA */
  54324. return EXPECT_RESULT();
  54325. }
  54326. static int test_stubs_are_stubs(void)
  54327. {
  54328. EXPECT_DECLS;
  54329. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  54330. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  54331. WOLFSSL_CTX* ctx = NULL;
  54332. WOLFSSL_CTX* ctxN = NULL;
  54333. #ifndef NO_WOLFSSL_CLIENT
  54334. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54335. #elif !defined(NO_WOLFSSL_SERVER)
  54336. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54337. #endif
  54338. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  54339. ExpectIntEQ((int) x(z), 0)
  54340. /* test logic, all stubs return same result regardless of ctx being NULL
  54341. * as there are no sanity checks, it's just a stub! If at some
  54342. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  54343. * if invalid inputs are supplied. Test calling both
  54344. * with and without valid inputs, if a stub functionality remains unchanged.
  54345. */
  54346. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  54347. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  54348. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  54349. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  54350. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  54351. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  54352. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  54353. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  54354. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  54355. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  54356. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  54357. wolfSSL_CTX_free(ctx);
  54358. ctx = NULL;
  54359. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  54360. * !NO_WOLFSSL_SERVER) */
  54361. return EXPECT_RESULT();
  54362. }
  54363. static int test_CONF_modules_xxx(void)
  54364. {
  54365. int res = TEST_SKIPPED;
  54366. #if defined(OPENSSL_EXTRA)
  54367. CONF_modules_free();
  54368. CONF_modules_unload(0);
  54369. CONF_modules_unload(1);
  54370. CONF_modules_unload(-1);
  54371. res = TEST_SUCCESS;
  54372. #endif /* OPENSSL_EXTRA */
  54373. return res;
  54374. }
  54375. static int test_CRYPTO_set_dynlock_xxx(void)
  54376. {
  54377. int res = TEST_SKIPPED;
  54378. #if defined(OPENSSL_EXTRA)
  54379. CRYPTO_set_dynlock_create_callback(
  54380. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  54381. CRYPTO_set_dynlock_create_callback(
  54382. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  54383. CRYPTO_set_dynlock_destroy_callback(
  54384. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  54385. CRYPTO_set_dynlock_destroy_callback(
  54386. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  54387. CRYPTO_set_dynlock_lock_callback(
  54388. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  54389. CRYPTO_set_dynlock_lock_callback(
  54390. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  54391. res = TEST_SUCCESS;
  54392. #endif /* OPENSSL_EXTRA */
  54393. return res;
  54394. }
  54395. static int test_CRYPTO_THREADID_xxx(void)
  54396. {
  54397. EXPECT_DECLS;
  54398. #if defined(OPENSSL_EXTRA)
  54399. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  54400. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  54401. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  54402. #endif /* OPENSSL_EXTRA */
  54403. return EXPECT_RESULT();
  54404. }
  54405. static int test_ENGINE_cleanup(void)
  54406. {
  54407. int res = TEST_SKIPPED;
  54408. #if defined(OPENSSL_EXTRA)
  54409. ENGINE_cleanup();
  54410. res = TEST_SUCCESS;
  54411. #endif /* OPENSSL_EXTRA */
  54412. return res;
  54413. }
  54414. static int test_wolfSSL_CTX_LoadCRL(void)
  54415. {
  54416. EXPECT_DECLS;
  54417. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  54418. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  54419. WOLFSSL_CTX* ctx = NULL;
  54420. WOLFSSL* ssl = NULL;
  54421. const char* badPath = "dummypath";
  54422. const char* validPath = "./certs/crl";
  54423. const char* validFilePath = "./certs/crl/cliCrl.pem";
  54424. const char* issuerCert = "./certs/client-cert.pem";
  54425. int derType = WOLFSSL_FILETYPE_ASN1;
  54426. int pemType = WOLFSSL_FILETYPE_PEM;
  54427. #ifdef HAVE_CRL_MONITOR
  54428. int monitor = WOLFSSL_CRL_MONITOR;
  54429. #else
  54430. int monitor = 0;
  54431. #endif
  54432. WOLFSSL_CERT_MANAGER* cm = NULL;
  54433. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  54434. BAD_FUNC_ARG)
  54435. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  54436. NOT_COMPILED_IN)
  54437. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  54438. WOLFSSL_SUCCESS)
  54439. #ifndef NO_WOLFSSL_CLIENT
  54440. #define NEW_CTX(ctx) ExpectNotNull( \
  54441. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  54442. #elif !defined(NO_WOLFSSL_SERVER)
  54443. #define NEW_CTX(ctx) ExpectNotNull( \
  54444. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  54445. #else
  54446. #define NEW_CTX(ctx) return
  54447. #endif
  54448. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  54449. NEW_CTX(ctx);
  54450. #ifndef HAVE_CRL_MONITOR
  54451. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  54452. wolfSSL_CTX_free(ctx);
  54453. NEW_CTX(ctx);
  54454. #endif
  54455. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  54456. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  54457. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  54458. wolfSSL_CTX_free(ctx);
  54459. ctx = NULL;
  54460. NEW_CTX(ctx);
  54461. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  54462. WOLFSSL_SUCCESS);
  54463. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  54464. wolfSSL_CTX_free(ctx);
  54465. ctx = NULL;
  54466. NEW_CTX(ctx);
  54467. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  54468. WOLFSSL_SUCCESS);
  54469. ExpectNotNull(ssl = wolfSSL_new(ctx));
  54470. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  54471. wolfSSL_free(ssl);
  54472. ssl = NULL;
  54473. wolfSSL_CTX_free(ctx);
  54474. ctx = NULL;
  54475. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54476. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  54477. WOLFSSL_SUCCESS);
  54478. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  54479. WOLFSSL_SUCCESS);
  54480. wolfSSL_CertManagerFree(cm);
  54481. #endif
  54482. return EXPECT_RESULT();
  54483. }
  54484. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  54485. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  54486. {
  54487. EXPECT_DECLS;
  54488. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  54489. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  54490. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  54491. return EXPECT_RESULT();
  54492. }
  54493. #endif
  54494. static int test_multiple_crls_same_issuer(void)
  54495. {
  54496. EXPECT_DECLS;
  54497. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  54498. test_ssl_cbf client_cbs, server_cbs;
  54499. struct {
  54500. const char* server_cert;
  54501. const char* server_key;
  54502. } test_params[] = {
  54503. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  54504. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  54505. };
  54506. size_t i;
  54507. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  54508. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54509. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54510. server_cbs.certPemFile = test_params[i].server_cert;
  54511. server_cbs.keyPemFile = test_params[i].server_key;
  54512. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  54513. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  54514. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54515. &server_cbs, NULL), TEST_FAIL);
  54516. }
  54517. #endif
  54518. return EXPECT_RESULT();
  54519. }
  54520. static int test_SetTmpEC_DHE_Sz(void)
  54521. {
  54522. EXPECT_DECLS;
  54523. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  54524. WOLFSSL_CTX *ctx = NULL;
  54525. WOLFSSL *ssl = NULL;
  54526. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54527. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  54528. ExpectNotNull(ssl = wolfSSL_new(ctx));
  54529. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  54530. wolfSSL_free(ssl);
  54531. wolfSSL_CTX_free(ctx);
  54532. #endif
  54533. return EXPECT_RESULT();
  54534. }
  54535. static int test_wolfSSL_CTX_get0_privatekey(void)
  54536. {
  54537. EXPECT_DECLS;
  54538. #ifdef OPENSSL_ALL
  54539. WOLFSSL_CTX* ctx = NULL;
  54540. (void)ctx;
  54541. #ifndef NO_RSA
  54542. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54543. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  54544. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  54545. WOLFSSL_FILETYPE_PEM));
  54546. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  54547. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  54548. WOLFSSL_FILETYPE_PEM));
  54549. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  54550. wolfSSL_CTX_free(ctx);
  54551. ctx = NULL;
  54552. #endif
  54553. #ifdef HAVE_ECC
  54554. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54555. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  54556. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  54557. WOLFSSL_FILETYPE_PEM));
  54558. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  54559. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  54560. WOLFSSL_FILETYPE_PEM));
  54561. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  54562. wolfSSL_CTX_free(ctx);
  54563. #endif
  54564. #endif
  54565. return EXPECT_RESULT();
  54566. }
  54567. static int test_wolfSSL_dtls_set_mtu(void)
  54568. {
  54569. EXPECT_DECLS;
  54570. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  54571. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  54572. !defined(WOLFSSL_NO_TLS12)
  54573. WOLFSSL_CTX* ctx = NULL;
  54574. WOLFSSL* ssl = NULL;
  54575. const char* testCertFile;
  54576. const char* testKeyFile;
  54577. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  54578. #ifndef NO_RSA
  54579. testCertFile = svrCertFile;
  54580. testKeyFile = svrKeyFile;
  54581. #elif defined(HAVE_ECC)
  54582. testCertFile = eccCertFile;
  54583. testKeyFile = eccKeyFile;
  54584. #endif
  54585. if (testCertFile != NULL && testKeyFile != NULL) {
  54586. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  54587. WOLFSSL_FILETYPE_PEM));
  54588. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54589. WOLFSSL_FILETYPE_PEM));
  54590. }
  54591. ExpectNotNull(ssl = wolfSSL_new(ctx));
  54592. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  54593. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  54594. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  54595. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  54596. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  54597. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  54598. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  54599. wolfSSL_free(ssl);
  54600. wolfSSL_CTX_free(ctx);
  54601. #endif
  54602. return EXPECT_RESULT();
  54603. }
  54604. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  54605. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  54606. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  54607. enum HandShakeType hsType, word16 length)
  54608. {
  54609. size_t idx = 0;
  54610. byte* l;
  54611. /* record layer */
  54612. /* handshake type */
  54613. out[idx++] = handshake;
  54614. /* protocol version */
  54615. out[idx++] = 0xfe;
  54616. out[idx++] = 0xfd; /* DTLS 1.2 */
  54617. /* epoch 0 */
  54618. XMEMSET(out + idx, 0, 2);
  54619. idx += 2;
  54620. /* sequence number */
  54621. XMEMSET(out + idx, 0, 6);
  54622. c32toa(seq, out + idx + 2);
  54623. idx += 6;
  54624. /* length in BE */
  54625. if (length)
  54626. c16toa(length, out + idx);
  54627. else
  54628. c16toa(outSz - idx - 2, out + idx);
  54629. idx += 2;
  54630. /* handshake layer */
  54631. /* handshake type */
  54632. out[idx++] = (byte)hsType;
  54633. /* length */
  54634. l = out + idx;
  54635. idx += 3;
  54636. /* message seq */
  54637. c16toa(0, out + idx);
  54638. idx += 2;
  54639. /* frag offset */
  54640. c32to24(0, out + idx);
  54641. idx += 3;
  54642. /* frag length */
  54643. c32to24((word32)outSz - (word32)idx - 3, l);
  54644. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  54645. idx += 3;
  54646. XMEMSET(out + idx, 0, outSz - idx);
  54647. }
  54648. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  54649. {
  54650. byte msg[] = "This is a msg for the client";
  54651. byte reply[40];
  54652. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  54653. reply[sizeof(reply) - 1] = '\0';
  54654. fprintf(stderr, "Client message: %s\n", reply);
  54655. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  54656. }
  54657. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  54658. {
  54659. byte ch[50];
  54660. int fd = wolfSSL_get_fd(ssl);
  54661. byte msg[] = "This is a msg for the server";
  54662. byte reply[40];
  54663. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  54664. /* Server should ignore this datagram */
  54665. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  54666. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  54667. /* Server should ignore this datagram */
  54668. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  54669. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  54670. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  54671. reply[sizeof(reply) - 1] = '\0';
  54672. fprintf(stderr, "Server response: %s\n", reply);
  54673. }
  54674. static int test_wolfSSL_dtls_plaintext(void)
  54675. {
  54676. callback_functions func_cb_client;
  54677. callback_functions func_cb_server;
  54678. size_t i;
  54679. struct test_params {
  54680. method_provider client_meth;
  54681. method_provider server_meth;
  54682. ssl_callback on_result_server;
  54683. ssl_callback on_result_client;
  54684. } params[] = {
  54685. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54686. test_wolfSSL_dtls_plaintext_server,
  54687. test_wolfSSL_dtls_plaintext_client},
  54688. };
  54689. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54690. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54691. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54692. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54693. func_cb_server.method = params[i].server_meth;
  54694. func_cb_client.method = params[i].client_meth;
  54695. func_cb_client.on_result = params[i].on_result_client;
  54696. func_cb_server.on_result = params[i].on_result_server;
  54697. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54698. if (!func_cb_client.return_code)
  54699. return TEST_FAIL;
  54700. if (!func_cb_server.return_code)
  54701. return TEST_FAIL;
  54702. }
  54703. return TEST_RES_CHECK(1);
  54704. }
  54705. #else
  54706. static int test_wolfSSL_dtls_plaintext(void) {
  54707. return TEST_SKIPPED;
  54708. }
  54709. #endif
  54710. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  54711. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  54712. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  54713. {
  54714. byte b[1100]; /* buffer for the messages to send */
  54715. size_t idx = 0;
  54716. size_t seq_offset = 0;
  54717. size_t msg_offset = 0;
  54718. int i;
  54719. int fd = wolfSSL_get_fd(ssl);
  54720. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  54721. word32 seq_number = 100; /* start high so server definitely reads this */
  54722. word16 msg_number = 50; /* start high so server has to buffer this */
  54723. AssertIntEQ(ret, 1);
  54724. /* Now let's start spamming the peer with fragments it needs to store */
  54725. XMEMSET(b, -1, sizeof(b));
  54726. /* record layer */
  54727. /* handshake type */
  54728. b[idx++] = 22;
  54729. /* protocol version */
  54730. b[idx++] = 0xfe;
  54731. b[idx++] = 0xfd; /* DTLS 1.2 */
  54732. /* epoch 0 */
  54733. XMEMSET(b + idx, 0, 2);
  54734. idx += 2;
  54735. /* sequence number */
  54736. XMEMSET(b + idx, 0, 6);
  54737. seq_offset = idx + 2; /* increment only the low 32 bits */
  54738. idx += 6;
  54739. /* static length in BE */
  54740. c16toa(42, b + idx);
  54741. idx += 2;
  54742. /* handshake layer */
  54743. /* cert type */
  54744. b[idx++] = 11;
  54745. /* length */
  54746. c32to24(1000, b + idx);
  54747. idx += 3;
  54748. /* message seq */
  54749. c16toa(0, b + idx);
  54750. msg_offset = idx;
  54751. idx += 2;
  54752. /* frag offset */
  54753. c32to24(500, b + idx);
  54754. idx += 3;
  54755. /* frag length */
  54756. c32to24(30, b + idx);
  54757. idx += 3;
  54758. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  54759. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  54760. seq_number++, msg_number++, i++) {
  54761. struct timespec delay;
  54762. XMEMSET(&delay, 0, sizeof(delay));
  54763. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  54764. c32toa(seq_number, b + seq_offset);
  54765. c16toa(msg_number, b + msg_offset);
  54766. ret = (int)send(fd, b, 55, 0);
  54767. nanosleep(&delay, NULL);
  54768. }
  54769. }
  54770. #ifdef WOLFSSL_DTLS13
  54771. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  54772. {
  54773. const word16 sendCountMax = 100;
  54774. byte b[150]; /* buffer for the messages to send */
  54775. size_t idx = 0;
  54776. size_t msg_offset = 0;
  54777. int fd = wolfSSL_get_fd(ssl);
  54778. word16 msg_number = 10; /* start high so server has to buffer this */
  54779. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  54780. AssertIntEQ(ret, 1);
  54781. /* Now let's start spamming the peer with fragments it needs to store */
  54782. XMEMSET(b, -1, sizeof(b));
  54783. /* handshake type */
  54784. b[idx++] = 11;
  54785. /* length */
  54786. c32to24(10000, b + idx);
  54787. idx += 3;
  54788. /* message_seq */
  54789. msg_offset = idx;
  54790. idx += 2;
  54791. /* fragment_offset */
  54792. c32to24(5000, b + idx);
  54793. idx += 3;
  54794. /* fragment_length */
  54795. c32to24(100, b + idx);
  54796. idx += 3;
  54797. /* fragment contents */
  54798. idx += 100;
  54799. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  54800. byte sendBuf[150];
  54801. int sendSz = sizeof(sendBuf);
  54802. struct timespec delay;
  54803. XMEMSET(&delay, 0, sizeof(delay));
  54804. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  54805. c16toa(msg_number, b + msg_offset);
  54806. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  54807. (int)idx, handshake, 0, 0, 0);
  54808. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  54809. nanosleep(&delay, NULL);
  54810. }
  54811. }
  54812. #endif
  54813. static int test_wolfSSL_dtls_fragments(void)
  54814. {
  54815. EXPECT_DECLS;
  54816. callback_functions func_cb_client;
  54817. callback_functions func_cb_server;
  54818. size_t i;
  54819. struct test_params {
  54820. method_provider client_meth;
  54821. method_provider server_meth;
  54822. ssl_callback spammer;
  54823. } params[] = {
  54824. #if !defined(WOLFSSL_NO_TLS12)
  54825. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54826. test_wolfSSL_dtls12_fragments_spammer},
  54827. #endif
  54828. #ifdef WOLFSSL_DTLS13
  54829. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54830. test_wolfSSL_dtls13_fragments_spammer},
  54831. #endif
  54832. };
  54833. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54834. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54835. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54836. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54837. func_cb_server.method = params[i].server_meth;
  54838. func_cb_client.method = params[i].client_meth;
  54839. func_cb_client.ssl_ready = params[i].spammer;
  54840. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54841. ExpectFalse(func_cb_client.return_code);
  54842. ExpectFalse(func_cb_server.return_code);
  54843. /* The socket should be closed by the server resulting in a
  54844. * socket error, fatal error or reading a close notify alert */
  54845. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  54846. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  54847. func_cb_client.last_err != FATAL_ERROR) {
  54848. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  54849. }
  54850. /* Check the server returned an error indicating the msg buffer
  54851. * was full */
  54852. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  54853. if (EXPECT_FAIL())
  54854. break;
  54855. }
  54856. return EXPECT_RESULT();
  54857. }
  54858. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  54859. {
  54860. int fd, ret;
  54861. byte alert_msg[] = {
  54862. 0x15, /* alert type */
  54863. 0xfe, 0xfd, /* version */
  54864. 0x00, 0x00, /* epoch */
  54865. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  54866. 0x00, 0x02, /* length */
  54867. 0x02, /* level: fatal */
  54868. 0x46 /* protocol version */
  54869. };
  54870. fd = wolfSSL_get_fd(ssl);
  54871. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  54872. AssertIntGT(ret, 0);
  54873. }
  54874. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  54875. {
  54876. callback_functions client_cbs, server_cbs;
  54877. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54878. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54879. client_cbs.doUdp = server_cbs.doUdp = 1;
  54880. if (version12) {
  54881. #if !defined(WOLFSSL_NO_TLS12)
  54882. client_cbs.method = wolfDTLSv1_2_client_method;
  54883. server_cbs.method = wolfDTLSv1_2_server_method;
  54884. #else
  54885. return TEST_SKIPPED;
  54886. #endif
  54887. }
  54888. else
  54889. {
  54890. #ifdef WOLFSSL_DTLS13
  54891. client_cbs.method = wolfDTLSv1_3_client_method;
  54892. server_cbs.method = wolfDTLSv1_3_server_method;
  54893. #else
  54894. return TEST_SKIPPED;
  54895. #endif /* WOLFSSL_DTLS13 */
  54896. }
  54897. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  54898. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54899. if (!client_cbs.return_code)
  54900. return TEST_FAIL;
  54901. if (!server_cbs.return_code)
  54902. return TEST_FAIL;
  54903. return TEST_SUCCESS;
  54904. }
  54905. static int test_wolfSSL_ignore_alert_before_cookie(void)
  54906. {
  54907. int ret;
  54908. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  54909. if (ret != 0)
  54910. return ret;
  54911. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  54912. if (ret != 0)
  54913. return ret;
  54914. return 0;
  54915. }
  54916. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  54917. {
  54918. int ret;
  54919. int fd;
  54920. byte bad_msg[] = {
  54921. 0x17, /* app data */
  54922. 0xaa, 0xfd, /* bad version */
  54923. 0x00, 0x01, /* epoch 1 */
  54924. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  54925. 0x00, 0x26, /* length: 38 bytes */
  54926. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  54927. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  54928. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  54929. 0x03, 0x18, 0x72
  54930. };
  54931. fd = wolfSSL_get_fd(ssl);
  54932. AssertIntGE(fd, 0);
  54933. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  54934. AssertIntEQ(ret, sizeof(bad_msg));
  54935. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  54936. AssertIntEQ(ret, sizeof("badrecordtest"));
  54937. }
  54938. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  54939. {
  54940. byte buf[100];
  54941. int ret;
  54942. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  54943. AssertIntGT(ret, 0);
  54944. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  54945. }
  54946. static int _test_wolfSSL_dtls_bad_record(
  54947. method_provider client_method, method_provider server_method)
  54948. {
  54949. callback_functions client_cbs, server_cbs;
  54950. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54951. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54952. client_cbs.doUdp = server_cbs.doUdp = 1;
  54953. client_cbs.method = client_method;
  54954. server_cbs.method = server_method;
  54955. client_cbs.on_result = test_wolfSSL_send_bad_record;
  54956. server_cbs.on_result = test_wolfSSL_read_string;
  54957. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54958. if (!client_cbs.return_code)
  54959. return TEST_FAIL;
  54960. if (!server_cbs.return_code)
  54961. return TEST_FAIL;
  54962. return TEST_SUCCESS;
  54963. }
  54964. static int test_wolfSSL_dtls_bad_record(void)
  54965. {
  54966. int ret = TEST_SUCCESS;
  54967. #if !defined(WOLFSSL_NO_TLS12)
  54968. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  54969. wolfDTLSv1_2_server_method);
  54970. #endif
  54971. #ifdef WOLFSSL_DTLS13
  54972. if (ret == TEST_SUCCESS) {
  54973. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  54974. wolfDTLSv1_3_server_method);
  54975. }
  54976. #endif /* WOLFSSL_DTLS13 */
  54977. return ret;
  54978. }
  54979. #else
  54980. static int test_wolfSSL_dtls_fragments(void) {
  54981. return TEST_SKIPPED;
  54982. }
  54983. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  54984. return TEST_SKIPPED;
  54985. }
  54986. static int test_wolfSSL_dtls_bad_record(void) {
  54987. return TEST_SKIPPED;
  54988. }
  54989. #endif
  54990. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  54991. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  54992. defined(HAVE_IO_TESTS_DEPENDENCIES)
  54993. static byte test_AEAD_fail_decryption = 0;
  54994. static byte test_AEAD_seq_num = 0;
  54995. static byte test_AEAD_done = 0;
  54996. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  54997. {
  54998. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  54999. if (ret > 0) {
  55000. if (test_AEAD_fail_decryption) {
  55001. /* Modify the packet to trigger a decryption failure */
  55002. buf[ret/2] ^= 0xFF;
  55003. if (test_AEAD_fail_decryption == 1)
  55004. test_AEAD_fail_decryption = 0;
  55005. }
  55006. }
  55007. (void)ctx;
  55008. return ret;
  55009. }
  55010. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  55011. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  55012. {
  55013. if (sendLimit)
  55014. w64Zero(sendLimit);
  55015. switch (ssl->specs.bulk_cipher_algorithm) {
  55016. case wolfssl_aes_gcm:
  55017. if (sendLimit)
  55018. *sendLimit = AEAD_AES_LIMIT;
  55019. FALL_THROUGH;
  55020. case wolfssl_chacha:
  55021. if (hardLimit)
  55022. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  55023. if (keyUpdateLimit)
  55024. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  55025. break;
  55026. case wolfssl_aes_ccm:
  55027. if (sendLimit)
  55028. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  55029. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  55030. if (hardLimit)
  55031. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  55032. if (keyUpdateLimit)
  55033. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  55034. }
  55035. else {
  55036. if (hardLimit)
  55037. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  55038. if (keyUpdateLimit)
  55039. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  55040. }
  55041. break;
  55042. default:
  55043. fprintf(stderr, "Unrecognized bulk cipher");
  55044. AssertFalse(1);
  55045. break;
  55046. }
  55047. }
  55048. static void test_AEAD_limit_client(WOLFSSL* ssl)
  55049. {
  55050. int ret;
  55051. int i;
  55052. int didReKey = 0;
  55053. char msgBuf[20];
  55054. w64wrapper hardLimit;
  55055. w64wrapper keyUpdateLimit;
  55056. w64wrapper counter;
  55057. w64wrapper sendLimit;
  55058. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  55059. w64Zero(&counter);
  55060. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  55061. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  55062. for (i = 0; i < 10; i++) {
  55063. /* Test some failed decryptions */
  55064. test_AEAD_fail_decryption = 1;
  55065. w64Increment(&counter);
  55066. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55067. /* Should succeed since decryption failures are dropped */
  55068. AssertIntGT(ret, 0);
  55069. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  55070. }
  55071. test_AEAD_fail_decryption = 1;
  55072. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  55073. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55074. /* 100 read calls should be enough to complete the key update */
  55075. w64Zero(&counter);
  55076. for (i = 0; i < 100; i++) {
  55077. /* Key update should be sent and negotiated */
  55078. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55079. AssertIntGT(ret, 0);
  55080. /* Epoch after one key update is 4 */
  55081. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  55082. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  55083. didReKey = 1;
  55084. break;
  55085. }
  55086. }
  55087. AssertTrue(didReKey);
  55088. if (!w64IsZero(sendLimit)) {
  55089. /* Test the sending limit for AEAD ciphers */
  55090. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  55091. test_AEAD_seq_num = 1;
  55092. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55093. AssertIntGT(ret, 0);
  55094. didReKey = 0;
  55095. w64Zero(&counter);
  55096. /* 100 read calls should be enough to complete the key update */
  55097. for (i = 0; i < 100; i++) {
  55098. /* Key update should be sent and negotiated */
  55099. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55100. AssertIntGT(ret, 0);
  55101. /* Epoch after another key update is 5 */
  55102. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  55103. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  55104. didReKey = 1;
  55105. break;
  55106. }
  55107. }
  55108. AssertTrue(didReKey);
  55109. }
  55110. test_AEAD_fail_decryption = 2;
  55111. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  55112. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55113. /* Connection should fail with a DECRYPT_ERROR */
  55114. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55115. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  55116. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  55117. test_AEAD_done = 1;
  55118. }
  55119. int counter = 0;
  55120. static void test_AEAD_limit_server(WOLFSSL* ssl)
  55121. {
  55122. char msgBuf[] = "Sending data";
  55123. int ret = WOLFSSL_SUCCESS;
  55124. w64wrapper sendLimit;
  55125. SOCKET_T fd = wolfSSL_get_fd(ssl);
  55126. struct timespec delay;
  55127. XMEMSET(&delay, 0, sizeof(delay));
  55128. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  55129. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  55130. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  55131. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  55132. while (!test_AEAD_done && ret > 0) {
  55133. counter++;
  55134. if (test_AEAD_seq_num) {
  55135. /* We need to update the seq number so that we can understand the
  55136. * peer. Otherwise we will incorrectly interpret the seq number. */
  55137. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  55138. AssertNotNull(e);
  55139. e->nextPeerSeqNumber = sendLimit;
  55140. test_AEAD_seq_num = 0;
  55141. }
  55142. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55143. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55144. nanosleep(&delay, NULL);
  55145. }
  55146. }
  55147. static int test_wolfSSL_dtls_AEAD_limit(void)
  55148. {
  55149. callback_functions func_cb_client;
  55150. callback_functions func_cb_server;
  55151. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55152. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55153. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55154. func_cb_server.method = wolfDTLSv1_3_server_method;
  55155. func_cb_client.method = wolfDTLSv1_3_client_method;
  55156. func_cb_server.on_result = test_AEAD_limit_server;
  55157. func_cb_client.on_result = test_AEAD_limit_client;
  55158. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55159. if (!func_cb_client.return_code)
  55160. return TEST_FAIL;
  55161. if (!func_cb_server.return_code)
  55162. return TEST_FAIL;
  55163. return TEST_SUCCESS;
  55164. }
  55165. #else
  55166. static int test_wolfSSL_dtls_AEAD_limit(void)
  55167. {
  55168. return TEST_SKIPPED;
  55169. }
  55170. #endif
  55171. #if defined(WOLFSSL_DTLS) && \
  55172. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  55173. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  55174. {
  55175. int fd, ret;
  55176. byte ch_msg[] = {
  55177. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55178. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55179. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  55180. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  55181. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  55182. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55183. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55184. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55185. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55186. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55187. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55188. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55189. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55190. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  55191. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  55192. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  55193. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  55194. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  55195. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  55196. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  55197. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  55198. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  55199. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  55200. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  55201. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  55202. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  55203. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  55204. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  55205. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  55206. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  55207. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  55208. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  55209. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  55210. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  55211. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  55212. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  55213. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  55214. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  55215. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  55216. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  55217. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  55218. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  55219. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  55220. 0x31, 0x68, 0x4c
  55221. };
  55222. fd = wolfSSL_get_fd(ssl);
  55223. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  55224. AssertIntGT(ret, 0);
  55225. /* consume the HRR otherwise handshake will fail */
  55226. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  55227. AssertIntGT(ret, 0);
  55228. }
  55229. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  55230. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  55231. {
  55232. int fd, ret;
  55233. byte ch_msh_invalid_cookie[] = {
  55234. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  55235. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  55236. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  55237. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  55238. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  55239. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55240. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55241. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55242. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55243. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55244. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55245. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55246. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55247. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  55248. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55249. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55250. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55251. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55252. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55253. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  55254. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  55255. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  55256. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  55257. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  55258. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  55259. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  55260. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  55261. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  55262. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  55263. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  55264. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  55265. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  55266. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  55267. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  55268. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  55269. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  55270. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  55271. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  55272. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  55273. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  55274. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  55275. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  55276. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  55277. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  55278. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  55279. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  55280. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  55281. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  55282. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  55283. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  55284. 0x23, 0x00, 0x00
  55285. };
  55286. byte alert_reply[50];
  55287. byte expected_alert_reply[] = {
  55288. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  55289. 0x02, 0x02, 0x2f
  55290. };
  55291. fd = wolfSSL_get_fd(ssl);
  55292. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  55293. AssertIntGT(ret, 0);
  55294. /* should reply with an illegal_parameter reply */
  55295. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  55296. AssertIntEQ(ret, sizeof(expected_alert_reply));
  55297. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  55298. }
  55299. #endif
  55300. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  55301. {
  55302. #ifndef NO_MD5
  55303. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  55304. #elif !defined(NO_SHA)
  55305. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  55306. #elif !defined(NO_SHA256)
  55307. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  55308. #else
  55309. #error "We need a digest to hash the WOLFSSL object"
  55310. #endif
  55311. byte hashBuf[WC_MAX_DIGEST_SIZE];
  55312. wc_HashAlg hash;
  55313. const TLSX* exts = ssl->extensions;
  55314. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  55315. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  55316. * InitHandshakeHashes */
  55317. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  55318. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  55319. /* Following fields are not important to compare */
  55320. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  55321. sslCopy.buffers.inputBuffer.buffer = NULL;
  55322. sslCopy.buffers.inputBuffer.bufferSize = 0;
  55323. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  55324. sslCopy.buffers.inputBuffer.offset = 0;
  55325. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  55326. sslCopy.buffers.outputBuffer.buffer = NULL;
  55327. sslCopy.buffers.outputBuffer.bufferSize = 0;
  55328. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  55329. sslCopy.buffers.outputBuffer.offset = 0;
  55330. sslCopy.error = 0;
  55331. sslCopy.curSize = 0;
  55332. sslCopy.curStartIdx = 0;
  55333. sslCopy.keys.curSeq_lo = 0;
  55334. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  55335. #ifdef WOLFSSL_DTLS13
  55336. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  55337. sslCopy.dtls13FastTimeout = 0;
  55338. #endif
  55339. sslCopy.keys.dtls_peer_handshake_number = 0;
  55340. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  55341. sslCopy.hsHashes = NULL;
  55342. #ifdef WOLFSSL_ASYNC_IO
  55343. #ifdef WOLFSSL_ASYNC_CRYPT
  55344. sslCopy.asyncDev = NULL;
  55345. #endif
  55346. sslCopy.async = NULL;
  55347. #endif
  55348. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  55349. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  55350. /* hash extension list */
  55351. while (exts != NULL) {
  55352. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  55353. exts = exts->next;
  55354. }
  55355. /* Hash suites */
  55356. if (sslCopy.suites != NULL) {
  55357. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  55358. sizeof(struct Suites)), 0);
  55359. }
  55360. /* Hash hsHashes */
  55361. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  55362. sizeof(*hsHashes)), 0);
  55363. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  55364. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  55365. return MakeWordFromHash(hashBuf);
  55366. }
  55367. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  55368. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  55369. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  55370. char *buf, int sz, void *ctx)
  55371. {
  55372. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  55373. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  55374. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  55375. }
  55376. else {
  55377. return WOLFSSL_CBIO_ERR_WANT_READ;
  55378. }
  55379. }
  55380. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  55381. {
  55382. /* Compare the ssl object before and after one ClientHello msg */
  55383. SOCKET_T fd = wolfSSL_get_fd(ssl);
  55384. int res;
  55385. int err;
  55386. word32 initHash;
  55387. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  55388. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  55389. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  55390. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  55391. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  55392. (void)initHash;
  55393. res = tcp_select(fd, 5);
  55394. /* We are expecting a msg. A timeout indicates failure. */
  55395. AssertIntEQ(res, TEST_RECV_READY);
  55396. res = wolfSSL_accept(ssl);
  55397. err = wolfSSL_get_error(ssl, res);
  55398. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  55399. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  55400. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  55401. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  55402. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  55403. }
  55404. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  55405. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  55406. {
  55407. int ret;
  55408. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  55409. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  55410. test_wolfSSL_dtls_compare_stateless(ssl);
  55411. }
  55412. #endif
  55413. static int test_wolfSSL_dtls_stateless(void)
  55414. {
  55415. callback_functions client_cbs, server_cbs;
  55416. size_t i;
  55417. struct {
  55418. method_provider client_meth;
  55419. method_provider server_meth;
  55420. ssl_callback client_ssl_ready;
  55421. ssl_callback server_ssl_ready;
  55422. } test_params[] = {
  55423. #if !defined(WOLFSSL_NO_TLS12)
  55424. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55425. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  55426. #endif
  55427. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  55428. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55429. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  55430. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55431. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  55432. #endif
  55433. };
  55434. if (0 == sizeof(test_params)){
  55435. return TEST_SKIPPED;
  55436. }
  55437. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  55438. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55439. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55440. client_cbs.doUdp = server_cbs.doUdp = 1;
  55441. client_cbs.method = test_params[i].client_meth;
  55442. server_cbs.method = test_params[i].server_meth;
  55443. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  55444. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  55445. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55446. if (!client_cbs.return_code)
  55447. return TEST_FAIL;
  55448. if (!server_cbs.return_code)
  55449. return TEST_FAIL;
  55450. }
  55451. return TEST_SUCCESS;
  55452. }
  55453. #else
  55454. static int test_wolfSSL_dtls_stateless(void)
  55455. {
  55456. return TEST_SKIPPED;
  55457. }
  55458. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  55459. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  55460. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  55461. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  55462. !defined(WOLFSSL_NO_CLIENT_AUTH))
  55463. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  55464. {
  55465. int ret;
  55466. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  55467. fprintf(stderr, "loading cert %s failed\n", certA);
  55468. fprintf(stderr, "Error: (%d): %s\n", ret,
  55469. wolfSSL_ERR_reason_error_string(ret));
  55470. return -1;
  55471. }
  55472. return 0;
  55473. }
  55474. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  55475. {
  55476. int ret;
  55477. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  55478. != WOLFSSL_SUCCESS) {
  55479. fprintf(stderr, "could not verify the cert: %s\n", certA);
  55480. fprintf(stderr, "Error: (%d): %s\n", ret,
  55481. wolfSSL_ERR_reason_error_string(ret));
  55482. return -1;
  55483. }
  55484. else {
  55485. fprintf(stderr, "successfully verified: %s\n", certA);
  55486. }
  55487. return 0;
  55488. }
  55489. #define LOAD_ONE_CA(a, b, c, d) \
  55490. do { \
  55491. (a) = load_ca_into_cm(c, d); \
  55492. if ((a) != 0) \
  55493. return (b); \
  55494. else \
  55495. (b)--; \
  55496. } while(0)
  55497. #define VERIFY_ONE_CERT(a, b, c, d) \
  55498. do { \
  55499. (a) = verify_cert_with_cm(c, d);\
  55500. if ((a) != 0) \
  55501. return (b); \
  55502. else \
  55503. (b)--; \
  55504. } while(0)
  55505. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  55506. {
  55507. int ret;
  55508. int i = -1;
  55509. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  55510. char chainGArr[9][50] = {"certs/ca-cert.pem",
  55511. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  55512. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  55513. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  55514. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  55515. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  55516. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  55517. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  55518. "certs/test-pathlen/chainG-entity.pem"};
  55519. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  55520. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  55521. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  55522. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  55523. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  55524. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  55525. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  55526. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  55527. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  55528. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  55529. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  55530. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  55531. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  55532. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  55533. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  55534. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  55535. /* test validating the entity twice, should have no effect on pathLen since
  55536. * entity/leaf cert */
  55537. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  55538. return ret;
  55539. }
  55540. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  55541. {
  55542. int ret;
  55543. int i = -1;
  55544. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  55545. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  55546. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  55547. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  55548. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  55549. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  55550. */
  55551. char chainHArr[6][50] = {"certs/ca-cert.pem",
  55552. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  55553. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  55554. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  55555. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  55556. "certs/test-pathlen/chainH-entity.pem"};
  55557. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  55558. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  55559. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  55560. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  55561. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  55562. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  55563. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  55564. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  55565. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  55566. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  55567. return ret;
  55568. }
  55569. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  55570. {
  55571. int ret;
  55572. int i = -1;
  55573. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  55574. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  55575. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  55576. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  55577. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  55578. */
  55579. char chainIArr[5][50] = {"certs/ca-cert.pem",
  55580. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  55581. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  55582. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  55583. "certs/test-pathlen/chainI-entity.pem"};
  55584. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  55585. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  55586. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  55587. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  55588. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  55589. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  55590. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  55591. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  55592. return ret;
  55593. }
  55594. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  55595. {
  55596. int ret;
  55597. int i = -1;
  55598. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  55599. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  55600. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  55601. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  55602. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  55603. */
  55604. char chainJArr[6][50] = {"certs/ca-cert.pem",
  55605. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  55606. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  55607. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  55608. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  55609. "certs/test-pathlen/chainJ-entity.pem"};
  55610. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  55611. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  55612. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  55613. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  55614. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  55615. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  55616. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  55617. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  55618. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  55619. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  55620. return ret;
  55621. }
  55622. static int test_various_pathlen_chains(void)
  55623. {
  55624. EXPECT_DECLS;
  55625. WOLFSSL_CERT_MANAGER* cm = NULL;
  55626. /* Test chain G (large chain with varying pathLens) */
  55627. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55628. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  55629. ExpectIntEQ(test_chainG(cm), -1);
  55630. #else
  55631. ExpectIntEQ(test_chainG(cm), 0);
  55632. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  55633. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55634. wolfSSL_CertManagerFree(cm);
  55635. /* end test chain G */
  55636. /* Test chain H (5 chain with same pathLens) */
  55637. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55638. ExpectIntLT(test_chainH(cm), 0);
  55639. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55640. wolfSSL_CertManagerFree(cm);
  55641. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55642. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55643. wolfSSL_CertManagerFree(cm);
  55644. /* end test chain H */
  55645. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  55646. * followed by 2 ICA's, should pass) */
  55647. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55648. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  55649. ExpectIntEQ(test_chainI(cm), -1);
  55650. #else
  55651. ExpectIntEQ(test_chainI(cm), 0);
  55652. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  55653. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55654. wolfSSL_CertManagerFree(cm);
  55655. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55656. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55657. wolfSSL_CertManagerFree(cm);
  55658. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  55659. * this time followed by 3 ICA's, should fail */
  55660. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55661. ExpectIntLT(test_chainJ(cm), 0);
  55662. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55663. wolfSSL_CertManagerFree(cm);
  55664. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55665. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55666. wolfSSL_CertManagerFree(cm);
  55667. return EXPECT_RESULT();
  55668. }
  55669. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  55670. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  55671. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  55672. {
  55673. EXPECT_DECLS;
  55674. byte ekm[100] = {0};
  55675. (void)ctx;
  55676. /* Success Cases */
  55677. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55678. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  55679. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55680. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  55681. /* Use some random context */
  55682. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55683. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  55684. /* Failure cases */
  55685. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55686. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  55687. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55688. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  55689. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55690. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  55691. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55692. "extended master secret", XSTR_SIZEOF("extended master secret"),
  55693. NULL, 0, 0), 0);
  55694. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55695. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  55696. return EXPECT_RESULT();
  55697. }
  55698. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  55699. {
  55700. wolfSSL_KeepArrays(ssl);
  55701. return TEST_SUCCESS;
  55702. }
  55703. static int test_export_keying_material(void)
  55704. {
  55705. EXPECT_DECLS;
  55706. test_ssl_cbf serverCb;
  55707. test_ssl_cbf clientCb;
  55708. XMEMSET(&serverCb, 0, sizeof(serverCb));
  55709. XMEMSET(&clientCb, 0, sizeof(clientCb));
  55710. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  55711. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  55712. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  55713. return EXPECT_RESULT();
  55714. }
  55715. #endif /* HAVE_KEYING_MATERIAL */
  55716. static int test_wolfSSL_THREADID_hash(void)
  55717. {
  55718. EXPECT_DECLS;
  55719. #if defined(OPENSSL_EXTRA)
  55720. CRYPTO_THREADID id;
  55721. CRYPTO_THREADID_current(NULL);
  55722. /* Hash result is unsigned long. */
  55723. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  55724. XMEMSET(&id, 0, sizeof(id));
  55725. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  55726. #endif /* OPENSSL_EXTRA */
  55727. return EXPECT_RESULT();
  55728. }
  55729. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  55730. {
  55731. EXPECT_DECLS;
  55732. #if defined(OPENSSL_EXTRA)
  55733. WOLFSSL_CTX* ctx = NULL;
  55734. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  55735. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  55736. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  55737. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  55738. #endif /* OPENSSL_EXTRA */
  55739. return EXPECT_RESULT();
  55740. }
  55741. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  55742. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  55743. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  55744. {
  55745. EXPECT_DECLS;
  55746. callback_functions* callbacks = NULL;
  55747. WOLFSSL_CTX* ctx = NULL;
  55748. WOLFSSL* ssl = NULL;
  55749. SOCKET_T sfd = 0;
  55750. SOCKET_T cfd = 0;
  55751. word16 port;
  55752. char msg[] = "I hear you fa shizzle!";
  55753. int len = (int) XSTRLEN(msg);
  55754. char input[1024];
  55755. int ret = 0;
  55756. int err = 0;
  55757. if (!args)
  55758. WOLFSSL_RETURN_FROM_THREAD(0);
  55759. ((func_args*)args)->return_code = TEST_FAIL;
  55760. callbacks = ((func_args*)args)->callbacks;
  55761. ctx = wolfSSL_CTX_new(callbacks->method());
  55762. #if defined(USE_WINDOWS_API)
  55763. port = ((func_args*)args)->signal->port;
  55764. #else
  55765. /* Let tcp_listen assign port */
  55766. port = 0;
  55767. #endif
  55768. #ifdef WOLFSSL_TIRTOS
  55769. fdOpenSession(Task_self());
  55770. #endif
  55771. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  55772. caCertFile, 0));
  55773. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  55774. svrCertFile, WOLFSSL_FILETYPE_PEM));
  55775. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  55776. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  55777. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  55778. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  55779. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55780. #elif !defined(NO_DH)
  55781. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  55782. #endif
  55783. if (callbacks->ctx_ready)
  55784. callbacks->ctx_ready(ctx);
  55785. ssl = wolfSSL_new(ctx);
  55786. ExpectNotNull(ssl);
  55787. /* listen and accept */
  55788. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  55789. CloseSocket(sfd);
  55790. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  55791. if (callbacks->ssl_ready)
  55792. callbacks->ssl_ready(ssl);
  55793. if (EXPECT_SUCCESS()) {
  55794. do {
  55795. err = 0; /* Reset error */
  55796. ret = wolfSSL_accept(ssl);
  55797. if (ret != WOLFSSL_SUCCESS) {
  55798. err = wolfSSL_get_error(ssl, 0);
  55799. }
  55800. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  55801. }
  55802. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55803. /* read and write data */
  55804. XMEMSET(input, 0, sizeof(input));
  55805. while (EXPECT_SUCCESS()) {
  55806. ret = wolfSSL_read(ssl, input, sizeof(input));
  55807. if (ret > 0) {
  55808. break;
  55809. }
  55810. else {
  55811. err = wolfSSL_get_error(ssl,ret);
  55812. if (err == WOLFSSL_ERROR_WANT_READ) {
  55813. continue;
  55814. }
  55815. break;
  55816. }
  55817. }
  55818. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  55819. do {
  55820. ret = wolfSSL_write(ssl, msg, len);
  55821. if (ret > 0) {
  55822. break;
  55823. }
  55824. } while (ret < 0);
  55825. }
  55826. /* bidirectional shutdown */
  55827. while (EXPECT_SUCCESS()) {
  55828. ret = wolfSSL_shutdown(ssl);
  55829. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  55830. if (ret == WOLFSSL_SUCCESS) {
  55831. break;
  55832. }
  55833. }
  55834. if (EXPECT_SUCCESS()) {
  55835. /* wait for the peer to disconnect the tcp connection */
  55836. do {
  55837. ret = wolfSSL_read(ssl, input, sizeof(input));
  55838. err = wolfSSL_get_error(ssl, ret);
  55839. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  55840. }
  55841. /* detect TCP disconnect */
  55842. ExpectIntLE(ret,WOLFSSL_FAILURE);
  55843. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  55844. ((func_args*)args)->return_code = EXPECT_RESULT();
  55845. wolfSSL_free(ssl);
  55846. wolfSSL_CTX_free(ctx);
  55847. CloseSocket(cfd);
  55848. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  55849. wc_ecc_fp_free(); /* free per thread cache */
  55850. #endif
  55851. WOLFSSL_RETURN_FROM_THREAD(0);
  55852. }
  55853. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  55854. {
  55855. EXPECT_DECLS;
  55856. callback_functions* callbacks = NULL;
  55857. WOLFSSL_CTX* ctx = NULL;
  55858. WOLFSSL* ssl = NULL;
  55859. SOCKET_T sfd = 0;
  55860. char msg[] = "hello wolfssl server!";
  55861. int len = (int) XSTRLEN(msg);
  55862. char input[1024];
  55863. int idx;
  55864. int ret, err;
  55865. if (!args)
  55866. WOLFSSL_RETURN_FROM_THREAD(0);
  55867. ((func_args*)args)->return_code = TEST_FAIL;
  55868. callbacks = ((func_args*)args)->callbacks;
  55869. ctx = wolfSSL_CTX_new(callbacks->method());
  55870. #ifdef WOLFSSL_TIRTOS
  55871. fdOpenSession(Task_self());
  55872. #endif
  55873. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  55874. caCertFile, 0));
  55875. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  55876. cliCertFile, WOLFSSL_FILETYPE_PEM));
  55877. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  55878. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  55879. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  55880. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  55881. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  55882. if (EXPECT_SUCCESS()) {
  55883. do {
  55884. err = 0; /* Reset error */
  55885. ret = wolfSSL_connect(ssl);
  55886. if (ret != WOLFSSL_SUCCESS) {
  55887. err = wolfSSL_get_error(ssl, 0);
  55888. }
  55889. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  55890. }
  55891. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  55892. if (EXPECT_SUCCESS()) {
  55893. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  55894. input[idx] = 0;
  55895. }
  55896. }
  55897. if (EXPECT_SUCCESS()) {
  55898. ret = wolfSSL_shutdown(ssl);
  55899. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  55900. ret = wolfSSL_shutdown(ssl);
  55901. }
  55902. }
  55903. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55904. ((func_args*)args)->return_code = EXPECT_RESULT();
  55905. wolfSSL_free(ssl);
  55906. wolfSSL_CTX_free(ctx);
  55907. CloseSocket(sfd);
  55908. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  55909. wc_ecc_fp_free(); /* free per thread cache */
  55910. #endif
  55911. WOLFSSL_RETURN_FROM_THREAD(0);
  55912. }
  55913. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  55914. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  55915. /* This test is to check wolfSSL_read behaves as same as
  55916. * openSSL when it is called after SSL_shutdown completes.
  55917. */
  55918. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  55919. {
  55920. EXPECT_DECLS;
  55921. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  55922. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  55923. tcp_ready ready;
  55924. func_args client_args;
  55925. func_args server_args;
  55926. THREAD_TYPE serverThread;
  55927. THREAD_TYPE clientThread;
  55928. callback_functions server_cbf;
  55929. callback_functions client_cbf;
  55930. #ifdef WOLFSSL_TIRTOS
  55931. fdOpenSession(Task_self());
  55932. #endif
  55933. StartTCP();
  55934. InitTcpReady(&ready);
  55935. #if defined(USE_WINDOWS_API)
  55936. /* use RNG to get random port if using windows */
  55937. ready.port = GetRandomPort();
  55938. #endif
  55939. XMEMSET(&client_args, 0, sizeof(func_args));
  55940. XMEMSET(&server_args, 0, sizeof(func_args));
  55941. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  55942. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  55943. server_cbf.method = wolfTLSv1_2_server_method;
  55944. client_cbf.method = wolfTLSv1_2_client_method;
  55945. server_args.callbacks = &server_cbf;
  55946. client_args.callbacks = &client_cbf;
  55947. server_args.signal = &ready;
  55948. client_args.signal = &ready;
  55949. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  55950. wait_tcp_ready(&server_args);
  55951. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  55952. join_thread(clientThread);
  55953. join_thread(serverThread);
  55954. ExpectTrue(client_args.return_code);
  55955. ExpectTrue(server_args.return_code);
  55956. FreeTcpReady(&ready);
  55957. #endif
  55958. return EXPECT_RESULT();
  55959. }
  55960. static int test_wolfSSL_CTX_get_min_proto_version(void)
  55961. {
  55962. EXPECT_DECLS;
  55963. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  55964. WOLFSSL_CTX *ctx = NULL;
  55965. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55966. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  55967. WOLFSSL_SUCCESS);
  55968. #ifdef WOLFSSL_ALLOW_SSLV3
  55969. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  55970. #else
  55971. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  55972. #endif
  55973. wolfSSL_CTX_free(ctx);
  55974. ctx = NULL;
  55975. #ifdef WOLFSSL_ALLOW_TLSV10
  55976. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  55977. #else
  55978. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55979. #endif
  55980. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  55981. WOLFSSL_SUCCESS);
  55982. #ifdef WOLFSSL_ALLOW_TLSV10
  55983. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  55984. #else
  55985. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  55986. #endif
  55987. wolfSSL_CTX_free(ctx);
  55988. ctx = NULL;
  55989. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55990. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  55991. WOLFSSL_SUCCESS);
  55992. #ifndef NO_OLD_TLS
  55993. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  55994. #else
  55995. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  55996. #endif
  55997. wolfSSL_CTX_free(ctx);
  55998. ctx = NULL;
  55999. #ifndef WOLFSSL_NO_TLS12
  56000. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  56001. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  56002. WOLFSSL_SUCCESS);
  56003. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  56004. wolfSSL_CTX_free(ctx);
  56005. ctx = NULL;
  56006. #endif
  56007. #ifdef WOLFSSL_TLS13
  56008. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  56009. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  56010. WOLFSSL_SUCCESS);
  56011. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  56012. wolfSSL_CTX_free(ctx);
  56013. ctx = NULL;
  56014. #endif
  56015. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56016. return EXPECT_RESULT();
  56017. }
  56018. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56019. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56020. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56021. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  56022. static int test_wolfSSL_set_SSL_CTX(void)
  56023. {
  56024. EXPECT_DECLS;
  56025. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  56026. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  56027. !defined(NO_RSA)
  56028. WOLFSSL_CTX *ctx1 = NULL;
  56029. WOLFSSL_CTX *ctx2 = NULL;
  56030. WOLFSSL *ssl = NULL;
  56031. const byte *session_id1 = (const byte *)"CTX1";
  56032. const byte *session_id2 = (const byte *)"CTX2";
  56033. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56034. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  56035. WOLFSSL_FILETYPE_PEM));
  56036. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  56037. WOLFSSL_FILETYPE_PEM));
  56038. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  56039. WOLFSSL_SUCCESS);
  56040. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  56041. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  56042. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  56043. WOLFSSL_SUCCESS);
  56044. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56045. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  56046. WOLFSSL_FILETYPE_PEM));
  56047. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  56048. WOLFSSL_FILETYPE_PEM));
  56049. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  56050. WOLFSSL_SUCCESS);
  56051. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  56052. WOLFSSL_SUCCESS);
  56053. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  56054. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  56055. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  56056. WOLFSSL_SUCCESS);
  56057. #ifdef HAVE_SESSION_TICKET
  56058. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  56059. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  56060. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  56061. #endif
  56062. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  56063. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56064. #ifdef WOLFSSL_INT_H
  56065. #ifdef WOLFSSL_SESSION_ID_CTX
  56066. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  56067. #endif
  56068. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  56069. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  56070. #endif
  56071. #ifdef HAVE_SESSION_TICKET
  56072. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56073. #endif
  56074. /* Set the ctx1 that has TLSv1.3 as max proto version */
  56075. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  56076. /* MUST not change proto versions of ssl */
  56077. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56078. #ifdef HAVE_SESSION_TICKET
  56079. /* MUST not change */
  56080. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56081. #endif
  56082. /* MUST change */
  56083. #ifdef WOLFSSL_INT_H
  56084. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  56085. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  56086. #ifdef WOLFSSL_SESSION_ID_CTX
  56087. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  56088. #endif
  56089. #endif
  56090. wolfSSL_free(ssl);
  56091. wolfSSL_CTX_free(ctx1);
  56092. wolfSSL_CTX_free(ctx2);
  56093. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56094. return EXPECT_RESULT();
  56095. }
  56096. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56097. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56098. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56099. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  56100. static int test_wolfSSL_security_level(void)
  56101. {
  56102. EXPECT_DECLS;
  56103. #if defined(OPENSSL_EXTRA)
  56104. SSL_CTX *ctx = NULL;
  56105. #ifdef WOLFSSL_TLS13
  56106. #ifdef NO_WOLFSSL_SERVER
  56107. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56108. #else
  56109. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56110. #endif
  56111. SSL_CTX_set_security_level(NULL, 1);
  56112. SSL_CTX_set_security_level(ctx, 1);
  56113. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  56114. /* Stub so nothing happens. */
  56115. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  56116. SSL_CTX_free(ctx);
  56117. #else
  56118. (void)ctx;
  56119. #endif
  56120. #endif
  56121. return EXPECT_RESULT();
  56122. }
  56123. static int test_wolfSSL_SSL_in_init(void)
  56124. {
  56125. EXPECT_DECLS;
  56126. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  56127. SSL_CTX* ctx = NULL;
  56128. SSL* ssl = NULL;
  56129. const char* testCertFile;
  56130. const char* testKeyFile;
  56131. #ifdef WOLFSSL_TLS13
  56132. #ifdef NO_WOLFSSL_SERVER
  56133. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56134. #else
  56135. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56136. #endif
  56137. #else
  56138. #ifdef NO_WOLFSSL_SERVER
  56139. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56140. #else
  56141. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56142. #endif
  56143. #endif
  56144. #ifndef NO_RSA
  56145. testCertFile = svrCertFile;
  56146. testKeyFile = svrKeyFile;
  56147. #elif defined(HAVE_ECC)
  56148. testCertFile = eccCertFile;
  56149. testKeyFile = eccKeyFile;
  56150. #else
  56151. testCertFile = NULL;
  56152. testKeyFile = NULL;
  56153. #endif
  56154. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  56155. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56156. SSL_FILETYPE_PEM));
  56157. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56158. SSL_FILETYPE_PEM));
  56159. }
  56160. ExpectNotNull(ssl = SSL_new(ctx));
  56161. ExpectIntEQ(SSL_in_init(ssl), 1);
  56162. SSL_CTX_free(ctx);
  56163. SSL_free(ssl);
  56164. #endif
  56165. return EXPECT_RESULT();
  56166. }
  56167. static int test_wolfSSL_CTX_set_timeout(void)
  56168. {
  56169. EXPECT_DECLS;
  56170. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  56171. int timeout;
  56172. WOLFSSL_CTX* ctx = NULL;
  56173. (void)timeout;
  56174. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56175. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  56176. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  56177. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  56178. */
  56179. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56180. /* giving 0 as timeout value sets default timeout */
  56181. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  56182. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  56183. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  56184. #else
  56185. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56186. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  56187. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  56188. #endif
  56189. wolfSSL_CTX_free(ctx);
  56190. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  56191. return EXPECT_RESULT();
  56192. }
  56193. static int test_wolfSSL_OpenSSL_version(void)
  56194. {
  56195. EXPECT_DECLS;
  56196. #if defined(OPENSSL_EXTRA)
  56197. const char* ver;
  56198. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  56199. ExpectNotNull(ver = OpenSSL_version(0));
  56200. #else
  56201. ExpectNotNull(ver = OpenSSL_version());
  56202. #endif
  56203. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  56204. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  56205. #endif
  56206. return EXPECT_RESULT();
  56207. }
  56208. static int test_CONF_CTX_CMDLINE(void)
  56209. {
  56210. EXPECT_DECLS;
  56211. #if defined(OPENSSL_ALL)
  56212. SSL_CTX* ctx = NULL;
  56213. SSL_CONF_CTX* cctx = NULL;
  56214. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  56215. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56216. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  56217. /* set flags */
  56218. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  56219. WOLFSSL_CONF_FLAG_CMDLINE);
  56220. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  56221. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  56222. /* cmd invalid command */
  56223. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  56224. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  56225. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  56226. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  56227. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  56228. /* cmd Certificate and Private Key*/
  56229. {
  56230. #if !defined(NO_CERTS) && !defined(NO_RSA)
  56231. const char* ourCert = svrCertFile;
  56232. const char* ourKey = svrKeyFile;
  56233. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  56234. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  56235. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  56236. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  56237. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56238. #endif
  56239. }
  56240. /* cmd curves */
  56241. {
  56242. #if defined(HAVE_ECC)
  56243. const char* curve = "secp256r1";
  56244. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  56245. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  56246. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56247. #endif
  56248. }
  56249. /* cmd CipherString */
  56250. {
  56251. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  56252. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  56253. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  56254. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56255. }
  56256. /* cmd DH parameter */
  56257. {
  56258. #if !defined(NO_DH) && !defined(NO_BIO)
  56259. const char* ourdhcert = "./certs/dh2048.pem";
  56260. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  56261. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  56262. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56263. #endif
  56264. }
  56265. SSL_CTX_free(ctx);
  56266. SSL_CONF_CTX_free(cctx);
  56267. #endif /* OPENSSL_EXTRA */
  56268. return EXPECT_RESULT();
  56269. }
  56270. static int test_CONF_CTX_FILE(void)
  56271. {
  56272. EXPECT_DECLS;
  56273. #if defined(OPENSSL_ALL)
  56274. SSL_CTX* ctx = NULL;
  56275. SSL_CONF_CTX* cctx = NULL;
  56276. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  56277. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56278. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  56279. /* set flags */
  56280. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  56281. WOLFSSL_CONF_FLAG_FILE);
  56282. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  56283. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  56284. /* sanity check */
  56285. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  56286. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  56287. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  56288. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  56289. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  56290. /* cmd Certificate and Private Key*/
  56291. {
  56292. #if !defined(NO_CERTS) && !defined(NO_RSA)
  56293. const char* ourCert = svrCertFile;
  56294. const char* ourKey = svrKeyFile;
  56295. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  56296. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  56297. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  56298. WOLFSSL_SUCCESS);
  56299. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  56300. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56301. #endif
  56302. }
  56303. /* cmd curves */
  56304. {
  56305. #if defined(HAVE_ECC)
  56306. const char* curve = "secp256r1";
  56307. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  56308. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  56309. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56310. #endif
  56311. }
  56312. /* cmd CipherString */
  56313. {
  56314. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  56315. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  56316. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  56317. WOLFSSL_SUCCESS);
  56318. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56319. }
  56320. /* cmd DH parameter */
  56321. {
  56322. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  56323. const char* ourdhcert = "./certs/dh3072.pem";
  56324. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  56325. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  56326. WOLFSSL_SUCCESS);
  56327. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56328. #endif
  56329. }
  56330. SSL_CTX_free(ctx);
  56331. SSL_CONF_CTX_free(cctx);
  56332. #endif /* OPENSSL_EXTRA */
  56333. return EXPECT_RESULT();
  56334. }
  56335. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  56336. {
  56337. EXPECT_DECLS;
  56338. #ifdef HAVE_EX_DATA
  56339. int idx1, idx2;
  56340. /* test for unsupported class index */
  56341. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  56342. 0,NULL, NULL, NULL, NULL ), -1);
  56343. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  56344. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  56345. 0,NULL, NULL, NULL, NULL ), -1);
  56346. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  56347. 0,NULL, NULL, NULL, NULL ), -1);
  56348. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  56349. 0,NULL, NULL, NULL, NULL ), -1);
  56350. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  56351. 0,NULL, NULL, NULL, NULL ), -1);
  56352. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  56353. 0,NULL, NULL, NULL, NULL ), -1);
  56354. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  56355. 0,NULL, NULL, NULL, NULL ), -1);
  56356. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  56357. 0,NULL, NULL, NULL, NULL ), -1);
  56358. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  56359. 0,NULL, NULL, NULL, NULL ), -1);
  56360. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  56361. 0,NULL, NULL, NULL, NULL ), -1);
  56362. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  56363. 0,NULL, NULL, NULL, NULL ), -1);
  56364. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  56365. 0,NULL, NULL, NULL, NULL ), -1);
  56366. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  56367. 0,NULL, NULL, NULL, NULL ), -1);
  56368. /* test for supported class index */
  56369. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  56370. 0,NULL, NULL, NULL, NULL );
  56371. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  56372. 0,NULL, NULL, NULL, NULL );
  56373. ExpectIntNE(idx1, -1);
  56374. ExpectIntNE(idx2, -1);
  56375. ExpectIntNE(idx1, idx2);
  56376. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  56377. 0,NULL, NULL, NULL, NULL );
  56378. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  56379. 0,NULL, NULL, NULL, NULL );
  56380. ExpectIntNE(idx1, -1);
  56381. ExpectIntNE(idx2, -1);
  56382. ExpectIntNE(idx1, idx2);
  56383. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  56384. 0,NULL, NULL, NULL, NULL );
  56385. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  56386. 0,NULL, NULL, NULL, NULL );
  56387. ExpectIntNE(idx1, -1);
  56388. ExpectIntNE(idx2, -1);
  56389. ExpectIntNE(idx1, idx2);
  56390. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  56391. 0,NULL, NULL, NULL, NULL );
  56392. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  56393. 0,NULL, NULL, NULL, NULL );
  56394. ExpectIntNE(idx1, -1);
  56395. ExpectIntNE(idx2, -1);
  56396. ExpectIntNE(idx1, idx2);
  56397. #endif /* HAVE_EX_DATA */
  56398. return EXPECT_RESULT();
  56399. }
  56400. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  56401. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56402. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56403. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56404. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  56405. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  56406. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  56407. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  56408. #define SESSION_NEW_IDX_PTR "Testing"
  56409. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  56410. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  56411. {
  56412. AssertNotNull(p);
  56413. AssertNull(ptr);
  56414. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  56415. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  56416. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  56417. }
  56418. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  56419. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  56420. void* arg)
  56421. {
  56422. EXPECT_DECLS;
  56423. ExpectNotNull(out);
  56424. ExpectNotNull(in);
  56425. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  56426. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  56427. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  56428. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  56429. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  56430. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  56431. if (EXPECT_SUCCESS()) {
  56432. return SSL_SUCCESS;
  56433. }
  56434. else {
  56435. return SSL_FAILURE;
  56436. }
  56437. }
  56438. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  56439. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  56440. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  56441. {
  56442. EXPECT_DECLS;
  56443. ExpectNotNull(p);
  56444. ExpectNull(ptr);
  56445. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  56446. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  56447. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  56448. if (EXPECT_SUCCESS()) {
  56449. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  56450. }
  56451. }
  56452. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  56453. {
  56454. EXPECT_DECLS;
  56455. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  56456. (void*)SESSION_NEW_IDX_PTR,
  56457. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  56458. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  56459. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  56460. SSL_SESSION* s = SSL_SESSION_new();
  56461. SSL_SESSION* d = NULL;
  56462. ExpectNotNull(s);
  56463. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  56464. ExpectNotNull(d = SSL_SESSION_dup(s));
  56465. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  56466. SSL_SESSION_free(s);
  56467. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  56468. SSL_SESSION_free(d);
  56469. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  56470. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  56471. crypto_ex_cb_ctx_session = NULL;
  56472. return EXPECT_RESULT();
  56473. }
  56474. #else
  56475. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  56476. {
  56477. return TEST_SKIPPED;
  56478. }
  56479. #endif
  56480. static int test_wolfSSL_set_psk_use_session_callback(void)
  56481. {
  56482. EXPECT_DECLS;
  56483. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  56484. SSL_CTX* ctx = NULL;
  56485. SSL* ssl = NULL;
  56486. const char* testCertFile;
  56487. const char* testKeyFile;
  56488. #ifdef WOLFSSL_TLS13
  56489. #ifdef NO_WOLFSSL_SERVER
  56490. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56491. #else
  56492. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56493. #endif
  56494. #else
  56495. #ifdef NO_WOLFSSL_SERVER
  56496. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56497. #else
  56498. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56499. #endif
  56500. #endif
  56501. #ifndef NO_RSA
  56502. testCertFile = svrCertFile;
  56503. testKeyFile = svrKeyFile;
  56504. #elif defined(HAVE_ECC)
  56505. testCertFile = eccCertFile;
  56506. testKeyFile = eccKeyFile;
  56507. #else
  56508. testCertFile = NULL;
  56509. testKeyFile = NULL;
  56510. #endif
  56511. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  56512. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56513. SSL_FILETYPE_PEM));
  56514. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56515. SSL_FILETYPE_PEM));
  56516. }
  56517. ExpectNotNull(ssl = SSL_new(ctx));
  56518. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  56519. SSL_CTX_free(ctx);
  56520. SSL_free(ssl);
  56521. #endif
  56522. return EXPECT_RESULT();
  56523. }
  56524. static int test_wolfSSL_ERR_strings(void)
  56525. {
  56526. EXPECT_DECLS;
  56527. #if !defined(NO_ERROR_STRINGS)
  56528. const char* err1 = "unsupported cipher suite";
  56529. const char* err2 = "wolfSSL PEM routines";
  56530. const char* err = NULL;
  56531. (void)err;
  56532. (void)err1;
  56533. (void)err2;
  56534. #if defined(OPENSSL_EXTRA)
  56535. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  56536. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  56537. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  56538. ExpectIntEQ((*err == '\0'), 1);
  56539. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  56540. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  56541. #else
  56542. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  56543. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  56544. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  56545. ExpectIntEQ((*err == '\0'), 1);
  56546. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  56547. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  56548. ExpectIntEQ((*err == '\0'), 1);
  56549. #endif
  56550. #endif
  56551. return EXPECT_RESULT();
  56552. }
  56553. static int test_wolfSSL_EVP_shake128(void)
  56554. {
  56555. EXPECT_DECLS;
  56556. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  56557. defined(WOLFSSL_SHAKE128)
  56558. const EVP_MD* md = NULL;
  56559. ExpectNotNull(md = EVP_shake128());
  56560. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  56561. #endif
  56562. return EXPECT_RESULT();
  56563. }
  56564. static int test_wolfSSL_EVP_shake256(void)
  56565. {
  56566. EXPECT_DECLS;
  56567. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  56568. defined(WOLFSSL_SHAKE256)
  56569. const EVP_MD* md = NULL;
  56570. ExpectNotNull(md = EVP_shake256());
  56571. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  56572. #endif
  56573. return EXPECT_RESULT();
  56574. }
  56575. /*
  56576. * Testing EVP digest API with SM3
  56577. */
  56578. static int test_wolfSSL_EVP_sm3(void)
  56579. {
  56580. int res = TEST_SKIPPED;
  56581. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  56582. EXPECT_DECLS;
  56583. const EVP_MD* md = NULL;
  56584. EVP_MD_CTX* mdCtx = NULL;
  56585. byte data[WC_SM3_BLOCK_SIZE * 4];
  56586. byte hash[WC_SM3_DIGEST_SIZE];
  56587. byte calcHash[WC_SM3_DIGEST_SIZE];
  56588. byte expHash[WC_SM3_DIGEST_SIZE] = {
  56589. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  56590. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  56591. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  56592. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  56593. };
  56594. word32 chunk;
  56595. word32 i;
  56596. unsigned int sz;
  56597. int ret;
  56598. XMEMSET(data, 0, sizeof(data));
  56599. md = EVP_sm3();
  56600. ExpectTrue(md != NULL);
  56601. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  56602. mdCtx = EVP_MD_CTX_new();
  56603. ExpectTrue(mdCtx != NULL);
  56604. /* Invalid Parameters */
  56605. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  56606. /* Valid Parameters */
  56607. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  56608. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  56609. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  56610. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  56611. /* Valid Parameters */
  56612. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  56613. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  56614. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  56615. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  56616. WOLFSSL_SUCCESS);
  56617. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  56618. WOLFSSL_SUCCESS);
  56619. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  56620. WOLFSSL_SUCCESS);
  56621. /* Ensure too many bytes for lengths. */
  56622. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  56623. WOLFSSL_SUCCESS);
  56624. /* Invalid Parameters */
  56625. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  56626. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  56627. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  56628. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  56629. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  56630. /* Valid Parameters */
  56631. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  56632. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  56633. /* Chunk tests. */
  56634. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  56635. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  56636. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  56637. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  56638. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  56639. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  56640. WOLFSSL_SUCCESS);
  56641. }
  56642. if (i < (word32)sizeof(data)) {
  56643. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  56644. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  56645. }
  56646. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  56647. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  56648. }
  56649. /* Not testing when the low 32-bit length overflows. */
  56650. ret = EVP_MD_CTX_cleanup(mdCtx);
  56651. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56652. wolfSSL_EVP_MD_CTX_free(mdCtx);
  56653. res = EXPECT_RESULT();
  56654. #endif
  56655. return res;
  56656. } /* END test_EVP_sm3 */
  56657. static int test_EVP_blake2(void)
  56658. {
  56659. EXPECT_DECLS;
  56660. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  56661. const EVP_MD* md = NULL;
  56662. (void)md;
  56663. #if defined(HAVE_BLAKE2)
  56664. ExpectNotNull(md = EVP_blake2b512());
  56665. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  56666. #endif
  56667. #if defined(HAVE_BLAKE2S)
  56668. ExpectNotNull(md = EVP_blake2s256());
  56669. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  56670. #endif
  56671. #endif
  56672. return EXPECT_RESULT();
  56673. }
  56674. #if defined(OPENSSL_EXTRA)
  56675. static void list_md_fn(const EVP_MD* m, const char* from,
  56676. const char* to, void* arg)
  56677. {
  56678. const char* mn;
  56679. BIO *bio;
  56680. (void) from;
  56681. (void) to;
  56682. (void) arg;
  56683. (void) mn;
  56684. (void) bio;
  56685. if (!m) {
  56686. /* alias */
  56687. AssertNull(m);
  56688. AssertNotNull(to);
  56689. }
  56690. else {
  56691. AssertNotNull(m);
  56692. AssertNull(to);
  56693. }
  56694. AssertNotNull(from);
  56695. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  56696. mn = EVP_get_digestbyname(from);
  56697. /* print to stderr */
  56698. AssertNotNull(arg);
  56699. bio = BIO_new(BIO_s_file());
  56700. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  56701. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  56702. BIO_free(bio);
  56703. #endif
  56704. }
  56705. #endif
  56706. static int test_EVP_MD_do_all(void)
  56707. {
  56708. int res = TEST_SKIPPED;
  56709. #if defined(OPENSSL_EXTRA)
  56710. EVP_MD_do_all(NULL, stderr);
  56711. EVP_MD_do_all(list_md_fn, stderr);
  56712. res = TEST_SUCCESS;
  56713. #endif
  56714. return res;
  56715. }
  56716. #if defined(OPENSSL_EXTRA)
  56717. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  56718. {
  56719. (void)arg;
  56720. (void)nm;
  56721. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  56722. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  56723. /* print to stderr */
  56724. AssertNotNull(arg);
  56725. BIO *bio = BIO_new(BIO_s_file());
  56726. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  56727. BIO_printf(bio, "%s\n", nm);
  56728. BIO_free(bio);
  56729. #endif
  56730. }
  56731. #endif
  56732. static int test_OBJ_NAME_do_all(void)
  56733. {
  56734. int res = TEST_SKIPPED;
  56735. #if defined(OPENSSL_EXTRA)
  56736. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  56737. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  56738. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  56739. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  56740. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  56741. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  56742. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  56743. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  56744. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  56745. res = TEST_SUCCESS;
  56746. #endif
  56747. return res;
  56748. }
  56749. static int test_SSL_CIPHER_get_xxx(void)
  56750. {
  56751. EXPECT_DECLS;
  56752. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  56753. !defined(NO_FILESYSTEM)
  56754. const SSL_CIPHER* cipher = NULL;
  56755. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  56756. int i, numCiphers = 0;
  56757. SSL_CTX* ctx = NULL;
  56758. SSL* ssl = NULL;
  56759. const char* testCertFile;
  56760. const char* testKeyFile;
  56761. char buf[256] = {0};
  56762. const char* cipher_id = NULL;
  56763. int expect_nid1 = NID_undef;
  56764. int expect_nid2 = NID_undef;
  56765. int expect_nid3 = NID_undef;
  56766. int expect_nid4 = NID_undef;
  56767. int expect_nid5 = 0;
  56768. const char* cipher_id2 = NULL;
  56769. int expect_nid21 = NID_undef;
  56770. int expect_nid22 = NID_undef;
  56771. int expect_nid23 = NID_undef;
  56772. int expect_nid24 = NID_undef;
  56773. int expect_nid25 = 0;
  56774. (void)cipher;
  56775. (void)supportedCiphers;
  56776. (void)i;
  56777. (void)numCiphers;
  56778. (void)ctx;
  56779. (void)ssl;
  56780. (void)testCertFile;
  56781. (void)testKeyFile;
  56782. #if defined(WOLFSSL_TLS13)
  56783. cipher_id = "TLS13-AES128-GCM-SHA256";
  56784. expect_nid1 = NID_auth_rsa;
  56785. expect_nid2 = NID_aes_128_gcm;
  56786. expect_nid3 = NID_sha256;
  56787. expect_nid4 = NID_kx_any;
  56788. expect_nid5 = 1;
  56789. #if !defined(WOLFSSL_NO_TLS12)
  56790. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  56791. expect_nid21 = NID_auth_rsa;
  56792. expect_nid22 = NID_aes_256_gcm;
  56793. expect_nid23 = NID_sha384;
  56794. expect_nid24 = NID_kx_ecdhe;
  56795. expect_nid25 = 1;
  56796. #endif
  56797. #endif
  56798. #ifdef NO_WOLFSSL_SERVER
  56799. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56800. #else
  56801. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56802. #endif
  56803. if (cipher_id) {
  56804. #ifndef NO_RSA
  56805. testCertFile = svrCertFile;
  56806. testKeyFile = svrKeyFile;
  56807. #elif defined(HAVE_ECC)
  56808. testCertFile = eccCertFile;
  56809. testKeyFile = eccKeyFile;
  56810. #else
  56811. testCertFile = NULL;
  56812. testKeyFile = NULL;
  56813. #endif
  56814. if (testCertFile != NULL && testKeyFile != NULL) {
  56815. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56816. SSL_FILETYPE_PEM));
  56817. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56818. SSL_FILETYPE_PEM));
  56819. }
  56820. ExpectNotNull(ssl = SSL_new(ctx));
  56821. ExpectIntEQ(SSL_in_init(ssl), 1);
  56822. supportedCiphers = SSL_get_ciphers(ssl);
  56823. numCiphers = sk_num(supportedCiphers);
  56824. for (i = 0; i < numCiphers; ++i) {
  56825. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  56826. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  56827. }
  56828. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  56829. break;
  56830. }
  56831. }
  56832. /* test case for */
  56833. if (i != numCiphers) {
  56834. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  56835. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  56836. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  56837. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  56838. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  56839. }
  56840. if (cipher_id2) {
  56841. for (i = 0; i < numCiphers; ++i) {
  56842. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  56843. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  56844. }
  56845. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  56846. break;
  56847. }
  56848. }
  56849. /* test case for */
  56850. if (i != numCiphers) {
  56851. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  56852. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  56853. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  56854. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  56855. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  56856. }
  56857. }
  56858. }
  56859. SSL_CTX_free(ctx);
  56860. SSL_free(ssl);
  56861. #endif
  56862. return EXPECT_RESULT();
  56863. }
  56864. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56865. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  56866. int* keyFormat)
  56867. {
  56868. int ret;
  56869. byte* key_buf = NULL;
  56870. size_t key_sz = 0;
  56871. EncryptedInfo encInfo;
  56872. XMEMSET(&encInfo, 0, sizeof(encInfo));
  56873. ret = load_file(privKeyFile, &key_buf, &key_sz);
  56874. if (ret == 0) {
  56875. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  56876. NULL, &encInfo, keyFormat);
  56877. }
  56878. if (key_buf != NULL) {
  56879. free(key_buf); key_buf = NULL;
  56880. }
  56881. (void)encInfo; /* not used in this test */
  56882. #ifdef DEBUG_WOLFSSL
  56883. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  56884. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  56885. (*pDer)->length);
  56886. #endif
  56887. return ret;
  56888. }
  56889. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  56890. {
  56891. int ret = CRYPTOCB_UNAVAILABLE;
  56892. const char* privKeyFile = (const char*)ctx;
  56893. DerBuffer* pDer = NULL;
  56894. int keyFormat = 0;
  56895. if (info->algo_type == WC_ALGO_TYPE_PK) {
  56896. #ifdef DEBUG_WOLFSSL
  56897. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  56898. #endif
  56899. #ifndef NO_RSA
  56900. if (info->pk.type == WC_PK_TYPE_RSA) {
  56901. switch (info->pk.rsa.type) {
  56902. case RSA_PUBLIC_ENCRYPT:
  56903. case RSA_PUBLIC_DECRYPT:
  56904. /* perform software based RSA public op */
  56905. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  56906. break;
  56907. case RSA_PRIVATE_ENCRYPT:
  56908. case RSA_PRIVATE_DECRYPT:
  56909. {
  56910. RsaKey key;
  56911. /* perform software based RSA private op */
  56912. #ifdef DEBUG_WOLFSSL
  56913. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  56914. #endif
  56915. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  56916. &keyFormat);
  56917. if (ret != 0) {
  56918. return ret;
  56919. }
  56920. ret = wc_InitRsaKey(&key, HEAP_HINT);
  56921. if (ret == 0) {
  56922. word32 keyIdx = 0;
  56923. /* load RSA private key and perform private transform */
  56924. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  56925. &key, pDer->length);
  56926. if (ret == 0) {
  56927. ret = wc_RsaFunction(
  56928. info->pk.rsa.in, info->pk.rsa.inLen,
  56929. info->pk.rsa.out, info->pk.rsa.outLen,
  56930. info->pk.rsa.type, &key, info->pk.rsa.rng);
  56931. }
  56932. else {
  56933. /* if decode fails, then fall-back to software based crypto */
  56934. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  56935. "key decode failed %d, falling back to "
  56936. "software\n", ret);
  56937. ret = CRYPTOCB_UNAVAILABLE;
  56938. }
  56939. wc_FreeRsaKey(&key);
  56940. }
  56941. wc_FreeDer(&pDer); pDer = NULL;
  56942. break;
  56943. }
  56944. }
  56945. #ifdef DEBUG_WOLFSSL
  56946. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  56947. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  56948. #endif
  56949. }
  56950. #endif /* !NO_RSA */
  56951. #ifdef HAVE_ECC
  56952. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  56953. /* mark this key as ephemeral */
  56954. if (info->pk.eckg.key != NULL) {
  56955. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  56956. sizeof(info->pk.eckg.key->label));
  56957. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  56958. }
  56959. }
  56960. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  56961. ecc_key key;
  56962. /* perform software based ECC sign */
  56963. #ifdef DEBUG_WOLFSSL
  56964. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  56965. #endif
  56966. if (info->pk.eccsign.key != NULL &&
  56967. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  56968. /* this is an empheral key */
  56969. #ifdef DEBUG_WOLFSSL
  56970. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  56971. "ephemeral key\n");
  56972. #endif
  56973. return CRYPTOCB_UNAVAILABLE;
  56974. }
  56975. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  56976. if (ret != 0) {
  56977. return ret;
  56978. }
  56979. ret = wc_ecc_init(&key);
  56980. if (ret == 0) {
  56981. word32 keyIdx = 0;
  56982. /* load ECC private key and perform private transform */
  56983. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  56984. &key, pDer->length);
  56985. if (ret == 0) {
  56986. ret = wc_ecc_sign_hash(
  56987. info->pk.eccsign.in, info->pk.eccsign.inlen,
  56988. info->pk.eccsign.out, info->pk.eccsign.outlen,
  56989. info->pk.eccsign.rng, &key);
  56990. }
  56991. else {
  56992. /* if decode fails, then fall-back to software based crypto */
  56993. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  56994. "decode failed %d, falling back to software\n", ret);
  56995. ret = CRYPTOCB_UNAVAILABLE;
  56996. }
  56997. wc_ecc_free(&key);
  56998. }
  56999. wc_FreeDer(&pDer); pDer = NULL;
  57000. #ifdef DEBUG_WOLFSSL
  57001. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  57002. ret, *info->pk.eccsign.outlen);
  57003. #endif
  57004. }
  57005. #endif /* HAVE_ECC */
  57006. #ifdef HAVE_ED25519
  57007. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  57008. ed25519_key key;
  57009. /* perform software based ED25519 sign */
  57010. #ifdef DEBUG_WOLFSSL
  57011. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  57012. #endif
  57013. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57014. if (ret != 0) {
  57015. return ret;
  57016. }
  57017. ret = wc_ed25519_init(&key);
  57018. if (ret == 0) {
  57019. word32 keyIdx = 0;
  57020. /* load ED25519 private key and perform private transform */
  57021. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  57022. &key, pDer->length);
  57023. if (ret == 0) {
  57024. /* calculate public key */
  57025. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  57026. if (ret == 0) {
  57027. key.pubKeySet = 1;
  57028. ret = wc_ed25519_sign_msg_ex(
  57029. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  57030. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  57031. &key, info->pk.ed25519sign.type,
  57032. info->pk.ed25519sign.context,
  57033. info->pk.ed25519sign.contextLen);
  57034. }
  57035. }
  57036. else {
  57037. /* if decode fails, then fall-back to software based crypto */
  57038. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  57039. "decode failed %d, falling back to software\n", ret);
  57040. ret = CRYPTOCB_UNAVAILABLE;
  57041. }
  57042. wc_ed25519_free(&key);
  57043. }
  57044. wc_FreeDer(&pDer); pDer = NULL;
  57045. #ifdef DEBUG_WOLFSSL
  57046. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  57047. ret, *info->pk.ed25519sign.outLen);
  57048. #endif
  57049. }
  57050. #endif /* HAVE_ED25519 */
  57051. }
  57052. (void)thisDevId;
  57053. (void)keyFormat;
  57054. return ret;
  57055. }
  57056. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57057. static int test_wc_CryptoCb_TLS(int tlsVer,
  57058. const char* cliCaPemFile, const char* cliCertPemFile,
  57059. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  57060. const char* svrCaPemFile, const char* svrCertPemFile,
  57061. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  57062. {
  57063. EXPECT_DECLS;
  57064. callback_functions client_cbf;
  57065. callback_functions server_cbf;
  57066. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57067. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57068. if (tlsVer == WOLFSSL_TLSV1_3) {
  57069. #ifdef WOLFSSL_TLS13
  57070. server_cbf.method = wolfTLSv1_3_server_method;
  57071. client_cbf.method = wolfTLSv1_3_client_method;
  57072. #endif
  57073. }
  57074. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57075. #ifndef WOLFSSL_NO_TLS12
  57076. server_cbf.method = wolfTLSv1_2_server_method;
  57077. client_cbf.method = wolfTLSv1_2_client_method;
  57078. #endif
  57079. }
  57080. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57081. #ifndef NO_OLD_TLS
  57082. server_cbf.method = wolfTLSv1_1_server_method;
  57083. client_cbf.method = wolfTLSv1_1_client_method;
  57084. #endif
  57085. }
  57086. else if (tlsVer == WOLFSSL_TLSV1) {
  57087. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57088. server_cbf.method = wolfTLSv1_server_method;
  57089. client_cbf.method = wolfTLSv1_client_method;
  57090. #endif
  57091. }
  57092. else if (tlsVer == WOLFSSL_SSLV3) {
  57093. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57094. defined(WOLFSSL_STATIC_RSA)
  57095. server_cbf.method = wolfSSLv3_server_method;
  57096. client_cbf.method = wolfSSLv3_client_method;
  57097. #endif
  57098. }
  57099. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57100. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57101. server_cbf.method = wolfDTLSv1_2_server_method;
  57102. client_cbf.method = wolfDTLSv1_2_client_method;
  57103. #endif
  57104. }
  57105. else if (tlsVer == WOLFSSL_DTLSV1) {
  57106. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57107. server_cbf.method = wolfDTLSv1_server_method;
  57108. client_cbf.method = wolfDTLSv1_client_method;
  57109. #endif
  57110. }
  57111. if (server_cbf.method == NULL) {
  57112. /* not enabled */
  57113. return TEST_SUCCESS;
  57114. }
  57115. /* Setup the keys for the TLS test */
  57116. client_cbf.certPemFile = cliCertPemFile;
  57117. client_cbf.keyPemFile = cliPubKeyPemFile;
  57118. client_cbf.caPemFile = cliCaPemFile;
  57119. server_cbf.certPemFile = svrCertPemFile;
  57120. server_cbf.keyPemFile = svrPubKeyPemFile;
  57121. server_cbf.caPemFile = svrCaPemFile;
  57122. /* Setup a crypto callback with pointer to private key file for testing */
  57123. client_cbf.devId = 1;
  57124. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  57125. (void*)cliPrivKeyPemFile);
  57126. server_cbf.devId = 2;
  57127. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  57128. (void*)svrPrivKeyPemFile);
  57129. /* Perform TLS server and client test */
  57130. /* First test is at WOLFSSL_CTX level */
  57131. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57132. /* Check for success */
  57133. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57134. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57135. if (EXPECT_SUCCESS()) {
  57136. /* Second test is a WOLFSSL object level */
  57137. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57138. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57139. }
  57140. /* Check for success */
  57141. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57142. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57143. /* Un register the devId's */
  57144. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  57145. client_cbf.devId = INVALID_DEVID;
  57146. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  57147. server_cbf.devId = INVALID_DEVID;
  57148. return EXPECT_RESULT();
  57149. }
  57150. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  57151. static int test_wc_CryptoCb(void)
  57152. {
  57153. EXPECT_DECLS;
  57154. #ifdef WOLF_CRYPTO_CB
  57155. /* TODO: Add crypto callback API tests */
  57156. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57157. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  57158. int tlsVer;
  57159. #endif
  57160. #ifndef NO_RSA
  57161. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57162. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57163. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  57164. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  57165. TEST_SUCCESS);
  57166. }
  57167. #endif
  57168. #ifdef HAVE_ECC
  57169. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57170. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57171. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  57172. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  57173. TEST_SUCCESS);
  57174. }
  57175. #endif
  57176. #ifdef HAVE_ED25519
  57177. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  57178. if (tlsVer == WOLFSSL_DTLSV1) continue;
  57179. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57180. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  57181. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  57182. TEST_SUCCESS);
  57183. }
  57184. #endif
  57185. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  57186. #endif /* WOLF_CRYPTO_CB */
  57187. return EXPECT_RESULT();
  57188. }
  57189. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57190. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57191. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  57192. const char* cliCaPemFile, const char* cliCertPemFile,
  57193. const char* cliPrivKeyPemFile,
  57194. const char* svrCaPemFile, const char* svrCertPemFile,
  57195. const char* svrPrivKeyPemFile,
  57196. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  57197. {
  57198. EXPECT_DECLS;
  57199. callback_functions client_cbf;
  57200. callback_functions server_cbf;
  57201. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57202. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57203. if (tlsVer == WOLFSSL_TLSV1_3) {
  57204. #ifdef WOLFSSL_TLS13
  57205. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  57206. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  57207. #endif
  57208. }
  57209. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57210. #ifndef WOLFSSL_NO_TLS12
  57211. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  57212. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  57213. #endif
  57214. }
  57215. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57216. #ifndef NO_OLD_TLS
  57217. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  57218. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  57219. #endif
  57220. }
  57221. else if (tlsVer == WOLFSSL_TLSV1) {
  57222. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57223. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  57224. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  57225. #endif
  57226. }
  57227. else if (tlsVer == WOLFSSL_SSLV3) {
  57228. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57229. defined(WOLFSSL_STATIC_RSA)
  57230. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  57231. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  57232. #endif
  57233. }
  57234. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57235. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57236. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  57237. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  57238. #endif
  57239. }
  57240. else if (tlsVer == WOLFSSL_DTLSV1) {
  57241. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57242. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  57243. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  57244. #endif
  57245. }
  57246. if (server_cbf.method_ex == NULL) {
  57247. /* not enabled */
  57248. return TEST_SUCCESS;
  57249. }
  57250. /* Setup the keys for the TLS test */
  57251. client_cbf.certPemFile = cliCertPemFile;
  57252. client_cbf.keyPemFile = cliPrivKeyPemFile;
  57253. client_cbf.caPemFile = cliCaPemFile;
  57254. server_cbf.certPemFile = svrCertPemFile;
  57255. server_cbf.keyPemFile = svrPrivKeyPemFile;
  57256. server_cbf.caPemFile = svrCaPemFile;
  57257. client_cbf.mem = cliMem;
  57258. client_cbf.memSz = cliMemSz;
  57259. server_cbf.mem = svrMem;
  57260. server_cbf.memSz = svrMemSz;
  57261. client_cbf.devId = INVALID_DEVID;
  57262. server_cbf.devId = INVALID_DEVID;
  57263. /* Perform TLS server and client test */
  57264. /* First test is at WOLFSSL_CTX level */
  57265. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57266. /* Check for success */
  57267. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57268. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57269. if (EXPECT_SUCCESS()) {
  57270. /* Second test is a WOLFSSL object level */
  57271. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57272. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57273. }
  57274. /* Check for success */
  57275. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57276. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57277. return EXPECT_RESULT();
  57278. }
  57279. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  57280. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  57281. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  57282. {
  57283. EXPECT_DECLS;
  57284. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  57285. WOLFSSL_MEM_STATS mem_stats;
  57286. WOLFSSL_MEM_CONN_STATS ssl_stats;
  57287. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  57288. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  57289. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57290. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  57291. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57292. #endif
  57293. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  57294. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  57295. /* this should fail because kMaxCtxClients == 2 */
  57296. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  57297. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  57298. #ifdef DEBUG_WOLFSSL
  57299. wolfSSL_PrintStatsConn(&ssl_stats);
  57300. #endif
  57301. (void)ssl_stats;
  57302. }
  57303. /* display collected statistics */
  57304. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  57305. #ifdef DEBUG_WOLFSSL
  57306. wolfSSL_PrintStats(&mem_stats);
  57307. #endif
  57308. (void)mem_stats;
  57309. }
  57310. wolfSSL_free(ssl1);
  57311. wolfSSL_free(ssl2);
  57312. return EXPECT_RESULT();
  57313. }
  57314. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  57315. static int test_wolfSSL_CTX_StaticMemory(void)
  57316. {
  57317. EXPECT_DECLS;
  57318. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  57319. wolfSSL_method_func method_func;
  57320. WOLFSSL_CTX* ctx;
  57321. const int kMaxCtxClients = 2;
  57322. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57323. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  57324. int tlsVer;
  57325. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  57326. #endif
  57327. #endif
  57328. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  57329. #ifndef NO_WOLFSSL_SERVER
  57330. #ifndef WOLFSSL_NO_TLS12
  57331. method_func = wolfTLSv1_2_server_method_ex;
  57332. #else
  57333. method_func = wolfTLSv1_3_server_method_ex;
  57334. #endif
  57335. #else
  57336. #ifndef WOLFSSL_NO_TLS12
  57337. method_func = wolfTLSv1_2_client_method_ex;
  57338. #else
  57339. method_func = wolfTLSv1_3_client_method_ex;
  57340. #endif
  57341. #endif
  57342. /* Test creating CTX directly from static memory pool */
  57343. ctx = NULL;
  57344. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  57345. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  57346. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  57347. wolfSSL_CTX_free(ctx);
  57348. ctx = NULL;
  57349. /* Test for heap allocated CTX, then assigning static pool to it */
  57350. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  57351. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  57352. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  57353. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  57354. wolfSSL_CTX_free(ctx);
  57355. /* TLS Level Tests using static memory */
  57356. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57357. #ifndef NO_RSA
  57358. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57359. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  57360. svrCertFile, cliCertFile, cliKeyFile,
  57361. cliCertFile, svrCertFile, svrKeyFile,
  57362. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  57363. TEST_SUCCESS);
  57364. }
  57365. #endif
  57366. #ifdef HAVE_ECC
  57367. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57368. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  57369. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  57370. cliEccCertFile, eccCertFile, eccKeyFile,
  57371. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  57372. TEST_SUCCESS);
  57373. }
  57374. #endif
  57375. #ifdef HAVE_ED25519
  57376. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  57377. if (tlsVer == WOLFSSL_DTLSV1) continue;
  57378. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  57379. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  57380. cliEdCertFile, edCertFile, edKeyFile,
  57381. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  57382. TEST_SUCCESS);
  57383. }
  57384. #endif
  57385. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  57386. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  57387. return EXPECT_RESULT();
  57388. }
  57389. static int test_openssl_FIPS_drbg(void)
  57390. {
  57391. EXPECT_DECLS;
  57392. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  57393. DRBG_CTX* dctx = NULL;
  57394. byte data1[32], data2[32], zeroData[32];
  57395. byte testSeed[16];
  57396. size_t dlen = sizeof(data1);
  57397. int i;
  57398. XMEMSET(data1, 0, dlen);
  57399. XMEMSET(data2, 0, dlen);
  57400. XMEMSET(zeroData, 0, sizeof(zeroData));
  57401. for (i = 0; i < (int)sizeof(testSeed); i++) {
  57402. testSeed[i] = (byte)i;
  57403. }
  57404. ExpectNotNull(dctx = FIPS_get_default_drbg());
  57405. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  57406. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  57407. WOLFSSL_SUCCESS);
  57408. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  57409. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  57410. WOLFSSL_SUCCESS);
  57411. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  57412. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  57413. WOLFSSL_SUCCESS);
  57414. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  57415. WOLFSSL_SUCCESS);
  57416. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  57417. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  57418. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  57419. #ifndef HAVE_GLOBAL_RNG
  57420. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  57421. wolfSSL_FIPS_drbg_free(dctx);
  57422. #endif
  57423. #endif
  57424. return EXPECT_RESULT();
  57425. }
  57426. static int test_wolfSSL_FIPS_mode(void)
  57427. {
  57428. EXPECT_DECLS;
  57429. #if defined(OPENSSL_ALL)
  57430. #ifdef HAVE_FIPS
  57431. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  57432. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  57433. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  57434. #else
  57435. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  57436. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  57437. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  57438. #endif
  57439. #endif
  57440. return EXPECT_RESULT();
  57441. }
  57442. #ifdef WOLFSSL_DTLS
  57443. /* Prints out the current window */
  57444. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  57445. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  57446. int i;
  57447. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  57448. word32 b = w[i];
  57449. int j;
  57450. /* Prints out a 32 bit binary number in big endian order */
  57451. for (j = 0; j < 32; j++, b <<= 1) {
  57452. if (b & (((word32)1) << 31))
  57453. fprintf(stderr, "1");
  57454. else
  57455. fprintf(stderr, "0");
  57456. }
  57457. fprintf(stderr, " ");
  57458. }
  57459. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  57460. #else
  57461. (void)h;
  57462. (void)l;
  57463. (void)w;
  57464. #endif
  57465. }
  57466. /* a - cur_hi
  57467. * b - cur_lo
  57468. * c - next_hi
  57469. * d - next_lo
  57470. * e - window
  57471. * f - expected next_hi
  57472. * g - expected next_lo
  57473. * h - expected window[1]
  57474. * i - expected window[0]
  57475. */
  57476. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  57477. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  57478. DUW_TEST_print_window_binary((a), (b), (e)); \
  57479. ExpectIntEQ((c), (f)); \
  57480. ExpectIntEQ((d), (g)); \
  57481. ExpectIntEQ((e)[1], (h)); \
  57482. ExpectIntEQ((e)[0], (i)); \
  57483. } while (0)
  57484. static int test_wolfSSL_DtlsUpdateWindow(void)
  57485. {
  57486. EXPECT_DECLS;
  57487. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  57488. word32 next_lo = 0;
  57489. word16 next_hi = 0;
  57490. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  57491. fprintf(stderr, "\n");
  57492. #endif
  57493. XMEMSET(window, 0, sizeof window);
  57494. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  57495. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  57496. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  57497. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  57498. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  57499. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  57500. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  57501. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  57502. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  57503. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  57504. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  57505. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  57506. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  57507. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  57508. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  57509. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  57510. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  57511. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  57512. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  57513. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  57514. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  57515. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  57516. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  57517. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  57518. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  57519. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  57520. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  57521. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  57522. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  57523. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  57524. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  57525. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  57526. return EXPECT_RESULT();
  57527. }
  57528. #endif /* WOLFSSL_DTLS */
  57529. #ifdef WOLFSSL_DTLS
  57530. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  57531. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  57532. {
  57533. DtlsMsg* cur;
  57534. static byte msg[100];
  57535. static byte msgInit = 0;
  57536. if (!msgInit) {
  57537. int i;
  57538. for (i = 0; i < 100; i++)
  57539. msg[i] = i + 1;
  57540. msgInit = 1;
  57541. }
  57542. /* Sanitize test parameters */
  57543. if (len > sizeof(msg))
  57544. return -1;
  57545. if (f_offset + f_len > sizeof(msg))
  57546. return -1;
  57547. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  57548. if (ssl->dtls_rx_msg_list == NULL)
  57549. return -100;
  57550. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  57551. return -200;
  57552. if (cur->fragBucketListCount != f_count)
  57553. return -300;
  57554. if (cur->ready != ready)
  57555. return -400;
  57556. if (cur->bytesReceived != bytesReceived)
  57557. return -500;
  57558. if (ready) {
  57559. if (cur->fragBucketList != NULL)
  57560. return -600;
  57561. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  57562. return -700;
  57563. }
  57564. else {
  57565. DtlsFragBucket* fb;
  57566. if (cur->fragBucketList == NULL)
  57567. return -800;
  57568. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  57569. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  57570. return -900;
  57571. }
  57572. }
  57573. return 0;
  57574. }
  57575. static int test_wolfSSL_DTLS_fragment_buckets(void)
  57576. {
  57577. EXPECT_DECLS;
  57578. WOLFSSL ssl[1];
  57579. XMEMSET(ssl, 0, sizeof(*ssl));
  57580. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  57581. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57582. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  57583. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  57584. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  57585. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  57586. /* Test all permutations of 3 regions */
  57587. /* 1 2 3 */
  57588. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57589. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  57590. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  57591. /* 1 3 2 */
  57592. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57593. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  57594. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  57595. /* 2 1 3 */
  57596. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  57597. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  57598. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  57599. /* 2 3 1 */
  57600. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  57601. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  57602. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  57603. /* 3 1 2 */
  57604. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  57605. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  57606. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  57607. /* 3 2 1 */
  57608. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  57609. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  57610. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  57611. /* Test overlapping regions */
  57612. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57613. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  57614. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  57615. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  57616. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  57617. /* Test overlapping multiple regions */
  57618. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57619. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  57620. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  57621. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  57622. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  57623. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  57624. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  57625. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  57626. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  57627. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  57628. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57629. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  57630. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  57631. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  57632. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  57633. ssl->dtls_rx_msg_list = NULL;
  57634. ssl->dtls_rx_msg_list_sz = 0;
  57635. return EXPECT_RESULT();
  57636. }
  57637. #endif
  57638. #if !defined(NO_FILESYSTEM) && \
  57639. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57640. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57641. static int test_wolfSSL_dtls_stateless2(void)
  57642. {
  57643. EXPECT_DECLS;
  57644. WOLFSSL *ssl_c = NULL;
  57645. WOLFSSL *ssl_c2 = NULL;
  57646. WOLFSSL *ssl_s = NULL;
  57647. struct test_memio_ctx test_ctx;
  57648. WOLFSSL_CTX *ctx_c = NULL;
  57649. WOLFSSL_CTX *ctx_s = NULL;
  57650. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57651. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57652. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57653. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  57654. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57655. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57656. /* send CH */
  57657. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57658. (ssl_c2->error == WANT_READ));
  57659. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57660. (ssl_s->error == WANT_READ));
  57661. ExpectIntNE(test_ctx.c_len, 0);
  57662. /* consume HRR */
  57663. test_ctx.c_len = 0;
  57664. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57665. wolfSSL_free(ssl_c2);
  57666. wolfSSL_free(ssl_c);
  57667. wolfSSL_free(ssl_s);
  57668. wolfSSL_CTX_free(ctx_c);
  57669. wolfSSL_CTX_free(ctx_s);
  57670. return EXPECT_RESULT();
  57671. }
  57672. #ifdef HAVE_MAX_FRAGMENT
  57673. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  57674. {
  57675. EXPECT_DECLS;
  57676. WOLFSSL *ssl_c = NULL;
  57677. WOLFSSL *ssl_c2 = NULL;
  57678. WOLFSSL *ssl_s = NULL;
  57679. struct test_memio_ctx test_ctx;
  57680. WOLFSSL_CTX *ctx_c = NULL;
  57681. WOLFSSL_CTX *ctx_s = NULL;
  57682. word16 max_fragment = 0;
  57683. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57684. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57685. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57686. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  57687. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  57688. WOLFSSL_SUCCESS);
  57689. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57690. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57691. if (ssl_s != NULL) {
  57692. max_fragment = ssl_s->max_fragment;
  57693. }
  57694. /* send CH */
  57695. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57696. (ssl_c2->error == WANT_READ));
  57697. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57698. (ssl_s->error == WANT_READ));
  57699. /* CH without cookie shouldn't change state */
  57700. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  57701. ExpectIntNE(test_ctx.c_len, 0);
  57702. /* consume HRR from buffer */
  57703. test_ctx.c_len = 0;
  57704. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57705. wolfSSL_free(ssl_c2);
  57706. wolfSSL_free(ssl_c);
  57707. wolfSSL_free(ssl_s);
  57708. wolfSSL_CTX_free(ctx_c);
  57709. wolfSSL_CTX_free(ctx_s);
  57710. return EXPECT_RESULT();
  57711. }
  57712. #endif /* HAVE_MAX_FRAGMENT */
  57713. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  57714. #define ROUNDS_WITH_HVR 4
  57715. #define ROUNDS_WITHOUT_HVR 2
  57716. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  57717. static int buf_is_hvr(const byte *data, int len)
  57718. {
  57719. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  57720. return 0;
  57721. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  57722. }
  57723. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  57724. {
  57725. EXPECT_DECLS;
  57726. struct test_memio_ctx test_ctx;
  57727. WOLFSSL_CTX *ctx_c = NULL;
  57728. WOLFSSL_CTX *ctx_s = NULL;
  57729. WOLFSSL *ssl_c = NULL;
  57730. WOLFSSL *ssl_s = NULL;
  57731. WOLFSSL_SESSION *sess = NULL;
  57732. int round_trips;
  57733. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57734. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  57735. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57736. #ifdef HAVE_SESSION_TICKET
  57737. if (useticket) {
  57738. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  57739. }
  57740. #endif
  57741. round_trips = ROUNDS_WITH_HVR;
  57742. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  57743. &round_trips), 0);
  57744. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  57745. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57746. wolfSSL_shutdown(ssl_c);
  57747. wolfSSL_shutdown(ssl_s);
  57748. wolfSSL_free(ssl_c);
  57749. ssl_c = NULL;
  57750. wolfSSL_free(ssl_s);
  57751. ssl_s = NULL;
  57752. test_ctx.c_len = test_ctx.s_len = 0;
  57753. /* make resumption invalid */
  57754. if (bad && (sess != NULL)) {
  57755. if (useticket) {
  57756. #ifdef HAVE_SESSION_TICKET
  57757. if (sess->ticket != NULL) {
  57758. sess->ticket[0] = !sess->ticket[0];
  57759. }
  57760. #endif /* HAVE_SESSION_TICKET */
  57761. }
  57762. else {
  57763. sess->sessionID[0] = !sess->sessionID[0];
  57764. }
  57765. }
  57766. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  57767. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  57768. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  57769. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  57770. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  57771. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  57772. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  57773. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57774. (ssl_c->error == WANT_READ));
  57775. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57776. (ssl_s->error == WANT_READ));
  57777. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  57778. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  57779. if (!useticket) {
  57780. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  57781. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  57782. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  57783. }
  57784. wolfSSL_SESSION_free(sess);
  57785. wolfSSL_free(ssl_c);
  57786. wolfSSL_free(ssl_s);
  57787. wolfSSL_CTX_free(ctx_c);
  57788. wolfSSL_CTX_free(ctx_s);
  57789. return EXPECT_RESULT();
  57790. }
  57791. static int test_wolfSSL_dtls_stateless_resume(void)
  57792. {
  57793. EXPECT_DECLS;
  57794. #ifdef HAVE_SESSION_TICKET
  57795. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  57796. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  57797. #endif /* HAVE_SESION_TICKET */
  57798. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  57799. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  57800. return EXPECT_RESULT();
  57801. }
  57802. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  57803. #if !defined(NO_OLD_TLS)
  57804. static int test_wolfSSL_dtls_stateless_downgrade(void)
  57805. {
  57806. EXPECT_DECLS;
  57807. WOLFSSL_CTX *ctx_c = NULL;
  57808. WOLFSSL_CTX *ctx_c2 = NULL;
  57809. WOLFSSL_CTX *ctx_s = NULL;
  57810. WOLFSSL *ssl_c = NULL;
  57811. WOLFSSL *ssl_c2 = NULL;
  57812. WOLFSSL *ssl_s = NULL;
  57813. struct test_memio_ctx test_ctx;
  57814. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57815. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57816. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57817. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  57818. WOLFSSL_SUCCESS);
  57819. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  57820. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  57821. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  57822. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  57823. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57824. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57825. /* send CH */
  57826. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57827. (ssl_c2->error == WANT_READ));
  57828. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57829. (ssl_s->error == WANT_READ));
  57830. ExpectIntNE(test_ctx.c_len, 0);
  57831. /* consume HRR */
  57832. test_ctx.c_len = 0;
  57833. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57834. wolfSSL_free(ssl_c2);
  57835. wolfSSL_free(ssl_c);
  57836. wolfSSL_free(ssl_s);
  57837. wolfSSL_CTX_free(ctx_c);
  57838. wolfSSL_CTX_free(ctx_c2);
  57839. wolfSSL_CTX_free(ctx_s);
  57840. return EXPECT_RESULT();
  57841. }
  57842. #endif /* !defined(NO_OLD_TLS) */
  57843. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57844. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  57845. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57846. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57847. static int test_WOLFSSL_dtls_version_alert(void)
  57848. {
  57849. EXPECT_DECLS;
  57850. struct test_memio_ctx test_ctx;
  57851. WOLFSSL_CTX *ctx_c = NULL;
  57852. WOLFSSL_CTX *ctx_s = NULL;
  57853. WOLFSSL *ssl_c = NULL;
  57854. WOLFSSL *ssl_s = NULL;
  57855. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57856. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57857. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  57858. /* client hello */
  57859. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57860. (ssl_c->error == WANT_READ));
  57861. /* hrr */
  57862. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57863. (ssl_s->error == WANT_READ));
  57864. /* client hello 1 */
  57865. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57866. (ssl_c->error == WANT_READ));
  57867. /* server hello */
  57868. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57869. (ssl_s->error == WANT_READ));
  57870. /* should fail */
  57871. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57872. (ssl_c->error == VERSION_ERROR));
  57873. /* shuould fail */
  57874. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57875. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  57876. wolfSSL_free(ssl_c);
  57877. wolfSSL_free(ssl_s);
  57878. wolfSSL_CTX_free(ctx_c);
  57879. wolfSSL_CTX_free(ctx_s);
  57880. return EXPECT_RESULT();
  57881. }
  57882. #else
  57883. static int test_WOLFSSL_dtls_version_alert(void)
  57884. {
  57885. return TEST_SKIPPED;
  57886. }
  57887. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  57888. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  57889. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  57890. */
  57891. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  57892. && defined(WOLFSSL_TLS13) && \
  57893. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  57894. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57895. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  57896. {
  57897. struct test_memio_ctx *test_ctx;
  57898. byte buf[2048];
  57899. int idx, sz;
  57900. word32 tmp;
  57901. int ret;
  57902. idx = 5; /* space for record header */
  57903. buf[idx] = session_ticket; /* type */
  57904. idx++;
  57905. tmp = OPAQUE32_LEN +
  57906. OPAQUE32_LEN +
  57907. OPAQUE8_LEN + nonceLength +
  57908. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  57909. c32to24(tmp, buf + idx);
  57910. idx += OPAQUE24_LEN;
  57911. c32toa((word32)12345, buf+idx); /* lifetime */
  57912. idx += OPAQUE32_LEN;
  57913. c32toa((word32)12345, buf+idx); /* add */
  57914. idx += OPAQUE32_LEN;
  57915. buf[idx] = nonceLength; /* nonce length */
  57916. idx++;
  57917. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  57918. idx += nonceLength;
  57919. tmp = 1; /* ticket len */
  57920. c16toa((word16)tmp, buf+idx);
  57921. idx += 2;
  57922. buf[idx] = 0xFF; /* ticket */
  57923. idx++;
  57924. tmp = 0; /* ext len */
  57925. c16toa((word16)tmp, buf+idx);
  57926. idx += 2;
  57927. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  57928. handshake, 0, 0, 0);
  57929. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  57930. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  57931. return !(ret == sz);
  57932. }
  57933. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  57934. {
  57935. int ret = 0;
  57936. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  57937. ret = -1;
  57938. }
  57939. else {
  57940. int i;
  57941. for (i = 0; i < len; i++) {
  57942. if (sess->ticketNonce.data[i] != len) {
  57943. ret = -1;
  57944. break;
  57945. }
  57946. }
  57947. }
  57948. return ret;
  57949. }
  57950. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57951. {
  57952. EXPECT_DECLS;
  57953. char *buf[1024];
  57954. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  57955. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  57956. (ssl_c->error == WANT_READ));
  57957. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  57958. return EXPECT_RESULT();
  57959. }
  57960. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57961. {
  57962. EXPECT_DECLS;
  57963. WOLFSSL_SESSION *sess = NULL;
  57964. WOLFSSL_SESSION *cached = NULL;
  57965. WOLFSSL_CTX *ctx = ssl_c->ctx;
  57966. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  57967. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57968. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  57969. NULL, ssl_c->options.side, 1,NULL), 0);
  57970. ExpectNotNull(cached = wolfSSL_SESSION_new());
  57971. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  57972. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  57973. wolfSSL_SESSION_free(cached);
  57974. wolfSSL_SESSION_free(sess);
  57975. return EXPECT_RESULT();
  57976. }
  57977. static int test_ticket_nonce_malloc(void)
  57978. {
  57979. EXPECT_DECLS;
  57980. struct test_memio_ctx test_ctx;
  57981. WOLFSSL_CTX *ctx_c = NULL;
  57982. WOLFSSL_CTX *ctx_s = NULL;
  57983. WOLFSSL *ssl_c = NULL;
  57984. WOLFSSL *ssl_s = NULL;
  57985. byte small;
  57986. byte medium;
  57987. byte big;
  57988. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57989. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57990. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  57991. /* will send ticket manually */
  57992. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  57993. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  57994. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  57995. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  57996. (ssl_s->options.handShakeDone == 0)) {
  57997. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  57998. (ssl_c->error == WANT_READ));
  57999. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  58000. (ssl_s->error == WANT_READ));
  58001. }
  58002. small = TLS13_TICKET_NONCE_STATIC_SZ;
  58003. medium = small + 20 <= 255 ? small + 20 : 255;
  58004. big = medium + 20 <= 255 ? small + 20 : 255;
  58005. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58006. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  58007. ssl_c->session->ticketNonce.dataStatic);
  58008. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58009. TEST_SUCCESS);
  58010. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  58011. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58012. TEST_SUCCESS);
  58013. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58014. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58015. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58016. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  58017. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58018. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58019. wolfSSL_free(ssl_c);
  58020. wolfSSL_free(ssl_s);
  58021. wolfSSL_CTX_free(ctx_c);
  58022. wolfSSL_CTX_free(ctx_s);
  58023. return EXPECT_RESULT();
  58024. }
  58025. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  58026. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  58027. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  58028. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58029. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  58030. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58031. static int test_ticket_ret_create(void)
  58032. {
  58033. EXPECT_DECLS;
  58034. WOLFSSL_CTX *ctx_c = NULL;
  58035. WOLFSSL_CTX *ctx_s = NULL;
  58036. WOLFSSL *ssl_c = NULL;
  58037. WOLFSSL *ssl_s = NULL;
  58038. byte ticket[SESSION_TICKET_LEN];
  58039. struct test_memio_ctx test_ctx;
  58040. WOLFSSL_SESSION *sess = NULL;
  58041. word16 ticketLen = 0;
  58042. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58043. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58044. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58045. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58046. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58047. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  58048. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58049. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58050. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  58051. if (sess != NULL) {
  58052. ticketLen = sess->ticketLen;
  58053. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  58054. }
  58055. wolfSSL_free(ssl_c);
  58056. ssl_c = NULL;
  58057. wolfSSL_free(ssl_s);
  58058. ssl_s = NULL;
  58059. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58060. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58061. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58062. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58063. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58064. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58065. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58066. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58067. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  58068. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  58069. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  58070. wolfSSL_SESSION_free(sess);
  58071. wolfSSL_free(ssl_c);
  58072. wolfSSL_free(ssl_s);
  58073. wolfSSL_CTX_free(ctx_c);
  58074. wolfSSL_CTX_free(ctx_s);
  58075. return EXPECT_RESULT();
  58076. }
  58077. #else
  58078. static int test_ticket_ret_create(void)
  58079. {
  58080. return TEST_SKIPPED;
  58081. }
  58082. #endif
  58083. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  58084. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  58085. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58086. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58087. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58088. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  58089. {
  58090. int ret;
  58091. WOLFSSL_SESSION* session = NULL;
  58092. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58093. if (!wolfSSL_is_server(ssl)) {
  58094. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58095. AssertNotNull(session);
  58096. }
  58097. do {
  58098. ret = wolfSSL_shutdown(ssl);
  58099. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58100. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58101. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58102. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58103. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58104. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58105. #endif
  58106. if (!wolfSSL_is_server(ssl)) {
  58107. /* client */
  58108. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58109. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58110. wolfSSL_set_session(ssl, session);
  58111. wolfSSL_SESSION_free(session);
  58112. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58113. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58114. }
  58115. else {
  58116. /* server */
  58117. /* Different ciphersuite so that the ticket will be invalidated based on
  58118. * the ciphersuite */
  58119. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  58120. WOLFSSL_SUCCESS);
  58121. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  58122. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58123. }
  58124. }
  58125. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  58126. {
  58127. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  58128. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58129. WOLFSSL_SUCCESS);
  58130. }
  58131. static int test_ticket_and_psk_mixing(void)
  58132. {
  58133. EXPECT_DECLS;
  58134. /* Test mixing tickets and regular PSK */
  58135. callback_functions client_cbs, server_cbs;
  58136. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58137. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58138. client_cbs.method = wolfTLSv1_3_client_method;
  58139. server_cbs.method = wolfTLSv1_3_server_method;
  58140. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  58141. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58142. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58143. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58144. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58145. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58146. return EXPECT_RESULT();
  58147. }
  58148. #else
  58149. static int test_ticket_and_psk_mixing(void)
  58150. {
  58151. return TEST_SKIPPED;
  58152. }
  58153. #endif
  58154. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  58155. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  58156. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58157. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58158. static int test_prioritize_psk_cb_called = FALSE;
  58159. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  58160. const char* identity, unsigned char* key, unsigned int key_max_len,
  58161. const char** ciphersuite)
  58162. {
  58163. test_prioritize_psk_cb_called = TRUE;
  58164. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  58165. }
  58166. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  58167. {
  58168. int ret;
  58169. WOLFSSL_SESSION* session = NULL;
  58170. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58171. if (!wolfSSL_is_server(ssl)) {
  58172. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58173. AssertNotNull(session);
  58174. }
  58175. do {
  58176. ret = wolfSSL_shutdown(ssl);
  58177. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58178. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58179. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58180. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  58181. * important. */
  58182. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58183. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58184. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58185. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58186. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58187. #endif
  58188. if (!wolfSSL_is_server(ssl)) {
  58189. /* client */
  58190. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58191. wolfSSL_set_session(ssl, session);
  58192. wolfSSL_SESSION_free(session);
  58193. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58194. }
  58195. else {
  58196. /* server */
  58197. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  58198. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58199. #ifdef WOLFSSL_PRIORITIZE_PSK
  58200. /* The ticket should be first tried with all ciphersuites and chosen */
  58201. AssertFalse(test_prioritize_psk_cb_called);
  58202. #else
  58203. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  58204. * callback that sets this var. */
  58205. AssertTrue(test_prioritize_psk_cb_called);
  58206. #endif
  58207. }
  58208. }
  58209. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  58210. {
  58211. if (!wolfSSL_is_server(ssl))
  58212. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58213. WOLFSSL_SUCCESS);
  58214. else
  58215. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58216. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58217. }
  58218. static int test_prioritize_psk(void)
  58219. {
  58220. EXPECT_DECLS;
  58221. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  58222. * of the PSK's will be followed instead of the ciphersuite. */
  58223. callback_functions client_cbs, server_cbs;
  58224. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58225. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58226. client_cbs.method = wolfTLSv1_3_client_method;
  58227. server_cbs.method = wolfTLSv1_3_server_method;
  58228. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  58229. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  58230. client_cbs.on_result = test_prioritize_psk_on_result;
  58231. server_cbs.on_result = test_prioritize_psk_on_result;
  58232. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58233. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58234. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58235. return EXPECT_RESULT();
  58236. }
  58237. #else
  58238. static int test_prioritize_psk(void)
  58239. {
  58240. return TEST_SKIPPED;
  58241. }
  58242. #endif
  58243. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  58244. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58245. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58246. !defined(WOLFSSL_NO_TLS12)
  58247. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  58248. {
  58249. EXPECT_DECLS;
  58250. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  58251. /* Set TLS 1.3 specific suite */
  58252. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  58253. return EXPECT_RESULT();
  58254. }
  58255. static int test_wolfSSL_CTX_set_ciphersuites(void)
  58256. {
  58257. EXPECT_DECLS;
  58258. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  58259. * do a 1.2 connection. */
  58260. test_ssl_cbf client_cbs, server_cbs;
  58261. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58262. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58263. client_cbs.method = wolfTLSv1_2_client_method;
  58264. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  58265. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  58266. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58267. &server_cbs, NULL), TEST_SUCCESS);
  58268. return EXPECT_RESULT();
  58269. }
  58270. #else
  58271. static int test_wolfSSL_CTX_set_ciphersuites(void)
  58272. {
  58273. return TEST_SKIPPED;
  58274. }
  58275. #endif
  58276. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  58277. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58278. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  58279. {
  58280. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  58281. return TEST_SUCCESS;
  58282. }
  58283. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  58284. {
  58285. EXPECT_DECLS;
  58286. WOLFSSL_ALERT_HISTORY h;
  58287. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  58288. ExpectIntEQ(h.last_rx.level, alert_fatal);
  58289. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  58290. return EXPECT_RESULT();
  58291. }
  58292. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  58293. {
  58294. EXPECT_DECLS;
  58295. test_ssl_cbf client_cbs, server_cbs;
  58296. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58297. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58298. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  58299. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  58300. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  58301. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  58302. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  58303. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58304. &server_cbs, NULL), TEST_FAIL);
  58305. return EXPECT_RESULT();
  58306. }
  58307. #else
  58308. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  58309. {
  58310. return TEST_SKIPPED;
  58311. }
  58312. #endif
  58313. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  58314. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58315. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58316. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  58317. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  58318. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  58319. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  58320. static int test_TLS_13_ticket_different_ciphers_run = 0;
  58321. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  58322. {
  58323. EXPECT_DECLS;
  58324. switch (test_TLS_13_ticket_different_ciphers_run) {
  58325. case 0:
  58326. /* First run */
  58327. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58328. WOLFSSL_SUCCESS);
  58329. if (wolfSSL_is_server(ssl)) {
  58330. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  58331. wolfSSL_get_SSL_CTX(ssl));
  58332. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  58333. test_TLS_13_ticket_different_ciphers_ctx));
  58334. }
  58335. break;
  58336. case 1:
  58337. /* Second run */
  58338. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58339. "TLS13-AES128-GCM-SHA256"),
  58340. WOLFSSL_SUCCESS);
  58341. if (!wolfSSL_is_server(ssl)) {
  58342. ExpectIntEQ(wolfSSL_set_session(ssl,
  58343. test_TLS_13_ticket_different_ciphers_session),
  58344. WOLFSSL_SUCCESS);
  58345. }
  58346. break;
  58347. default:
  58348. /* Bad state? */
  58349. Fail(("Should not enter here"), ("Should not enter here"));
  58350. }
  58351. return EXPECT_RESULT();
  58352. }
  58353. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  58354. {
  58355. EXPECT_DECLS;
  58356. switch (test_TLS_13_ticket_different_ciphers_run) {
  58357. case 0:
  58358. /* First run */
  58359. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  58360. wolfSSL_get1_session(ssl));
  58361. break;
  58362. case 1:
  58363. /* Second run */
  58364. ExpectTrue(wolfSSL_session_reused(ssl));
  58365. break;
  58366. default:
  58367. /* Bad state? */
  58368. Fail(("Should not enter here"), ("Should not enter here"));
  58369. }
  58370. return EXPECT_RESULT();
  58371. }
  58372. static int test_TLS_13_ticket_different_ciphers(void)
  58373. {
  58374. EXPECT_DECLS;
  58375. /* Check that we handle the connection when the ticket doesn't match
  58376. * the first ciphersuite. */
  58377. test_ssl_cbf client_cbs, server_cbs;
  58378. struct test_params {
  58379. method_provider client_meth;
  58380. method_provider server_meth;
  58381. int doUdp;
  58382. } params[] = {
  58383. #ifdef WOLFSSL_DTLS13
  58384. /* Test that the stateless code handles sessions correctly */
  58385. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  58386. #endif
  58387. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  58388. };
  58389. size_t i;
  58390. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  58391. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58392. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58393. test_TLS_13_ticket_different_ciphers_run = 0;
  58394. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  58395. client_cbs.method = params[i].client_meth;
  58396. server_cbs.method = params[i].server_meth;
  58397. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  58398. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  58399. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  58400. server_cbs.ticNoInit = 1;
  58401. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58402. &server_cbs, NULL), TEST_SUCCESS);
  58403. test_TLS_13_ticket_different_ciphers_run++;
  58404. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  58405. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58406. &server_cbs, NULL), TEST_SUCCESS);
  58407. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  58408. test_TLS_13_ticket_different_ciphers_session = NULL;
  58409. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  58410. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  58411. }
  58412. return EXPECT_RESULT();
  58413. }
  58414. #else
  58415. static int test_TLS_13_ticket_different_ciphers(void)
  58416. {
  58417. return TEST_SKIPPED;
  58418. }
  58419. #endif
  58420. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  58421. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58422. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  58423. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  58424. byte test_extra_alerts_wrong_cs_sh[] = {
  58425. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  58426. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  58427. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  58428. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  58429. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  58430. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  58431. 0xbb, 0x07, 0x54, 0x1c,
  58432. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  58433. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  58434. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  58435. };
  58436. static int test_extra_alerts_wrong_cs(void)
  58437. {
  58438. EXPECT_DECLS;
  58439. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  58440. struct test_memio_ctx test_ctx;
  58441. WOLFSSL_CTX *ctx_c = NULL;
  58442. WOLFSSL_ALERT_HISTORY h;
  58443. WOLFSSL *ssl_c = NULL;
  58444. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58445. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  58446. wolfTLSv1_2_client_method, NULL), 0);
  58447. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  58448. WOLFSSL_SUCCESS);
  58449. /* CH */
  58450. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58451. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58452. WOLFSSL_ERROR_WANT_READ);
  58453. /* consume CH */
  58454. test_ctx.s_len = 0;
  58455. /* inject SH */
  58456. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  58457. sizeof(test_extra_alerts_wrong_cs_sh));
  58458. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  58459. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58460. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58461. WOLFSSL_ERROR_WANT_READ);
  58462. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58463. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  58464. ExpectIntEQ(h.last_tx.level, alert_fatal);
  58465. wolfSSL_free(ssl_c);
  58466. wolfSSL_CTX_free(ctx_c);
  58467. #endif
  58468. return EXPECT_RESULT();
  58469. }
  58470. #else
  58471. static int test_extra_alerts_wrong_cs(void)
  58472. {
  58473. return TEST_SKIPPED;
  58474. }
  58475. #endif
  58476. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  58477. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  58478. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  58479. {
  58480. tail_len -= msg_length;
  58481. XMEMMOVE(msg, msg + msg_length, tail_len);
  58482. *len = *len - msg_length;
  58483. }
  58484. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  58485. byte *found)
  58486. {
  58487. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  58488. const unsigned int _RECORD_HEADER_SZ = 5;
  58489. const int _change_cipher_hs = 55;
  58490. const int _change_cipher = 20;
  58491. const int _handshake = 22;
  58492. unsigned int tail_len;
  58493. byte *idx, *curr;
  58494. word8 currType;
  58495. word16 rLength;
  58496. word32 hLength;
  58497. idx = buf;
  58498. tail_len = *len;
  58499. *found = 0;
  58500. while (tail_len > _RECORD_HEADER_SZ) {
  58501. curr = idx;
  58502. currType = *idx;
  58503. ato16(idx + 3, &rLength);
  58504. idx += _RECORD_HEADER_SZ;
  58505. tail_len -= _RECORD_HEADER_SZ;
  58506. if (tail_len < rLength)
  58507. return -1;
  58508. if (type == _change_cipher_hs && currType == _change_cipher) {
  58509. if (rLength != 1)
  58510. return -1;
  58511. /* match */
  58512. test_remove_msg(curr, *len - (int)(curr - buf),
  58513. len, _RECORD_HEADER_SZ + 1);
  58514. *found = 1;
  58515. return 0;
  58516. }
  58517. if (currType != _handshake) {
  58518. idx += rLength;
  58519. tail_len -= rLength;
  58520. continue;
  58521. }
  58522. if (rLength < _HANDSHAKE_HEADER_SZ)
  58523. return -1;
  58524. currType = *idx;
  58525. ato24(idx+1, &hLength);
  58526. hLength += _HANDSHAKE_HEADER_SZ;
  58527. if (tail_len < hLength)
  58528. return -1;
  58529. if (currType != type) {
  58530. idx += hLength;
  58531. tail_len -= hLength;
  58532. continue;
  58533. }
  58534. /* match */
  58535. test_remove_msg(curr, *len - (int)(curr - buf), len,
  58536. hLength + _RECORD_HEADER_SZ);
  58537. *found = 1;
  58538. return 0;
  58539. }
  58540. /* not found */
  58541. return 0;
  58542. }
  58543. static int test_remove_hs_message(byte hs_message_type,
  58544. int extra_round, byte alert_type)
  58545. {
  58546. EXPECT_DECLS;
  58547. WOLFSSL_CTX *ctx_c = NULL;
  58548. WOLFSSL_CTX *ctx_s = NULL;
  58549. WOLFSSL *ssl_c = NULL;
  58550. WOLFSSL *ssl_s = NULL;
  58551. struct test_memio_ctx test_ctx;
  58552. WOLFSSL_ALERT_HISTORY h;
  58553. byte found = 0;
  58554. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58555. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58556. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58557. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58558. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58559. WOLFSSL_ERROR_WANT_READ);
  58560. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58561. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58562. WOLFSSL_ERROR_WANT_READ);
  58563. if (extra_round) {
  58564. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58565. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58566. WOLFSSL_ERROR_WANT_READ);
  58567. /* this will complete handshake from server side */
  58568. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58569. }
  58570. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  58571. &test_ctx.c_len, hs_message_type, &found), 0);
  58572. if (!found) {
  58573. wolfSSL_free(ssl_c);
  58574. wolfSSL_CTX_free(ctx_c);
  58575. wolfSSL_free(ssl_s);
  58576. wolfSSL_CTX_free(ctx_s);
  58577. return TEST_SKIPPED;
  58578. }
  58579. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58580. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58581. WOLFSSL_ERROR_WANT_READ);
  58582. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58583. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  58584. ExpectIntEQ(h.last_tx.level, alert_fatal);
  58585. wolfSSL_free(ssl_c);
  58586. wolfSSL_CTX_free(ctx_c);
  58587. wolfSSL_free(ssl_s);
  58588. wolfSSL_CTX_free(ctx_s);
  58589. return EXPECT_RESULT();
  58590. }
  58591. static int test_extra_alerts_skip_hs(void)
  58592. {
  58593. EXPECT_DECLS;
  58594. const byte _server_key_exchange = 12;
  58595. const byte _server_hello = 2;
  58596. const byte _certificate = 11;
  58597. /* server_hello */
  58598. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  58599. unexpected_message), TEST_FAIL);
  58600. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  58601. 0xff), TEST_FAIL);
  58602. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  58603. unexpected_message), TEST_FAIL);
  58604. return EXPECT_RESULT();
  58605. }
  58606. #else
  58607. static int test_extra_alerts_skip_hs(void)
  58608. {
  58609. return TEST_SKIPPED;
  58610. }
  58611. #endif
  58612. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58613. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  58614. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  58615. unsigned char* key, unsigned int key_max_len)
  58616. {
  58617. (void)ssl;
  58618. (void)id;
  58619. (void)key_max_len;
  58620. /* zero means error */
  58621. key[0] = 0x10;
  58622. return 1;
  58623. }
  58624. static int test_extra_alerts_bad_psk(void)
  58625. {
  58626. EXPECT_DECLS;
  58627. WOLFSSL_CTX *ctx_c = NULL;
  58628. WOLFSSL_CTX *ctx_s = NULL;
  58629. WOLFSSL *ssl_c = NULL;
  58630. WOLFSSL *ssl_s = NULL;
  58631. struct test_memio_ctx test_ctx;
  58632. WOLFSSL_ALERT_HISTORY h;
  58633. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58634. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58635. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58636. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  58637. WOLFSSL_SUCCESS);
  58638. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  58639. WOLFSSL_SUCCESS);
  58640. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  58641. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58642. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58643. WOLFSSL_ERROR_WANT_READ);
  58644. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58645. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58646. WOLFSSL_ERROR_WANT_READ);
  58647. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58648. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58649. WOLFSSL_ERROR_WANT_READ);
  58650. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58651. ExpectIntEQ(h.last_tx.code, handshake_failure);
  58652. ExpectIntEQ(h.last_tx.level, alert_fatal);
  58653. wolfSSL_free(ssl_c);
  58654. wolfSSL_CTX_free(ctx_c);
  58655. wolfSSL_free(ssl_s);
  58656. wolfSSL_CTX_free(ctx_s);
  58657. return EXPECT_RESULT();
  58658. }
  58659. #else
  58660. static int test_extra_alerts_bad_psk(void)
  58661. {
  58662. return TEST_SKIPPED;
  58663. }
  58664. #endif
  58665. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58666. && !defined(NO_PSK)
  58667. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  58668. const char* hint, char* identity, unsigned int id_max_len,
  58669. unsigned char* key, unsigned int key_max_len)
  58670. {
  58671. (void)ssl;
  58672. (void)hint;
  58673. (void)key_max_len;
  58674. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  58675. XSTRNCPY(identity, "Client_identity", id_max_len);
  58676. key[0] = 0x20;
  58677. return 1;
  58678. }
  58679. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  58680. const char* id, unsigned char* key, unsigned int key_max_len)
  58681. {
  58682. (void)ssl;
  58683. (void)id;
  58684. (void)key_max_len;
  58685. /* zero means error */
  58686. key[0] = 0x10;
  58687. return 1;
  58688. }
  58689. #endif
  58690. static int test_tls13_bad_psk_binder(void)
  58691. {
  58692. EXPECT_DECLS;
  58693. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58694. && !defined(NO_PSK)
  58695. WOLFSSL_CTX *ctx_c = NULL;
  58696. WOLFSSL_CTX *ctx_s = NULL;
  58697. WOLFSSL *ssl_c = NULL;
  58698. WOLFSSL *ssl_s = NULL;
  58699. struct test_memio_ctx test_ctx;
  58700. WOLFSSL_ALERT_HISTORY h;
  58701. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58702. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58703. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58704. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  58705. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  58706. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58707. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58708. WOLFSSL_ERROR_WANT_READ);
  58709. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58710. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58711. BAD_BINDER);
  58712. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58713. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58714. FATAL_ERROR);
  58715. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58716. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  58717. ExpectIntEQ(h.last_rx.level, alert_fatal);
  58718. wolfSSL_free(ssl_c);
  58719. wolfSSL_CTX_free(ctx_c);
  58720. wolfSSL_free(ssl_s);
  58721. wolfSSL_CTX_free(ctx_s);
  58722. #endif
  58723. return EXPECT_RESULT();
  58724. }
  58725. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  58726. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58727. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  58728. int sz, void *ctx)
  58729. {
  58730. static int sentServerHello = FALSE;
  58731. if (!sentServerHello) {
  58732. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  58733. size_t i;
  58734. if (sz < (int)sizeof(renegExt))
  58735. return WOLFSSL_CBIO_ERR_GENERAL;
  58736. /* Remove SCR from ServerHello */
  58737. for (i = 0; i < sz - sizeof(renegExt); i++) {
  58738. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  58739. /* Found the extension. Change it to something unrecognized. */
  58740. buf[i+1] = 0x11;
  58741. break;
  58742. }
  58743. }
  58744. sentServerHello = TRUE;
  58745. }
  58746. return EmbedSend(ssl, buf, sz, ctx);
  58747. }
  58748. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  58749. {
  58750. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  58751. }
  58752. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  58753. {
  58754. WOLFSSL_ALERT_HISTORY h;
  58755. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  58756. AssertIntEQ(h.last_rx.code, handshake_failure);
  58757. AssertIntEQ(h.last_rx.level, alert_fatal);
  58758. }
  58759. static int test_harden_no_secure_renegotiation(void)
  58760. {
  58761. EXPECT_DECLS;
  58762. callback_functions client_cbs, server_cbs;
  58763. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58764. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58765. client_cbs.method = wolfTLSv1_2_client_method;
  58766. server_cbs.method = wolfTLSv1_2_server_method;
  58767. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  58768. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  58769. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58770. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  58771. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  58772. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  58773. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  58774. server_cbs.last_err == FATAL_ERROR);
  58775. return EXPECT_RESULT();
  58776. }
  58777. #else
  58778. static int test_harden_no_secure_renegotiation(void)
  58779. {
  58780. return TEST_SKIPPED;
  58781. }
  58782. #endif
  58783. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58784. static int test_override_alt_cert_chain_cert_cb(int preverify,
  58785. WOLFSSL_X509_STORE_CTX* store)
  58786. {
  58787. fprintf(stderr, "preverify: %d\n", preverify);
  58788. fprintf(stderr, "store->error: %d\n", store->error);
  58789. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  58790. if (store->error == OCSP_INVALID_STATUS) {
  58791. fprintf(stderr, "Overriding OCSP error\n");
  58792. return 1;
  58793. }
  58794. #ifndef WOLFSSL_ALT_CERT_CHAINS
  58795. else if ((store->error == ASN_NO_SIGNER_E ||
  58796. store->error == ASN_SELF_SIGNED_E
  58797. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  58798. defined(HAVE_WEBSERVER)
  58799. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  58800. #endif
  58801. ) && store->error_depth == store->totalCerts - 1) {
  58802. fprintf(stderr, "Overriding no signer error only for root cert\n");
  58803. return 1;
  58804. }
  58805. #endif
  58806. else
  58807. return preverify;
  58808. }
  58809. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  58810. int urlSz, unsigned char* request, int requestSz,
  58811. unsigned char** response)
  58812. {
  58813. (void)ioCtx;
  58814. (void)url;
  58815. (void)urlSz;
  58816. (void)request;
  58817. (void)requestSz;
  58818. (void)response;
  58819. return -1;
  58820. }
  58821. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  58822. {
  58823. EXPECT_DECLS;
  58824. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  58825. test_override_alt_cert_chain_cert_cb);
  58826. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  58827. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  58828. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  58829. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  58830. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  58831. WOLFSSL_SUCCESS);
  58832. return EXPECT_RESULT();
  58833. }
  58834. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  58835. {
  58836. EXPECT_DECLS;
  58837. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  58838. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  58839. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  58840. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  58841. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  58842. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  58843. WOLFSSL_SUCCESS);
  58844. return EXPECT_RESULT();
  58845. }
  58846. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  58847. {
  58848. EXPECT_DECLS;
  58849. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  58850. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  58851. return EXPECT_RESULT();
  58852. }
  58853. static int test_override_alt_cert_chain(void)
  58854. {
  58855. EXPECT_DECLS;
  58856. size_t i;
  58857. struct test_params {
  58858. ctx_cb client_ctx_cb;
  58859. ctx_cb server_ctx_cb;
  58860. int result;
  58861. } params[] = {
  58862. {test_override_alt_cert_chain_client_ctx_ready,
  58863. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  58864. {test_override_alt_cert_chain_client_ctx_ready2,
  58865. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  58866. };
  58867. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  58868. test_ssl_cbf client_cbs, server_cbs;
  58869. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58870. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58871. fprintf(stderr, "test config: %d\n", (int)i);
  58872. client_cbs.ctx_ready = params[i].client_ctx_cb;
  58873. server_cbs.ctx_ready = params[i].server_ctx_cb;
  58874. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58875. &server_cbs, NULL), params[i].result);
  58876. ExpectIntEQ(client_cbs.return_code, params[i].result);
  58877. ExpectIntEQ(server_cbs.return_code, params[i].result);
  58878. }
  58879. return EXPECT_RESULT();
  58880. }
  58881. #else
  58882. static int test_override_alt_cert_chain(void)
  58883. {
  58884. return TEST_SKIPPED;
  58885. }
  58886. #endif
  58887. #if defined(HAVE_RPK)
  58888. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  58889. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  58890. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  58891. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  58892. {
  58893. int ret = WOLFSSL_SUCCESS;
  58894. (void)mode;
  58895. (void)strctx;
  58896. WOLFSSL_ENTER("MyRpkVerifyCb");
  58897. return ret;
  58898. }
  58899. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  58900. static WC_INLINE int test_rpk_memio_setup(
  58901. struct test_memio_ctx *ctx,
  58902. WOLFSSL_CTX **ctx_c,
  58903. WOLFSSL_CTX **ctx_s,
  58904. WOLFSSL **ssl_c,
  58905. WOLFSSL **ssl_s,
  58906. method_provider method_c,
  58907. method_provider method_s,
  58908. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  58909. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  58910. const char* pkey_c, int fmt_kc, /* client private key and format */
  58911. const char* pkey_s, int fmt_ks /* server private key and format */
  58912. )
  58913. {
  58914. int ret;
  58915. if (ctx_c != NULL && *ctx_c == NULL) {
  58916. *ctx_c = wolfSSL_CTX_new(method_c());
  58917. if (*ctx_c == NULL) {
  58918. return -1;
  58919. }
  58920. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  58921. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  58922. if (ret != WOLFSSL_SUCCESS) {
  58923. return -1;
  58924. }
  58925. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  58926. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  58927. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  58928. if (ret != WOLFSSL_SUCCESS) {
  58929. return -1;
  58930. }
  58931. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  58932. if (ret != WOLFSSL_SUCCESS) {
  58933. return -1;
  58934. }
  58935. }
  58936. if (ctx_s != NULL && *ctx_s == NULL) {
  58937. *ctx_s = wolfSSL_CTX_new(method_s());
  58938. if (*ctx_s == NULL) {
  58939. return -1;
  58940. }
  58941. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  58942. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  58943. if (ret != WOLFSSL_SUCCESS) {
  58944. return -1;
  58945. }
  58946. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  58947. if (ret != WOLFSSL_SUCCESS) {
  58948. return -1;
  58949. }
  58950. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  58951. if (ret != WOLFSSL_SUCCESS) {
  58952. return -1;
  58953. }
  58954. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  58955. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  58956. if (ctx->s_ciphers != NULL) {
  58957. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  58958. if (ret != WOLFSSL_SUCCESS) {
  58959. return -1;
  58960. }
  58961. }
  58962. }
  58963. if (ctx_c != NULL && ssl_c != NULL) {
  58964. *ssl_c = wolfSSL_new(*ctx_c);
  58965. if (*ssl_c == NULL) {
  58966. return -1;
  58967. }
  58968. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  58969. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  58970. }
  58971. if (ctx_s != NULL && ssl_s != NULL) {
  58972. *ssl_s = wolfSSL_new(*ctx_s);
  58973. if (*ssl_s == NULL) {
  58974. return -1;
  58975. }
  58976. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  58977. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  58978. #if !defined(NO_DH)
  58979. SetDH(*ssl_s);
  58980. #endif
  58981. }
  58982. return 0;
  58983. }
  58984. #endif /* HAVE_RPK */
  58985. static int test_rpk_set_xxx_cert_type(void)
  58986. {
  58987. EXPECT_DECLS;
  58988. #if defined(HAVE_RPK)
  58989. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  58990. WOLFSSL_CTX* ctx = NULL;
  58991. WOLFSSL* ssl = NULL;
  58992. int tp;
  58993. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  58994. ExpectNotNull(ctx);
  58995. ssl = wolfSSL_new(ctx);
  58996. ExpectNotNull(ssl);
  58997. /*--------------------------------------------*/
  58998. /* tests for wolfSSL_CTX_set_client_cert_type */
  58999. /*--------------------------------------------*/
  59000. /* illegal parameter test caces */
  59001. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  59002. MAX_CLIENT_CERT_TYPE_CNT),
  59003. BAD_FUNC_ARG);
  59004. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59005. sizeof(ctype)),
  59006. BAD_FUNC_ARG);
  59007. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59008. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59009. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59010. MAX_CLIENT_CERT_TYPE_CNT),
  59011. BAD_FUNC_ARG);
  59012. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59013. ctype[1] = 10; /* set unknown cert type */
  59014. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59015. MAX_CLIENT_CERT_TYPE_CNT),
  59016. BAD_FUNC_ARG);
  59017. /* pass larger type count */
  59018. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59019. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59020. ctype[2] = 1; /* pass unacceptable type count */
  59021. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59022. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59023. BAD_FUNC_ARG);
  59024. /* should accept NULL for type buffer */
  59025. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  59026. MAX_CLIENT_CERT_TYPE_CNT),
  59027. WOLFSSL_SUCCESS);
  59028. /* should accept zero for type count */
  59029. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59030. 0),
  59031. WOLFSSL_SUCCESS);
  59032. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59033. MAX_CLIENT_CERT_TYPE_CNT),
  59034. WOLFSSL_SUCCESS);
  59035. /*--------------------------------------------*/
  59036. /* tests for wolfSSL_CTX_set_server_cert_type */
  59037. /*--------------------------------------------*/
  59038. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  59039. MAX_SERVER_CERT_TYPE_CNT),
  59040. BAD_FUNC_ARG);
  59041. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59042. sizeof(ctype)),
  59043. BAD_FUNC_ARG);
  59044. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59045. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59046. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59047. MAX_SERVER_CERT_TYPE_CNT),
  59048. BAD_FUNC_ARG);
  59049. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59050. ctype[1] = 10; /* set unknown cert type */
  59051. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59052. MAX_SERVER_CERT_TYPE_CNT),
  59053. BAD_FUNC_ARG);
  59054. /* pass larger type count */
  59055. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59056. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59057. ctype[2] = 1; /* pass unacceptable type count */
  59058. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59059. MAX_SERVER_CERT_TYPE_CNT + 1),
  59060. BAD_FUNC_ARG);
  59061. /* should accept NULL for type buffer */
  59062. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  59063. MAX_SERVER_CERT_TYPE_CNT),
  59064. WOLFSSL_SUCCESS);
  59065. /* should accept zero for type count */
  59066. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59067. 0),
  59068. WOLFSSL_SUCCESS);
  59069. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59070. MAX_CLIENT_CERT_TYPE_CNT),
  59071. WOLFSSL_SUCCESS);
  59072. /*--------------------------------------------*/
  59073. /* tests for wolfSSL_set_client_cert_type */
  59074. /*--------------------------------------------*/
  59075. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  59076. MAX_CLIENT_CERT_TYPE_CNT),
  59077. BAD_FUNC_ARG);
  59078. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59079. sizeof(ctype)),
  59080. BAD_FUNC_ARG);
  59081. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59082. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59083. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59084. MAX_CLIENT_CERT_TYPE_CNT),
  59085. BAD_FUNC_ARG);
  59086. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59087. ctype[1] = 10; /* set unknown cert type */
  59088. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59089. MAX_CLIENT_CERT_TYPE_CNT),
  59090. BAD_FUNC_ARG);
  59091. /* pass larger type count */
  59092. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59093. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59094. ctype[2] = 1; /* pass unacceptable type count */
  59095. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59096. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59097. BAD_FUNC_ARG);
  59098. /* should accept NULL for type buffer */
  59099. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  59100. MAX_CLIENT_CERT_TYPE_CNT),
  59101. WOLFSSL_SUCCESS);
  59102. /* should accept zero for type count */
  59103. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59104. 0),
  59105. WOLFSSL_SUCCESS);
  59106. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59107. MAX_CLIENT_CERT_TYPE_CNT),
  59108. WOLFSSL_SUCCESS);
  59109. /*--------------------------------------------*/
  59110. /* tests for wolfSSL_CTX_set_server_cert_type */
  59111. /*--------------------------------------------*/
  59112. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  59113. MAX_SERVER_CERT_TYPE_CNT),
  59114. BAD_FUNC_ARG);
  59115. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59116. sizeof(ctype)),
  59117. BAD_FUNC_ARG);
  59118. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59119. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59120. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59121. MAX_SERVER_CERT_TYPE_CNT),
  59122. BAD_FUNC_ARG);
  59123. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59124. ctype[1] = 10; /* set unknown cert type */
  59125. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59126. MAX_SERVER_CERT_TYPE_CNT),
  59127. BAD_FUNC_ARG);
  59128. /* pass larger type count */
  59129. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59130. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59131. ctype[2] = 1; /* pass unacceptable type count */
  59132. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59133. MAX_SERVER_CERT_TYPE_CNT + 1),
  59134. BAD_FUNC_ARG);
  59135. /* should accept NULL for type buffer */
  59136. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  59137. MAX_SERVER_CERT_TYPE_CNT),
  59138. WOLFSSL_SUCCESS);
  59139. /* should accept zero for type count */
  59140. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59141. 0),
  59142. WOLFSSL_SUCCESS);
  59143. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59144. MAX_SERVER_CERT_TYPE_CNT),
  59145. WOLFSSL_SUCCESS);
  59146. /*------------------------------------------------*/
  59147. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  59148. /*------------------------------------------------*/
  59149. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  59150. BAD_FUNC_ARG);
  59151. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  59152. BAD_FUNC_ARG);
  59153. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  59154. BAD_FUNC_ARG);
  59155. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  59156. BAD_FUNC_ARG);
  59157. /* clean up */
  59158. wolfSSL_free(ssl);
  59159. wolfSSL_CTX_free(ctx);
  59160. #endif
  59161. return EXPECT_RESULT();
  59162. }
  59163. static int test_tls13_rpk_handshake(void)
  59164. {
  59165. EXPECT_DECLS;
  59166. #if defined(HAVE_RPK)
  59167. int ret = 0;
  59168. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  59169. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  59170. struct test_memio_ctx test_ctx;
  59171. int err;
  59172. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  59173. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  59174. int typeCnt_c;
  59175. int typeCnt_s;
  59176. int tp;
  59177. (void)err;
  59178. (void)typeCnt_c;
  59179. (void)typeCnt_s;
  59180. (void)certType_c;
  59181. (void)certType_s;
  59182. /* TLS1.2
  59183. * Both client and server load x509 cert and start handshaking.
  59184. * Check no negotiation occurred.
  59185. */
  59186. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59187. ExpectIntEQ(
  59188. test_rpk_memio_setup(
  59189. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59190. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  59191. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59192. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59193. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59194. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  59195. , 0);
  59196. /* set client certificate type in client end */
  59197. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59198. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59199. typeCnt_c = 2;
  59200. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59201. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59202. typeCnt_s = 2;
  59203. /* both clien and server do not call client/server_cert_type APIs,
  59204. * expecting default settings works and no negotiation performed.
  59205. */
  59206. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59207. return TEST_FAIL;
  59208. /* confirm no negotiation occurred */
  59209. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59210. WOLFSSL_SUCCESS);
  59211. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59212. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59213. WOLFSSL_SUCCESS);
  59214. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59215. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59216. WOLFSSL_SUCCESS);
  59217. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59218. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59219. WOLFSSL_SUCCESS);
  59220. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59221. wolfSSL_free(ssl_c);
  59222. wolfSSL_CTX_free(ctx_c);
  59223. wolfSSL_free(ssl_s);
  59224. wolfSSL_CTX_free(ctx_s);
  59225. ssl_c = ssl_s = NULL;
  59226. ctx_c = ctx_s = NULL;
  59227. /* Both client and server load x509 cert and start handshaking.
  59228. * Check no negotiation occurred.
  59229. */
  59230. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59231. ExpectIntEQ(
  59232. test_rpk_memio_setup(
  59233. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59234. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59235. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59236. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59237. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59238. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59239. , 0);
  59240. /* set client certificate type in client end */
  59241. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59242. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59243. typeCnt_c = 2;
  59244. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59245. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59246. typeCnt_s = 2;
  59247. /* both clien and server do not call client/server_cert_type APIs,
  59248. * expecting default settings works and no negotiation performed.
  59249. */
  59250. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59251. return TEST_FAIL;
  59252. /* confirm no negotiation occurred */
  59253. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59254. WOLFSSL_SUCCESS);
  59255. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59256. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59257. WOLFSSL_SUCCESS);
  59258. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59259. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59260. WOLFSSL_SUCCESS);
  59261. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59262. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59263. WOLFSSL_SUCCESS);
  59264. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59265. wolfSSL_free(ssl_c);
  59266. wolfSSL_CTX_free(ctx_c);
  59267. wolfSSL_free(ssl_s);
  59268. wolfSSL_CTX_free(ctx_s);
  59269. ssl_c = ssl_s = NULL;
  59270. ctx_c = ctx_s = NULL;
  59271. /* Both client and server load RPK cert and start handshaking.
  59272. * Confirm negotiated cert types match as expected.
  59273. */
  59274. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59275. ExpectIntEQ(
  59276. test_rpk_memio_setup(
  59277. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59278. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59279. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59280. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59281. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59282. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59283. , 0);
  59284. /* set client certificate type in client end */
  59285. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59286. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59287. typeCnt_c = 2;
  59288. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59289. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59290. typeCnt_s = 2;
  59291. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59292. WOLFSSL_SUCCESS);
  59293. /* set server certificate type in client end */
  59294. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59295. WOLFSSL_SUCCESS);
  59296. /* set client certificate type in server end */
  59297. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59298. WOLFSSL_SUCCESS);
  59299. /* set server certificate type in server end */
  59300. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59301. WOLFSSL_SUCCESS);
  59302. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59303. return TEST_FAIL;
  59304. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59305. WOLFSSL_SUCCESS);
  59306. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59307. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59308. WOLFSSL_SUCCESS);
  59309. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59310. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59311. WOLFSSL_SUCCESS);
  59312. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59313. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59314. WOLFSSL_SUCCESS);
  59315. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59316. wolfSSL_free(ssl_c);
  59317. wolfSSL_CTX_free(ctx_c);
  59318. wolfSSL_free(ssl_s);
  59319. wolfSSL_CTX_free(ctx_s);
  59320. ssl_c = ssl_s = NULL;
  59321. ctx_c = ctx_s = NULL;
  59322. /* TLS1.2
  59323. * Both client and server load RPK cert and start handshaking.
  59324. * Confirm negotiated cert types match as expected.
  59325. */
  59326. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59327. ExpectIntEQ(
  59328. test_rpk_memio_setup(
  59329. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59330. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  59331. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59332. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59333. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59334. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59335. , 0);
  59336. /* set client certificate type in client end */
  59337. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59338. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59339. typeCnt_c = 2;
  59340. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59341. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59342. typeCnt_s = 2;
  59343. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59344. WOLFSSL_SUCCESS);
  59345. /* set server certificate type in client end */
  59346. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59347. WOLFSSL_SUCCESS);
  59348. /* set client certificate type in server end */
  59349. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59350. WOLFSSL_SUCCESS);
  59351. /* set server certificate type in server end */
  59352. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59353. WOLFSSL_SUCCESS);
  59354. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59355. return TEST_FAIL;
  59356. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59357. WOLFSSL_SUCCESS);
  59358. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59359. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59360. WOLFSSL_SUCCESS);
  59361. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59362. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59363. WOLFSSL_SUCCESS);
  59364. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59365. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59366. WOLFSSL_SUCCESS);
  59367. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59368. wolfSSL_free(ssl_c);
  59369. wolfSSL_CTX_free(ctx_c);
  59370. wolfSSL_free(ssl_s);
  59371. wolfSSL_CTX_free(ctx_s);
  59372. ssl_c = ssl_s = NULL;
  59373. ctx_c = ctx_s = NULL;
  59374. /* Both client and server load x509 cert.
  59375. * Have client call set_client_cert_type with both RPK and x509.
  59376. * This doesn't makes client add client cert type extension to ClientHello,
  59377. * since it does not load RPK cert actually.
  59378. */
  59379. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59380. ExpectIntEQ(
  59381. test_rpk_memio_setup(
  59382. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59383. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59384. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59385. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59386. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59387. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59388. , 0);
  59389. /* set client certificate type in client end */
  59390. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59391. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59392. typeCnt_c = 2;
  59393. /* client indicates both RPK and x509 certs are available but loaded RPK
  59394. * cert only. It does not have client add client-cert-type extension in CH.
  59395. */
  59396. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59397. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59398. typeCnt_c = 2;
  59399. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59400. WOLFSSL_SUCCESS);
  59401. /* client indicates both RPK and x509 certs are acceptable */
  59402. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59403. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59404. typeCnt_s = 2;
  59405. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59406. WOLFSSL_SUCCESS);
  59407. /* server indicates both RPK and x509 certs are acceptable */
  59408. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59409. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59410. typeCnt_c = 2;
  59411. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59412. WOLFSSL_SUCCESS);
  59413. /* server should indicate only RPK cert is available */
  59414. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59415. certType_s[1] = -1;
  59416. typeCnt_s = 1;
  59417. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59418. WOLFSSL_SUCCESS);
  59419. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59420. return TEST_FAIL;
  59421. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  59422. * be returned as cert type.
  59423. */
  59424. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59425. WOLFSSL_SUCCESS);
  59426. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59427. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59428. WOLFSSL_SUCCESS);
  59429. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59430. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59431. WOLFSSL_SUCCESS);
  59432. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59433. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59434. WOLFSSL_SUCCESS);
  59435. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59436. wolfSSL_free(ssl_c);
  59437. wolfSSL_CTX_free(ctx_c);
  59438. wolfSSL_free(ssl_s);
  59439. wolfSSL_CTX_free(ctx_s);
  59440. ssl_c = ssl_s = NULL;
  59441. ctx_c = ctx_s = NULL;
  59442. /* Have client load RPK cert and have server load x509 cert.
  59443. * Check the negotiation result from both ends.
  59444. */
  59445. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59446. ExpectIntEQ(
  59447. test_rpk_memio_setup(
  59448. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59449. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59450. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59451. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59452. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59453. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59454. , 0);
  59455. /* have client tell to use RPK cert */
  59456. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59457. certType_c[1] = -1;
  59458. typeCnt_c = 1;
  59459. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59460. WOLFSSL_SUCCESS);
  59461. /* have client tell to accept both RPK and x509 cert */
  59462. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59463. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  59464. typeCnt_s = 2;
  59465. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59466. WOLFSSL_SUCCESS);
  59467. /* have server accept to both RPK and x509 cert */
  59468. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  59469. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  59470. typeCnt_c = 2;
  59471. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59472. WOLFSSL_SUCCESS);
  59473. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  59474. * end, expecting the default setting works.
  59475. */
  59476. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59477. return TEST_FAIL;
  59478. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59479. WOLFSSL_SUCCESS);
  59480. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59481. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59482. WOLFSSL_SUCCESS);
  59483. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59484. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59485. WOLFSSL_SUCCESS);
  59486. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59487. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59488. WOLFSSL_SUCCESS);
  59489. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59490. wolfSSL_free(ssl_c);
  59491. wolfSSL_CTX_free(ctx_c);
  59492. wolfSSL_free(ssl_s);
  59493. wolfSSL_CTX_free(ctx_s);
  59494. ssl_c = ssl_s = NULL;
  59495. ctx_c = ctx_s = NULL;
  59496. /* Have both client and server load RPK cert, however, have server
  59497. * indicate its cert type x509.
  59498. * Client is expected to detect the cert type mismatch then to send alert
  59499. * with "unsupported_certificate".
  59500. */
  59501. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59502. ExpectIntEQ(
  59503. test_rpk_memio_setup(
  59504. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59505. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59506. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59507. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  59508. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59509. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59510. , 0);
  59511. /* have client tell to use RPK cert */
  59512. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59513. certType_c[1] = -1;
  59514. typeCnt_c = 1;
  59515. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59516. WOLFSSL_SUCCESS);
  59517. /* have client tell to accept both RPK and x509 cert */
  59518. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59519. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  59520. typeCnt_s = 2;
  59521. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59522. WOLFSSL_SUCCESS);
  59523. /* have server accept to both RPK and x509 cert */
  59524. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  59525. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  59526. typeCnt_c = 2;
  59527. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59528. WOLFSSL_SUCCESS);
  59529. /* have server tell to use x509 cert intentionally. This will bring
  59530. * certificate type mismatch in client side.
  59531. */
  59532. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59533. certType_s[1] = -1;
  59534. typeCnt_s = 1;
  59535. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59536. WOLFSSL_SUCCESS);
  59537. /* expect client detect cert type mismatch then send Alert */
  59538. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  59539. if (ret != -1)
  59540. return TEST_FAIL;
  59541. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  59542. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59543. WOLFSSL_SUCCESS);
  59544. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59545. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59546. WOLFSSL_SUCCESS);
  59547. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59548. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59549. WOLFSSL_SUCCESS);
  59550. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59551. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59552. WOLFSSL_SUCCESS);
  59553. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59554. wolfSSL_free(ssl_c);
  59555. wolfSSL_CTX_free(ctx_c);
  59556. wolfSSL_free(ssl_s);
  59557. wolfSSL_CTX_free(ctx_s);
  59558. ssl_c = ssl_s = NULL;
  59559. ctx_c = ctx_s = NULL;
  59560. /* Have client load x509 cert and server load RPK cert,
  59561. * however, have client indicate its cert type RPK.
  59562. * Server is expected to detect the cert type mismatch then to send alert
  59563. * with "unsupported_certificate".
  59564. */
  59565. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59566. ExpectIntEQ(
  59567. test_rpk_memio_setup(
  59568. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59569. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59570. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59571. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59572. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59573. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59574. , 0);
  59575. /* have client tell to use RPK cert intentionally */
  59576. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59577. certType_c[1] = -1;
  59578. typeCnt_c = 1;
  59579. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59580. WOLFSSL_SUCCESS);
  59581. /* have client tell to accept both RPK and x509 cert */
  59582. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59583. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  59584. typeCnt_s = 2;
  59585. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59586. WOLFSSL_SUCCESS);
  59587. /* have server accept to both RPK and x509 cert */
  59588. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  59589. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  59590. typeCnt_c = 2;
  59591. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59592. WOLFSSL_SUCCESS);
  59593. /* have server tell to use x509 cert intentionally. This will bring
  59594. * certificate type mismatch in client side.
  59595. */
  59596. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59597. certType_s[1] = -1;
  59598. typeCnt_s = 1;
  59599. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59600. WOLFSSL_SUCCESS);
  59601. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  59602. /* expect server detect cert type mismatch then send Alert */
  59603. ExpectIntNE(ret, 0);
  59604. err = wolfSSL_get_error(ssl_c, ret);
  59605. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  59606. /* client did not load RPK cert actually, so negotiation did not happen */
  59607. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59608. WOLFSSL_SUCCESS);
  59609. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59610. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59611. WOLFSSL_SUCCESS);
  59612. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59613. /* client did not load RPK cert actually, so negotiation did not happen */
  59614. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59615. WOLFSSL_SUCCESS);
  59616. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59617. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59618. WOLFSSL_SUCCESS);
  59619. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59620. wolfSSL_free(ssl_c);
  59621. wolfSSL_CTX_free(ctx_c);
  59622. wolfSSL_free(ssl_s);
  59623. wolfSSL_CTX_free(ctx_s);
  59624. ssl_c = ssl_s = NULL;
  59625. ctx_c = ctx_s = NULL;
  59626. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59627. /* Both client and server load RPK cert and set certificate verify
  59628. * callbacks then start handshaking.
  59629. * Confirm both side can refer the peer's cert.
  59630. */
  59631. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59632. ExpectIntEQ(
  59633. test_rpk_memio_setup(
  59634. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59635. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59636. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59637. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59638. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59639. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59640. , 0);
  59641. /* set client certificate type in client end */
  59642. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59643. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59644. typeCnt_c = 2;
  59645. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59646. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59647. typeCnt_s = 2;
  59648. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59649. WOLFSSL_SUCCESS);
  59650. /* set server certificate type in client end */
  59651. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59652. WOLFSSL_SUCCESS);
  59653. /* set client certificate type in server end */
  59654. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59655. WOLFSSL_SUCCESS);
  59656. /* set server certificate type in server end */
  59657. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59658. WOLFSSL_SUCCESS);
  59659. /* set certificate verify callback to both client and server */
  59660. int isServer = 0;
  59661. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  59662. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  59663. isServer = 1;
  59664. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  59665. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  59666. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  59667. if (ret != 0)
  59668. return TEST_FAIL;
  59669. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59670. WOLFSSL_SUCCESS);
  59671. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59672. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59673. WOLFSSL_SUCCESS);
  59674. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59675. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59676. WOLFSSL_SUCCESS);
  59677. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59678. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59679. WOLFSSL_SUCCESS);
  59680. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59681. wolfSSL_free(ssl_c);
  59682. wolfSSL_CTX_free(ctx_c);
  59683. wolfSSL_free(ssl_s);
  59684. wolfSSL_CTX_free(ctx_s);
  59685. ssl_c = ssl_s = NULL;
  59686. ctx_c = ctx_s = NULL;
  59687. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  59688. #endif /* HAVE_RPK */
  59689. return EXPECT_RESULT();
  59690. }
  59691. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59692. static int test_dtls13_bad_epoch_ch(void)
  59693. {
  59694. EXPECT_DECLS;
  59695. WOLFSSL_CTX *ctx_c = NULL;
  59696. WOLFSSL_CTX *ctx_s = NULL;
  59697. WOLFSSL *ssl_c = NULL;
  59698. WOLFSSL *ssl_s = NULL;
  59699. struct test_memio_ctx test_ctx;
  59700. const int EPOCH_OFF = 3;
  59701. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59702. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59703. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59704. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  59705. * with just one message */
  59706. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  59707. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59708. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59709. WOLFSSL_ERROR_WANT_READ);
  59710. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  59711. /* first CH should use epoch 0x0 */
  59712. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  59713. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  59714. /* change epoch to 2 */
  59715. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  59716. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59717. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59718. WOLFSSL_ERROR_WANT_READ);
  59719. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  59720. /* resend the CH */
  59721. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59722. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59723. wolfSSL_free(ssl_c);
  59724. wolfSSL_CTX_free(ctx_c);
  59725. wolfSSL_free(ssl_s);
  59726. wolfSSL_CTX_free(ctx_s);
  59727. return EXPECT_RESULT();
  59728. }
  59729. #else
  59730. static int test_dtls13_bad_epoch_ch(void)
  59731. {
  59732. return TEST_SKIPPED;
  59733. }
  59734. #endif
  59735. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  59736. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  59737. {
  59738. EXPECT_DECLS;
  59739. WOLFSSL_SESSION *sess = NULL;
  59740. /* Setup the session to avoid errors */
  59741. ssl->session->timeout = -1;
  59742. ssl->session->side = WOLFSSL_CLIENT_END;
  59743. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  59744. defined(HAVE_SESSION_TICKET))
  59745. ssl->session->version = ssl->version;
  59746. #endif
  59747. /* Force a short session ID to be sent */
  59748. ssl->session->sessionIDSz = 4;
  59749. #ifndef NO_SESSION_CACHE_REF
  59750. /* Allow the client cache to be used */
  59751. ssl->session->idLen = 4;
  59752. #endif
  59753. ssl->session->isSetup = 1;
  59754. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  59755. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  59756. return EXPECT_RESULT();
  59757. }
  59758. static int test_short_session_id(void)
  59759. {
  59760. EXPECT_DECLS;
  59761. test_ssl_cbf client_cbf;
  59762. test_ssl_cbf server_cbf;
  59763. size_t i;
  59764. struct {
  59765. method_provider client_meth;
  59766. method_provider server_meth;
  59767. const char* tls_version;
  59768. } params[] = {
  59769. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  59770. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  59771. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  59772. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  59773. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  59774. #ifdef WOLFSSL_DTLS13
  59775. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  59776. #endif
  59777. #endif
  59778. #ifndef WOLFSSL_NO_TLS12
  59779. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  59780. #ifdef WOLFSSL_DTLS
  59781. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  59782. #endif
  59783. #endif
  59784. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  59785. !defined(NO_DES3))
  59786. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  59787. #ifdef WOLFSSL_DTLS
  59788. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  59789. #endif
  59790. #endif
  59791. };
  59792. fprintf(stderr, "\n");
  59793. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  59794. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  59795. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  59796. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  59797. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  59798. client_cbf.method = params[i].client_meth;
  59799. server_cbf.method = params[i].server_meth;
  59800. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  59801. &server_cbf, NULL), TEST_SUCCESS);
  59802. }
  59803. return EXPECT_RESULT();
  59804. }
  59805. #else
  59806. static int test_short_session_id(void)
  59807. {
  59808. return TEST_SKIPPED;
  59809. }
  59810. #endif
  59811. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  59812. && defined(WOLFSSL_DTLS13)
  59813. static byte* test_find_string(const char *string,
  59814. byte *buf, int buf_size)
  59815. {
  59816. int string_size, i;
  59817. string_size = (int)XSTRLEN(string);
  59818. for (i = 0; i < buf_size - string_size - 1; i++) {
  59819. if (XSTRCMP((char*)&buf[i], string) == 0)
  59820. return &buf[i];
  59821. }
  59822. return NULL;
  59823. }
  59824. static int test_wolfSSL_dtls13_null_cipher(void)
  59825. {
  59826. EXPECT_DECLS;
  59827. WOLFSSL_CTX *ctx_c = NULL;
  59828. WOLFSSL_CTX *ctx_s = NULL;
  59829. WOLFSSL *ssl_c = NULL;
  59830. WOLFSSL *ssl_s = NULL;
  59831. struct test_memio_ctx test_ctx;
  59832. const char *test_str = "test";
  59833. int test_str_size;
  59834. byte buf[255], *ptr = NULL;
  59835. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59836. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  59837. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59838. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59839. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59840. test_str_size = XSTRLEN("test") + 1;
  59841. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59842. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  59843. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  59844. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59845. /* check that the packet was sent cleartext */
  59846. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  59847. test_ctx.s_len));
  59848. if (ptr != NULL) {
  59849. /* modify the message */
  59850. *ptr = 'H';
  59851. /* bad messages should be ignored in DTLS */
  59852. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  59853. ExpectIntEQ(ssl_s->error, WANT_READ);
  59854. }
  59855. wolfSSL_free(ssl_c);
  59856. wolfSSL_free(ssl_s);
  59857. wolfSSL_CTX_free(ctx_c);
  59858. wolfSSL_CTX_free(ctx_s);
  59859. return TEST_SUCCESS;
  59860. }
  59861. #else
  59862. static int test_wolfSSL_dtls13_null_cipher(void)
  59863. {
  59864. return TEST_SKIPPED;
  59865. }
  59866. #endif
  59867. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59868. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59869. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  59870. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  59871. {
  59872. SOCKADDR_S peer;
  59873. XSOCKLENT len;
  59874. int ret;
  59875. XMEMSET((byte*)&peer, 0, sizeof(peer));
  59876. len = sizeof(peer);
  59877. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  59878. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  59879. return -1;
  59880. switch (peer.ss_family) {
  59881. #ifdef WOLFSSL_IPV6
  59882. case WOLFSSL_IP6: {
  59883. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  59884. break;
  59885. }
  59886. #endif /* WOLFSSL_IPV6 */
  59887. case WOLFSSL_IP4:
  59888. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  59889. break;
  59890. default:
  59891. return -1;
  59892. }
  59893. return 0;
  59894. }
  59895. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  59896. {
  59897. char buf[1] = {'t'};
  59898. SOCKADDR_IN_T addr;
  59899. int sock_fd;
  59900. word16 port;
  59901. int err;
  59902. (void)ssl;
  59903. (void)ctx;
  59904. if (ssl == NULL)
  59905. return -1;
  59906. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  59907. if (err != 0)
  59908. return -1;
  59909. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  59910. if (sock_fd == -1)
  59911. return -1;
  59912. build_addr(&addr, wolfSSLIP, port, 1, 0);
  59913. /* send a packet to the server. Being another socket, the kernel will ensure
  59914. * the source port will be different. */
  59915. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  59916. sizeof(addr));
  59917. close(sock_fd);
  59918. if (err == -1)
  59919. return -1;
  59920. return 0;
  59921. }
  59922. /* setup a SSL session but just after the handshake send a packet to the server
  59923. * with a source address different than the one of the connected client. The I/O
  59924. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  59925. * is done in test_dtls_msg_from_other_peer_cb */
  59926. static int test_dtls_msg_from_other_peer(void)
  59927. {
  59928. EXPECT_DECLS;
  59929. callback_functions client_cbs;
  59930. callback_functions server_cbs;
  59931. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  59932. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  59933. client_cbs.method = wolfDTLSv1_2_client_method;
  59934. server_cbs.method = wolfDTLSv1_2_server_method;
  59935. client_cbs.doUdp = 1;
  59936. server_cbs.doUdp = 1;
  59937. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  59938. test_dtls_msg_from_other_peer_cb);
  59939. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  59940. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  59941. return EXPECT_RESULT();
  59942. }
  59943. #else
  59944. static int test_dtls_msg_from_other_peer(void)
  59945. {
  59946. return TEST_SKIPPED;
  59947. }
  59948. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59949. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59950. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  59951. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  59952. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59953. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  59954. static int test_dtls_ipv6_check(void)
  59955. {
  59956. EXPECT_DECLS;
  59957. WOLFSSL_CTX *ctx_c = NULL;
  59958. WOLFSSL_CTX *ctx_s = NULL;
  59959. WOLFSSL *ssl_c = NULL;
  59960. WOLFSSL *ssl_s = NULL;
  59961. SOCKADDR_IN fake_addr6;
  59962. int sockfd = -1;
  59963. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  59964. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  59965. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  59966. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  59967. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59968. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  59969. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59970. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  59971. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  59972. /* mimic a sockaddr_in6 struct, this way we can't test without
  59973. * WOLFSSL_IPV6 */
  59974. fake_addr6.sin_family = WOLFSSL_IP6;
  59975. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  59976. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  59977. /* can't return error here, as the peer is opaque for wolfssl library at
  59978. * this point */
  59979. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  59980. WOLFSSL_SUCCESS);
  59981. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  59982. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  59983. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59984. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  59985. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  59986. WOLFSSL_SUCCESS);
  59987. /* reuse the socket */
  59988. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  59989. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  59990. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59991. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  59992. if (sockfd != -1)
  59993. close(sockfd);
  59994. wolfSSL_free(ssl_c);
  59995. wolfSSL_CTX_free(ctx_c);
  59996. wolfSSL_free(ssl_s);
  59997. wolfSSL_CTX_free(ctx_s);
  59998. return EXPECT_RESULT();
  59999. }
  60000. #else
  60001. static int test_dtls_ipv6_check(void)
  60002. {
  60003. return TEST_SKIPPED;
  60004. }
  60005. #endif
  60006. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60007. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60008. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  60009. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  60010. {
  60011. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60012. }
  60013. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  60014. {
  60015. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  60016. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  60017. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  60018. }
  60019. else {
  60020. char testMsg[] = "Message after SCR";
  60021. char msgBuf[sizeof(testMsg)];
  60022. int ret;
  60023. if (!wolfSSL_is_server(ssl)) {
  60024. AssertIntEQ(WOLFSSL_SUCCESS,
  60025. wolfSSL_set_session(ssl,
  60026. test_wolfSSL_SCR_after_resumption_session));
  60027. }
  60028. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60029. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60030. sizeof(testMsg));
  60031. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60032. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  60033. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60034. AssertIntEQ(ret, sizeof(msgBuf));
  60035. }
  60036. }
  60037. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  60038. {
  60039. AssertIntEQ(WOLFSSL_SUCCESS,
  60040. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  60041. }
  60042. static int test_wolfSSL_SCR_after_resumption(void)
  60043. {
  60044. EXPECT_DECLS;
  60045. callback_functions func_cb_client;
  60046. callback_functions func_cb_server;
  60047. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60048. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60049. func_cb_client.method = wolfTLSv1_2_client_method;
  60050. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60051. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60052. func_cb_server.method = wolfTLSv1_2_server_method;
  60053. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60054. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60055. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60056. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60057. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  60058. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60059. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60060. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60061. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60062. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  60063. return EXPECT_RESULT();
  60064. }
  60065. #else
  60066. static int test_wolfSSL_SCR_after_resumption(void)
  60067. {
  60068. return TEST_SKIPPED;
  60069. }
  60070. #endif
  60071. static int test_wolfSSL_configure_args(void)
  60072. {
  60073. EXPECT_DECLS;
  60074. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  60075. ExpectNotNull(wolfSSL_configure_args());
  60076. #endif
  60077. return EXPECT_RESULT();
  60078. }
  60079. static int test_dtls_no_extensions(void)
  60080. {
  60081. EXPECT_DECLS;
  60082. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60083. !defined(WOLFSSL_NO_TLS12)
  60084. WOLFSSL *ssl_s = NULL;
  60085. WOLFSSL_CTX *ctx_s = NULL;
  60086. struct test_memio_ctx test_ctx;
  60087. const byte chNoExtensions[] = {
  60088. /* Handshake type */
  60089. 0x16,
  60090. /* Version */
  60091. 0xfe, 0xff,
  60092. /* Epoch */
  60093. 0x00, 0x00,
  60094. /* Seq number */
  60095. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60096. /* Length */
  60097. 0x00, 0x40,
  60098. /* CH type */
  60099. 0x01,
  60100. /* Length */
  60101. 0x00, 0x00, 0x34,
  60102. /* Msg Seq */
  60103. 0x00, 0x00,
  60104. /* Frag offset */
  60105. 0x00, 0x00, 0x00,
  60106. /* Frag length */
  60107. 0x00, 0x00, 0x34,
  60108. /* Version */
  60109. 0xfe, 0xff,
  60110. /* Random */
  60111. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  60112. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  60113. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  60114. /* Session Length */
  60115. 0x00,
  60116. /* Cookie Length */
  60117. 0x00,
  60118. /* CS Length */
  60119. 0x00, 0x0c,
  60120. /* CS */
  60121. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  60122. /* Comp Meths Length */
  60123. 0x01,
  60124. /* Comp Meths */
  60125. 0x00
  60126. /* And finally... no extensions */
  60127. };
  60128. int i;
  60129. #ifdef OPENSSL_EXTRA
  60130. int repeats = 2;
  60131. #else
  60132. int repeats = 1;
  60133. #endif
  60134. for (i = 0; i < repeats; i++) {
  60135. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60136. ssl_s = NULL;
  60137. ctx_s = NULL;
  60138. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  60139. NULL, wolfDTLS_server_method), 0);
  60140. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  60141. test_ctx.s_len = sizeof(chNoExtensions);
  60142. #ifdef OPENSSL_EXTRA
  60143. if (i > 0) {
  60144. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  60145. WOLFSSL_SUCCESS);
  60146. }
  60147. #endif
  60148. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  60149. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60150. /* Expecting a handshake msg. Either HVR or SH. */
  60151. ExpectIntGT(test_ctx.c_len, 0);
  60152. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  60153. wolfSSL_free(ssl_s);
  60154. wolfSSL_CTX_free(ctx_s);
  60155. }
  60156. #endif
  60157. return EXPECT_RESULT();
  60158. }
  60159. static int test_TLSX_CA_NAMES_bad_extension(void)
  60160. {
  60161. EXPECT_DECLS;
  60162. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60163. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  60164. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  60165. defined(HAVE_NULL_CIPHER)
  60166. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  60167. * parse the CA Names extension. Otherwise it will return other non-related
  60168. * errors. If CA Names will be parsed in more configurations, that should
  60169. * be reflected in the macro guard above. */
  60170. WOLFSSL *ssl_c = NULL;
  60171. WOLFSSL_CTX *ctx_c = NULL;
  60172. struct test_memio_ctx test_ctx;
  60173. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  60174. const byte shBadCaNamesExt[] = {
  60175. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60176. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60177. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60178. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60179. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60180. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60181. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  60182. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  60183. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  60184. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60185. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  60186. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  60187. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  60188. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  60189. };
  60190. const byte shBadCaNamesExt2[] = {
  60191. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60192. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60193. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60194. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60195. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60196. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60197. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  60198. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  60199. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  60200. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60201. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  60202. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  60203. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  60204. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  60205. };
  60206. int i = 0;
  60207. for (i = 0; i < 2; i++) {
  60208. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60209. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60210. wolfTLSv1_3_client_method, NULL), 0);
  60211. switch (i) {
  60212. case 0:
  60213. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  60214. sizeof(shBadCaNamesExt));
  60215. test_ctx.c_len = sizeof(shBadCaNamesExt);
  60216. break;
  60217. case 1:
  60218. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  60219. sizeof(shBadCaNamesExt2));
  60220. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  60221. break;
  60222. }
  60223. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60224. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  60225. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  60226. #else
  60227. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  60228. #endif
  60229. wolfSSL_free(ssl_c);
  60230. ssl_c = NULL;
  60231. wolfSSL_CTX_free(ctx_c);
  60232. ctx_c = NULL;
  60233. }
  60234. #endif
  60235. return EXPECT_RESULT();
  60236. }
  60237. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60238. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60239. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  60240. {
  60241. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  60242. WOLFSSL_SUCCESS);
  60243. }
  60244. static int test_dtls_1_0_hvr_downgrade(void)
  60245. {
  60246. EXPECT_DECLS;
  60247. callback_functions func_cb_client;
  60248. callback_functions func_cb_server;
  60249. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60250. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60251. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  60252. func_cb_client.method = wolfDTLS_client_method;
  60253. func_cb_server.method = wolfDTLSv1_2_server_method;
  60254. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  60255. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60256. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60257. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60258. return EXPECT_RESULT();
  60259. }
  60260. #else
  60261. static int test_dtls_1_0_hvr_downgrade(void)
  60262. {
  60263. EXPECT_DECLS;
  60264. return EXPECT_RESULT();
  60265. }
  60266. #endif
  60267. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  60268. defined(HAVE_SESSION_TICKET)
  60269. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  60270. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  60271. {
  60272. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  60273. AssertNotNull(test_session_ticket_no_id_session);
  60274. }
  60275. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  60276. {
  60277. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  60278. }
  60279. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  60280. {
  60281. test_session_ticket_no_id_session->sessionIDSz = 0;
  60282. AssertIntEQ(WOLFSSL_SUCCESS,
  60283. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  60284. }
  60285. static int test_session_ticket_no_id(void)
  60286. {
  60287. /* We are testing an expired (invalid crypto context in out case since the
  60288. * ctx changes) session ticket being sent with the session ID being 0
  60289. * length. */
  60290. EXPECT_DECLS;
  60291. callback_functions func_cb_client;
  60292. callback_functions func_cb_server;
  60293. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60294. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60295. func_cb_client.method = wolfTLSv1_2_client_method;
  60296. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  60297. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  60298. func_cb_server.method = wolfTLSv1_2_server_method;
  60299. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  60300. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60301. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60302. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60303. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60304. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60305. func_cb_client.method = wolfTLSv1_2_client_method;
  60306. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  60307. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  60308. func_cb_server.method = wolfTLSv1_2_server_method;
  60309. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  60310. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60311. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60312. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60313. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  60314. return EXPECT_RESULT();
  60315. }
  60316. #else
  60317. static int test_session_ticket_no_id(void)
  60318. {
  60319. EXPECT_DECLS;
  60320. return EXPECT_RESULT();
  60321. }
  60322. #endif
  60323. static int test_session_ticket_hs_update(void)
  60324. {
  60325. EXPECT_DECLS;
  60326. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60327. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  60328. struct test_memio_ctx test_ctx;
  60329. struct test_memio_ctx test_ctx2;
  60330. struct test_memio_ctx test_ctx3;
  60331. WOLFSSL_CTX *ctx_c = NULL;
  60332. WOLFSSL_CTX *ctx_s = NULL;
  60333. WOLFSSL *ssl_c = NULL;
  60334. WOLFSSL *ssl_c2 = NULL;
  60335. WOLFSSL *ssl_c3 = NULL;
  60336. WOLFSSL *ssl_s = NULL;
  60337. WOLFSSL *ssl_s2 = NULL;
  60338. WOLFSSL *ssl_s3 = NULL;
  60339. WOLFSSL_SESSION *sess = NULL;
  60340. byte read_data[1];
  60341. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60342. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  60343. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  60344. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60345. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  60346. /* Generate tickets */
  60347. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60348. wolfSSL_SetLoggingPrefix("client");
  60349. /* Read the ticket msg */
  60350. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  60351. WOLFSSL_FATAL_ERROR);
  60352. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60353. WOLFSSL_ERROR_WANT_READ);
  60354. wolfSSL_SetLoggingPrefix(NULL);
  60355. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  60356. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  60357. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  60358. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  60359. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  60360. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  60361. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  60362. wolfSSL_SetLoggingPrefix("client");
  60363. /* Exchange initial flights for the second connection */
  60364. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  60365. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  60366. WOLFSSL_ERROR_WANT_READ);
  60367. wolfSSL_SetLoggingPrefix(NULL);
  60368. wolfSSL_SetLoggingPrefix("server");
  60369. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  60370. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  60371. WOLFSSL_ERROR_WANT_READ);
  60372. wolfSSL_SetLoggingPrefix(NULL);
  60373. /* Complete third connection so that new tickets are exchanged */
  60374. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  60375. /* Read the ticket msg */
  60376. wolfSSL_SetLoggingPrefix("client");
  60377. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  60378. WOLFSSL_FATAL_ERROR);
  60379. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  60380. WOLFSSL_ERROR_WANT_READ);
  60381. wolfSSL_SetLoggingPrefix(NULL);
  60382. /* Complete second connection */
  60383. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  60384. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  60385. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  60386. wolfSSL_free(ssl_c);
  60387. wolfSSL_free(ssl_c2);
  60388. wolfSSL_free(ssl_c3);
  60389. wolfSSL_free(ssl_s);
  60390. wolfSSL_free(ssl_s2);
  60391. wolfSSL_free(ssl_s3);
  60392. wolfSSL_CTX_free(ctx_c);
  60393. wolfSSL_CTX_free(ctx_s);
  60394. wolfSSL_SESSION_free(sess);
  60395. #endif
  60396. return EXPECT_RESULT();
  60397. }
  60398. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60399. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60400. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  60401. {
  60402. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  60403. WOLFSSL_SUCCESS);
  60404. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60405. }
  60406. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  60407. {
  60408. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60409. }
  60410. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  60411. {
  60412. char testMsg[] = "Message after SCR";
  60413. char msgBuf[sizeof(testMsg)];
  60414. if (wolfSSL_is_server(ssl)) {
  60415. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  60416. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  60417. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  60418. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60419. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60420. sizeof(testMsg));
  60421. }
  60422. else {
  60423. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60424. sizeof(testMsg));
  60425. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  60426. }
  60427. }
  60428. static int test_dtls_downgrade_scr_server(void)
  60429. {
  60430. EXPECT_DECLS;
  60431. callback_functions func_cb_client;
  60432. callback_functions func_cb_server;
  60433. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60434. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60435. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  60436. func_cb_client.method = wolfDTLSv1_2_client_method;
  60437. func_cb_server.method = wolfDTLS_server_method;
  60438. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  60439. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  60440. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  60441. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  60442. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60443. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60444. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60445. return EXPECT_RESULT();
  60446. }
  60447. #else
  60448. static int test_dtls_downgrade_scr_server(void)
  60449. {
  60450. EXPECT_DECLS;
  60451. return EXPECT_RESULT();
  60452. }
  60453. #endif
  60454. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60455. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60456. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  60457. {
  60458. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  60459. WOLFSSL_SUCCESS);
  60460. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60461. }
  60462. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  60463. {
  60464. char testMsg[] = "Message after SCR";
  60465. char msgBuf[sizeof(testMsg)];
  60466. if (wolfSSL_is_server(ssl)) {
  60467. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  60468. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  60469. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  60470. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60471. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60472. sizeof(testMsg));
  60473. }
  60474. else {
  60475. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60476. sizeof(testMsg));
  60477. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  60478. }
  60479. }
  60480. static int test_dtls_downgrade_scr(void)
  60481. {
  60482. EXPECT_DECLS;
  60483. callback_functions func_cb_client;
  60484. callback_functions func_cb_server;
  60485. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60486. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60487. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  60488. func_cb_client.method = wolfDTLS_client_method;
  60489. func_cb_server.method = wolfDTLSv1_2_server_method;
  60490. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  60491. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  60492. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  60493. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60494. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60495. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60496. return EXPECT_RESULT();
  60497. }
  60498. #else
  60499. static int test_dtls_downgrade_scr(void)
  60500. {
  60501. EXPECT_DECLS;
  60502. return EXPECT_RESULT();
  60503. }
  60504. #endif
  60505. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  60506. && !defined(WOLFSSL_NO_TLS12)
  60507. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  60508. char *data, int sz, void *ctx)
  60509. {
  60510. static int call_counter = 0;
  60511. call_counter++;
  60512. (void)ssl;
  60513. (void)data;
  60514. (void)sz;
  60515. (void)ctx;
  60516. switch (call_counter) {
  60517. case 1:
  60518. case 2:
  60519. return WOLFSSL_CBIO_ERR_TIMEOUT;
  60520. case 3:
  60521. return WOLFSSL_CBIO_ERR_WANT_READ;
  60522. default:
  60523. AssertIntLE(call_counter, 3);
  60524. return -1;
  60525. }
  60526. }
  60527. #endif
  60528. /* Make sure we don't send acks before getting a server hello */
  60529. static int test_dtls_client_hello_timeout_downgrade(void)
  60530. {
  60531. EXPECT_DECLS;
  60532. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  60533. && !defined(WOLFSSL_NO_TLS12)
  60534. WOLFSSL_CTX *ctx_c = NULL;
  60535. WOLFSSL_CTX *ctx_s = NULL;
  60536. WOLFSSL *ssl_c = NULL;
  60537. WOLFSSL *ssl_s = NULL;
  60538. struct test_memio_ctx test_ctx;
  60539. DtlsRecordLayerHeader* dtlsRH;
  60540. size_t len;
  60541. byte sequence_number[8];
  60542. int i;
  60543. for (i = 0; i < 2; i++) {
  60544. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60545. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60546. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  60547. if (i == 0) {
  60548. /* First time simulate timeout in IO layer */
  60549. /* CH1 */
  60550. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60551. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60552. /* HVR */
  60553. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60554. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60555. /* CH2 */
  60556. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60557. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60558. /* SH flight */
  60559. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60560. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60561. /* Drop the SH */
  60562. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  60563. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60564. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  60565. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  60566. (sizeof(DtlsRecordLayerHeader) + len));
  60567. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  60568. /* Read the remainder of the flight */
  60569. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60570. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60571. wolfSSL_SSLSetIORecv(ssl_c,
  60572. test_dtls_client_hello_timeout_downgrade_read_cb);
  60573. /* CH3 */
  60574. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60575. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60576. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  60577. }
  60578. else {
  60579. /* Second time call wolfSSL_dtls_got_timeout */
  60580. /* CH1 */
  60581. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60582. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60583. /* HVR */
  60584. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60585. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60586. /* CH2 */
  60587. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60588. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60589. /* SH flight */
  60590. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60591. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60592. /* Drop the SH */
  60593. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  60594. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60595. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  60596. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  60597. (sizeof(DtlsRecordLayerHeader) + len));
  60598. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  60599. /* Read the remainder of the flight */
  60600. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60601. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60602. /* Quick timeout should be set as we received at least one msg */
  60603. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  60604. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60605. /* Quick timeout should be cleared after a quick timeout */
  60606. /* CH3 */
  60607. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  60608. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60609. }
  60610. /* Parse out to make sure we got exactly one ClientHello message */
  60611. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60612. /* Second ClientHello after HVR */
  60613. sequence_number[7] = 2;
  60614. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  60615. ExpectIntEQ(dtlsRH->type, handshake);
  60616. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60617. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60618. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60619. sizeof(sequence_number)), 0);
  60620. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60621. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60622. /* Connection should be able to continue */
  60623. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60624. wolfSSL_free(ssl_c);
  60625. wolfSSL_free(ssl_s);
  60626. wolfSSL_CTX_free(ctx_c);
  60627. wolfSSL_CTX_free(ctx_s);
  60628. ssl_c = NULL;
  60629. ssl_s = NULL;
  60630. ctx_c = NULL;
  60631. ctx_s = NULL;
  60632. if (!EXPECT_SUCCESS())
  60633. break;
  60634. }
  60635. #endif
  60636. return EXPECT_RESULT();
  60637. }
  60638. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60639. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  60640. int sz, void *ctx)
  60641. {
  60642. static int call_counter = 0;
  60643. call_counter++;
  60644. (void)ssl;
  60645. (void)data;
  60646. (void)sz;
  60647. (void)ctx;
  60648. switch (call_counter) {
  60649. case 1:
  60650. return WOLFSSL_CBIO_ERR_TIMEOUT;
  60651. case 2:
  60652. return WOLFSSL_CBIO_ERR_WANT_READ;
  60653. default:
  60654. AssertIntLE(call_counter, 2);
  60655. return -1;
  60656. }
  60657. }
  60658. #endif
  60659. /* Make sure we don't send acks before getting a server hello */
  60660. static int test_dtls_client_hello_timeout(void)
  60661. {
  60662. EXPECT_DECLS;
  60663. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60664. WOLFSSL *ssl_c = NULL;
  60665. WOLFSSL_CTX *ctx_c = NULL;
  60666. struct test_memio_ctx test_ctx;
  60667. DtlsRecordLayerHeader* dtlsRH;
  60668. size_t idx;
  60669. size_t len;
  60670. byte sequence_number[8];
  60671. int i;
  60672. for (i = 0; i < 2; i++) {
  60673. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60674. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60675. wolfDTLSv1_3_client_method, NULL), 0);
  60676. if (i == 0) {
  60677. /* First time simulate timeout in IO layer */
  60678. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  60679. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60680. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60681. }
  60682. else {
  60683. /* Second time call wolfSSL_dtls_got_timeout */
  60684. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60685. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60686. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60687. }
  60688. /* Parse out to make sure we got exactly two ClientHello messages */
  60689. idx = 0;
  60690. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60691. /* First ClientHello */
  60692. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  60693. ExpectIntEQ(dtlsRH->type, handshake);
  60694. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60695. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60696. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60697. sizeof(sequence_number)), 0);
  60698. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60699. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60700. idx += sizeof(DtlsRecordLayerHeader) + len;
  60701. /* Second ClientHello */
  60702. sequence_number[7] = 1;
  60703. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  60704. ExpectIntEQ(dtlsRH->type, handshake);
  60705. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60706. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60707. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60708. sizeof(sequence_number)), 0);
  60709. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60710. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60711. wolfSSL_free(ssl_c);
  60712. wolfSSL_CTX_free(ctx_c);
  60713. ssl_c = NULL;
  60714. ctx_c = NULL;
  60715. if (!EXPECT_SUCCESS())
  60716. break;
  60717. }
  60718. #endif
  60719. return EXPECT_RESULT();
  60720. }
  60721. /* DTLS test when dropping the changed cipher spec message */
  60722. static int test_dtls_dropped_ccs(void)
  60723. {
  60724. EXPECT_DECLS;
  60725. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60726. && !defined(WOLFSSL_NO_TLS12)
  60727. WOLFSSL_CTX *ctx_c = NULL;
  60728. WOLFSSL_CTX *ctx_s = NULL;
  60729. WOLFSSL *ssl_c = NULL;
  60730. WOLFSSL *ssl_s = NULL;
  60731. struct test_memio_ctx test_ctx;
  60732. DtlsRecordLayerHeader* dtlsRH;
  60733. size_t len;
  60734. byte data[1];
  60735. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60736. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60737. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60738. /* CH1 */
  60739. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60740. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60741. /* HVR */
  60742. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60743. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60744. /* CH2 */
  60745. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60746. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60747. /* Server first flight */
  60748. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60749. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60750. /* Client flight */
  60751. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60752. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60753. /* Server ccs + finished */
  60754. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  60755. /* Drop the ccs */
  60756. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  60757. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60758. ExpectIntEQ(len, 1);
  60759. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  60760. if (EXPECT_SUCCESS()) {
  60761. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  60762. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  60763. (sizeof(DtlsRecordLayerHeader) + len));
  60764. }
  60765. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  60766. /* Client rtx flight */
  60767. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60768. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60769. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60770. /* Server ccs + finished rtx */
  60771. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  60772. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60773. /* Client processes finished */
  60774. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  60775. wolfSSL_free(ssl_c);
  60776. wolfSSL_free(ssl_s);
  60777. wolfSSL_CTX_free(ctx_c);
  60778. wolfSSL_CTX_free(ctx_s);
  60779. #endif
  60780. return EXPECT_RESULT();
  60781. }
  60782. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60783. && !defined(WOLFSSL_NO_TLS12)
  60784. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  60785. byte seq_num)
  60786. {
  60787. EXPECT_DECLS;
  60788. DtlsRecordLayerHeader* dtlsRH;
  60789. byte sequence_number[8];
  60790. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60791. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  60792. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  60793. ExpectIntEQ(dtlsRH->type, handshake);
  60794. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60795. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60796. sequence_number[7] = seq_num;
  60797. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60798. sizeof(sequence_number)), 0);
  60799. return EXPECT_RESULT();
  60800. }
  60801. #endif
  60802. /*
  60803. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  60804. * and after a HelloRetryRequest. This is testing the server side as it is
  60805. * operating statelessly and should copy the sequence number of the ClientHello.
  60806. */
  60807. static int test_dtls_seq_num_downgrade(void)
  60808. {
  60809. EXPECT_DECLS;
  60810. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60811. && !defined(WOLFSSL_NO_TLS12)
  60812. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60813. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60814. struct test_memio_ctx test_ctx;
  60815. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60816. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60817. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  60818. /* CH1 */
  60819. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60820. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60821. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  60822. test_ctx.s_len, 0), TEST_SUCCESS);
  60823. /* HVR */
  60824. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60825. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60826. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  60827. test_ctx.c_len, 0), TEST_SUCCESS);
  60828. /* CH2 */
  60829. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60830. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60831. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  60832. test_ctx.s_len, 1), TEST_SUCCESS);
  60833. /* Server first flight */
  60834. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60835. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60836. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  60837. test_ctx.c_len, 1), TEST_SUCCESS);
  60838. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60839. wolfSSL_free(ssl_c);
  60840. wolfSSL_CTX_free(ctx_c);
  60841. wolfSSL_free(ssl_s);
  60842. wolfSSL_CTX_free(ctx_s);
  60843. #endif
  60844. return EXPECT_RESULT();
  60845. }
  60846. /**
  60847. * Make sure we don't send RSA Signature Hash Algorithms in the
  60848. * CertificateRequest when we don't have any such ciphers set.
  60849. * @return EXPECT_RESULT()
  60850. */
  60851. static int test_certreq_sighash_algos(void)
  60852. {
  60853. EXPECT_DECLS;
  60854. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60855. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  60856. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  60857. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  60858. WOLFSSL_CTX *ctx_c = NULL;
  60859. WOLFSSL_CTX *ctx_s = NULL;
  60860. WOLFSSL *ssl_c = NULL;
  60861. WOLFSSL *ssl_s = NULL;
  60862. struct test_memio_ctx test_ctx;
  60863. int idx = 0;
  60864. int maxIdx = 0;
  60865. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60866. test_ctx.c_ciphers = test_ctx.s_ciphers =
  60867. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  60868. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60869. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  60870. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  60871. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  60872. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  60873. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  60874. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60875. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  60876. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60877. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  60878. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60879. WOLFSSL_ERROR_WANT_READ);
  60880. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  60881. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60882. WOLFSSL_ERROR_WANT_READ);
  60883. /* Find the CertificateRequest message */
  60884. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  60885. word16 len;
  60886. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  60887. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  60888. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  60889. ato16(test_ctx.c_buff + idx, &len);
  60890. idx += OPAQUE16_LEN;
  60891. if (test_ctx.c_buff[idx] == certificate_request) {
  60892. idx++;
  60893. /* length */
  60894. idx += OPAQUE24_LEN;
  60895. /* cert types */
  60896. idx += 1 + test_ctx.c_buff[idx];
  60897. /* Sig algos */
  60898. ato16(test_ctx.c_buff + idx, &len);
  60899. idx += OPAQUE16_LEN;
  60900. maxIdx = idx + (int)len;
  60901. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  60902. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  60903. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  60904. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  60905. else
  60906. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  60907. }
  60908. break;
  60909. }
  60910. else {
  60911. idx += (int)len;
  60912. }
  60913. }
  60914. ExpectIntLT(idx, test_ctx.c_len);
  60915. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60916. wolfSSL_free(ssl_c);
  60917. wolfSSL_free(ssl_s);
  60918. wolfSSL_CTX_free(ctx_c);
  60919. wolfSSL_CTX_free(ctx_s);
  60920. #endif
  60921. return EXPECT_RESULT();
  60922. }
  60923. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60924. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  60925. {
  60926. EXPECT_DECLS;
  60927. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  60928. myVerifyAction = VERIFY_USE_PREVERFIY;
  60929. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60930. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60931. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60932. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60933. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  60934. WOLFSSL_SUCCESS);
  60935. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60936. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  60937. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60938. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60939. "./certs/crl/ca-int.pem",
  60940. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60941. return EXPECT_RESULT();
  60942. }
  60943. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  60944. {
  60945. EXPECT_DECLS;
  60946. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  60947. myVerifyAction = VERIFY_USE_PREVERFIY;
  60948. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60949. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60950. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60951. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60952. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60953. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60954. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  60955. WOLFSSL_SUCCESS);
  60956. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60957. "./certs/crl/ca-int2.pem",
  60958. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60959. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60960. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  60961. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60962. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60963. "./certs/crl/ca-int.pem",
  60964. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60965. return EXPECT_RESULT();
  60966. }
  60967. #endif
  60968. static int test_revoked_loaded_int_cert(void)
  60969. {
  60970. EXPECT_DECLS;
  60971. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60972. test_ssl_cbf client_cbf;
  60973. test_ssl_cbf server_cbf;
  60974. struct {
  60975. const char* certPemFile;
  60976. const char* keyPemFile;
  60977. ctx_cb client_ctx_ready;
  60978. } test_params[] = {
  60979. {"./certs/intermediate/ca-int2-cert.pem",
  60980. "./certs/intermediate/ca-int2-key.pem",
  60981. test_revoked_loaded_int_cert_ctx_ready1},
  60982. {"./certs/intermediate/server-chain.pem",
  60983. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  60984. {"./certs/intermediate/server-chain-short.pem",
  60985. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  60986. };
  60987. size_t i;
  60988. printf("\n");
  60989. for (i = 0; i < XELEM_CNT(test_params); i++) {
  60990. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60991. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60992. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  60993. server_cbf.certPemFile = test_params[i].certPemFile;
  60994. server_cbf.keyPemFile = test_params[i].keyPemFile;
  60995. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  60996. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60997. &server_cbf, NULL), TEST_FAIL);
  60998. #ifndef WOLFSSL_HAPROXY
  60999. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  61000. #else
  61001. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  61002. #endif
  61003. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  61004. if (!EXPECT_SUCCESS())
  61005. break;
  61006. printf("\t%s passed\n", test_params[i].certPemFile);
  61007. }
  61008. #endif
  61009. return EXPECT_RESULT();
  61010. }
  61011. static int test_dtls13_frag_ch_pq(void)
  61012. {
  61013. EXPECT_DECLS;
  61014. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61015. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  61016. WOLFSSL_CTX *ctx_c = NULL;
  61017. WOLFSSL_CTX *ctx_s = NULL;
  61018. WOLFSSL *ssl_c = NULL;
  61019. WOLFSSL *ssl_s = NULL;
  61020. struct test_memio_ctx test_ctx;
  61021. const char *test_str = "test";
  61022. int test_str_size;
  61023. byte buf[255];
  61024. int group = WOLFSSL_KYBER_LEVEL5;
  61025. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61026. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61027. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61028. /* Add in a large post-quantum key share to make the CH long. */
  61029. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  61030. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  61031. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61032. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61033. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  61034. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  61035. test_str_size = XSTRLEN("test") + 1;
  61036. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61037. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61038. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61039. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61040. wolfSSL_free(ssl_c);
  61041. wolfSSL_free(ssl_s);
  61042. wolfSSL_CTX_free(ctx_c);
  61043. wolfSSL_CTX_free(ctx_s);
  61044. #endif
  61045. return EXPECT_RESULT();
  61046. }
  61047. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61048. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61049. static int test_dtls_frag_ch_count_records(byte* b, int len)
  61050. {
  61051. DtlsRecordLayerHeader* dtlsRH;
  61052. int records = 0;
  61053. size_t recordLen;
  61054. while (len > 0) {
  61055. records++;
  61056. dtlsRH = (DtlsRecordLayerHeader*)b;
  61057. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  61058. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  61059. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  61060. }
  61061. return records;
  61062. }
  61063. #endif
  61064. static int test_dtls_frag_ch(void)
  61065. {
  61066. EXPECT_DECLS;
  61067. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61068. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61069. WOLFSSL_CTX *ctx_c = NULL;
  61070. WOLFSSL_CTX *ctx_s = NULL;
  61071. WOLFSSL *ssl_c = NULL;
  61072. WOLFSSL *ssl_s = NULL;
  61073. struct test_memio_ctx test_ctx;
  61074. static unsigned int DUMMY_MTU = 256;
  61075. unsigned char four_frag_CH[] = {
  61076. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61077. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61078. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  61079. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  61080. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  61081. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61082. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61083. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61084. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61085. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  61086. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  61087. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  61088. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  61089. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  61090. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  61091. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  61092. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  61093. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  61094. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  61095. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61096. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  61097. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  61098. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  61099. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  61100. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  61101. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  61102. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  61103. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  61104. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  61105. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  61106. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  61107. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  61108. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  61109. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  61110. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  61111. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  61112. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  61113. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  61114. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  61115. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  61116. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  61117. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  61118. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  61119. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  61120. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  61121. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  61122. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  61123. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  61124. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  61125. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  61126. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  61127. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  61128. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  61129. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  61130. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  61131. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  61132. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  61133. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  61134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  61135. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  61136. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  61137. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  61138. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  61139. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  61140. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  61141. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  61142. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  61143. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  61144. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  61145. 0xa5, 0xcb, 0x68, 0xf3
  61146. };
  61147. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61148. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61149. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61150. /* Fragment msgs */
  61151. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  61152. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  61153. /* Add in some key shares to make the CH long */
  61154. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  61155. WOLFSSL_SUCCESS);
  61156. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  61157. WOLFSSL_SUCCESS);
  61158. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  61159. WOLFSSL_SUCCESS);
  61160. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  61161. WOLFSSL_SUCCESS);
  61162. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61163. /* Reject fragmented first CH */
  61164. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  61165. sizeof(four_frag_CH)), 4);
  61166. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  61167. test_ctx.s_len = sizeof(four_frag_CH);
  61168. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  61169. int s_len = test_ctx.s_len;
  61170. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61171. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61172. /* Fail if we didn't advance the buffer to avoid infinite loops */
  61173. ExpectIntLT(test_ctx.s_len, s_len);
  61174. }
  61175. /* Expect all fragments read */
  61176. ExpectIntEQ(test_ctx.s_len, 0);
  61177. /* Expect quietly dropping fragmented first CH */
  61178. ExpectIntEQ(test_ctx.c_len, 0);
  61179. /* CH1 */
  61180. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61181. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61182. /* Count records. Expect 1 unfragmented CH */
  61183. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61184. test_ctx.s_len), 1);
  61185. /* HRR */
  61186. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61187. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61188. /* CH2 */
  61189. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61190. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61191. /* Count records. Expect fragmented CH */
  61192. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61193. test_ctx.s_len), 1);
  61194. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61195. wolfSSL_free(ssl_c);
  61196. wolfSSL_free(ssl_s);
  61197. wolfSSL_CTX_free(ctx_c);
  61198. wolfSSL_CTX_free(ctx_s);
  61199. ssl_c = ssl_s = NULL;
  61200. ctx_c = ctx_s = NULL;
  61201. #endif
  61202. return EXPECT_RESULT();
  61203. }
  61204. static int test_dtls_empty_keyshare_with_cookie(void)
  61205. {
  61206. EXPECT_DECLS;
  61207. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61208. WOLFSSL_CTX *ctx_s = NULL;
  61209. WOLFSSL *ssl_s = NULL;
  61210. struct test_memio_ctx test_ctx;
  61211. unsigned char ch_empty_keyshare_with_cookie[] = {
  61212. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  61213. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  61214. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  61215. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  61216. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  61217. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61218. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61219. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61220. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61221. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  61222. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  61223. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  61224. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  61225. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  61226. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  61227. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  61228. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  61229. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  61230. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  61231. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  61232. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  61233. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  61234. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  61235. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  61236. };
  61237. DtlsRecordLayerHeader* dtlsRH;
  61238. byte sequence_number[8];
  61239. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61240. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61241. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  61242. sizeof(ch_empty_keyshare_with_cookie));
  61243. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  61244. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  61245. NULL, wolfDTLSv1_3_server_method), 0);
  61246. /* CH1 */
  61247. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61248. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61249. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  61250. ExpectIntEQ(test_ctx.c_len, 15);
  61251. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61252. ExpectIntEQ(dtlsRH->type, alert);
  61253. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61254. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61255. sequence_number[7] = 1;
  61256. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61257. sizeof(sequence_number)), 0);
  61258. ExpectIntEQ(dtlsRH->length[0], 0);
  61259. ExpectIntEQ(dtlsRH->length[1], 2);
  61260. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  61261. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  61262. wolfSSL_free(ssl_s);
  61263. wolfSSL_CTX_free(ctx_s);
  61264. #endif
  61265. return EXPECT_RESULT();
  61266. }
  61267. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61268. defined(HAVE_LIBOQS)
  61269. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  61270. {
  61271. int group = WOLFSSL_KYBER_LEVEL5;
  61272. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  61273. }
  61274. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  61275. {
  61276. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  61277. }
  61278. #endif
  61279. static int test_tls13_pq_groups(void)
  61280. {
  61281. EXPECT_DECLS;
  61282. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61283. defined(HAVE_LIBOQS)
  61284. callback_functions func_cb_client;
  61285. callback_functions func_cb_server;
  61286. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61287. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61288. func_cb_client.method = wolfTLSv1_3_client_method;
  61289. func_cb_server.method = wolfTLSv1_3_server_method;
  61290. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  61291. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  61292. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  61293. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61294. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61295. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61296. #endif
  61297. return EXPECT_RESULT();
  61298. }
  61299. static int test_tls13_early_data(void)
  61300. {
  61301. EXPECT_DECLS;
  61302. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61303. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  61304. int written = 0;
  61305. int read = 0;
  61306. size_t i;
  61307. int splitEarlyData;
  61308. char msg[] = "This is early data";
  61309. char msg2[] = "This is client data";
  61310. char msg3[] = "This is server data";
  61311. char msg4[] = "This is server immediate data";
  61312. char msgBuf[50];
  61313. struct {
  61314. method_provider client_meth;
  61315. method_provider server_meth;
  61316. const char* tls_version;
  61317. int isUdp;
  61318. } params[] = {
  61319. #ifdef WOLFSSL_TLS13
  61320. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61321. "TLS 1.3", 0 },
  61322. #endif
  61323. #ifdef WOLFSSL_DTLS13
  61324. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  61325. "DTLS 1.3", 1 },
  61326. #endif
  61327. };
  61328. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  61329. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  61330. struct test_memio_ctx test_ctx;
  61331. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61332. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61333. WOLFSSL_SESSION *sess = NULL;
  61334. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61335. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  61336. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  61337. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  61338. /* Get a ticket so that we can do 0-RTT on the next connection */
  61339. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61340. /* Make sure we read the ticket */
  61341. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  61342. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61343. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61344. wolfSSL_free(ssl_c);
  61345. ssl_c = NULL;
  61346. wolfSSL_free(ssl_s);
  61347. ssl_s = NULL;
  61348. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61349. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61350. params[i].client_meth, params[i].server_meth), 0);
  61351. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  61352. #ifdef WOLFSSL_DTLS13
  61353. if (params[i].isUdp) {
  61354. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  61355. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  61356. #else
  61357. /* Let's test this but we generally don't recommend turning off the
  61358. * cookie exchange */
  61359. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  61360. #endif
  61361. }
  61362. #endif
  61363. /* Test 0-RTT data */
  61364. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  61365. &written), sizeof(msg));
  61366. ExpectIntEQ(written, sizeof(msg));
  61367. if (splitEarlyData) {
  61368. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  61369. &written), sizeof(msg));
  61370. ExpectIntEQ(written, sizeof(msg));
  61371. }
  61372. /* Read first 0-RTT data (if split otherwise entire data) */
  61373. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  61374. &read), sizeof(msg));
  61375. ExpectIntEQ(read, sizeof(msg));
  61376. ExpectStrEQ(msg, msgBuf);
  61377. /* Test 0.5-RTT data */
  61378. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  61379. if (splitEarlyData) {
  61380. /* Read second 0-RTT data */
  61381. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  61382. &read), sizeof(msg));
  61383. ExpectIntEQ(read, sizeof(msg));
  61384. ExpectStrEQ(msg, msgBuf);
  61385. }
  61386. if (params[i].isUdp) {
  61387. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61388. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  61389. /* Read server 0.5-RTT data */
  61390. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  61391. ExpectStrEQ(msg4, msgBuf);
  61392. /* Complete handshake */
  61393. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61394. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61395. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  61396. * a user would loop until it is true but here we control both sides so we
  61397. * just assert the expected value. wolfSSL_read_early_data does not provide
  61398. * handshake status to us with non-blocking IO and we can't use
  61399. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  61400. * parsing logic. */
  61401. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  61402. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  61403. &read), 0);
  61404. ExpectIntEQ(read, 0);
  61405. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  61406. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61407. }
  61408. else {
  61409. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61410. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  61411. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  61412. &read), 0);
  61413. ExpectIntEQ(read, 0);
  61414. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  61415. /* Read server 0.5-RTT data */
  61416. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  61417. ExpectStrEQ(msg4, msgBuf);
  61418. }
  61419. /* Test bi-directional write */
  61420. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  61421. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  61422. ExpectStrEQ(msg2, msgBuf);
  61423. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  61424. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  61425. ExpectStrEQ(msg3, msgBuf);
  61426. ExpectTrue(wolfSSL_session_reused(ssl_c));
  61427. ExpectTrue(wolfSSL_session_reused(ssl_s));
  61428. wolfSSL_SESSION_free(sess);
  61429. wolfSSL_free(ssl_c);
  61430. wolfSSL_free(ssl_s);
  61431. wolfSSL_CTX_free(ctx_c);
  61432. wolfSSL_CTX_free(ctx_s);
  61433. }
  61434. }
  61435. #endif
  61436. return EXPECT_RESULT();
  61437. }
  61438. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  61439. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  61440. {
  61441. EXPECT_DECLS;
  61442. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  61443. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  61444. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  61445. return EXPECT_RESULT();
  61446. }
  61447. #endif
  61448. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  61449. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  61450. {
  61451. EXPECT_DECLS;
  61452. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  61453. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  61454. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  61455. return EXPECT_RESULT();
  61456. }
  61457. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  61458. {
  61459. EXPECT_DECLS;
  61460. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  61461. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  61462. 0), 1);
  61463. return EXPECT_RESULT();
  61464. }
  61465. #endif
  61466. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  61467. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  61468. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  61469. {
  61470. EXPECT_DECLS;
  61471. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  61472. return EXPECT_RESULT();
  61473. }
  61474. #endif
  61475. static int test_self_signed_stapling(void)
  61476. {
  61477. EXPECT_DECLS;
  61478. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  61479. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  61480. test_ssl_cbf client_cbf;
  61481. test_ssl_cbf server_cbf;
  61482. size_t i;
  61483. struct {
  61484. method_provider client_meth;
  61485. method_provider server_meth;
  61486. ctx_cb client_ctx;
  61487. const char* tls_version;
  61488. } params[] = {
  61489. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  61490. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61491. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  61492. #endif
  61493. #ifndef WOLFSSL_NO_TLS12
  61494. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  61495. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61496. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  61497. #endif
  61498. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  61499. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61500. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  61501. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61502. test_self_signed_stapling_client_v2_multi_ctx_ready,
  61503. "TLSv1_2 v2 multi" },
  61504. #endif
  61505. #endif
  61506. };
  61507. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  61508. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61509. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61510. printf("\nTesting self-signed cert with status request: %s\n",
  61511. params[i].tls_version);
  61512. client_cbf.method = params[i].client_meth;
  61513. client_cbf.ctx_ready = params[i].client_ctx;
  61514. server_cbf.method = params[i].server_meth;
  61515. server_cbf.certPemFile = "certs/ca-cert.pem";
  61516. server_cbf.keyPemFile = "certs/ca-key.pem";
  61517. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  61518. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61519. &server_cbf, NULL), TEST_SUCCESS);
  61520. }
  61521. #endif
  61522. return EXPECT_RESULT();
  61523. }
  61524. static int test_tls_multi_handshakes_one_record(void)
  61525. {
  61526. EXPECT_DECLS;
  61527. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  61528. struct test_memio_ctx test_ctx;
  61529. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61530. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61531. RecordLayerHeader* rh = NULL;
  61532. byte *len ;
  61533. int newRecIdx = RECORD_HEADER_SZ;
  61534. int idx = 0;
  61535. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61536. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61537. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  61538. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61539. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61540. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  61541. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61542. /* Combine server handshake msgs into one record */
  61543. while (idx < test_ctx.c_len) {
  61544. word16 recLen;
  61545. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  61546. len = &rh->length[0];
  61547. ato16((const byte*)len, &recLen);
  61548. idx += RECORD_HEADER_SZ;
  61549. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  61550. (size_t)recLen);
  61551. newRecIdx += recLen;
  61552. idx += recLen;
  61553. }
  61554. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  61555. len = &rh->length[0];
  61556. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  61557. test_ctx.c_len = newRecIdx;
  61558. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61559. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61560. wolfSSL_free(ssl_c);
  61561. wolfSSL_free(ssl_s);
  61562. wolfSSL_CTX_free(ctx_c);
  61563. wolfSSL_CTX_free(ctx_s);
  61564. #endif
  61565. return EXPECT_RESULT();
  61566. }
  61567. static int test_write_dup(void)
  61568. {
  61569. EXPECT_DECLS;
  61570. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  61571. size_t i, j;
  61572. char hiWorld[] = "dup message";
  61573. char readData[sizeof(hiWorld) + 5];
  61574. struct {
  61575. method_provider client_meth;
  61576. method_provider server_meth;
  61577. const char* version_name;
  61578. int version;
  61579. } methods[] = {
  61580. #ifndef WOLFSSL_NO_TLS12
  61581. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  61582. #endif
  61583. #ifdef WOLFSSL_TLS13
  61584. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  61585. #endif
  61586. };
  61587. struct {
  61588. const char* cipher;
  61589. int version;
  61590. } ciphers[] = {
  61591. /* For simplicity the macros are copied from internal.h */
  61592. /* TLS 1.2 */
  61593. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  61594. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  61595. #ifndef NO_RSA
  61596. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  61597. #endif
  61598. #endif
  61599. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  61600. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  61601. #endif
  61602. #endif
  61603. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  61604. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  61605. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61606. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  61607. #endif
  61608. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61609. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  61610. #endif
  61611. #endif
  61612. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  61613. && !defined(NO_TLS) && !defined(NO_AES)
  61614. #ifdef HAVE_AESGCM
  61615. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61616. #ifndef NO_RSA
  61617. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  61618. #endif
  61619. #endif
  61620. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61621. #ifndef NO_RSA
  61622. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  61623. #endif
  61624. #endif
  61625. #endif
  61626. #endif
  61627. /* TLS 1.3 */
  61628. #ifdef WOLFSSL_TLS13
  61629. #ifdef HAVE_AESGCM
  61630. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61631. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  61632. #endif
  61633. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61634. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  61635. #endif
  61636. #endif
  61637. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  61638. #ifndef NO_SHA256
  61639. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  61640. #endif
  61641. #endif
  61642. #ifdef HAVE_AESCCM
  61643. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61644. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  61645. #endif
  61646. #endif
  61647. #endif
  61648. };
  61649. for (i = 0; i < XELEM_CNT(methods); i++) {
  61650. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  61651. struct test_memio_ctx test_ctx;
  61652. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61653. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61654. WOLFSSL *ssl_c2 = NULL;
  61655. if (methods[i].version != ciphers[j].version)
  61656. continue;
  61657. if (i == 0 && j == 0)
  61658. printf("\n");
  61659. printf("Testing %s with %s... ", methods[i].version_name,
  61660. ciphers[j].cipher);
  61661. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61662. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  61663. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61664. methods[i].client_meth, methods[i].server_meth), 0);
  61665. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61666. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  61667. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  61668. WRITE_DUP_WRITE_E);
  61669. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  61670. sizeof(hiWorld));
  61671. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  61672. sizeof(hiWorld));
  61673. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  61674. sizeof(hiWorld));
  61675. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  61676. WRITE_DUP_READ_E);
  61677. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  61678. sizeof(hiWorld));
  61679. if (EXPECT_SUCCESS())
  61680. printf("ok\n");
  61681. else
  61682. printf("failed\n");
  61683. wolfSSL_free(ssl_c);
  61684. wolfSSL_free(ssl_c2);
  61685. wolfSSL_free(ssl_s);
  61686. wolfSSL_CTX_free(ctx_c);
  61687. wolfSSL_CTX_free(ctx_s);
  61688. }
  61689. }
  61690. #endif
  61691. return EXPECT_RESULT();
  61692. }
  61693. static int test_read_write_hs(void)
  61694. {
  61695. EXPECT_DECLS;
  61696. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  61697. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  61698. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  61699. struct test_memio_ctx test_ctx;
  61700. uint8_t test_buffer[16];
  61701. unsigned int test;
  61702. /* test == 0 : client writes, server reads */
  61703. /* test == 1 : server writes, client reads */
  61704. for (test = 0; test < 2; test++) {
  61705. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61706. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61707. wolfTLSv1_2_client_method,
  61708. wolfTLSv1_2_server_method), 0);
  61709. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  61710. /* CH -> */
  61711. if (test == 0) {
  61712. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61713. } else {
  61714. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61715. sizeof(test_buffer)), -1);
  61716. }
  61717. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61718. /* <- SH + SKE + SHD */
  61719. if (test == 0) {
  61720. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61721. sizeof(test_buffer)), -1);
  61722. } else {
  61723. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  61724. }
  61725. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61726. /* -> CKE + CLIENT FINISHED */
  61727. if (test == 0) {
  61728. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61729. } else {
  61730. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61731. sizeof(test_buffer)), -1);
  61732. }
  61733. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61734. /* abide clang static analyzer */
  61735. if (ssl_s != NULL) {
  61736. /* disable group message to separate sending of ChangeCipherspec
  61737. * from Finished */
  61738. ssl_s->options.groupMessages = 0;
  61739. }
  61740. /* allow writing of CS, but not FINISHED */
  61741. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  61742. /* <- CS */
  61743. if (test == 0) {
  61744. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61745. sizeof(test_buffer)), -1);
  61746. } else {
  61747. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  61748. }
  61749. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  61750. /* move CS message where the client can read it */
  61751. memmove(test_ctx.c_buff,
  61752. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  61753. test_ctx.c_len = 6;
  61754. /* read CS */
  61755. if (test == 0) {
  61756. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61757. } else {
  61758. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61759. sizeof(test_buffer)), -1);
  61760. }
  61761. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61762. ExpectIntEQ(test_ctx.c_len, 0);
  61763. if (test == 0) {
  61764. /* send SERVER FINISHED */
  61765. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61766. sizeof(test_buffer)), -1);
  61767. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  61768. WOLFSSL_ERROR_WANT_READ);
  61769. } else {
  61770. /* send SERVER FINISHED + App Data */
  61771. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  61772. }
  61773. ExpectIntGT(test_ctx.c_len, 0);
  61774. /* Send and receive the data */
  61775. if (test == 0) {
  61776. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  61777. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61778. sizeof(test_buffer)), 5);
  61779. } else {
  61780. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61781. sizeof(test_buffer)), 5);
  61782. }
  61783. ExpectBufEQ(test_buffer, "hello", 5);
  61784. wolfSSL_free(ssl_c);
  61785. wolfSSL_free(ssl_s);
  61786. wolfSSL_CTX_free(ctx_c);
  61787. wolfSSL_CTX_free(ctx_s);
  61788. ssl_c = ssl_s = NULL;
  61789. ctx_c = ctx_s = NULL;
  61790. }
  61791. #endif
  61792. return EXPECT_RESULT();
  61793. }
  61794. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  61795. static const char* test_get_signature_nid_siglag;
  61796. static int test_get_signature_nid_sig;
  61797. static int test_get_signature_nid_hash;
  61798. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  61799. {
  61800. EXPECT_DECLS;
  61801. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  61802. if (!wolfSSL_is_server(ssl)) {
  61803. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  61804. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  61805. }
  61806. return EXPECT_RESULT();
  61807. }
  61808. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  61809. {
  61810. EXPECT_DECLS;
  61811. int nid = 0;
  61812. (void)ctx;
  61813. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  61814. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61815. ExpectIntEQ(nid, test_get_signature_nid_sig);
  61816. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61817. ExpectIntEQ(nid, test_get_signature_nid_hash);
  61818. }
  61819. else /* No sigalg info on static ciphersuite */
  61820. return TEST_SUCCESS;
  61821. return EXPECT_RESULT();
  61822. }
  61823. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  61824. {
  61825. EXPECT_DECLS;
  61826. int nid = 0;
  61827. (void)ctx;
  61828. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61829. ExpectIntEQ(nid, test_get_signature_nid_sig);
  61830. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61831. ExpectIntEQ(nid, test_get_signature_nid_hash);
  61832. return EXPECT_RESULT();
  61833. }
  61834. #endif
  61835. static int test_get_signature_nid(void)
  61836. {
  61837. EXPECT_DECLS;
  61838. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  61839. test_ssl_cbf client_cbf;
  61840. test_ssl_cbf server_cbf;
  61841. size_t i;
  61842. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  61843. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  61844. caCertFile }
  61845. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  61846. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  61847. caEccCertFile }
  61848. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  61849. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  61850. caCertFile }
  61851. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  61852. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  61853. caEccCertFile }
  61854. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  61855. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  61856. caEdCertFile }
  61857. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  61858. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  61859. caEd448CertFile }
  61860. struct {
  61861. const char* siglag;
  61862. int sig_nid;
  61863. int hash_nid;
  61864. int tls_ver;
  61865. const char* server_cert;
  61866. const char* server_key;
  61867. const char* client_ca;
  61868. } params[] = {
  61869. #ifndef NO_RSA
  61870. #ifndef NO_SHA256
  61871. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  61872. #ifdef WC_RSA_PSS
  61873. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  61874. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  61875. #endif
  61876. #endif
  61877. #ifdef WOLFSSL_SHA512
  61878. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  61879. #ifdef WC_RSA_PSS
  61880. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  61881. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  61882. #endif
  61883. #endif
  61884. #endif
  61885. #ifdef HAVE_ECC
  61886. #ifndef NO_SHA256
  61887. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  61888. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  61889. #endif
  61890. #endif
  61891. #ifdef HAVE_ED25519
  61892. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  61893. #endif
  61894. #ifdef HAVE_ED448
  61895. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  61896. #endif
  61897. };
  61898. printf("\n");
  61899. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  61900. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61901. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61902. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  61903. params[i].siglag);
  61904. switch (params[i].tls_ver) {
  61905. #ifndef WOLFSSL_NO_TLS12
  61906. case WOLFSSL_TLSV1_2:
  61907. client_cbf.method = wolfTLSv1_2_client_method;
  61908. server_cbf.method = wolfTLSv1_2_server_method;
  61909. break;
  61910. #endif
  61911. #ifdef WOLFSSL_TLS13
  61912. case WOLFSSL_TLSV1_3:
  61913. client_cbf.method = wolfTLSv1_3_client_method;
  61914. server_cbf.method = wolfTLSv1_3_server_method;
  61915. break;
  61916. #endif
  61917. default:
  61918. printf("skipping\n");
  61919. continue;
  61920. }
  61921. test_get_signature_nid_siglag = params[i].siglag;
  61922. test_get_signature_nid_sig = params[i].sig_nid;
  61923. test_get_signature_nid_hash = params[i].hash_nid;
  61924. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  61925. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  61926. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  61927. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  61928. server_cbf.certPemFile = params[i].server_cert;
  61929. server_cbf.keyPemFile = params[i].server_key;
  61930. client_cbf.caPemFile = params[i].client_ca;
  61931. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61932. &server_cbf, NULL), TEST_SUCCESS);
  61933. if (EXPECT_SUCCESS())
  61934. printf("passed\n");
  61935. }
  61936. #endif
  61937. return EXPECT_RESULT();
  61938. }
  61939. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61940. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  61941. {
  61942. #ifndef NO_MD5
  61943. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  61944. #elif !defined(NO_SHA)
  61945. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  61946. #elif !defined(NO_SHA256)
  61947. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  61948. #else
  61949. #error "We need a digest to hash the Signer object"
  61950. #endif
  61951. byte hashBuf[WC_MAX_DIGEST_SIZE];
  61952. wc_HashAlg hash;
  61953. size_t i;
  61954. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  61955. for (i = 0; i < CA_TABLE_SIZE; i++) {
  61956. Signer* cur;
  61957. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  61958. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  61959. sizeof(*cur)), 0);
  61960. }
  61961. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  61962. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  61963. return MakeWordFromHash(hashBuf);
  61964. }
  61965. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  61966. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  61967. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  61968. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  61969. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  61970. {
  61971. EXPECT_DECLS;
  61972. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  61973. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  61974. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  61975. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  61976. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  61977. return EXPECT_RESULT();
  61978. }
  61979. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  61980. {
  61981. EXPECT_DECLS;
  61982. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  61983. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  61984. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  61985. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  61986. return EXPECT_RESULT();
  61987. }
  61988. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  61989. {
  61990. EXPECT_DECLS;
  61991. WOLFSSL_CERT_MANAGER* cm;
  61992. (void)ssl;
  61993. /* WARNING: this approach bypasses the reference counter check in
  61994. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  61995. * cause unexpected behaviour when other active connections try accessing
  61996. * the caTable. */
  61997. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  61998. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  61999. WOLFSSL_SUCCESS);
  62000. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62001. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62002. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  62003. return EXPECT_RESULT();
  62004. }
  62005. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  62006. {
  62007. EXPECT_DECLS;
  62008. WOLFSSL_CTX* ctx;
  62009. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  62010. return EXPECT_RESULT();
  62011. }
  62012. #endif
  62013. static int test_tls_cert_store_unchanged(void)
  62014. {
  62015. EXPECT_DECLS;
  62016. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62017. test_ssl_cbf client_cbf;
  62018. test_ssl_cbf server_cbf;
  62019. int i;
  62020. for (i = 0; i < 2; i++) {
  62021. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62022. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62023. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  62024. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  62025. sizeof(test_tls_cert_store_unchanged_before_hashes));
  62026. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  62027. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  62028. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62029. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62030. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62031. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62032. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62033. switch (i) {
  62034. case 0:
  62035. client_cbf.on_ctx_cleanup =
  62036. test_tls_cert_store_unchanged_ctx_cleanup;
  62037. server_cbf.on_ctx_cleanup =
  62038. test_tls_cert_store_unchanged_ctx_cleanup;
  62039. break;
  62040. case 1:
  62041. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62042. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62043. break;
  62044. default:
  62045. Fail(("Should not enter here"), ("Entered here"));
  62046. }
  62047. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  62048. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  62049. server_cbf.caPemFile = caCertFile;
  62050. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62051. &server_cbf, NULL), TEST_SUCCESS);
  62052. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  62053. test_tls_cert_store_unchanged_after_hashes,
  62054. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62055. }
  62056. #endif
  62057. return EXPECT_RESULT();
  62058. }
  62059. /*----------------------------------------------------------------------------*
  62060. | Main
  62061. *----------------------------------------------------------------------------*/
  62062. typedef int (*TEST_FUNC)(void);
  62063. typedef struct {
  62064. const char *name;
  62065. TEST_FUNC func;
  62066. byte run:1;
  62067. byte fail:1;
  62068. } TEST_CASE;
  62069. #define TEST_DECL(func) { #func, func, 0, 0 }
  62070. int testAll = 1;
  62071. TEST_CASE testCases[] = {
  62072. TEST_DECL(test_fileAccess),
  62073. /*********************************
  62074. * wolfcrypt
  62075. *********************************/
  62076. TEST_DECL(test_ForceZero),
  62077. TEST_DECL(test_wolfCrypt_Init),
  62078. /* Locking with Compat Mutex */
  62079. TEST_DECL(test_wc_SetMutexCb),
  62080. TEST_DECL(test_wc_LockMutex_ex),
  62081. /* Digests */
  62082. TEST_DECL(test_wc_InitMd5),
  62083. TEST_DECL(test_wc_Md5Update),
  62084. TEST_DECL(test_wc_Md5Final),
  62085. TEST_DECL(test_wc_InitSha),
  62086. TEST_DECL(test_wc_ShaUpdate),
  62087. TEST_DECL(test_wc_ShaFinal),
  62088. TEST_DECL(test_wc_InitSha256),
  62089. TEST_DECL(test_wc_Sha256Update),
  62090. TEST_DECL(test_wc_Sha256Final),
  62091. TEST_DECL(test_wc_Sha256FinalRaw),
  62092. TEST_DECL(test_wc_Sha256GetFlags),
  62093. TEST_DECL(test_wc_Sha256Free),
  62094. TEST_DECL(test_wc_Sha256GetHash),
  62095. TEST_DECL(test_wc_Sha256Copy),
  62096. TEST_DECL(test_wc_InitSha224),
  62097. TEST_DECL(test_wc_Sha224Update),
  62098. TEST_DECL(test_wc_Sha224Final),
  62099. TEST_DECL(test_wc_Sha224SetFlags),
  62100. TEST_DECL(test_wc_Sha224GetFlags),
  62101. TEST_DECL(test_wc_Sha224Free),
  62102. TEST_DECL(test_wc_Sha224GetHash),
  62103. TEST_DECL(test_wc_Sha224Copy),
  62104. TEST_DECL(test_wc_InitSha512),
  62105. TEST_DECL(test_wc_Sha512Update),
  62106. TEST_DECL(test_wc_Sha512Final),
  62107. TEST_DECL(test_wc_Sha512GetFlags),
  62108. TEST_DECL(test_wc_Sha512FinalRaw),
  62109. TEST_DECL(test_wc_Sha512Free),
  62110. TEST_DECL(test_wc_Sha512GetHash),
  62111. TEST_DECL(test_wc_Sha512Copy),
  62112. TEST_DECL(test_wc_InitSha512_224),
  62113. TEST_DECL(test_wc_Sha512_224Update),
  62114. TEST_DECL(test_wc_Sha512_224Final),
  62115. TEST_DECL(test_wc_Sha512_224GetFlags),
  62116. TEST_DECL(test_wc_Sha512_224FinalRaw),
  62117. TEST_DECL(test_wc_Sha512_224Free),
  62118. TEST_DECL(test_wc_Sha512_224GetHash),
  62119. TEST_DECL(test_wc_Sha512_224Copy),
  62120. TEST_DECL(test_wc_InitSha512_256),
  62121. TEST_DECL(test_wc_Sha512_256Update),
  62122. TEST_DECL(test_wc_Sha512_256Final),
  62123. TEST_DECL(test_wc_Sha512_256GetFlags),
  62124. TEST_DECL(test_wc_Sha512_256FinalRaw),
  62125. TEST_DECL(test_wc_Sha512_256Free),
  62126. TEST_DECL(test_wc_Sha512_256GetHash),
  62127. TEST_DECL(test_wc_Sha512_256Copy),
  62128. TEST_DECL(test_wc_InitSha384),
  62129. TEST_DECL(test_wc_Sha384Update),
  62130. TEST_DECL(test_wc_Sha384Final),
  62131. TEST_DECL(test_wc_Sha384GetFlags),
  62132. TEST_DECL(test_wc_Sha384FinalRaw),
  62133. TEST_DECL(test_wc_Sha384Free),
  62134. TEST_DECL(test_wc_Sha384GetHash),
  62135. TEST_DECL(test_wc_Sha384Copy),
  62136. TEST_DECL(test_wc_InitBlake2b),
  62137. TEST_DECL(test_wc_InitBlake2b_WithKey),
  62138. TEST_DECL(test_wc_InitBlake2s_WithKey),
  62139. TEST_DECL(test_wc_InitRipeMd),
  62140. TEST_DECL(test_wc_RipeMdUpdate),
  62141. TEST_DECL(test_wc_RipeMdFinal),
  62142. TEST_DECL(test_wc_InitSha3),
  62143. TEST_DECL(testing_wc_Sha3_Update),
  62144. TEST_DECL(test_wc_Sha3_224_Final),
  62145. TEST_DECL(test_wc_Sha3_256_Final),
  62146. TEST_DECL(test_wc_Sha3_384_Final),
  62147. TEST_DECL(test_wc_Sha3_512_Final),
  62148. TEST_DECL(test_wc_Sha3_224_Copy),
  62149. TEST_DECL(test_wc_Sha3_256_Copy),
  62150. TEST_DECL(test_wc_Sha3_384_Copy),
  62151. TEST_DECL(test_wc_Sha3_512_Copy),
  62152. TEST_DECL(test_wc_Sha3_GetFlags),
  62153. TEST_DECL(test_wc_InitShake256),
  62154. TEST_DECL(testing_wc_Shake256_Update),
  62155. TEST_DECL(test_wc_Shake256_Final),
  62156. TEST_DECL(test_wc_Shake256_Copy),
  62157. TEST_DECL(test_wc_Shake256Hash),
  62158. /* SM3 Digest */
  62159. TEST_DECL(test_wc_InitSm3Free),
  62160. TEST_DECL(test_wc_Sm3UpdateFinal),
  62161. TEST_DECL(test_wc_Sm3GetHash),
  62162. TEST_DECL(test_wc_Sm3Copy),
  62163. TEST_DECL(test_wc_Sm3FinalRaw),
  62164. TEST_DECL(test_wc_Sm3GetSetFlags),
  62165. TEST_DECL(test_wc_Sm3Hash),
  62166. TEST_DECL(test_wc_HashInit),
  62167. TEST_DECL(test_wc_HashSetFlags),
  62168. TEST_DECL(test_wc_HashGetFlags),
  62169. /* HMAC */
  62170. TEST_DECL(test_wc_Md5HmacSetKey),
  62171. TEST_DECL(test_wc_Md5HmacUpdate),
  62172. TEST_DECL(test_wc_Md5HmacFinal),
  62173. TEST_DECL(test_wc_ShaHmacSetKey),
  62174. TEST_DECL(test_wc_ShaHmacUpdate),
  62175. TEST_DECL(test_wc_ShaHmacFinal),
  62176. TEST_DECL(test_wc_Sha224HmacSetKey),
  62177. TEST_DECL(test_wc_Sha224HmacUpdate),
  62178. TEST_DECL(test_wc_Sha224HmacFinal),
  62179. TEST_DECL(test_wc_Sha256HmacSetKey),
  62180. TEST_DECL(test_wc_Sha256HmacUpdate),
  62181. TEST_DECL(test_wc_Sha256HmacFinal),
  62182. TEST_DECL(test_wc_Sha384HmacSetKey),
  62183. TEST_DECL(test_wc_Sha384HmacUpdate),
  62184. TEST_DECL(test_wc_Sha384HmacFinal),
  62185. /* CMAC */
  62186. TEST_DECL(test_wc_InitCmac),
  62187. TEST_DECL(test_wc_CmacUpdate),
  62188. TEST_DECL(test_wc_CmacFinal),
  62189. TEST_DECL(test_wc_AesCmacGenerate),
  62190. /* Cipher */
  62191. TEST_DECL(test_wc_AesGcmStream),
  62192. TEST_DECL(test_wc_Des3_SetIV),
  62193. TEST_DECL(test_wc_Des3_SetKey),
  62194. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  62195. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  62196. TEST_DECL(test_wc_Des3_EcbEncrypt),
  62197. TEST_DECL(test_wc_Chacha_SetKey),
  62198. TEST_DECL(test_wc_Chacha_Process),
  62199. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  62200. TEST_DECL(test_wc_Poly1305SetKey),
  62201. TEST_DECL(test_wc_CamelliaSetKey),
  62202. TEST_DECL(test_wc_CamelliaSetIV),
  62203. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  62204. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  62205. TEST_DECL(test_wc_Arc4SetKey),
  62206. TEST_DECL(test_wc_Arc4Process),
  62207. TEST_DECL(test_wc_Rc2SetKey),
  62208. TEST_DECL(test_wc_Rc2SetIV),
  62209. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  62210. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  62211. /* AES cipher and GMAC. */
  62212. TEST_DECL(test_wc_AesSetKey),
  62213. TEST_DECL(test_wc_AesSetIV),
  62214. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  62215. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  62216. TEST_DECL(test_wc_AesGcmSetKey),
  62217. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  62218. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  62219. TEST_DECL(test_wc_GmacSetKey),
  62220. TEST_DECL(test_wc_GmacUpdate),
  62221. TEST_DECL(test_wc_AesCcmSetKey),
  62222. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  62223. #if defined(WOLFSSL_AES_EAX) && \
  62224. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  62225. TEST_DECL(test_wc_AesEaxVectors),
  62226. TEST_DECL(test_wc_AesEaxEncryptAuth),
  62227. TEST_DECL(test_wc_AesEaxDecryptAuth),
  62228. #endif /* WOLFSSL_AES_EAX */
  62229. /* SM4 cipher */
  62230. TEST_DECL(test_wc_Sm4),
  62231. TEST_DECL(test_wc_Sm4Ecb),
  62232. TEST_DECL(test_wc_Sm4Cbc),
  62233. TEST_DECL(test_wc_Sm4Ctr),
  62234. TEST_DECL(test_wc_Sm4Gcm),
  62235. TEST_DECL(test_wc_Sm4Ccm),
  62236. /* RNG tests */
  62237. #ifdef HAVE_HASHDRBG
  62238. #ifdef TEST_RESEED_INTERVAL
  62239. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  62240. #endif
  62241. TEST_DECL(test_wc_RNG_GenerateBlock),
  62242. #endif
  62243. TEST_DECL(test_get_rand_digit),
  62244. TEST_DECL(test_wc_InitRngNonce),
  62245. TEST_DECL(test_wc_InitRngNonce_ex),
  62246. /* MP API tests */
  62247. TEST_DECL(test_get_digit_count),
  62248. TEST_DECL(test_mp_cond_copy),
  62249. TEST_DECL(test_mp_rand),
  62250. TEST_DECL(test_get_digit),
  62251. TEST_DECL(test_wc_export_int),
  62252. /* RSA */
  62253. TEST_DECL(test_wc_InitRsaKey),
  62254. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  62255. TEST_DECL(test_wc_RsaPublicKeyDecode),
  62256. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  62257. TEST_DECL(test_wc_MakeRsaKey),
  62258. TEST_DECL(test_wc_CheckProbablePrime),
  62259. TEST_DECL(test_wc_RsaPSS_Verify),
  62260. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  62261. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  62262. TEST_DECL(test_wc_RsaKeyToDer),
  62263. TEST_DECL(test_wc_RsaKeyToPublicDer),
  62264. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  62265. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  62266. TEST_DECL(test_wc_RsaEncryptSize),
  62267. TEST_DECL(test_wc_RsaSSL_SignVerify),
  62268. TEST_DECL(test_wc_RsaFlattenPublicKey),
  62269. TEST_DECL(test_RsaDecryptBoundsCheck),
  62270. /* DSA */
  62271. TEST_DECL(test_wc_InitDsaKey),
  62272. TEST_DECL(test_wc_DsaSignVerify),
  62273. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  62274. TEST_DECL(test_wc_MakeDsaKey),
  62275. TEST_DECL(test_wc_DsaKeyToDer),
  62276. TEST_DECL(test_wc_DsaKeyToPublicDer),
  62277. TEST_DECL(test_wc_DsaImportParamsRaw),
  62278. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  62279. TEST_DECL(test_wc_DsaExportParamsRaw),
  62280. TEST_DECL(test_wc_DsaExportKeyRaw),
  62281. /* DH */
  62282. TEST_DECL(test_wc_DhPublicKeyDecode),
  62283. /* wolfCrypt ECC tests */
  62284. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  62285. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  62286. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  62287. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  62288. !defined(HAVE_SELFTEST) && \
  62289. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  62290. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  62291. #endif
  62292. TEST_DECL(test_wc_ecc_make_key),
  62293. TEST_DECL(test_wc_ecc_init),
  62294. TEST_DECL(test_wc_ecc_check_key),
  62295. TEST_DECL(test_wc_ecc_get_generator),
  62296. TEST_DECL(test_wc_ecc_size),
  62297. TEST_DECL(test_wc_ecc_params),
  62298. TEST_DECL(test_wc_ecc_signVerify_hash),
  62299. TEST_DECL(test_wc_ecc_shared_secret),
  62300. TEST_DECL(test_wc_ecc_export_x963),
  62301. TEST_DECL(test_wc_ecc_export_x963_ex),
  62302. TEST_DECL(test_wc_ecc_import_x963),
  62303. TEST_DECL(test_wc_ecc_import_private_key),
  62304. TEST_DECL(test_wc_ecc_export_private_only),
  62305. TEST_DECL(test_wc_ecc_rs_to_sig),
  62306. TEST_DECL(test_wc_ecc_import_raw),
  62307. TEST_DECL(test_wc_ecc_import_unsigned),
  62308. TEST_DECL(test_wc_ecc_sig_size),
  62309. TEST_DECL(test_wc_ecc_ctx_new),
  62310. TEST_DECL(test_wc_ecc_ctx_reset),
  62311. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  62312. TEST_DECL(test_wc_ecc_ctx_set_info),
  62313. TEST_DECL(test_wc_ecc_encryptDecrypt),
  62314. TEST_DECL(test_wc_ecc_del_point),
  62315. TEST_DECL(test_wc_ecc_pointFns),
  62316. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  62317. TEST_DECL(test_wc_ecc_verify_hash_ex),
  62318. TEST_DECL(test_wc_ecc_mulmod),
  62319. TEST_DECL(test_wc_ecc_is_valid_idx),
  62320. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  62321. TEST_DECL(test_wc_ecc_sig_size_calc),
  62322. TEST_DECL(test_wc_EccPrivateKeyToDer),
  62323. /* SM2 elliptic curve */
  62324. TEST_DECL(test_wc_ecc_sm2_make_key),
  62325. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  62326. TEST_DECL(test_wc_ecc_sm2_create_digest),
  62327. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  62328. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  62329. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  62330. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  62331. /* Curve25519 */
  62332. TEST_DECL(test_wc_curve25519_init),
  62333. TEST_DECL(test_wc_curve25519_size),
  62334. TEST_DECL(test_wc_curve25519_export_key_raw),
  62335. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  62336. TEST_DECL(test_wc_curve25519_make_key),
  62337. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  62338. TEST_DECL(test_wc_curve25519_make_pub),
  62339. TEST_DECL(test_wc_curve25519_export_public_ex),
  62340. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  62341. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  62342. TEST_DECL(test_wc_curve25519_import_private),
  62343. /* ED25519 */
  62344. TEST_DECL(test_wc_ed25519_make_key),
  62345. TEST_DECL(test_wc_ed25519_init),
  62346. TEST_DECL(test_wc_ed25519_sign_msg),
  62347. TEST_DECL(test_wc_ed25519_import_public),
  62348. TEST_DECL(test_wc_ed25519_import_private_key),
  62349. TEST_DECL(test_wc_ed25519_export),
  62350. TEST_DECL(test_wc_ed25519_size),
  62351. TEST_DECL(test_wc_ed25519_exportKey),
  62352. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  62353. TEST_DECL(test_wc_Ed25519KeyToDer),
  62354. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  62355. /* Curve448 */
  62356. TEST_DECL(test_wc_curve448_make_key),
  62357. TEST_DECL(test_wc_curve448_shared_secret_ex),
  62358. TEST_DECL(test_wc_curve448_export_public_ex),
  62359. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  62360. TEST_DECL(test_wc_curve448_export_key_raw),
  62361. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  62362. TEST_DECL(test_wc_curve448_import_private),
  62363. TEST_DECL(test_wc_curve448_init),
  62364. TEST_DECL(test_wc_curve448_size),
  62365. /* Ed448 */
  62366. TEST_DECL(test_wc_ed448_make_key),
  62367. TEST_DECL(test_wc_ed448_init),
  62368. TEST_DECL(test_wc_ed448_sign_msg),
  62369. TEST_DECL(test_wc_ed448_import_public),
  62370. TEST_DECL(test_wc_ed448_import_private_key),
  62371. TEST_DECL(test_wc_ed448_export),
  62372. TEST_DECL(test_wc_ed448_size),
  62373. TEST_DECL(test_wc_ed448_exportKey),
  62374. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  62375. TEST_DECL(test_wc_Ed448KeyToDer),
  62376. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  62377. /* Signature API */
  62378. TEST_DECL(test_wc_SignatureGetSize_ecc),
  62379. TEST_DECL(test_wc_SignatureGetSize_rsa),
  62380. /* PEM and DER APIs. */
  62381. TEST_DECL(test_wc_PemToDer),
  62382. TEST_DECL(test_wc_AllocDer),
  62383. TEST_DECL(test_wc_CertPemToDer),
  62384. TEST_DECL(test_wc_KeyPemToDer),
  62385. TEST_DECL(test_wc_PubKeyPemToDer),
  62386. TEST_DECL(test_wc_PemPubKeyToDer),
  62387. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  62388. TEST_DECL(test_wc_CheckCertSigPubKey),
  62389. /* wolfCrypt ASN tests */
  62390. TEST_DECL(test_ToTraditional),
  62391. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  62392. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  62393. /* Certificate */
  62394. TEST_DECL(test_wc_SetSubjectRaw),
  62395. TEST_DECL(test_wc_GetSubjectRaw),
  62396. TEST_DECL(test_wc_SetIssuerRaw),
  62397. TEST_DECL(test_wc_SetIssueBuffer),
  62398. TEST_DECL(test_wc_SetSubjectKeyId),
  62399. TEST_DECL(test_wc_SetSubject),
  62400. TEST_DECL(test_CheckCertSignature),
  62401. TEST_DECL(test_wc_ParseCert),
  62402. TEST_DECL(test_wc_ParseCert_Error),
  62403. TEST_DECL(test_MakeCertWithPathLen),
  62404. TEST_DECL(test_MakeCertWithCaFalse),
  62405. TEST_DECL(test_wc_SetKeyUsage),
  62406. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  62407. TEST_DECL(test_wc_SetSubjectBuffer),
  62408. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  62409. /* wolfcrypt PKCS#7 */
  62410. TEST_DECL(test_wc_PKCS7_New),
  62411. TEST_DECL(test_wc_PKCS7_Init),
  62412. TEST_DECL(test_wc_PKCS7_InitWithCert),
  62413. TEST_DECL(test_wc_PKCS7_EncodeData),
  62414. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  62415. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  62416. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  62417. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  62418. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  62419. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  62420. TEST_DECL(test_wc_PKCS7_Degenerate),
  62421. TEST_DECL(test_wc_PKCS7_BER),
  62422. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  62423. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  62424. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  62425. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  62426. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  62427. /* wolfCrypt PKCS#12 */
  62428. TEST_DECL(test_wc_i2d_PKCS12),
  62429. /*
  62430. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  62431. * avoid memory leaks.
  62432. */
  62433. TEST_DECL(test_wolfCrypt_Cleanup),
  62434. TEST_DECL(test_wolfSSL_Init),
  62435. TEST_DECL(test_dual_alg_support),
  62436. /*********************************
  62437. * OpenSSL compatibility API tests
  62438. *********************************/
  62439. /* If at some point a stub get implemented this test should fail indicating
  62440. * a need to implement a new test case
  62441. */
  62442. TEST_DECL(test_stubs_are_stubs),
  62443. /* ASN.1 compatibility API tests */
  62444. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  62445. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  62446. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  62447. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  62448. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  62449. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  62450. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  62451. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  62452. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  62453. TEST_DECL(test_wolfSSL_ASN1_get_object),
  62454. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  62455. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  62456. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  62457. TEST_DECL(test_wolfSSL_ASN1_STRING),
  62458. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  62459. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  62460. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  62461. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  62462. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  62463. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  62464. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  62465. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  62466. TEST_DECL(test_wolfSSL_ASN1_TIME),
  62467. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  62468. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  62469. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  62470. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  62471. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  62472. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  62473. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  62474. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  62475. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  62476. TEST_DECL(test_wolfSSL_lhash),
  62477. TEST_DECL(test_wolfSSL_certs),
  62478. TEST_DECL(test_wolfSSL_private_keys),
  62479. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  62480. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  62481. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  62482. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  62483. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  62484. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  62485. #ifndef NO_BIO
  62486. TEST_DECL(test_wolfSSL_BIO),
  62487. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  62488. TEST_DECL(test_wolfSSL_PEM_read_bio),
  62489. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  62490. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  62491. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  62492. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  62493. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  62494. #endif
  62495. /* EVP API testing */
  62496. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  62497. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  62498. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  62499. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  62500. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  62501. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  62502. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  62503. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  62504. TEST_DECL(test_wolfSSL_EVP_shake128),
  62505. TEST_DECL(test_wolfSSL_EVP_shake256),
  62506. TEST_DECL(test_wolfSSL_EVP_sm3),
  62507. TEST_DECL(test_EVP_blake2),
  62508. #ifdef OPENSSL_ALL
  62509. TEST_DECL(test_wolfSSL_EVP_md4),
  62510. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  62511. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  62512. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  62513. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  62514. #endif
  62515. TEST_DECL(test_EVP_MD_do_all),
  62516. TEST_DECL(test_wolfSSL_EVP_MD_size),
  62517. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  62518. TEST_DECL(test_wolfSSL_EVP_Digest),
  62519. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  62520. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  62521. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  62522. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  62523. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  62524. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  62525. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  62526. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  62527. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  62528. TEST_DECL(test_wolfssl_EVP_chacha20),
  62529. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  62530. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  62531. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  62532. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  62533. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  62534. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  62535. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  62536. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  62537. #ifdef OPENSSL_ALL
  62538. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  62539. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  62540. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  62541. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  62542. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  62543. TEST_DECL(test_wolfSSL_EVP_rc4),
  62544. TEST_DECL(test_wolfSSL_EVP_enc_null),
  62545. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  62546. TEST_DECL(test_wolfSSL_EVP_mdc2),
  62547. TEST_DECL(test_evp_cipher_aes_gcm),
  62548. #endif
  62549. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  62550. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  62551. #ifdef OPENSSL_EXTRA
  62552. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  62553. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  62554. #endif
  62555. #ifdef OPENSSL_ALL
  62556. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  62557. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  62558. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  62559. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  62560. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  62561. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  62562. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  62563. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  62564. #endif
  62565. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  62566. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  62567. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  62568. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  62569. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  62570. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  62571. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  62572. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  62573. #ifndef NO_BIO
  62574. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  62575. #endif
  62576. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  62577. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  62578. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  62579. #ifndef NO_BIO
  62580. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  62581. #endif /* !NO_BIO */
  62582. #endif
  62583. #ifdef OPENSSL_ALL
  62584. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  62585. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  62586. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  62587. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  62588. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  62589. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  62590. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  62591. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  62592. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  62593. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  62594. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  62595. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  62596. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  62597. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  62598. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  62599. #endif
  62600. TEST_DECL(test_EVP_PKEY_rsa),
  62601. TEST_DECL(test_EVP_PKEY_ec),
  62602. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  62603. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  62604. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  62605. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  62606. TEST_DECL(test_EVP_PKEY_cmp),
  62607. #ifdef OPENSSL_ALL
  62608. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  62609. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  62610. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  62611. #endif
  62612. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  62613. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  62614. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  62615. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  62616. #endif
  62617. #ifndef NO_BIO
  62618. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  62619. TEST_DECL(test_wolfSSL_GetLoggingCb),
  62620. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  62621. TEST_DECL(test_wc_ERR_remove_state),
  62622. TEST_DECL(test_wc_ERR_print_errors_fp),
  62623. #endif
  62624. TEST_DECL(test_wolfSSL_configure_args),
  62625. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  62626. TEST_DECL(test_wolfSSL_set1_curves_list),
  62627. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  62628. TEST_DECL(test_wolfSSL_OtherName),
  62629. TEST_DECL(test_wolfSSL_FPKI),
  62630. TEST_DECL(test_wolfSSL_URI),
  62631. TEST_DECL(test_wolfSSL_TBS),
  62632. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  62633. TEST_DECL(test_X509_STORE_untrusted),
  62634. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  62635. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  62636. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  62637. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  62638. TEST_DECL(test_wolfSSL_X509_Name_canon),
  62639. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  62640. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  62641. TEST_DECL(test_wolfSSL_X509_NID),
  62642. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  62643. TEST_DECL(test_wolfSSL_get0_param),
  62644. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  62645. TEST_DECL(test_wolfSSL_set1_host),
  62646. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  62647. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  62648. TEST_DECL(test_wolfSSL_X509_STORE),
  62649. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  62650. TEST_DECL(test_X509_STORE_get0_objects),
  62651. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  62652. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  62653. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  62654. TEST_DECL(test_wolfSSL_X509_cmp_time),
  62655. TEST_DECL(test_wolfSSL_X509_time_adj),
  62656. /* X509 tests */
  62657. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  62658. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  62659. TEST_DECL(test_wolfSSL_X509_check_host),
  62660. TEST_DECL(test_wolfSSL_X509_check_email),
  62661. TEST_DECL(test_wolfSSL_X509_check_private_key),
  62662. TEST_DECL(test_wolfSSL_X509),
  62663. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  62664. TEST_DECL(test_wolfSSL_X509_sign),
  62665. TEST_DECL(test_wolfSSL_X509_sign2),
  62666. TEST_DECL(test_wolfSSL_X509_verify),
  62667. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  62668. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  62669. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  62670. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  62671. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  62672. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  62673. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  62674. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  62675. TEST_DECL(test_wolfSSL_X509_NAME),
  62676. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  62677. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  62678. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  62679. TEST_DECL(test_wolfSSL_X509_set_name),
  62680. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  62681. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  62682. TEST_DECL(test_wolfSSL_X509_set_version),
  62683. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  62684. TEST_DECL(test_wolfSSL_X509_CRL),
  62685. TEST_DECL(test_wolfSSL_i2d_X509),
  62686. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  62687. TEST_DECL(test_wolfSSL_PEM_read_X509),
  62688. TEST_DECL(test_wolfSSL_X509_check_ca),
  62689. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  62690. TEST_DECL(test_wolfSSL_make_cert),
  62691. #ifndef NO_BIO
  62692. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  62693. TEST_DECL(test_wolfSSL_X509_INFO),
  62694. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  62695. #endif
  62696. #ifdef OPENSSL_ALL
  62697. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  62698. #endif
  62699. TEST_DECL(test_wolfSSL_X509_CA_num),
  62700. TEST_DECL(test_wolfSSL_X509_get_version),
  62701. #ifndef NO_BIO
  62702. TEST_DECL(test_wolfSSL_X509_print),
  62703. TEST_DECL(test_wolfSSL_X509_CRL_print),
  62704. #endif
  62705. TEST_DECL(test_X509_get_signature_nid),
  62706. /* X509 extension testing. */
  62707. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  62708. TEST_DECL(test_wolfSSL_X509_get_ext),
  62709. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  62710. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  62711. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  62712. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  62713. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  62714. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  62715. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  62716. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  62717. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  62718. TEST_DECL(test_wolfSSL_X509V3_EXT),
  62719. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  62720. TEST_DECL(test_wolfSSL_X509_cmp),
  62721. TEST_DECL(test_GENERAL_NAME_set0_othername),
  62722. TEST_DECL(test_othername_and_SID_ext),
  62723. TEST_DECL(test_wolfSSL_dup_CA_list),
  62724. /* OpenSSL sk_X509 API test */
  62725. TEST_DECL(test_sk_X509),
  62726. /* OpenSSL sk_X509_CRL API test */
  62727. TEST_DECL(test_sk_X509_CRL),
  62728. /* OpenSSL X509 REQ API test */
  62729. TEST_DECL(test_X509_REQ),
  62730. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  62731. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  62732. TEST_DECL(test_X509_LOOKUP_add_dir),
  62733. /* RAND compatibility API */
  62734. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  62735. TEST_DECL(test_wolfSSL_RAND_bytes),
  62736. TEST_DECL(test_wolfSSL_RAND),
  62737. /* BN compatibility API */
  62738. TEST_DECL(test_wolfSSL_BN_CTX),
  62739. TEST_DECL(test_wolfSSL_BN),
  62740. TEST_DECL(test_wolfSSL_BN_init),
  62741. TEST_DECL(test_wolfSSL_BN_enc_dec),
  62742. TEST_DECL(test_wolfSSL_BN_word),
  62743. TEST_DECL(test_wolfSSL_BN_bits),
  62744. TEST_DECL(test_wolfSSL_BN_shift),
  62745. TEST_DECL(test_wolfSSL_BN_math),
  62746. TEST_DECL(test_wolfSSL_BN_math_mod),
  62747. TEST_DECL(test_wolfSSL_BN_math_other),
  62748. TEST_DECL(test_wolfSSL_BN_rand),
  62749. TEST_DECL(test_wolfSSL_BN_prime),
  62750. /* OpenSSL PKCS5 API test */
  62751. TEST_DECL(test_wolfSSL_PKCS5),
  62752. /* OpenSSL PKCS8 API test */
  62753. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  62754. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  62755. /* OpenSSL PKCS7 API test */
  62756. TEST_DECL(test_wolfssl_PKCS7),
  62757. TEST_DECL(test_wolfSSL_PKCS7_certs),
  62758. TEST_DECL(test_wolfSSL_PKCS7_sign),
  62759. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  62760. #ifndef NO_BIO
  62761. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  62762. #ifdef HAVE_SMIME
  62763. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  62764. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  62765. #endif /* HAVE_SMIME */
  62766. #endif /* !NO_BIO */
  62767. /* OpenSSL PKCS12 API test */
  62768. TEST_DECL(test_wolfSSL_PKCS12),
  62769. /* Can't memory test as callbacks use Assert. */
  62770. TEST_DECL(test_error_queue_per_thread),
  62771. TEST_DECL(test_wolfSSL_ERR_put_error),
  62772. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  62773. #ifndef NO_BIO
  62774. TEST_DECL(test_wolfSSL_ERR_print_errors),
  62775. #endif
  62776. TEST_DECL(test_OBJ_NAME_do_all),
  62777. TEST_DECL(test_wolfSSL_OBJ),
  62778. TEST_DECL(test_wolfSSL_OBJ_cmp),
  62779. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  62780. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  62781. #ifdef OPENSSL_ALL
  62782. TEST_DECL(test_wolfSSL_OBJ_ln),
  62783. TEST_DECL(test_wolfSSL_OBJ_sn),
  62784. #endif
  62785. #ifndef NO_BIO
  62786. TEST_DECL(test_wolfSSL_BIO_gets),
  62787. TEST_DECL(test_wolfSSL_BIO_puts),
  62788. TEST_DECL(test_wolfSSL_BIO_dump),
  62789. /* Can't memory test as server hangs. */
  62790. TEST_DECL(test_wolfSSL_BIO_should_retry),
  62791. TEST_DECL(test_wolfSSL_BIO_write),
  62792. TEST_DECL(test_wolfSSL_BIO_printf),
  62793. TEST_DECL(test_wolfSSL_BIO_f_md),
  62794. TEST_DECL(test_wolfSSL_BIO_up_ref),
  62795. TEST_DECL(test_wolfSSL_BIO_reset),
  62796. TEST_DECL(test_wolfSSL_BIO_get_len),
  62797. #endif
  62798. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62799. TEST_DECL(test_wolfSSL_check_domain),
  62800. #endif
  62801. TEST_DECL(test_wolfSSL_cert_cb),
  62802. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  62803. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  62804. TEST_DECL(test_wolfSSL_sigalg_info),
  62805. /* Can't memory test as tcp_connect aborts. */
  62806. TEST_DECL(test_wolfSSL_SESSION),
  62807. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  62808. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  62809. TEST_DECL(test_wolfSSL_ticket_keys),
  62810. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  62811. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  62812. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  62813. TEST_DECL(test_wolfSSL_verify_mode),
  62814. TEST_DECL(test_wolfSSL_verify_depth),
  62815. TEST_DECL(test_wolfSSL_verify_result),
  62816. TEST_DECL(test_wolfSSL_msg_callback),
  62817. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  62818. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  62819. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  62820. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  62821. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  62822. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  62823. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  62824. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  62825. TEST_DECL(test_wolfSSL_PEM_read),
  62826. TEST_DECL(test_wolfSSL_OpenSSL_version),
  62827. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  62828. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  62829. TEST_DECL(test_CONF_modules_xxx),
  62830. #ifdef OPENSSL_ALL
  62831. TEST_DECL(test_wolfSSL_TXT_DB),
  62832. TEST_DECL(test_wolfSSL_NCONF),
  62833. #endif
  62834. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  62835. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  62836. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  62837. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  62838. TEST_DECL(test_CRYPTO_THREADID_xxx),
  62839. TEST_DECL(test_ENGINE_cleanup),
  62840. /* test the no op functions for compatibility */
  62841. TEST_DECL(test_no_op_functions),
  62842. /* OpenSSL error API tests */
  62843. TEST_DECL(test_ERR_load_crypto_strings),
  62844. #ifdef OPENSSL_ALL
  62845. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  62846. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  62847. TEST_DECL(test_wolfSSL_CTX_ctrl),
  62848. #endif /* OPENSSL_ALL */
  62849. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  62850. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  62851. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  62852. /*********************************
  62853. * Crypto API tests
  62854. *********************************/
  62855. TEST_DECL(test_wolfSSL_MD4),
  62856. TEST_DECL(test_wolfSSL_MD5),
  62857. TEST_DECL(test_wolfSSL_MD5_Transform),
  62858. TEST_DECL(test_wolfSSL_SHA),
  62859. TEST_DECL(test_wolfSSL_SHA_Transform),
  62860. TEST_DECL(test_wolfSSL_SHA224),
  62861. TEST_DECL(test_wolfSSL_SHA256),
  62862. TEST_DECL(test_wolfSSL_SHA256_Transform),
  62863. TEST_DECL(test_wolfSSL_SHA512_Transform),
  62864. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  62865. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  62866. TEST_DECL(test_wolfSSL_HMAC_CTX),
  62867. TEST_DECL(test_wolfSSL_HMAC),
  62868. TEST_DECL(test_wolfSSL_CMAC),
  62869. TEST_DECL(test_wolfSSL_DES),
  62870. TEST_DECL(test_wolfSSL_DES_ncbc),
  62871. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  62872. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  62873. TEST_DECL(test_wolfSSL_AES_encrypt),
  62874. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  62875. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  62876. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  62877. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  62878. TEST_DECL(test_wolfSSL_RC4),
  62879. TEST_DECL(test_wolfSSL_RSA),
  62880. TEST_DECL(test_wolfSSL_RSA_DER),
  62881. TEST_DECL(test_wolfSSL_RSA_print),
  62882. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  62883. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  62884. TEST_DECL(test_wolfSSL_RSA_get0_key),
  62885. TEST_DECL(test_wolfSSL_RSA_meth),
  62886. TEST_DECL(test_wolfSSL_RSA_verify),
  62887. TEST_DECL(test_wolfSSL_RSA_sign),
  62888. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  62889. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  62890. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  62891. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  62892. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  62893. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  62894. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  62895. TEST_DECL(test_wolfSSL_RSA_ex_data),
  62896. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  62897. TEST_DECL(test_wolfSSL_RSA_To_Der),
  62898. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  62899. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  62900. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  62901. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  62902. TEST_DECL(test_wolfSSL_DH),
  62903. TEST_DECL(test_wolfSSL_DH_dup),
  62904. TEST_DECL(test_wolfSSL_DH_check),
  62905. TEST_DECL(test_wolfSSL_DH_prime),
  62906. TEST_DECL(test_wolfSSL_DH_1536_prime),
  62907. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  62908. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  62909. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  62910. TEST_DECL(test_wolfSSL_d2i_DHparams),
  62911. TEST_DECL(test_wolfSSL_DH_LoadDer),
  62912. TEST_DECL(test_wolfSSL_i2d_DHparams),
  62913. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  62914. TEST_DECL(test_wolfSSL_EC_GROUP),
  62915. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  62916. TEST_DECL(test_wolfSSL_EC_POINT),
  62917. TEST_DECL(test_wolfSSL_SPAKE),
  62918. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  62919. TEST_DECL(test_EC_i2d),
  62920. TEST_DECL(test_wolfSSL_EC_curve),
  62921. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  62922. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  62923. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  62924. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  62925. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  62926. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  62927. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  62928. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  62929. TEST_DECL(test_ECDSA_size_sign),
  62930. TEST_DECL(test_ECDH_compute_key),
  62931. #endif
  62932. #ifdef OPENSSL_EXTRA
  62933. TEST_DECL(test_ED25519),
  62934. TEST_DECL(test_ED448),
  62935. #endif
  62936. TEST_DECL(test_DSA_do_sign_verify),
  62937. #ifdef OPENSSL_ALL
  62938. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  62939. TEST_DECL(test_wolfSSL_DSA_SIG),
  62940. #endif
  62941. TEST_DECL(test_openssl_generate_key_and_cert),
  62942. TEST_DECL(test_wolfSSL_FIPS_mode),
  62943. TEST_DECL(test_openssl_FIPS_drbg),
  62944. /*********************************
  62945. * CertManager API tests
  62946. *********************************/
  62947. TEST_DECL(test_wolfSSL_CertManagerAPI),
  62948. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  62949. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  62950. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  62951. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  62952. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  62953. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  62954. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  62955. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  62956. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  62957. TEST_DECL(test_wolfSSL_CertManagerCRL),
  62958. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  62959. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  62960. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  62961. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  62962. !defined(WOLFSSL_NO_CLIENT_AUTH))
  62963. TEST_DECL(test_various_pathlen_chains),
  62964. #endif
  62965. /*********************************
  62966. * SSL/TLS API tests
  62967. *********************************/
  62968. TEST_DECL(test_wolfSSL_Method_Allocators),
  62969. #ifndef NO_WOLFSSL_SERVER
  62970. TEST_DECL(test_wolfSSL_CTX_new),
  62971. #endif
  62972. TEST_DECL(test_server_wolfSSL_new),
  62973. TEST_DECL(test_client_wolfSSL_new),
  62974. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  62975. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  62976. TEST_DECL(test_for_double_Free),
  62977. #endif
  62978. TEST_DECL(test_wolfSSL_set_options),
  62979. #ifdef WOLFSSL_TLS13
  62980. /* TLS v1.3 API tests */
  62981. TEST_DECL(test_tls13_apis),
  62982. TEST_DECL(test_tls13_cipher_suites),
  62983. #endif
  62984. TEST_DECL(test_wolfSSL_tmp_dh),
  62985. TEST_DECL(test_wolfSSL_ctrl),
  62986. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  62987. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  62988. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  62989. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  62990. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  62991. #endif
  62992. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  62993. TEST_DECL(test_wolfSSL_security_level),
  62994. TEST_DECL(test_wolfSSL_SSL_in_init),
  62995. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  62996. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  62997. TEST_DECL(test_CONF_CTX_FILE),
  62998. TEST_DECL(test_CONF_CTX_CMDLINE),
  62999. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63000. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  63001. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  63002. /* Bad certificate signature tests */
  63003. TEST_DECL(test_EccSigFailure_cm),
  63004. TEST_DECL(test_RsaSigFailure_cm),
  63005. #endif /* NO_CERTS */
  63006. /* PKCS8 testing */
  63007. TEST_DECL(test_wolfSSL_no_password_cb),
  63008. TEST_DECL(test_wolfSSL_PKCS8),
  63009. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  63010. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  63011. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63012. TEST_DECL(test_wolfSSL_get_finished),
  63013. /* Uses Assert in handshake callback. */
  63014. TEST_DECL(test_wolfSSL_CTX_add_session),
  63015. /* Large number of memory allocations. */
  63016. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  63017. /* Large number of memory allocations. */
  63018. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  63019. /* Large number of memory allocations. */
  63020. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  63021. /* Large number of memory allocations. */
  63022. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  63023. /* Large number of memory allocations. */
  63024. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  63025. /* Large number of memory allocations. */
  63026. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  63027. #endif
  63028. TEST_DECL(test_SSL_CIPHER_get_xxx),
  63029. TEST_DECL(test_wolfSSL_ERR_strings),
  63030. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  63031. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  63032. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  63033. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  63034. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  63035. /* Large number of memory allocations. */
  63036. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  63037. TEST_DECL(test_wolfSSL_CertRsaPss),
  63038. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  63039. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  63040. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  63041. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  63042. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  63043. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  63044. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  63045. TEST_DECL(test_multiple_crls_same_issuer),
  63046. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  63047. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  63048. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  63049. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  63050. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  63051. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  63052. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  63053. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  63054. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  63055. TEST_DECL(test_SetTmpEC_DHE_Sz),
  63056. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  63057. #ifdef WOLFSSL_DTLS
  63058. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  63059. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  63060. #endif
  63061. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  63062. /* Uses Assert in handshake callback. */
  63063. TEST_DECL(test_wolfSSL_dtls_plaintext),
  63064. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63065. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63066. TEST_DECL(test_wolfSSL_read_write),
  63067. /* Can't memory test as server hangs if client fails before second connect.
  63068. */
  63069. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  63070. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  63071. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  63072. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  63073. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  63074. /* Can't memory test as server hangs. */
  63075. TEST_DECL(test_wolfSSL_dtls_export),
  63076. /* Uses Assert in handshake callback. */
  63077. TEST_DECL(test_wolfSSL_tls_export),
  63078. #endif
  63079. TEST_DECL(test_wolfSSL_dtls_export_peers),
  63080. TEST_DECL(test_wolfSSL_SetMinVersion),
  63081. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  63082. /* wolfSSL handshake APIs. */
  63083. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  63084. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  63085. TEST_DECL(test_wolfSSL_BUF),
  63086. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  63087. /* Can't memory test as server hangs. */
  63088. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  63089. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  63090. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  63091. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  63092. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  63093. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  63094. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  63095. /* Can't memory test as server hangs. */
  63096. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  63097. TEST_DECL(test_wolfSSL_Tls13_postauth),
  63098. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  63099. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  63100. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  63101. TEST_DECL(test_wolfSSL_THREADID_hash),
  63102. /* TLS extensions tests */
  63103. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63104. #ifdef HAVE_SNI
  63105. TEST_DECL(test_wolfSSL_UseSNI_params),
  63106. /* Uses Assert in handshake callback. */
  63107. TEST_DECL(test_wolfSSL_UseSNI_connection),
  63108. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  63109. #endif /* HAVE_SNI */
  63110. #endif
  63111. TEST_DECL(test_wolfSSL_UseTrustedCA),
  63112. TEST_DECL(test_wolfSSL_UseMaxFragment),
  63113. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  63114. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  63115. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  63116. /* Uses Assert in handshake callback. */
  63117. TEST_DECL(test_wolfSSL_UseALPN_connection),
  63118. TEST_DECL(test_wolfSSL_UseALPN_params),
  63119. #endif
  63120. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  63121. /* Uses Assert in handshake callback. */
  63122. TEST_DECL(test_wolfSSL_set_alpn_protos),
  63123. #endif
  63124. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  63125. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  63126. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  63127. TEST_DECL(test_tls_ext_duplicate),
  63128. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  63129. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63130. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  63131. /* Uses Assert in handshake callback. */
  63132. TEST_DECL(test_wolfSSL_Tls13_ECH),
  63133. #endif
  63134. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  63135. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  63136. /* OCSP Stapling */
  63137. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  63138. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  63139. TEST_DECL(test_self_signed_stapling),
  63140. /* Multicast */
  63141. TEST_DECL(test_wolfSSL_mcast),
  63142. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  63143. TEST_DECL(test_wolfSSL_msgCb),
  63144. TEST_DECL(test_wolfSSL_either_side),
  63145. TEST_DECL(test_wolfSSL_DTLS_either_side),
  63146. /* Uses Assert in handshake callback. */
  63147. TEST_DECL(test_wolfSSL_dtls_fragments),
  63148. /* Uses Assert in handshake callback. */
  63149. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  63150. /* Uses Assert in handshake callback. */
  63151. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  63152. /* Uses Assert in handshake callback. */
  63153. TEST_DECL(test_wolfSSL_dtls_bad_record),
  63154. /* Uses Assert in handshake callback. */
  63155. TEST_DECL(test_wolfSSL_dtls_stateless),
  63156. TEST_DECL(test_generate_cookie),
  63157. #ifndef NO_BIO
  63158. /* Can't memory test as server hangs. */
  63159. TEST_DECL(test_wolfSSL_BIO_connect),
  63160. /* Can't memory test as server Asserts in thread. */
  63161. TEST_DECL(test_wolfSSL_BIO_accept),
  63162. TEST_DECL(test_wolfSSL_BIO_tls),
  63163. #endif
  63164. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  63165. TEST_DECL(test_DhCallbacks),
  63166. #endif
  63167. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63168. TEST_DECL(test_export_keying_material),
  63169. #endif
  63170. /* Can't memory test as client/server Asserts in thread. */
  63171. TEST_DECL(test_ticket_and_psk_mixing),
  63172. /* Can't memory test as client/server Asserts in thread. */
  63173. TEST_DECL(test_prioritize_psk),
  63174. /* Can't memory test as client/server hangs. */
  63175. TEST_DECL(test_wc_CryptoCb),
  63176. /* Can't memory test as client/server hangs. */
  63177. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  63178. #if !defined(NO_FILESYSTEM) && \
  63179. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63180. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63181. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  63182. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  63183. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  63184. #ifdef HAVE_MAX_FRAGMENT
  63185. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  63186. #endif /* HAVE_MAX_FRAGMENT */
  63187. #ifndef NO_RSA
  63188. TEST_DECL(test_wolfSSL_dtls_stateless2),
  63189. #if !defined(NO_OLD_TLS)
  63190. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  63191. #endif /* !defined(NO_OLD_TLS) */
  63192. #endif /* ! NO_RSA */
  63193. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63194. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  63195. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  63196. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  63197. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  63198. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  63199. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  63200. && defined(WOLFSSL_TLS13) && \
  63201. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  63202. TEST_DECL(test_ticket_nonce_malloc),
  63203. #endif
  63204. TEST_DECL(test_ticket_ret_create),
  63205. TEST_DECL(test_extra_alerts_wrong_cs),
  63206. TEST_DECL(test_extra_alerts_skip_hs),
  63207. TEST_DECL(test_extra_alerts_bad_psk),
  63208. TEST_DECL(test_tls13_bad_psk_binder),
  63209. /* Can't memory test as client/server Asserts. */
  63210. TEST_DECL(test_harden_no_secure_renegotiation),
  63211. TEST_DECL(test_override_alt_cert_chain),
  63212. TEST_DECL(test_rpk_set_xxx_cert_type),
  63213. TEST_DECL(test_tls13_rpk_handshake),
  63214. TEST_DECL(test_dtls13_bad_epoch_ch),
  63215. TEST_DECL(test_short_session_id),
  63216. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  63217. /* Can't memory test as client/server hangs. */
  63218. TEST_DECL(test_dtls_msg_from_other_peer),
  63219. TEST_DECL(test_dtls_ipv6_check),
  63220. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  63221. TEST_DECL(test_dtls_no_extensions),
  63222. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  63223. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  63224. TEST_DECL(test_session_ticket_no_id),
  63225. TEST_DECL(test_session_ticket_hs_update),
  63226. TEST_DECL(test_dtls_downgrade_scr_server),
  63227. TEST_DECL(test_dtls_downgrade_scr),
  63228. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  63229. TEST_DECL(test_dtls_client_hello_timeout),
  63230. TEST_DECL(test_dtls_dropped_ccs),
  63231. TEST_DECL(test_dtls_seq_num_downgrade),
  63232. TEST_DECL(test_certreq_sighash_algos),
  63233. TEST_DECL(test_revoked_loaded_int_cert),
  63234. TEST_DECL(test_dtls_frag_ch),
  63235. TEST_DECL(test_dtls13_frag_ch_pq),
  63236. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  63237. TEST_DECL(test_tls13_pq_groups),
  63238. TEST_DECL(test_tls13_early_data),
  63239. TEST_DECL(test_tls_multi_handshakes_one_record),
  63240. TEST_DECL(test_write_dup),
  63241. TEST_DECL(test_read_write_hs),
  63242. TEST_DECL(test_get_signature_nid),
  63243. TEST_DECL(test_tls_cert_store_unchanged),
  63244. /* This test needs to stay at the end to clean up any caches allocated. */
  63245. TEST_DECL(test_wolfSSL_Cleanup)
  63246. };
  63247. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  63248. static void TestSetup(void)
  63249. {
  63250. /* Stub, for now. Add common test setup code here. */
  63251. }
  63252. static void TestCleanup(void)
  63253. {
  63254. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  63255. /* Clear any errors added to the error queue during the test run. */
  63256. wolfSSL_ERR_clear_error();
  63257. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  63258. }
  63259. /* Print out all API test cases with numeric identifier.
  63260. */
  63261. void ApiTest_PrintTestCases(void)
  63262. {
  63263. int i;
  63264. printf("All Test Cases:\n");
  63265. for (i = 0; i < TEST_CASE_CNT; i++) {
  63266. printf("%3d: %s\n", i + 1, testCases[i].name);
  63267. }
  63268. }
  63269. /* Add test case with index to the list to run.
  63270. *
  63271. * @param [in] idx Index of test case to run starting at 1.
  63272. * @return 0 on success.
  63273. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  63274. */
  63275. int ApiTest_RunIdx(int idx)
  63276. {
  63277. if (idx < 1 || idx > TEST_CASE_CNT) {
  63278. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  63279. return BAD_FUNC_ARG;
  63280. }
  63281. testAll = 0;
  63282. testCases[idx-1].run = 1;
  63283. return 0;
  63284. }
  63285. /* Add test case with name to the list to run.
  63286. *
  63287. * @param [in] name Name of test case to run.
  63288. * @return 0 on success.
  63289. * @return BAD_FUNC_ARG when name is not a known test case name.
  63290. */
  63291. int ApiTest_RunName(char* name)
  63292. {
  63293. int i;
  63294. for (i = 0; i < TEST_CASE_CNT; i++) {
  63295. if (XSTRCMP(testCases[i].name, name) == 0) {
  63296. testAll = 0;
  63297. testCases[i].run = 1;
  63298. return 0;
  63299. }
  63300. }
  63301. printf("Test case name not found: %s\n", name);
  63302. printf("Use --list to see all test case names.\n");
  63303. return BAD_FUNC_ARG;
  63304. }
  63305. /* Converts the result code to a string.
  63306. *
  63307. * @param [in] res Test result code.
  63308. * @return String describing test result.
  63309. */
  63310. static const char* apitest_res_string(int res)
  63311. {
  63312. const char* str = "invalid result";
  63313. switch (res) {
  63314. case TEST_SUCCESS:
  63315. str = "passed";
  63316. break;
  63317. case TEST_FAIL:
  63318. str = "failed";
  63319. break;
  63320. case TEST_SKIPPED:
  63321. str = "skipped";
  63322. break;
  63323. }
  63324. return str;
  63325. }
  63326. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63327. static double gettime_secs(void)
  63328. #if defined(_MSC_VER) && defined(_WIN32)
  63329. {
  63330. /* there's no gettimeofday for Windows, so we'll use system time */
  63331. #define EPOCH_DIFF 11644473600LL
  63332. FILETIME currentFileTime;
  63333. GetSystemTimePreciseAsFileTime(&currentFileTime);
  63334. ULARGE_INTEGER uli = { 0, 0 };
  63335. uli.LowPart = currentFileTime.dwLowDateTime;
  63336. uli.HighPart = currentFileTime.dwHighDateTime;
  63337. /* Convert to seconds since Unix epoch */
  63338. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  63339. }
  63340. #else
  63341. {
  63342. struct timeval tv;
  63343. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  63344. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  63345. }
  63346. #endif
  63347. #endif
  63348. int ApiTest(void)
  63349. {
  63350. int i;
  63351. int ret;
  63352. int res = 0;
  63353. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63354. double timeDiff;
  63355. #endif
  63356. printf(" Begin API Tests\n");
  63357. fflush(stdout);
  63358. /* we must perform init and cleanup if not all tests are running */
  63359. if (!testAll) {
  63360. #ifdef WOLFCRYPT_ONLY
  63361. if (wolfCrypt_Init() != 0) {
  63362. printf("wolfCrypt Initialization failed\n");
  63363. res = 1;
  63364. }
  63365. #else
  63366. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  63367. printf("wolfSSL Initialization failed\n");
  63368. res = 1;
  63369. }
  63370. #endif
  63371. }
  63372. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  63373. if (res == 0) {
  63374. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  63375. printf("failed to create tmp dir\n");
  63376. res = 1;
  63377. }
  63378. else {
  63379. tmpDirNameSet = 1;
  63380. }
  63381. }
  63382. #endif
  63383. if (res == 0) {
  63384. for (i = 0; i < TEST_CASE_CNT; ++i) {
  63385. EXPECT_DECLS;
  63386. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  63387. currentTestName = testCases[i].name;
  63388. #endif
  63389. /* When not testing all cases then skip if not marked for running.
  63390. */
  63391. if (!testAll && !testCases[i].run) {
  63392. continue;
  63393. }
  63394. TestSetup();
  63395. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  63396. fflush(stdout);
  63397. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63398. timeDiff = gettime_secs();
  63399. #endif
  63400. ret = testCases[i].func();
  63401. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63402. timeDiff = gettime_secs() - timeDiff;
  63403. #endif
  63404. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63405. if (ret != TEST_SKIPPED) {
  63406. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  63407. }
  63408. else
  63409. #endif
  63410. {
  63411. printf(" %s\n", apitest_res_string(ret));
  63412. }
  63413. fflush(stdout);
  63414. /* if return code is < 0 and not skipped then assert error */
  63415. Expect((ret > 0 || ret == TEST_SKIPPED),
  63416. ("Test failed\n"),
  63417. ("ret %d", ret));
  63418. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  63419. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  63420. TestCleanup();
  63421. }
  63422. }
  63423. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  63424. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  63425. wc_ecc_fp_free(); /* free per thread cache */
  63426. #endif
  63427. if (!testAll) {
  63428. #ifdef WOLFCRYPT_ONLY
  63429. wolfCrypt_Cleanup();
  63430. #else
  63431. wolfSSL_Cleanup();
  63432. #endif
  63433. }
  63434. (void)testDevId;
  63435. if (res != 0) {
  63436. printf("\nFAILURES:\n");
  63437. for (i = 0; i < TEST_CASE_CNT; ++i) {
  63438. if (testCases[i].fail) {
  63439. printf(" %3d: %s\n", i + 1, testCases[i].name);
  63440. }
  63441. }
  63442. printf("\n");
  63443. fflush(stdout);
  63444. }
  63445. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  63446. if (tmpDirNameSet) {
  63447. printf("\nBinary dumps of the memio streams can be found in the\n"
  63448. "%s directory. This can be imported into\n"
  63449. "Wireshark by transforming the file with\n"
  63450. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  63451. "And then loading test_output.dump.hex into Wireshark using\n"
  63452. "the \"Import from Hex Dump...\" option and selecting the\n"
  63453. "TCP encapsulation option.\n", tmpDirName);
  63454. }
  63455. #endif
  63456. printf(" End API Tests\n");
  63457. fflush(stdout);
  63458. return res;
  63459. }