api.c 2.4 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608666096661066611666126661366614666156661666617666186661966620666216662266623666246662566626666276662866629666306663166632666336663466635666366663766638666396664066641666426664366644666456664666647666486664966650666516665266653666546665566656666576665866659666606666166662666636666466665666666666766668666696667066671666726667366674666756667666677666786667966680666816668266683666846668566686666876668866689666906669166692666936669466695666966669766698666996670066701667026670366704667056670666707667086670966710667116671266713667146671566716667176671866719667206672166722667236672466725667266672766728667296673066731667326673366734667356673666737667386673966740667416674266743667446674566746667476674866749667506675166752667536675466755667566675766758667596676066761667626676366764667656676666767667686676966770667716677266773667746677566776667776677866779667806678166782667836678466785667866678766788667896679066791667926679366794667956679666797667986679966800668016680266803668046680566806668076680866809668106681166812668136681466815668166681766818668196682066821668226682366824668256682666827668286682966830668316683266833668346683566836668376683866839668406684166842668436684466845668466684766848668496685066851668526685366854668556685666857668586685966860668616686266863668646686566866668676686866869668706687166872668736687466875668766687766878668796688066881668826688366884668856688666887668886688966890668916689266893668946689566896668976689866899669006690166902669036690466905669066690766908669096691066911669126691366914669156691666917669186691966920669216692266923669246692566926669276692866929669306693166932669336693466935669366693766938669396694066941669426694366944669456694666947669486694966950669516695266953669546695566956669576695866959669606696166962669636696466965669666696766968669696697066971669726697366974669756697666977669786697966980669816698266983669846698566986669876698866989669906699166992669936699466995669966699766998669996700067001670026700367004670056700667007670086700967010670116701267013670146701567016670176701867019670206702167022670236702467025670266702767028670296703067031670326703367034670356703667037670386703967040670416704267043670446704567046670476704867049670506705167052670536705467055670566705767058670596706067061670626706367064670656706667067670686706967070670716707267073670746707567076670776707867079670806708167082670836708467085670866708767088670896709067091670926709367094670956709667097670986709967100671016710267103671046710567106671076710867109671106711167112671136711467115671166711767118671196712067121671226712367124671256712667127671286712967130671316713267133671346713567136671376713867139671406714167142671436714467145671466714767148671496715067151671526715367154671556715667157671586715967160671616716267163671646716567166671676716867169671706717167172671736717467175671766717767178671796718067181671826718367184671856718667187671886718967190671916719267193671946719567196671976719867199672006720167202672036720467205672066720767208672096721067211672126721367214672156721667217672186721967220672216722267223672246722567226672276722867229672306723167232672336723467235672366723767238672396724067241672426724367244672456724667247672486724967250672516725267253672546725567256672576725867259672606726167262672636726467265672666726767268672696727067271672726727367274672756727667277672786727967280672816728267283672846728567286672876728867289672906729167292672936729467295672966729767298672996730067301673026730367304673056730667307673086730967310673116731267313673146731567316673176731867319673206732167322673236732467325673266732767328673296733067331673326733367334673356733667337673386733967340673416734267343673446734567346673476734867349673506735167352673536735467355673566735767358673596736067361673626736367364673656736667367673686736967370673716737267373673746737567376673776737867379673806738167382673836738467385673866738767388673896739067391673926739367394673956739667397673986739967400674016740267403674046740567406674076740867409674106741167412674136741467415674166741767418674196742067421674226742367424674256742667427674286742967430674316743267433674346743567436674376743867439674406744167442674436744467445674466744767448674496745067451674526745367454674556745667457674586745967460674616746267463674646746567466674676746867469674706747167472674736747467475674766747767478674796748067481674826748367484674856748667487674886748967490674916749267493674946749567496674976749867499675006750167502675036750467505675066750767508675096751067511675126751367514675156751667517675186751967520675216752267523675246752567526675276752867529675306753167532675336753467535675366753767538675396754067541675426754367544675456754667547675486754967550675516755267553675546755567556675576755867559675606756167562675636756467565675666756767568675696757067571675726757367574675756757667577675786757967580675816758267583675846758567586675876758867589675906759167592675936759467595675966759767598675996760067601676026760367604676056760667607676086760967610676116761267613676146761567616676176761867619676206762167622676236762467625676266762767628676296763067631676326763367634676356763667637676386763967640676416764267643676446764567646676476764867649676506765167652676536765467655676566765767658676596766067661676626766367664676656766667667676686766967670676716767267673676746767567676676776767867679676806768167682676836768467685676866768767688676896769067691676926769367694676956769667697676986769967700677016770267703677046770567706677076770867709677106771167712677136771467715677166771767718677196772067721677226772367724677256772667727677286772967730677316773267733677346773567736677376773867739677406774167742677436774467745677466774767748677496775067751677526775367754677556775667757677586775967760677616776267763677646776567766677676776867769677706777167772677736777467775677766777767778677796778067781677826778367784677856778667787677886778967790677916779267793677946779567796677976779867799678006780167802678036780467805678066780767808678096781067811678126781367814678156781667817678186781967820678216782267823678246782567826678276782867829678306783167832678336783467835678366783767838678396784067841678426784367844678456784667847678486784967850678516785267853678546785567856678576785867859678606786167862678636786467865678666786767868678696787067871678726787367874678756787667877678786787967880678816788267883678846788567886678876788867889678906789167892678936789467895678966789767898678996790067901679026790367904679056790667907679086790967910679116791267913679146791567916679176791867919679206792167922679236792467925679266792767928679296793067931679326793367934679356793667937679386793967940679416794267943679446794567946679476794867949679506795167952679536795467955679566795767958679596796067961679626796367964679656796667967679686796967970679716797267973679746797567976679776797867979679806798167982679836798467985679866798767988679896799067991679926799367994679956799667997679986799968000680016800268003680046800568006680076800868009680106801168012680136801468015680166801768018680196802068021680226802368024680256802668027680286802968030680316803268033680346803568036680376803868039680406804168042680436804468045680466804768048680496805068051680526805368054680556805668057680586805968060680616806268063680646806568066680676806868069680706807168072680736807468075680766807768078680796808068081680826808368084680856808668087680886808968090680916809268093680946809568096680976809868099681006810168102681036810468105681066810768108681096811068111681126811368114681156811668117681186811968120681216812268123681246812568126681276812868129681306813168132681336813468135681366813768138681396814068141681426814368144681456814668147681486814968150681516815268153681546815568156681576815868159681606816168162681636816468165681666816768168681696817068171681726817368174681756817668177681786817968180681816818268183681846818568186681876818868189681906819168192681936819468195681966819768198681996820068201682026820368204682056820668207682086820968210682116821268213682146821568216682176821868219682206822168222682236822468225682266822768228682296823068231682326823368234682356823668237682386823968240682416824268243682446824568246682476824868249682506825168252682536825468255682566825768258682596826068261682626826368264682656826668267682686826968270682716827268273682746827568276682776827868279682806828168282682836828468285682866828768288682896829068291682926829368294682956829668297682986829968300683016830268303683046830568306683076830868309683106831168312683136831468315683166831768318683196832068321683226832368324683256832668327683286832968330683316833268333683346833568336683376833868339683406834168342683436834468345683466834768348683496835068351683526835368354683556835668357683586835968360683616836268363683646836568366683676836868369683706837168372683736837468375683766837768378683796838068381683826838368384683856838668387683886838968390683916839268393683946839568396683976839868399684006840168402684036840468405684066840768408684096841068411684126841368414684156841668417684186841968420684216842268423684246842568426684276842868429684306843168432684336843468435684366843768438684396844068441684426844368444684456844668447684486844968450684516845268453684546845568456684576845868459684606846168462684636846468465684666846768468684696847068471684726847368474684756847668477684786847968480684816848268483684846848568486684876848868489684906849168492684936849468495684966849768498684996850068501685026850368504685056850668507685086850968510685116851268513685146851568516685176851868519685206852168522685236852468525685266852768528685296853068531685326853368534685356853668537685386853968540685416854268543685446854568546685476854868549685506855168552685536855468555685566855768558685596856068561685626856368564685656856668567685686856968570685716857268573685746857568576685776857868579685806858168582685836858468585685866858768588685896859068591685926859368594685956859668597685986859968600686016860268603686046860568606686076860868609686106861168612686136861468615686166861768618686196862068621686226862368624686256862668627686286862968630686316863268633686346863568636686376863868639686406864168642686436864468645686466864768648686496865068651686526865368654686556865668657686586865968660686616866268663686646866568666686676866868669686706867168672686736867468675686766867768678686796868068681686826868368684686856868668687686886868968690686916869268693686946869568696686976869868699687006870168702687036870468705687066870768708687096871068711687126871368714687156871668717687186871968720687216872268723687246872568726687276872868729687306873168732687336873468735687366873768738687396874068741687426874368744687456874668747687486874968750687516875268753687546875568756687576875868759687606876168762687636876468765687666876768768687696877068771687726877368774687756877668777687786877968780687816878268783687846878568786687876878868789687906879168792687936879468795687966879768798687996880068801688026880368804688056880668807688086880968810688116881268813688146881568816688176881868819688206882168822688236882468825688266882768828688296883068831688326883368834688356883668837688386883968840688416884268843688446884568846688476884868849688506885168852688536885468855688566885768858688596886068861688626886368864688656886668867688686886968870688716887268873688746887568876688776887868879688806888168882688836888468885688866888768888688896889068891688926889368894688956889668897688986889968900689016890268903689046890568906689076890868909689106891168912689136891468915689166891768918689196892068921689226892368924689256892668927689286892968930689316893268933689346893568936689376893868939689406894168942689436894468945689466894768948689496895068951689526895368954689556895668957689586895968960689616896268963689646896568966689676896868969689706897168972689736897468975689766897768978689796898068981689826898368984689856898668987689886898968990689916899268993689946899568996689976899868999690006900169002690036900469005690066900769008690096901069011690126901369014690156901669017690186901969020690216902269023690246902569026690276902869029690306903169032690336903469035690366903769038690396904069041690426904369044690456904669047690486904969050690516905269053690546905569056690576905869059690606906169062690636906469065690666906769068690696907069071690726907369074690756907669077690786907969080690816908269083690846908569086690876908869089690906909169092690936909469095690966909769098690996910069101691026910369104691056910669107691086910969110691116911269113691146911569116691176911869119691206912169122691236912469125691266912769128691296913069131691326913369134691356913669137691386913969140691416914269143691446914569146691476914869149691506915169152691536915469155691566915769158691596916069161691626916369164691656916669167691686916969170691716917269173691746917569176691776917869179691806918169182691836918469185691866918769188691896919069191691926919369194691956919669197691986919969200692016920269203692046920569206692076920869209692106921169212692136921469215692166921769218692196922069221692226922369224692256922669227692286922969230692316923269233692346923569236692376923869239692406924169242692436924469245692466924769248692496925069251692526925369254692556925669257692586925969260692616926269263692646926569266692676926869269692706927169272692736927469275692766927769278692796928069281692826928369284692856928669287692886928969290692916929269293692946929569296692976929869299693006930169302693036930469305693066930769308693096931069311693126931369314693156931669317693186931969320693216932269323693246932569326693276932869329693306933169332693336933469335693366933769338693396934069341693426934369344693456934669347693486934969350693516935269353693546935569356693576935869359693606936169362693636936469365693666936769368693696937069371693726937369374693756937669377693786937969380693816938269383693846938569386693876938869389693906939169392693936939469395693966939769398693996940069401694026940369404694056940669407694086940969410694116941269413694146941569416694176941869419694206942169422694236942469425694266942769428694296943069431694326943369434694356943669437694386943969440694416944269443694446944569446694476944869449694506945169452694536945469455694566945769458694596946069461694626946369464694656946669467694686946969470694716947269473694746947569476694776947869479694806948169482694836948469485694866948769488694896949069491694926949369494694956949669497694986949969500695016950269503695046950569506695076950869509695106951169512695136951469515695166951769518695196952069521695226952369524695256952669527695286952969530695316953269533695346953569536695376953869539695406954169542695436954469545695466954769548695496955069551695526955369554695556955669557695586955969560695616956269563695646956569566695676956869569695706957169572695736957469575695766957769578695796958069581695826958369584695856958669587695886958969590695916959269593695946959569596695976959869599696006960169602696036960469605696066960769608696096961069611696126961369614696156961669617696186961969620696216962269623696246962569626696276962869629696306963169632696336963469635696366963769638696396964069641696426964369644696456964669647696486964969650696516965269653696546965569656696576965869659696606966169662696636966469665696666966769668696696967069671696726967369674696756967669677696786967969680696816968269683696846968569686696876968869689696906969169692696936969469695696966969769698696996970069701697026970369704697056970669707697086970969710697116971269713697146971569716697176971869719697206972169722697236972469725697266972769728697296973069731697326973369734697356973669737697386973969740697416974269743697446974569746697476974869749697506975169752697536975469755697566975769758697596976069761697626976369764697656976669767697686976969770697716977269773697746977569776697776977869779697806978169782697836978469785697866978769788697896979069791697926979369794697956979669797697986979969800698016980269803698046980569806698076980869809698106981169812698136981469815698166981769818698196982069821698226982369824698256982669827698286982969830698316983269833698346983569836698376983869839698406984169842698436984469845698466984769848698496985069851698526985369854698556985669857698586985969860698616986269863698646986569866698676986869869698706987169872698736987469875698766987769878698796988069881698826988369884698856988669887698886988969890698916989269893698946989569896698976989869899699006990169902699036990469905699066990769908699096991069911699126991369914699156991669917699186991969920699216992269923699246992569926699276992869929699306993169932699336993469935699366993769938699396994069941699426994369944699456994669947699486994969950699516995269953699546995569956699576995869959699606996169962699636996469965699666996769968699696997069971699726997369974699756997669977699786997969980699816998269983699846998569986699876998869989699906999169992699936999469995699966999769998699997000070001700027000370004700057000670007700087000970010700117001270013700147001570016700177001870019700207002170022700237002470025700267002770028700297003070031700327003370034700357003670037700387003970040700417004270043700447004570046700477004870049700507005170052700537005470055700567005770058700597006070061700627006370064700657006670067700687006970070700717007270073700747007570076700777007870079700807008170082700837008470085700867008770088700897009070091700927009370094700957009670097700987009970100701017010270103701047010570106701077010870109701107011170112701137011470115701167011770118701197012070121701227012370124701257012670127701287012970130701317013270133701347013570136701377013870139701407014170142701437014470145701467014770148701497015070151701527015370154701557015670157701587015970160701617016270163701647016570166701677016870169701707017170172701737017470175701767017770178701797018070181701827018370184701857018670187701887018970190701917019270193701947019570196701977019870199702007020170202702037020470205702067020770208702097021070211702127021370214702157021670217702187021970220702217022270223702247022570226702277022870229702307023170232702337023470235702367023770238702397024070241702427024370244702457024670247702487024970250702517025270253702547025570256702577025870259702607026170262702637026470265702667026770268702697027070271702727027370274702757027670277702787027970280702817028270283702847028570286702877028870289702907029170292702937029470295702967029770298702997030070301703027030370304703057030670307703087030970310703117031270313703147031570316703177031870319703207032170322703237032470325703267032770328703297033070331703327033370334703357033670337703387033970340703417034270343703447034570346703477034870349703507035170352703537035470355703567035770358703597036070361703627036370364703657036670367703687036970370703717037270373703747037570376703777037870379703807038170382703837038470385703867038770388703897039070391703927039370394703957039670397703987039970400704017040270403704047040570406704077040870409704107041170412704137041470415704167041770418704197042070421704227042370424704257042670427704287042970430704317043270433704347043570436704377043870439704407044170442704437044470445704467044770448704497045070451704527045370454704557045670457704587045970460704617046270463704647046570466704677046870469704707047170472704737047470475704767047770478704797048070481704827048370484704857048670487704887048970490704917049270493704947049570496704977049870499705007050170502705037050470505705067050770508705097051070511705127051370514705157051670517705187051970520705217052270523705247052570526705277052870529705307053170532705337053470535705367053770538705397054070541705427054370544705457054670547705487054970550705517055270553705547055570556705577055870559705607056170562705637056470565705667056770568705697057070571705727057370574705757057670577705787057970580705817058270583705847058570586705877058870589705907059170592705937059470595705967059770598705997060070601706027060370604706057060670607706087060970610706117061270613706147061570616706177061870619706207062170622706237062470625706267062770628706297063070631706327063370634706357063670637706387063970640706417064270643706447064570646706477064870649706507065170652706537065470655706567065770658706597066070661706627066370664706657066670667706687066970670706717067270673706747067570676706777067870679706807068170682706837068470685706867068770688706897069070691706927069370694706957069670697706987069970700707017070270703707047070570706707077070870709707107071170712707137071470715707167071770718707197072070721707227072370724707257072670727707287072970730707317073270733707347073570736707377073870739707407074170742707437074470745707467074770748707497075070751707527075370754707557075670757707587075970760707617076270763707647076570766707677076870769707707077170772707737077470775707767077770778707797078070781707827078370784707857078670787707887078970790707917079270793707947079570796707977079870799708007080170802708037080470805708067080770808708097081070811708127081370814708157081670817708187081970820708217082270823708247082570826708277082870829708307083170832708337083470835708367083770838708397084070841708427084370844708457084670847708487084970850708517085270853708547085570856708577085870859708607086170862708637086470865708667086770868708697087070871708727087370874708757087670877708787087970880708817088270883708847088570886708877088870889708907089170892708937089470895708967089770898708997090070901709027090370904709057090670907709087090970910709117091270913709147091570916709177091870919709207092170922709237092470925709267092770928709297093070931709327093370934709357093670937709387093970940709417094270943709447094570946709477094870949709507095170952709537095470955709567095770958709597096070961709627096370964709657096670967709687096970970709717097270973709747097570976709777097870979709807098170982709837098470985709867098770988709897099070991709927099370994709957099670997709987099971000710017100271003710047100571006710077100871009710107101171012710137101471015710167101771018710197102071021710227102371024710257102671027710287102971030710317103271033710347103571036710377103871039710407104171042710437104471045710467104771048710497105071051710527105371054710557105671057710587105971060710617106271063710647106571066710677106871069710707107171072710737107471075710767107771078710797108071081710827108371084710857108671087710887108971090710917109271093710947109571096710977109871099711007110171102711037110471105711067110771108711097111071111711127111371114711157111671117711187111971120711217112271123711247112571126711277112871129711307113171132711337113471135711367113771138711397114071141711427114371144711457114671147711487114971150711517115271153711547115571156711577115871159711607116171162711637116471165711667116771168711697117071171711727117371174711757117671177711787117971180711817118271183711847118571186711877118871189711907119171192711937119471195711967119771198711997120071201712027120371204712057120671207712087120971210712117121271213712147121571216712177121871219712207122171222712237122471225712267122771228712297123071231712327123371234712357123671237712387123971240712417124271243712447124571246712477124871249712507125171252712537125471255712567125771258712597126071261712627126371264712657126671267712687126971270712717127271273712747127571276712777127871279712807128171282712837128471285712867128771288712897129071291712927129371294712957129671297712987129971300713017130271303713047130571306713077130871309713107131171312713137131471315713167131771318713197132071321713227132371324713257132671327713287132971330713317133271333713347133571336713377133871339713407134171342713437134471345713467134771348713497135071351713527135371354713557135671357713587135971360713617136271363713647136571366713677136871369713707137171372713737137471375713767137771378713797138071381713827138371384713857138671387713887138971390713917139271393713947139571396713977139871399714007140171402714037140471405714067140771408714097141071411714127141371414714157141671417714187141971420714217142271423714247142571426714277142871429714307143171432714337143471435714367143771438714397144071441714427144371444714457144671447714487144971450714517145271453714547145571456714577145871459714607146171462714637146471465714667146771468714697147071471714727147371474714757147671477714787147971480714817148271483714847148571486714877148871489714907149171492714937149471495714967149771498714997150071501715027150371504715057150671507715087150971510715117151271513715147151571516715177151871519715207152171522715237152471525715267152771528715297153071531715327153371534715357153671537715387153971540715417154271543715447154571546715477154871549715507155171552715537155471555715567155771558715597156071561715627156371564715657156671567715687156971570715717157271573715747157571576715777157871579715807158171582715837158471585715867158771588715897159071591715927159371594715957159671597715987159971600716017160271603716047160571606716077160871609716107161171612716137161471615716167161771618716197162071621716227162371624716257162671627716287162971630716317163271633716347163571636716377163871639716407164171642716437164471645716467164771648716497165071651716527165371654716557165671657716587165971660716617166271663716647166571666716677166871669716707167171672716737167471675716767167771678716797168071681716827168371684716857168671687716887168971690716917169271693716947169571696
  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. /*
  23508. * Testing wc_PKCS7_EncodeSignedData()
  23509. */
  23510. static int test_wc_PKCS7_EncodeSignedData(void)
  23511. {
  23512. EXPECT_DECLS;
  23513. #if defined(HAVE_PKCS7)
  23514. PKCS7* pkcs7 = NULL;
  23515. WC_RNG rng;
  23516. byte output[FOURK_BUF];
  23517. byte badOut[1];
  23518. word32 outputSz = (word32)sizeof(output);
  23519. word32 badOutSz = 0;
  23520. byte data[] = "Test data to encode.";
  23521. #ifndef NO_RSA
  23522. #if defined(USE_CERT_BUFFERS_2048)
  23523. byte key[sizeof(client_key_der_2048)];
  23524. byte cert[sizeof(client_cert_der_2048)];
  23525. word32 keySz = (word32)sizeof(key);
  23526. word32 certSz = (word32)sizeof(cert);
  23527. XMEMSET(key, 0, keySz);
  23528. XMEMSET(cert, 0, certSz);
  23529. XMEMCPY(key, client_key_der_2048, keySz);
  23530. XMEMCPY(cert, client_cert_der_2048, certSz);
  23531. #elif defined(USE_CERT_BUFFERS_1024)
  23532. byte key[sizeof_client_key_der_1024];
  23533. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23534. word32 keySz = (word32)sizeof(key);
  23535. word32 certSz = (word32)sizeof(cert);
  23536. XMEMSET(key, 0, keySz);
  23537. XMEMSET(cert, 0, certSz);
  23538. XMEMCPY(key, client_key_der_1024, keySz);
  23539. XMEMCPY(cert, client_cert_der_1024, certSz);
  23540. #else
  23541. unsigned char cert[ONEK_BUF];
  23542. unsigned char key[ONEK_BUF];
  23543. XFILE fp = XBADFILE;
  23544. int certSz;
  23545. int keySz;
  23546. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23547. XBADFILE);
  23548. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23549. fp), 0);
  23550. if (fp != XBADFILE) {
  23551. XFCLOSE(fp);
  23552. fp = XBADFILE;
  23553. }
  23554. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23555. XBADFILE);
  23556. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23557. 0);
  23558. if (fp != XBADFILE)
  23559. XFCLOSE(fp);
  23560. #endif
  23561. #elif defined(HAVE_ECC)
  23562. #if defined(USE_CERT_BUFFERS_256)
  23563. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23564. unsigned char key[sizeof(ecc_clikey_der_256)];
  23565. int certSz = (int)sizeof(cert);
  23566. int keySz = (int)sizeof(key);
  23567. XMEMSET(cert, 0, certSz);
  23568. XMEMSET(key, 0, keySz);
  23569. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23570. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23571. #else
  23572. unsigned char cert[ONEK_BUF];
  23573. unsigned char key[ONEK_BUF];
  23574. XFILE fp = XBADFILE;
  23575. int certSz;
  23576. int keySz;
  23577. ExpectTrue((fp = XOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23578. XBADFILE);
  23579. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23580. if (fp != XBADFILE) {
  23581. XFCLOSE(fp);
  23582. fp = XBADFILE;
  23583. }
  23584. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23585. XBADFILE);
  23586. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23587. if (fp != XBADFILE)
  23588. XFCLOSE(fp);
  23589. #endif
  23590. #endif
  23591. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23592. XMEMSET(output, 0, outputSz);
  23593. ExpectIntEQ(wc_InitRng(&rng), 0);
  23594. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23595. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23596. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23597. if (pkcs7 != NULL) {
  23598. pkcs7->content = data;
  23599. pkcs7->contentSz = (word32)sizeof(data);
  23600. pkcs7->privateKey = key;
  23601. pkcs7->privateKeySz = (word32)sizeof(key);
  23602. pkcs7->encryptOID = RSAk;
  23603. #ifdef NO_SHA
  23604. pkcs7->hashOID = SHA256h;
  23605. #else
  23606. pkcs7->hashOID = SHAh;
  23607. #endif
  23608. pkcs7->rng = &rng;
  23609. }
  23610. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23611. wc_PKCS7_Free(pkcs7);
  23612. pkcs7 = NULL;
  23613. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23614. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23615. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23616. #ifdef ASN_BER_TO_DER
  23617. wc_PKCS7_Free(pkcs7);
  23618. /* reinitialize and test setting stream mode */
  23619. {
  23620. int signedSz;
  23621. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23622. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23623. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23624. if (pkcs7 != NULL) {
  23625. pkcs7->content = data;
  23626. pkcs7->contentSz = (word32)sizeof(data);
  23627. pkcs7->privateKey = key;
  23628. pkcs7->privateKeySz = (word32)sizeof(key);
  23629. pkcs7->encryptOID = RSAk;
  23630. #ifdef NO_SHA
  23631. pkcs7->hashOID = SHA256h;
  23632. #else
  23633. pkcs7->hashOID = SHAh;
  23634. #endif
  23635. pkcs7->rng = &rng;
  23636. }
  23637. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  23638. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1), 0);
  23639. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1), BAD_FUNC_ARG);
  23640. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  23641. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  23642. outputSz), 0);
  23643. wc_PKCS7_Free(pkcs7);
  23644. pkcs7 = NULL;
  23645. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23646. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23647. /* use exact signed buffer size since BER encoded */
  23648. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
  23649. }
  23650. #endif
  23651. /* Pass in bad args. */
  23652. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  23653. BAD_FUNC_ARG);
  23654. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23655. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23656. badOutSz), BAD_FUNC_ARG);
  23657. if (pkcs7 != NULL) {
  23658. pkcs7->hashOID = 0; /* bad hashOID */
  23659. }
  23660. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  23661. BAD_FUNC_ARG);
  23662. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23663. !defined(NO_RSA) && !defined(NO_SHA256)
  23664. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23665. * Example callback assumes SHA-256, so only run test if compiled in. */
  23666. wc_PKCS7_Free(pkcs7);
  23667. pkcs7 = NULL;
  23668. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23669. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23670. if (pkcs7 != NULL) {
  23671. pkcs7->content = data;
  23672. pkcs7->contentSz = (word32)sizeof(data);
  23673. pkcs7->privateKey = key;
  23674. pkcs7->privateKeySz = (word32)sizeof(key);
  23675. pkcs7->encryptOID = RSAk;
  23676. pkcs7->hashOID = SHA256h;
  23677. pkcs7->rng = &rng;
  23678. }
  23679. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23680. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23681. #endif
  23682. wc_PKCS7_Free(pkcs7);
  23683. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23684. #endif
  23685. return EXPECT_RESULT();
  23686. } /* END test_wc_PKCS7_EncodeSignedData */
  23687. /*
  23688. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23689. */
  23690. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23691. {
  23692. EXPECT_DECLS;
  23693. #if defined(HAVE_PKCS7)
  23694. int i;
  23695. PKCS7* pkcs7 = NULL;
  23696. WC_RNG rng;
  23697. byte outputHead[FOURK_BUF/2];
  23698. byte outputFoot[FOURK_BUF/2];
  23699. word32 outputHeadSz = (word32)sizeof(outputHead);
  23700. word32 outputFootSz = (word32)sizeof(outputFoot);
  23701. byte data[FOURK_BUF];
  23702. wc_HashAlg hash;
  23703. #ifdef NO_SHA
  23704. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23705. #else
  23706. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23707. #endif
  23708. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23709. word32 hashSz = wc_HashGetDigestSize(hashType);
  23710. #ifndef NO_RSA
  23711. #if defined(USE_CERT_BUFFERS_2048)
  23712. byte key[sizeof(client_key_der_2048)];
  23713. byte cert[sizeof(client_cert_der_2048)];
  23714. word32 keySz = (word32)sizeof(key);
  23715. word32 certSz = (word32)sizeof(cert);
  23716. XMEMSET(key, 0, keySz);
  23717. XMEMSET(cert, 0, certSz);
  23718. XMEMCPY(key, client_key_der_2048, keySz);
  23719. XMEMCPY(cert, client_cert_der_2048, certSz);
  23720. #elif defined(USE_CERT_BUFFERS_1024)
  23721. byte key[sizeof_client_key_der_1024];
  23722. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23723. word32 keySz = (word32)sizeof(key);
  23724. word32 certSz = (word32)sizeof(cert);
  23725. XMEMSET(key, 0, keySz);
  23726. XMEMSET(cert, 0, certSz);
  23727. XMEMCPY(key, client_key_der_1024, keySz);
  23728. XMEMCPY(cert, client_cert_der_1024, certSz);
  23729. #else
  23730. unsigned char cert[ONEK_BUF];
  23731. unsigned char key[ONEK_BUF];
  23732. XFILE fp = XBADFILE;
  23733. int certSz;
  23734. int keySz;
  23735. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23736. XBADFILE);
  23737. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23738. fp), 0);
  23739. if (fp != XBADFILE) {
  23740. XFCLOSE(fp);
  23741. fp = XBADFILE;
  23742. }
  23743. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23744. XBADFILE);
  23745. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23746. 0);
  23747. if (fp != XBADFILE)
  23748. XFCLOSE(fp);
  23749. #endif
  23750. #elif defined(HAVE_ECC)
  23751. #if defined(USE_CERT_BUFFERS_256)
  23752. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23753. unsigned char key[sizeof(ecc_clikey_der_256)];
  23754. int certSz = (int)sizeof(cert);
  23755. int keySz = (int)sizeof(key);
  23756. XMEMSET(cert, 0, certSz);
  23757. XMEMSET(key, 0, keySz);
  23758. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23759. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23760. #else
  23761. unsigned char cert[ONEK_BUF];
  23762. unsigned char key[ONEK_BUF];
  23763. XFILE fp = XBADFILE;
  23764. int certSz;
  23765. int keySz;
  23766. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23767. XBADFILE);
  23768. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23769. fp), 0);
  23770. if (fp != XBADFILE) {
  23771. XFCLOSE(fp);
  23772. fp = XBADFILE;
  23773. }
  23774. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23775. XBADFILE);
  23776. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23777. 0);
  23778. if (fp != XBADFILE)
  23779. XFCLOSE(fp);
  23780. #endif
  23781. #endif
  23782. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23783. /* initialize large data with sequence */
  23784. for (i=0; i<(int)sizeof(data); i++)
  23785. data[i] = i & 0xff;
  23786. XMEMSET(outputHead, 0, outputHeadSz);
  23787. XMEMSET(outputFoot, 0, outputFootSz);
  23788. ExpectIntEQ(wc_InitRng(&rng), 0);
  23789. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23790. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23791. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23792. if (pkcs7 != NULL) {
  23793. pkcs7->content = NULL; /* not used for ex */
  23794. pkcs7->contentSz = (word32)sizeof(data);
  23795. pkcs7->privateKey = key;
  23796. pkcs7->privateKeySz = (word32)sizeof(key);
  23797. pkcs7->encryptOID = RSAk;
  23798. #ifdef NO_SHA
  23799. pkcs7->hashOID = SHA256h;
  23800. #else
  23801. pkcs7->hashOID = SHAh;
  23802. #endif
  23803. pkcs7->rng = &rng;
  23804. }
  23805. /* calculate hash for content */
  23806. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23807. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23808. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23809. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23810. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23811. /* Perform PKCS7 sign using hash directly */
  23812. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23813. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23814. ExpectIntGT(outputHeadSz, 0);
  23815. ExpectIntGT(outputFootSz, 0);
  23816. wc_PKCS7_Free(pkcs7);
  23817. pkcs7 = NULL;
  23818. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23819. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23820. /* required parameter even on verify when using _ex, if using outputHead
  23821. * and outputFoot */
  23822. if (pkcs7 != NULL) {
  23823. pkcs7->contentSz = (word32)sizeof(data);
  23824. }
  23825. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23826. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23827. wc_PKCS7_Free(pkcs7);
  23828. pkcs7 = NULL;
  23829. /* assembly complete PKCS7 sign and use normal verify */
  23830. {
  23831. byte* output = NULL;
  23832. word32 outputSz = 0;
  23833. ExpectNotNull(output = (byte*)XMALLOC(
  23834. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  23835. DYNAMIC_TYPE_TMP_BUFFER));
  23836. if (output != NULL) {
  23837. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23838. outputSz += outputHeadSz;
  23839. XMEMCPY(&output[outputSz], data, sizeof(data));
  23840. outputSz += sizeof(data);
  23841. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23842. outputSz += outputFootSz;
  23843. }
  23844. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23845. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23846. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23847. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23848. }
  23849. /* Pass in bad args. */
  23850. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23851. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23852. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23853. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23854. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23855. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23856. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23857. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23858. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23859. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23860. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23861. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23862. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23863. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  23864. if (pkcs7 != NULL) {
  23865. pkcs7->hashOID = 0; /* bad hashOID */
  23866. }
  23867. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23868. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23869. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23870. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23871. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23872. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23873. #ifndef NO_PKCS7_STREAM
  23874. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23875. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23876. #else
  23877. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23878. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  23879. #endif
  23880. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23881. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23882. #ifndef NO_PKCS7_STREAM
  23883. /* can pass in 0 buffer length with streaming API */
  23884. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23885. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23886. #else
  23887. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23888. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23889. #endif
  23890. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23891. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  23892. #ifndef NO_PKCS7_STREAM
  23893. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23894. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  23895. #else
  23896. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23897. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  23898. #endif
  23899. wc_PKCS7_Free(pkcs7);
  23900. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23901. #endif
  23902. return EXPECT_RESULT();
  23903. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  23904. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  23905. /**
  23906. * Loads certs/keys from files or buffers into the argument buffers,
  23907. * helper function called by CreatePKCS7SignedData().
  23908. *
  23909. * Returns 0 on success, negative on error.
  23910. */
  23911. static int LoadPKCS7SignedDataCerts(
  23912. int useIntermediateCertChain, int pkAlgoType,
  23913. byte* intCARoot, word32* intCARootSz,
  23914. byte* intCA1, word32* intCA1Sz,
  23915. byte* intCA2, word32* intCA2Sz,
  23916. byte* cert, word32* certSz,
  23917. byte* key, word32* keySz)
  23918. {
  23919. EXPECT_DECLS;
  23920. int ret = 0;
  23921. XFILE fp = XBADFILE;
  23922. #ifndef NO_RSA
  23923. const char* intCARootRSA = "./certs/ca-cert.der";
  23924. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  23925. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  23926. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  23927. const char* intServKeyRSA = "./certs/server-key.der";
  23928. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  23929. const char* cli1024Cert = "./certs/1024/client-cert.der";
  23930. const char* cli1024Key = "./certs/1024/client-key.der";
  23931. #endif
  23932. #endif
  23933. #ifdef HAVE_ECC
  23934. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  23935. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  23936. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  23937. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  23938. const char* intServKeyECC = "./certs/ecc-key.der";
  23939. #ifndef USE_CERT_BUFFERS_256
  23940. const char* cliEccCert = "./certs/client-ecc-cert.der";
  23941. const char* cliEccKey = "./certs/client-ecc-key.der";
  23942. #endif
  23943. #endif
  23944. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  23945. ((useIntermediateCertChain == 1) &&
  23946. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  23947. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  23948. return BAD_FUNC_ARG;
  23949. }
  23950. /* Read/load certs and keys to use for signing based on PK type and chain */
  23951. switch (pkAlgoType) {
  23952. #ifndef NO_RSA
  23953. case RSA_TYPE:
  23954. if (useIntermediateCertChain == 1) {
  23955. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  23956. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  23957. if (fp != XBADFILE) {
  23958. XFCLOSE(fp);
  23959. fp = XBADFILE;
  23960. }
  23961. ExpectIntGT(*intCARootSz, 0);
  23962. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  23963. if (fp != XBADFILE) {
  23964. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23965. XFCLOSE(fp);
  23966. fp = XBADFILE;
  23967. }
  23968. ExpectIntGT(*intCA1Sz, 0);
  23969. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  23970. if (fp != XBADFILE) {
  23971. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23972. XFCLOSE(fp);
  23973. fp = XBADFILE;
  23974. }
  23975. ExpectIntGT(*intCA2Sz, 0);
  23976. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  23977. if (fp != XBADFILE) {
  23978. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23979. XFCLOSE(fp);
  23980. fp = XBADFILE;
  23981. }
  23982. ExpectIntGT(*certSz, 0);
  23983. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  23984. if (fp != XBADFILE) {
  23985. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23986. XFCLOSE(fp);
  23987. fp = XBADFILE;
  23988. }
  23989. ExpectIntGT(*keySz, 0);
  23990. }
  23991. else {
  23992. #if defined(USE_CERT_BUFFERS_2048)
  23993. *keySz = sizeof_client_key_der_2048;
  23994. *certSz = sizeof_client_cert_der_2048;
  23995. XMEMCPY(key, client_key_der_2048, *keySz);
  23996. XMEMCPY(cert, client_cert_der_2048, *certSz);
  23997. #elif defined(USE_CERT_BUFFERS_1024)
  23998. *keySz = sizeof_client_key_der_1024;
  23999. *certSz = sizeof_client_cert_der_1024;
  24000. XMEMCPY(key, client_key_der_1024, *keySz);
  24001. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24002. #else
  24003. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24004. if (fp != XBADFILE) {
  24005. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24006. XFCLOSE(fp);
  24007. fp = XBADFILE;
  24008. }
  24009. ExpectIntGT(*keySz, 0);
  24010. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24011. if (fp != XBADFILE) {
  24012. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24013. XFCLOSE(fp);
  24014. fp = XBADFILE;
  24015. }
  24016. ExpectIntGT(*certSz, 0);
  24017. #endif /* USE_CERT_BUFFERS_2048 */
  24018. }
  24019. break;
  24020. #endif /* !NO_RSA */
  24021. #ifdef HAVE_ECC
  24022. case ECC_TYPE:
  24023. if (useIntermediateCertChain == 1) {
  24024. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24025. if (fp != XBADFILE) {
  24026. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24027. fp);
  24028. XFCLOSE(fp);
  24029. fp = XBADFILE;
  24030. }
  24031. ExpectIntGT(*intCARootSz, 0);
  24032. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24033. if (fp != XBADFILE) {
  24034. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24035. XFCLOSE(fp);
  24036. fp = XBADFILE;
  24037. }
  24038. ExpectIntGT(*intCA1Sz, 0);
  24039. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24040. if (fp != XBADFILE) {
  24041. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24042. XFCLOSE(fp);
  24043. fp = XBADFILE;
  24044. }
  24045. ExpectIntGT(*intCA2Sz, 0);
  24046. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24047. if (fp != XBADFILE) {
  24048. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24049. XFCLOSE(fp);
  24050. fp = XBADFILE;
  24051. }
  24052. ExpectIntGT(*certSz, 0);
  24053. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24054. if (fp != XBADFILE) {
  24055. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24056. XFCLOSE(fp);
  24057. fp = XBADFILE;
  24058. }
  24059. ExpectIntGT(*keySz, 0);
  24060. }
  24061. else {
  24062. #if defined(USE_CERT_BUFFERS_256)
  24063. *keySz = sizeof_ecc_clikey_der_256;
  24064. *certSz = sizeof_cliecc_cert_der_256;
  24065. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24066. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24067. #else
  24068. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24069. if (fp != XBADFILE) {
  24070. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24071. XFCLOSE(fp);
  24072. fp = XBADFILE;
  24073. }
  24074. ExpectIntGT(*keySz, 0);
  24075. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24076. if (fp != XBADFILE) {
  24077. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24078. XFCLOSE(fp);
  24079. fp = XBADFILE;
  24080. }
  24081. ExpectIntGT(*certSz, 0);
  24082. #endif /* USE_CERT_BUFFERS_256 */
  24083. }
  24084. break;
  24085. #endif /* HAVE_ECC */
  24086. default:
  24087. WOLFSSL_MSG("Unsupported SignedData PK type");
  24088. ret = BAD_FUNC_ARG;
  24089. break;
  24090. }
  24091. if (EXPECT_FAIL() && (ret == 0)) {
  24092. ret = BAD_FUNC_ARG;
  24093. }
  24094. return ret;
  24095. }
  24096. /**
  24097. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24098. *
  24099. * output output buffer to place SignedData
  24100. * outputSz size of output buffer
  24101. * data data buffer to be signed
  24102. * dataSz size of data buffer
  24103. * withAttribs [1/0] include attributes in SignedData message
  24104. * detachedSig [1/0] create detached signature, no content
  24105. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24106. * root CAs in bundle
  24107. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24108. *
  24109. * Return size of bundle created on success, negative on error */
  24110. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24111. byte* data, word32 dataSz,
  24112. int withAttribs, int detachedSig,
  24113. int useIntermediateCertChain,
  24114. int pkAlgoType)
  24115. {
  24116. EXPECT_DECLS;
  24117. int ret = 0;
  24118. WC_RNG rng;
  24119. PKCS7* pkcs7 = NULL;
  24120. static byte messageTypeOid[] =
  24121. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24122. 0x09, 0x02 };
  24123. static byte messageType[] = { 0x13, 2, '1', '9' };
  24124. PKCS7Attrib attribs[] =
  24125. {
  24126. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24127. sizeof(messageType) }
  24128. };
  24129. byte intCARoot[TWOK_BUF];
  24130. byte intCA1[TWOK_BUF];
  24131. byte intCA2[TWOK_BUF];
  24132. byte cert[TWOK_BUF];
  24133. byte key[TWOK_BUF];
  24134. word32 intCARootSz = sizeof(intCARoot);
  24135. word32 intCA1Sz = sizeof(intCA1);
  24136. word32 intCA2Sz = sizeof(intCA2);
  24137. word32 certSz = sizeof(cert);
  24138. word32 keySz = sizeof(key);
  24139. XMEMSET(intCARoot, 0, intCARootSz);
  24140. XMEMSET(intCA1, 0, intCA1Sz);
  24141. XMEMSET(intCA2, 0, intCA2Sz);
  24142. XMEMSET(cert, 0, certSz);
  24143. XMEMSET(key, 0, keySz);
  24144. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24145. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24146. cert, &certSz, key, &keySz);
  24147. ExpectIntEQ(ret, 0);
  24148. XMEMSET(output, 0, outputSz);
  24149. ExpectIntEQ(wc_InitRng(&rng), 0);
  24150. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24151. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24152. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24153. if (useIntermediateCertChain == 1) {
  24154. /* Add intermediate and root CA certs into SignedData Certs SET */
  24155. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24156. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24157. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24158. }
  24159. if (pkcs7 != NULL) {
  24160. pkcs7->content = data;
  24161. pkcs7->contentSz = dataSz;
  24162. pkcs7->privateKey = key;
  24163. pkcs7->privateKeySz = (word32)sizeof(key);
  24164. if (pkAlgoType == RSA_TYPE) {
  24165. pkcs7->encryptOID = RSAk;
  24166. }
  24167. else {
  24168. pkcs7->encryptOID = ECDSAk;
  24169. }
  24170. #ifdef NO_SHA
  24171. pkcs7->hashOID = SHA256h;
  24172. #else
  24173. pkcs7->hashOID = SHAh;
  24174. #endif
  24175. pkcs7->rng = &rng;
  24176. if (withAttribs) {
  24177. /* include a signed attribute */
  24178. pkcs7->signedAttribs = attribs;
  24179. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24180. }
  24181. }
  24182. if (detachedSig) {
  24183. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24184. }
  24185. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24186. ExpectIntGT(outputSz, 0);
  24187. wc_PKCS7_Free(pkcs7);
  24188. pkcs7 = NULL;
  24189. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24190. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24191. if (detachedSig && (pkcs7 != NULL)) {
  24192. pkcs7->content = data;
  24193. pkcs7->contentSz = dataSz;
  24194. }
  24195. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24196. wc_PKCS7_Free(pkcs7);
  24197. wc_FreeRng(&rng);
  24198. if (EXPECT_FAIL()) {
  24199. outputSz = 0;
  24200. }
  24201. return outputSz;
  24202. }
  24203. #endif
  24204. /*
  24205. * Testing wc_PKCS_VerifySignedData()
  24206. */
  24207. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24208. {
  24209. EXPECT_DECLS;
  24210. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24211. PKCS7* pkcs7 = NULL;
  24212. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24213. word32 outputSz = sizeof(output);
  24214. byte data[] = "Test data to encode.";
  24215. byte badOut[1];
  24216. word32 badOutSz = 0;
  24217. byte badContent[] = "This is different content than was signed";
  24218. wc_HashAlg hash;
  24219. #ifdef NO_SHA
  24220. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24221. #else
  24222. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24223. #endif
  24224. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24225. word32 hashSz = wc_HashGetDigestSize(hashType);
  24226. #ifndef NO_RSA
  24227. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24228. /* contentType OID (1.2.840.113549.1.9.3) */
  24229. static const byte contentTypeOid[] =
  24230. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24231. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24232. static const byte dataType[] =
  24233. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24234. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24235. static const byte messageDigestOid[] =
  24236. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24237. #ifndef NO_ASN_TIME
  24238. /* signingTime OID () */
  24239. static const byte signingTimeOid[] =
  24240. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24241. #endif
  24242. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24243. int dateLength = 0;
  24244. byte dateFormat;
  24245. const byte* datePart = NULL;
  24246. struct tm timearg;
  24247. time_t now;
  24248. struct tm* nowTm = NULL;
  24249. #ifdef NEED_TMP_TIME
  24250. struct tm tmpTimeStorage;
  24251. struct tm* tmpTime = &tmpTimeStorage;
  24252. #endif
  24253. #endif /* !NO_ASN && !NO_ASN_TIME */
  24254. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24255. /* Success test with RSA certs/key */
  24256. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24257. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  24258. /* calculate hash for content, used later */
  24259. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24260. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24261. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24262. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24263. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24264. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24265. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24266. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24267. /* Check that decoded signed attributes are correct */
  24268. /* messageDigest should be first */
  24269. if (pkcs7 != NULL) {
  24270. decodedAttrib = pkcs7->decodedAttrib;
  24271. }
  24272. ExpectNotNull(decodedAttrib);
  24273. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  24274. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  24275. decodedAttrib->oidSz), 0);
  24276. /* + 2 for OCTET STRING and length bytes */
  24277. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  24278. ExpectNotNull(decodedAttrib->value);
  24279. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  24280. #ifndef NO_ASN_TIME
  24281. /* signingTime should be second */
  24282. if (decodedAttrib != NULL) {
  24283. decodedAttrib = decodedAttrib->next;
  24284. }
  24285. ExpectNotNull(decodedAttrib);
  24286. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  24287. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  24288. decodedAttrib->oidSz), 0);
  24289. ExpectIntGT(decodedAttrib->valueSz, 0);
  24290. ExpectNotNull(decodedAttrib->value);
  24291. #endif
  24292. /* Verify signingTime if ASN and time are available */
  24293. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24294. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24295. &datePart, &dateFormat, &dateLength), 0);
  24296. ExpectNotNull(datePart);
  24297. ExpectIntGT(dateLength, 0);
  24298. XMEMSET(&timearg, 0, sizeof(timearg));
  24299. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24300. &timearg), 0);
  24301. /* Get current time and compare year/month/day against attribute value */
  24302. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24303. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24304. ExpectNotNull(nowTm);
  24305. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24306. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24307. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24308. #endif /* !NO_ASN && !NO_ASN_TIME */
  24309. /* contentType should be third */
  24310. if (decodedAttrib != NULL) {
  24311. decodedAttrib = decodedAttrib->next;
  24312. }
  24313. ExpectNotNull(decodedAttrib);
  24314. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24315. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24316. decodedAttrib->oidSz), 0);
  24317. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24318. ExpectNotNull(decodedAttrib->value);
  24319. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24320. 0);
  24321. #endif /* !NO_RSA */
  24322. /* Test bad args. */
  24323. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24324. BAD_FUNC_ARG);
  24325. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24326. BAD_FUNC_ARG);
  24327. #ifndef NO_PKCS7_STREAM
  24328. /* can pass in 0 buffer length with streaming API */
  24329. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24330. badOutSz), WC_PKCS7_WANT_READ_E);
  24331. #else
  24332. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24333. badOutSz), BAD_FUNC_ARG);
  24334. #endif
  24335. wc_PKCS7_Free(pkcs7);
  24336. pkcs7 = NULL;
  24337. #ifndef NO_RSA
  24338. /* Try RSA certs/key/sig first */
  24339. outputSz = sizeof(output);
  24340. XMEMSET(output, 0, outputSz);
  24341. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24342. (word32)sizeof(data),
  24343. 1, 1, 0, RSA_TYPE)), 0);
  24344. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24345. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24346. if (pkcs7 != NULL) {
  24347. pkcs7->content = badContent;
  24348. pkcs7->contentSz = sizeof(badContent);
  24349. }
  24350. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24351. SIG_VERIFY_E);
  24352. wc_PKCS7_Free(pkcs7);
  24353. pkcs7 = NULL;
  24354. /* Test success case with detached signature and valid content */
  24355. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24356. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24357. if (pkcs7 != NULL) {
  24358. pkcs7->content = data;
  24359. pkcs7->contentSz = sizeof(data);
  24360. }
  24361. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24362. wc_PKCS7_Free(pkcs7);
  24363. pkcs7 = NULL;
  24364. /* verify using pre-computed content digest only (no content) */
  24365. {
  24366. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24367. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24368. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24369. output, outputSz, NULL, 0), 0);
  24370. wc_PKCS7_Free(pkcs7);
  24371. pkcs7 = NULL;
  24372. }
  24373. #endif /* !NO_RSA */
  24374. /* Test verify on signedData containing intermediate/root CA certs */
  24375. #ifndef NO_RSA
  24376. outputSz = sizeof(output);
  24377. XMEMSET(output, 0, outputSz);
  24378. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24379. (word32)sizeof(data),
  24380. 0, 0, 1, RSA_TYPE)), 0);
  24381. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24382. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24383. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24384. wc_PKCS7_Free(pkcs7);
  24385. pkcs7 = NULL;
  24386. #endif /* !NO_RSA */
  24387. #endif
  24388. return EXPECT_RESULT();
  24389. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  24390. /*
  24391. * Testing wc_PKCS_VerifySignedData()
  24392. */
  24393. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  24394. {
  24395. EXPECT_DECLS;
  24396. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  24397. PKCS7* pkcs7 = NULL;
  24398. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24399. word32 outputSz = sizeof(output);
  24400. byte data[] = "Test data to encode.";
  24401. byte badContent[] = "This is different content than was signed";
  24402. wc_HashAlg hash;
  24403. #ifdef NO_SHA
  24404. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24405. #else
  24406. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24407. #endif
  24408. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24409. word32 hashSz = wc_HashGetDigestSize(hashType);
  24410. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24411. /* Success test with ECC certs/key */
  24412. outputSz = sizeof(output);
  24413. XMEMSET(output, 0, outputSz);
  24414. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24415. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  24416. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24417. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24418. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24419. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24420. wc_PKCS7_Free(pkcs7);
  24421. pkcs7 = NULL;
  24422. /* Invalid content should error, use detached signature so we can
  24423. * easily change content */
  24424. outputSz = sizeof(output);
  24425. XMEMSET(output, 0, outputSz);
  24426. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24427. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  24428. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24429. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24430. if (pkcs7 != NULL) {
  24431. pkcs7->content = badContent;
  24432. pkcs7->contentSz = sizeof(badContent);
  24433. }
  24434. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24435. SIG_VERIFY_E);
  24436. wc_PKCS7_Free(pkcs7);
  24437. pkcs7 = NULL;
  24438. /* Test success case with detached signature and valid content */
  24439. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24440. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24441. if (pkcs7 != NULL) {
  24442. pkcs7->content = data;
  24443. pkcs7->contentSz = sizeof(data);
  24444. }
  24445. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24446. wc_PKCS7_Free(pkcs7);
  24447. pkcs7 = NULL;
  24448. /* verify using pre-computed content digest only (no content) */
  24449. {
  24450. /* calculate hash for content */
  24451. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24452. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24453. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24454. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24455. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24456. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24457. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24458. output, outputSz, NULL, 0), 0);
  24459. wc_PKCS7_Free(pkcs7);
  24460. pkcs7 = NULL;
  24461. }
  24462. /* Test verify on signedData containing intermediate/root CA certs */
  24463. outputSz = sizeof(output);
  24464. XMEMSET(output, 0, outputSz);
  24465. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24466. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  24467. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24468. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24469. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24470. wc_PKCS7_Free(pkcs7);
  24471. pkcs7 = NULL;
  24472. #endif
  24473. return EXPECT_RESULT();
  24474. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  24475. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  24476. !defined(NO_AES_256)
  24477. static const byte defKey[] = {
  24478. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24479. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24480. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24481. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24482. };
  24483. static byte aesHandle[32]; /* simulated hardware key handle */
  24484. /* return 0 on success */
  24485. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  24486. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  24487. byte* in, int inSz, byte* out, void* usrCtx)
  24488. {
  24489. int ret;
  24490. Aes aes;
  24491. if (usrCtx == NULL) {
  24492. /* no simulated handle passed in */
  24493. return -1;
  24494. }
  24495. switch (encryptOID) {
  24496. case AES256CBCb:
  24497. if (ivSz != AES_BLOCK_SIZE)
  24498. return BAD_FUNC_ARG;
  24499. break;
  24500. default:
  24501. WOLFSSL_MSG("Unsupported content cipher type for test");
  24502. return ALGO_ID_E;
  24503. };
  24504. /* simulate using handle to get key */
  24505. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  24506. if (ret == 0) {
  24507. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  24508. if (ret == 0)
  24509. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  24510. wc_AesFree(&aes);
  24511. }
  24512. (void)aad;
  24513. (void)aadSz;
  24514. (void)authTag;
  24515. (void)authTagSz;
  24516. (void)pkcs7;
  24517. return ret;
  24518. }
  24519. /* returns key size on success */
  24520. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24521. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24522. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24523. {
  24524. int ret = -1;
  24525. (void)cekSz;
  24526. (void)cek;
  24527. (void)outSz;
  24528. (void)keyIdSz;
  24529. (void)direction;
  24530. (void)orginKey; /* used with KAKRI */
  24531. (void)orginKeySz;
  24532. if (out == NULL)
  24533. return BAD_FUNC_ARG;
  24534. if (keyId[0] != 0x00) {
  24535. return -1;
  24536. }
  24537. if (type != (int)PKCS7_KEKRI) {
  24538. return -1;
  24539. }
  24540. switch (keyWrapAlgo) {
  24541. case AES256_WRAP:
  24542. /* simulate setting a handle for later decryption but use key
  24543. * as handle in the test case here */
  24544. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24545. aesHandle, sizeof(aesHandle), NULL);
  24546. if (ret < 0)
  24547. return ret;
  24548. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24549. if (ret < 0)
  24550. return ret;
  24551. /* return key size on success */
  24552. return sizeof(defKey);
  24553. default:
  24554. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24555. return BAD_KEYWRAP_ALG_E;
  24556. };
  24557. }
  24558. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24559. /*
  24560. * Testing wc_PKCS7_EncodeEnvelopedData()
  24561. */
  24562. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  24563. {
  24564. EXPECT_DECLS;
  24565. #if defined(HAVE_PKCS7)
  24566. PKCS7* pkcs7 = NULL;
  24567. #ifdef ASN_BER_TO_DER
  24568. int encodedSz = 0;
  24569. #endif
  24570. #ifdef ECC_TIMING_RESISTANT
  24571. WC_RNG rng;
  24572. #endif
  24573. word32 tempWrd32 = 0;
  24574. byte* tmpBytePtr = NULL;
  24575. const char input[] = "Test data to encode.";
  24576. int i;
  24577. int testSz = 0;
  24578. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24579. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24580. byte* rsaCert = NULL;
  24581. byte* rsaPrivKey = NULL;
  24582. word32 rsaCertSz;
  24583. word32 rsaPrivKeySz;
  24584. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24585. !defined(USE_CERT_BUFFERS_2048) )
  24586. static const char* rsaClientCert = "./certs/client-cert.der";
  24587. static const char* rsaClientKey = "./certs/client-key.der";
  24588. rsaCertSz = (word32)sizeof(rsaClientCert);
  24589. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24590. #endif
  24591. #endif
  24592. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24593. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24594. byte* eccCert = NULL;
  24595. byte* eccPrivKey = NULL;
  24596. word32 eccCertSz;
  24597. word32 eccPrivKeySz;
  24598. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24599. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24600. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24601. #endif
  24602. #endif
  24603. /* Generic buffer size. */
  24604. byte output[ONEK_BUF];
  24605. byte decoded[sizeof(input)/sizeof(char)];
  24606. int decodedSz = 0;
  24607. #ifndef NO_FILESYSTEM
  24608. XFILE certFile = XBADFILE;
  24609. XFILE keyFile = XBADFILE;
  24610. #endif
  24611. #ifdef ECC_TIMING_RESISTANT
  24612. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24613. #endif
  24614. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24615. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24616. /* RSA certs and keys. */
  24617. #if defined(USE_CERT_BUFFERS_1024)
  24618. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24619. /* Allocate buffer space. */
  24620. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24621. DYNAMIC_TYPE_TMP_BUFFER));
  24622. /* Init buffer. */
  24623. if (rsaCert != NULL) {
  24624. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24625. }
  24626. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24627. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24628. DYNAMIC_TYPE_TMP_BUFFER));
  24629. if (rsaPrivKey != NULL) {
  24630. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24631. }
  24632. #elif defined(USE_CERT_BUFFERS_2048)
  24633. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24634. /* Allocate buffer */
  24635. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24636. DYNAMIC_TYPE_TMP_BUFFER));
  24637. /* Init buffer. */
  24638. if (rsaCert != NULL) {
  24639. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24640. }
  24641. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24642. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24643. DYNAMIC_TYPE_TMP_BUFFER));
  24644. if (rsaPrivKey != NULL) {
  24645. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24646. }
  24647. #else
  24648. /* File system. */
  24649. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  24650. rsaCertSz = (word32)FOURK_BUF;
  24651. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24652. DYNAMIC_TYPE_TMP_BUFFER));
  24653. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  24654. certFile)) > 0);
  24655. if (certFile != XBADFILE)
  24656. XFCLOSE(certFile);
  24657. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  24658. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24659. DYNAMIC_TYPE_TMP_BUFFER));
  24660. rsaPrivKeySz = (word32)FOURK_BUF;
  24661. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  24662. keyFile)) > 0);
  24663. if (keyFile != XBADFILE)
  24664. XFCLOSE(keyFile);
  24665. #endif /* USE_CERT_BUFFERS */
  24666. #endif /* NO_RSA */
  24667. /* ECC */
  24668. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24669. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24670. #ifdef USE_CERT_BUFFERS_256
  24671. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24672. DYNAMIC_TYPE_TMP_BUFFER));
  24673. /* Init buffer. */
  24674. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  24675. if (eccCert != NULL) {
  24676. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  24677. }
  24678. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24679. DYNAMIC_TYPE_TMP_BUFFER));
  24680. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  24681. if (eccPrivKey != NULL) {
  24682. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  24683. }
  24684. #else /* File system. */
  24685. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  24686. eccCertSz = (word32)FOURK_BUF;
  24687. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24688. DYNAMIC_TYPE_TMP_BUFFER));
  24689. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  24690. certFile)) > 0);
  24691. if (certFile != XBADFILE) {
  24692. XFCLOSE(certFile);
  24693. }
  24694. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  24695. eccPrivKeySz = (word32)FOURK_BUF;
  24696. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24697. DYNAMIC_TYPE_TMP_BUFFER));
  24698. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  24699. keyFile)) > 0);
  24700. if (keyFile != XBADFILE) {
  24701. XFCLOSE(keyFile);
  24702. }
  24703. #endif /* USE_CERT_BUFFERS_256 */
  24704. #endif /* END HAVE_ECC */
  24705. #ifndef NO_FILESYSTEM
  24706. /* Silence. */
  24707. (void)keyFile;
  24708. (void)certFile;
  24709. #endif
  24710. {
  24711. const pkcs7EnvelopedVector testVectors[] = {
  24712. /* DATA is a global variable defined in the makefile. */
  24713. #if !defined(NO_RSA)
  24714. #ifndef NO_DES3
  24715. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24716. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24717. #endif /* NO_DES3 */
  24718. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24719. #ifndef NO_AES_128
  24720. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24721. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24722. #endif
  24723. #ifndef NO_AES_192
  24724. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24725. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24726. #endif
  24727. #ifndef NO_AES_256
  24728. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24729. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24730. #endif
  24731. #endif /* NO_AES && HAVE_AES_CBC */
  24732. #endif /* NO_RSA */
  24733. #if defined(HAVE_ECC)
  24734. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24735. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24736. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24737. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  24738. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24739. #endif
  24740. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24741. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24742. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  24743. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24744. #endif
  24745. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24746. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24747. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  24748. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24749. #endif
  24750. #endif /* NO_AES && HAVE_AES_CBC*/
  24751. #endif /* END HAVE_ECC */
  24752. }; /* END pkcs7EnvelopedVector */
  24753. #ifdef ECC_TIMING_RESISTANT
  24754. ExpectIntEQ(wc_InitRng(&rng), 0);
  24755. #endif
  24756. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24757. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24758. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24759. for (i = 0; i < testSz; i++) {
  24760. #ifdef ASN_BER_TO_DER
  24761. /* test setting stream mode */
  24762. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24763. (word32)(testVectors + i)->certSz), 0);
  24764. if (pkcs7 != NULL) {
  24765. #ifdef ECC_TIMING_RESISTANT
  24766. pkcs7->rng = &rng;
  24767. #endif
  24768. pkcs7->content = (byte*)(testVectors + i)->content;
  24769. pkcs7->contentSz = (testVectors + i)->contentSz;
  24770. pkcs7->contentOID = (testVectors + i)->contentOID;
  24771. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24772. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24773. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24774. pkcs7->privateKey = (testVectors + i)->privateKey;
  24775. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24776. }
  24777. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1), 0);
  24778. ExpectIntGE(encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24779. (word32)sizeof(output)), 0);
  24780. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24781. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  24782. ExpectIntGE(decodedSz, 0);
  24783. /* Verify the size of each buffer. */
  24784. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24785. wc_PKCS7_Free(pkcs7);
  24786. pkcs7 = NULL;
  24787. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24788. #endif
  24789. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24790. (word32)(testVectors + i)->certSz), 0);
  24791. if (pkcs7 != NULL) {
  24792. #ifdef ECC_TIMING_RESISTANT
  24793. pkcs7->rng = &rng;
  24794. #endif
  24795. pkcs7->content = (byte*)(testVectors + i)->content;
  24796. pkcs7->contentSz = (testVectors + i)->contentSz;
  24797. pkcs7->contentOID = (testVectors + i)->contentOID;
  24798. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24799. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24800. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24801. pkcs7->privateKey = (testVectors + i)->privateKey;
  24802. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24803. }
  24804. #ifdef ASN_BER_TO_DER
  24805. /* test without setting stream mode */
  24806. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  24807. #endif
  24808. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24809. (word32)sizeof(output)), 0);
  24810. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24811. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24812. ExpectIntGE(decodedSz, 0);
  24813. /* Verify the size of each buffer. */
  24814. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24815. /* Don't free the last time through the loop. */
  24816. if (i < testSz - 1) {
  24817. wc_PKCS7_Free(pkcs7);
  24818. pkcs7 = NULL;
  24819. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24820. }
  24821. } /* END test loop. */
  24822. }
  24823. /* Test bad args. */
  24824. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24825. (word32)sizeof(output)), BAD_FUNC_ARG);
  24826. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24827. (word32)sizeof(output)), BAD_FUNC_ARG);
  24828. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24829. /* Decode. */
  24830. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24831. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24832. BAD_FUNC_ARG);
  24833. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24834. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24835. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24836. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24837. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24838. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24839. BAD_FUNC_ARG);
  24840. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24841. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24842. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24843. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24844. /* only a failure for KARI test cases */
  24845. if (pkcs7 != NULL) {
  24846. tempWrd32 = pkcs7->singleCertSz;
  24847. pkcs7->singleCertSz = 0;
  24848. }
  24849. #if defined(WOLFSSL_ASN_TEMPLATE)
  24850. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24851. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24852. BUFFER_E);
  24853. #else
  24854. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24855. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24856. ASN_PARSE_E);
  24857. #endif
  24858. if (pkcs7 != NULL) {
  24859. pkcs7->singleCertSz = tempWrd32;
  24860. tmpBytePtr = pkcs7->singleCert;
  24861. pkcs7->singleCert = NULL;
  24862. }
  24863. #ifndef NO_RSA
  24864. #if defined(NO_PKCS7_STREAM)
  24865. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  24866. * is returned from kari parse which gets set to bad func arg */
  24867. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24868. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24869. BAD_FUNC_ARG);
  24870. #else
  24871. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24872. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24873. ASN_PARSE_E);
  24874. #endif
  24875. #endif /* !NO_RSA */
  24876. if (pkcs7 != NULL) {
  24877. pkcs7->singleCert = tmpBytePtr;
  24878. }
  24879. #endif
  24880. if (pkcs7 != NULL) {
  24881. tempWrd32 = pkcs7->privateKeySz;
  24882. pkcs7->privateKeySz = 0;
  24883. }
  24884. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24885. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24886. BAD_FUNC_ARG);
  24887. if (pkcs7 != NULL) {
  24888. pkcs7->privateKeySz = tempWrd32;
  24889. tmpBytePtr = pkcs7->privateKey;
  24890. pkcs7->privateKey = NULL;
  24891. }
  24892. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24893. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24894. BAD_FUNC_ARG);
  24895. if (pkcs7 != NULL) {
  24896. pkcs7->privateKey = tmpBytePtr;
  24897. }
  24898. wc_PKCS7_Free(pkcs7);
  24899. pkcs7 = NULL;
  24900. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24901. /* test of decrypt callback with KEKRI enveloped data */
  24902. {
  24903. int envelopedSz = 0;
  24904. const byte keyId[] = { 0x00 };
  24905. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24906. if (pkcs7 != NULL) {
  24907. pkcs7->content = (byte*)input;
  24908. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24909. pkcs7->contentOID = DATA;
  24910. pkcs7->encryptOID = AES256CBCb;
  24911. }
  24912. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24913. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24914. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24915. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24916. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24917. (word32)sizeof(output))), 0);
  24918. wc_PKCS7_Free(pkcs7);
  24919. pkcs7 = NULL;
  24920. /* decode envelopedData */
  24921. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24922. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24923. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24924. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24925. envelopedSz, decoded, sizeof(decoded))), 0);
  24926. wc_PKCS7_Free(pkcs7);
  24927. pkcs7 = NULL;
  24928. }
  24929. #endif /* !NO_AES && !NO_AES_256 */
  24930. #ifndef NO_RSA
  24931. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24932. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24933. #endif /* NO_RSA */
  24934. #ifdef HAVE_ECC
  24935. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24936. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24937. #endif /* HAVE_ECC */
  24938. #ifdef ECC_TIMING_RESISTANT
  24939. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24940. #endif
  24941. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  24942. !defined(NO_RSA) && !defined(NO_SHA)
  24943. {
  24944. byte out[7];
  24945. byte *cms = NULL;
  24946. word32 cmsSz;
  24947. XFILE cmsFile = XBADFILE;
  24948. XMEMSET(out, 0, sizeof(out));
  24949. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24950. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  24951. != XBADFILE);
  24952. cmsSz = (word32)FOURK_BUF;
  24953. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24954. DYNAMIC_TYPE_TMP_BUFFER));
  24955. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  24956. if (cmsFile != XBADFILE)
  24957. XFCLOSE(cmsFile);
  24958. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24959. sizeof_client_cert_der_2048), 0);
  24960. if (pkcs7 != NULL) {
  24961. pkcs7->privateKey = (byte*)client_key_der_2048;
  24962. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24963. }
  24964. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24965. 2), 0);
  24966. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24967. sizeof(out)), 0);
  24968. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24969. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  24970. wc_PKCS7_Free(pkcs7);
  24971. pkcs7 = NULL;
  24972. }
  24973. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  24974. #endif /* HAVE_PKCS7 */
  24975. return EXPECT_RESULT();
  24976. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  24977. /*
  24978. * Testing wc_PKCS7_EncodeEncryptedData()
  24979. */
  24980. static int test_wc_PKCS7_EncodeEncryptedData(void)
  24981. {
  24982. EXPECT_DECLS;
  24983. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24984. PKCS7* pkcs7 = NULL;
  24985. byte* tmpBytePtr = NULL;
  24986. byte encrypted[TWOK_BUF];
  24987. byte decoded[TWOK_BUF];
  24988. word32 tmpWrd32 = 0;
  24989. int tmpInt = 0;
  24990. int decodedSz = 0;
  24991. int encryptedSz = 0;
  24992. int testSz = 0;
  24993. int i = 0;
  24994. const byte data[] = { /* Hello World */
  24995. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24996. 0x72,0x6c,0x64
  24997. };
  24998. #ifndef NO_DES3
  24999. byte desKey[] = {
  25000. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25001. };
  25002. byte des3Key[] = {
  25003. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25004. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25005. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25006. };
  25007. #endif
  25008. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25009. #ifndef NO_AES_128
  25010. byte aes128Key[] = {
  25011. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25012. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25013. };
  25014. #endif
  25015. #ifndef NO_AES_192
  25016. byte aes192Key[] = {
  25017. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25018. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25019. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25020. };
  25021. #endif
  25022. #ifndef NO_AES_256
  25023. byte aes256Key[] = {
  25024. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25025. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25026. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25027. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25028. };
  25029. #endif
  25030. #endif /* !NO_AES && HAVE_AES_CBC */
  25031. const pkcs7EncryptedVector testVectors[] =
  25032. {
  25033. #ifndef NO_DES3
  25034. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25035. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25036. #endif /* !NO_DES3 */
  25037. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25038. #ifndef NO_AES_128
  25039. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25040. sizeof(aes128Key)},
  25041. #endif
  25042. #ifndef NO_AES_192
  25043. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25044. sizeof(aes192Key)},
  25045. #endif
  25046. #ifndef NO_AES_256
  25047. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25048. sizeof(aes256Key)},
  25049. #endif
  25050. #endif /* !NO_AES && HAVE_AES_CBC */
  25051. };
  25052. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25053. for (i = 0; i < testSz; i++) {
  25054. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25055. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25056. if (pkcs7 != NULL) {
  25057. pkcs7->content = (byte*)testVectors[i].content;
  25058. pkcs7->contentSz = testVectors[i].contentSz;
  25059. pkcs7->contentOID = testVectors[i].contentOID;
  25060. pkcs7->encryptOID = testVectors[i].encryptOID;
  25061. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25062. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25063. pkcs7->heap = HEAP_HINT;
  25064. }
  25065. /* encode encryptedData */
  25066. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25067. sizeof(encrypted)), 0);
  25068. /* Decode encryptedData */
  25069. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  25070. encryptedSz, decoded, sizeof(decoded)), 0);
  25071. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25072. /* Keep values for last itr. */
  25073. if (i < testSz - 1) {
  25074. wc_PKCS7_Free(pkcs7);
  25075. pkcs7 = NULL;
  25076. }
  25077. }
  25078. if (pkcs7 == NULL || testSz == 0) {
  25079. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25080. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25081. }
  25082. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25083. sizeof(encrypted)),BAD_FUNC_ARG);
  25084. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25085. sizeof(encrypted)), BAD_FUNC_ARG);
  25086. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25087. 0), BAD_FUNC_ARG);
  25088. /* Testing the struct. */
  25089. if (pkcs7 != NULL) {
  25090. tmpBytePtr = pkcs7->content;
  25091. pkcs7->content = NULL;
  25092. }
  25093. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25094. sizeof(encrypted)), BAD_FUNC_ARG);
  25095. if (pkcs7 != NULL) {
  25096. pkcs7->content = tmpBytePtr;
  25097. tmpWrd32 = pkcs7->contentSz;
  25098. pkcs7->contentSz = 0;
  25099. }
  25100. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25101. sizeof(encrypted)), BAD_FUNC_ARG);
  25102. if (pkcs7 != NULL) {
  25103. pkcs7->contentSz = tmpWrd32;
  25104. tmpInt = pkcs7->encryptOID;
  25105. pkcs7->encryptOID = 0;
  25106. }
  25107. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25108. sizeof(encrypted)), BAD_FUNC_ARG);
  25109. if (pkcs7 != NULL) {
  25110. pkcs7->encryptOID = tmpInt;
  25111. tmpBytePtr = pkcs7->encryptionKey;
  25112. pkcs7->encryptionKey = NULL;
  25113. }
  25114. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25115. sizeof(encrypted)), BAD_FUNC_ARG);
  25116. if (pkcs7 != NULL) {
  25117. pkcs7->encryptionKey = tmpBytePtr;
  25118. tmpWrd32 = pkcs7->encryptionKeySz;
  25119. pkcs7->encryptionKeySz = 0;
  25120. }
  25121. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25122. sizeof(encrypted)), BAD_FUNC_ARG);
  25123. if (pkcs7 != NULL) {
  25124. pkcs7->encryptionKeySz = tmpWrd32;
  25125. }
  25126. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25127. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25128. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25129. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25130. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25131. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25132. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25133. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25134. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25135. decoded, 0), BAD_FUNC_ARG);
  25136. /* Test struct fields */
  25137. if (pkcs7 != NULL) {
  25138. tmpBytePtr = pkcs7->encryptionKey;
  25139. pkcs7->encryptionKey = NULL;
  25140. }
  25141. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25142. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25143. if (pkcs7 != NULL) {
  25144. pkcs7->encryptionKey = tmpBytePtr;
  25145. pkcs7->encryptionKeySz = 0;
  25146. }
  25147. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25148. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25149. wc_PKCS7_Free(pkcs7);
  25150. #endif
  25151. return EXPECT_RESULT();
  25152. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25153. /*
  25154. * Testing wc_PKCS7_Degenerate()
  25155. */
  25156. static int test_wc_PKCS7_Degenerate(void)
  25157. {
  25158. EXPECT_DECLS;
  25159. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25160. PKCS7* pkcs7 = NULL;
  25161. char fName[] = "./certs/test-degenerate.p7b";
  25162. XFILE f = XBADFILE;
  25163. byte der[4096];
  25164. word32 derSz = 0;
  25165. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25166. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25167. if (f != XBADFILE)
  25168. XFCLOSE(f);
  25169. /* test degenerate success */
  25170. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25171. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25172. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25173. #ifndef NO_RSA
  25174. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25175. #else
  25176. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25177. #endif
  25178. wc_PKCS7_Free(pkcs7);
  25179. pkcs7 = NULL;
  25180. /* test with turning off degenerate cases */
  25181. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25182. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25183. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25184. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25185. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  25186. PKCS7_NO_SIGNER_E);
  25187. wc_PKCS7_Free(pkcs7);
  25188. #endif
  25189. return EXPECT_RESULT();
  25190. } /* END test_wc_PKCS7_Degenerate() */
  25191. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25192. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  25193. static byte berContent[] = {
  25194. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25195. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  25196. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  25197. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  25198. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  25199. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  25200. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  25201. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  25202. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  25203. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  25204. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  25205. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  25206. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  25207. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  25208. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  25209. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  25210. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  25211. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  25212. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25213. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25214. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  25215. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25216. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25217. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  25218. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  25219. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  25220. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25221. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  25222. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  25223. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  25224. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  25225. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  25226. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  25227. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  25228. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  25229. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  25230. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  25231. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  25232. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  25233. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  25234. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  25235. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  25236. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  25237. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  25238. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25239. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  25240. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  25241. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  25242. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  25243. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  25244. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  25245. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  25246. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  25247. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  25248. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  25249. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  25250. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  25251. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  25252. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  25253. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  25254. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  25255. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  25256. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  25257. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  25258. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  25259. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  25260. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  25261. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  25262. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  25263. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  25264. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  25265. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  25266. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  25267. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  25268. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  25269. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  25270. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  25271. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  25272. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  25273. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  25274. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  25275. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  25276. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  25277. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  25278. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  25279. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  25280. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  25281. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  25282. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  25283. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  25284. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  25285. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  25286. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  25287. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  25288. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  25289. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  25290. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25291. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25292. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25293. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25294. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25295. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25296. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25297. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25298. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25299. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25300. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25301. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25302. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25303. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25304. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25305. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25306. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25307. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25308. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25309. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25310. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25311. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25312. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25313. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25314. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25315. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25316. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25317. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25318. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25319. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25320. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25321. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25322. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25323. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25324. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25325. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25326. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25327. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25328. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25329. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25330. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25331. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25332. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25333. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25334. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25335. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25336. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25337. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25338. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25339. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25340. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25341. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25342. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25343. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25344. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25345. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25346. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25347. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25348. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25349. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25350. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25351. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25352. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25353. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25354. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25355. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25356. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25357. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25358. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25359. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25360. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25361. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25362. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25363. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25364. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25365. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25366. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25367. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25368. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25369. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25370. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25371. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25372. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25373. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25374. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25375. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25376. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25377. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25378. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25379. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25380. 0x00, 0x00, 0x00, 0x00, 0x00
  25381. };
  25382. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  25383. * !NO_DES3 && !NO_SHA
  25384. */
  25385. /*
  25386. * Testing wc_PKCS7_BER()
  25387. */
  25388. static int test_wc_PKCS7_BER(void)
  25389. {
  25390. EXPECT_DECLS;
  25391. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25392. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  25393. PKCS7* pkcs7 = NULL;
  25394. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25395. XFILE f = XBADFILE;
  25396. byte der[4096];
  25397. #ifndef NO_DES3
  25398. byte decoded[2048];
  25399. #endif
  25400. word32 derSz = 0;
  25401. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25402. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25403. if (f != XBADFILE) {
  25404. XFCLOSE(f);
  25405. f = XBADFILE;
  25406. }
  25407. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25408. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25409. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25410. #ifndef NO_RSA
  25411. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25412. #else
  25413. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25414. #endif
  25415. wc_PKCS7_Free(pkcs7);
  25416. pkcs7 = NULL;
  25417. #ifndef NO_DES3
  25418. /* decode BER content */
  25419. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  25420. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25421. if (f != XBADFILE) {
  25422. XFCLOSE(f);
  25423. f = XBADFILE;
  25424. }
  25425. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25426. #ifndef NO_RSA
  25427. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25428. #else
  25429. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25430. #endif
  25431. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  25432. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25433. if (f != XBADFILE) {
  25434. XFCLOSE(f);
  25435. f = XBADFILE;
  25436. }
  25437. if (pkcs7 != NULL) {
  25438. pkcs7->privateKey = der;
  25439. pkcs7->privateKeySz = derSz;
  25440. }
  25441. #ifndef NO_RSA
  25442. #ifdef WOLFSSL_SP_MATH
  25443. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25444. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25445. #else
  25446. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25447. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25448. #endif
  25449. #else
  25450. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25451. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  25452. #endif
  25453. wc_PKCS7_Free(pkcs7);
  25454. #endif /* !NO_DES3 */
  25455. #endif
  25456. return EXPECT_RESULT();
  25457. } /* END test_wc_PKCS7_BER() */
  25458. static int test_wc_PKCS7_signed_enveloped(void)
  25459. {
  25460. EXPECT_DECLS;
  25461. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  25462. !defined(NO_FILESYSTEM)
  25463. XFILE f = XBADFILE;
  25464. PKCS7* pkcs7 = NULL;
  25465. #ifdef HAVE_AES_CBC
  25466. PKCS7* inner = NULL;
  25467. #endif
  25468. WC_RNG rng;
  25469. unsigned char key[FOURK_BUF/2];
  25470. unsigned char cert[FOURK_BUF/2];
  25471. unsigned char env[FOURK_BUF];
  25472. int envSz = FOURK_BUF;
  25473. int keySz = 0;
  25474. int certSz = 0;
  25475. unsigned char sig[FOURK_BUF * 2];
  25476. int sigSz = FOURK_BUF * 2;
  25477. #ifdef HAVE_AES_CBC
  25478. unsigned char decoded[FOURK_BUF];
  25479. int decodedSz = FOURK_BUF;
  25480. #endif
  25481. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25482. /* load cert */
  25483. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  25484. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  25485. if (f != XBADFILE) {
  25486. XFCLOSE(f);
  25487. f = XBADFILE;
  25488. }
  25489. /* load key */
  25490. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  25491. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  25492. if (f != XBADFILE) {
  25493. XFCLOSE(f);
  25494. f = XBADFILE;
  25495. }
  25496. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  25497. /* sign cert for envelope */
  25498. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25499. ExpectIntEQ(wc_InitRng(&rng), 0);
  25500. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25501. if (pkcs7 != NULL) {
  25502. pkcs7->content = cert;
  25503. pkcs7->contentSz = certSz;
  25504. pkcs7->contentOID = DATA;
  25505. pkcs7->privateKey = key;
  25506. pkcs7->privateKeySz = keySz;
  25507. pkcs7->encryptOID = RSAk;
  25508. pkcs7->hashOID = SHA256h;
  25509. pkcs7->rng = &rng;
  25510. }
  25511. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25512. wc_PKCS7_Free(pkcs7);
  25513. pkcs7 = NULL;
  25514. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25515. #ifdef HAVE_AES_CBC
  25516. /* create envelope */
  25517. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25518. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25519. if (pkcs7 != NULL) {
  25520. pkcs7->content = sig;
  25521. pkcs7->contentSz = sigSz;
  25522. pkcs7->contentOID = DATA;
  25523. pkcs7->encryptOID = AES256CBCb;
  25524. pkcs7->privateKey = key;
  25525. pkcs7->privateKeySz = keySz;
  25526. }
  25527. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  25528. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  25529. wc_PKCS7_Free(pkcs7);
  25530. pkcs7 = NULL;
  25531. #endif
  25532. /* create bad signed enveloped data */
  25533. sigSz = FOURK_BUF * 2;
  25534. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25535. ExpectIntEQ(wc_InitRng(&rng), 0);
  25536. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25537. if (pkcs7 != NULL) {
  25538. pkcs7->content = env;
  25539. pkcs7->contentSz = envSz;
  25540. pkcs7->contentOID = DATA;
  25541. pkcs7->privateKey = key;
  25542. pkcs7->privateKeySz = keySz;
  25543. pkcs7->encryptOID = RSAk;
  25544. pkcs7->hashOID = SHA256h;
  25545. pkcs7->rng = &rng;
  25546. }
  25547. /* Set no certs in bundle for this test. */
  25548. if (pkcs7 != NULL) {
  25549. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  25550. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  25551. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  25552. }
  25553. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25554. wc_PKCS7_Free(pkcs7);
  25555. pkcs7 = NULL;
  25556. /* check verify fails */
  25557. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25558. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25559. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  25560. PKCS7_SIGNEEDS_CHECK);
  25561. /* try verifying the signature manually */
  25562. {
  25563. RsaKey rKey;
  25564. word32 idx = 0;
  25565. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  25566. WC_MAX_DIGEST_SIZE];
  25567. int digestSz = 0;
  25568. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  25569. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  25570. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  25571. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  25572. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  25573. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  25574. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  25575. /* verify was success */
  25576. }
  25577. wc_PKCS7_Free(pkcs7);
  25578. pkcs7 = NULL;
  25579. /* initializing the PKCS7 struct with the signing certificate should pass */
  25580. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25581. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25582. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25583. wc_PKCS7_Free(pkcs7);
  25584. pkcs7 = NULL;
  25585. /* create valid degenerate bundle */
  25586. sigSz = FOURK_BUF * 2;
  25587. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25588. if (pkcs7 != NULL) {
  25589. pkcs7->content = env;
  25590. pkcs7->contentSz = envSz;
  25591. pkcs7->contentOID = DATA;
  25592. pkcs7->privateKey = key;
  25593. pkcs7->privateKeySz = keySz;
  25594. pkcs7->encryptOID = RSAk;
  25595. pkcs7->hashOID = SHA256h;
  25596. pkcs7->rng = &rng;
  25597. }
  25598. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  25599. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25600. wc_PKCS7_Free(pkcs7);
  25601. pkcs7 = NULL;
  25602. wc_FreeRng(&rng);
  25603. /* check verify */
  25604. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25605. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25606. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25607. ExpectNotNull(pkcs7->content);
  25608. #ifdef HAVE_AES_CBC
  25609. /* check decode */
  25610. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  25611. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  25612. if (inner != NULL) {
  25613. inner->privateKey = key;
  25614. inner->privateKeySz = keySz;
  25615. }
  25616. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  25617. pkcs7->contentSz, decoded, decodedSz)), 0);
  25618. wc_PKCS7_Free(inner);
  25619. inner = NULL;
  25620. #endif
  25621. wc_PKCS7_Free(pkcs7);
  25622. pkcs7 = NULL;
  25623. #ifdef HAVE_AES_CBC
  25624. /* check cert set */
  25625. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25626. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25627. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  25628. ExpectNotNull(pkcs7->singleCert);
  25629. ExpectIntNE(pkcs7->singleCertSz, 0);
  25630. wc_PKCS7_Free(pkcs7);
  25631. pkcs7 = NULL;
  25632. #endif
  25633. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  25634. return EXPECT_RESULT();
  25635. }
  25636. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  25637. {
  25638. EXPECT_DECLS;
  25639. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25640. && !defined(NO_AES)
  25641. PKCS7* pkcs7 = NULL;
  25642. void* heap = NULL;
  25643. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25644. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25645. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  25646. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  25647. wc_PKCS7_Free(pkcs7);
  25648. #endif
  25649. return EXPECT_RESULT();
  25650. }
  25651. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  25652. {
  25653. EXPECT_DECLS;
  25654. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25655. && !defined(NO_AES)
  25656. PKCS7* pkcs7 = NULL;
  25657. void* heap = NULL;
  25658. WOLFSSL_CTX* ctx = NULL;
  25659. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25660. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25661. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25662. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  25663. wc_PKCS7_Free(pkcs7);
  25664. #endif
  25665. return EXPECT_RESULT();
  25666. }
  25667. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  25668. {
  25669. EXPECT_DECLS;
  25670. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25671. && !defined(NO_AES)
  25672. PKCS7* pkcs7 = NULL;
  25673. void* heap = NULL;
  25674. WOLFSSL_CTX* ctx = NULL;
  25675. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25676. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25677. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25678. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  25679. wc_PKCS7_Free(pkcs7);
  25680. #endif
  25681. return EXPECT_RESULT();
  25682. }
  25683. static int test_wc_PKCS7_DecodeCompressedData(void)
  25684. {
  25685. EXPECT_DECLS;
  25686. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25687. && !defined(NO_AES) && defined(HAVE_LIBZ)
  25688. PKCS7* pkcs7 = NULL;
  25689. void* heap = NULL;
  25690. byte out[4096];
  25691. byte* decompressed = NULL;
  25692. int outSz;
  25693. int decompressedSz;
  25694. const char* cert = "./certs/client-cert.pem";
  25695. byte* cert_buf = NULL;
  25696. size_t cert_sz = 0;
  25697. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  25698. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  25699. DYNAMIC_TYPE_TMP_BUFFER)));
  25700. decompressedSz = (int)cert_sz;
  25701. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25702. if (pkcs7 != NULL) {
  25703. pkcs7->content = (byte*)cert_buf;
  25704. pkcs7->contentSz = (word32)cert_sz;
  25705. pkcs7->contentOID = DATA;
  25706. }
  25707. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  25708. sizeof(out))), 0);
  25709. wc_PKCS7_Free(pkcs7);
  25710. pkcs7 = NULL;
  25711. /* compressed key should be smaller than when started */
  25712. ExpectIntLT(outSz, cert_sz);
  25713. /* test decompression */
  25714. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25715. ExpectIntEQ(pkcs7->contentOID, 0);
  25716. /* fail case with out buffer too small */
  25717. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25718. decompressed, outSz), 0);
  25719. /* success case */
  25720. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25721. decompressed, decompressedSz), cert_sz);
  25722. ExpectIntEQ(pkcs7->contentOID, DATA);
  25723. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25724. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25725. decompressed = NULL;
  25726. /* test decompression function with different 'max' inputs */
  25727. outSz = sizeof(out);
  25728. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  25729. 0);
  25730. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  25731. out, outSz, 0, heap), 0);
  25732. ExpectNull(decompressed);
  25733. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  25734. out, outSz, 0, heap), 0);
  25735. ExpectNotNull(decompressed);
  25736. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25737. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25738. decompressed = NULL;
  25739. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  25740. out, outSz, 0, heap), 0);
  25741. ExpectNotNull(decompressed);
  25742. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25743. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25744. if (cert_buf != NULL)
  25745. free(cert_buf);
  25746. wc_PKCS7_Free(pkcs7);
  25747. #endif
  25748. return EXPECT_RESULT();
  25749. }
  25750. static int test_wc_i2d_PKCS12(void)
  25751. {
  25752. EXPECT_DECLS;
  25753. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  25754. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25755. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  25756. WC_PKCS12* pkcs12 = NULL;
  25757. unsigned char der[FOURK_BUF * 2];
  25758. unsigned char* pt;
  25759. int derSz = 0;
  25760. unsigned char out[FOURK_BUF * 2];
  25761. int outSz = FOURK_BUF * 2;
  25762. const char p12_f[] = "./certs/test-servercert.p12";
  25763. XFILE f = XBADFILE;
  25764. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  25765. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  25766. if (f != XBADFILE)
  25767. XFCLOSE(f);
  25768. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25769. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  25770. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25771. ExpectIntEQ(outSz, derSz);
  25772. outSz = derSz - 1;
  25773. pt = out;
  25774. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  25775. outSz = derSz;
  25776. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  25777. ExpectIntEQ((pt == out), 0);
  25778. pt = NULL;
  25779. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  25780. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  25781. wc_PKCS12_free(pkcs12);
  25782. pkcs12 = NULL;
  25783. /* Run the same test but use wc_d2i_PKCS12_fp. */
  25784. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25785. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25786. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25787. ExpectIntEQ(outSz, derSz);
  25788. wc_PKCS12_free(pkcs12);
  25789. pkcs12 = NULL;
  25790. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  25791. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25792. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25793. ExpectIntEQ(outSz, derSz);
  25794. wc_PKCS12_free(pkcs12);
  25795. pkcs12 = NULL;
  25796. #endif
  25797. return EXPECT_RESULT();
  25798. }
  25799. /* Testing wc_SignatureGetSize() for signature type ECC */
  25800. static int test_wc_SignatureGetSize_ecc(void)
  25801. {
  25802. EXPECT_DECLS;
  25803. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  25804. enum wc_SignatureType sig_type;
  25805. word32 key_len;
  25806. ecc_key ecc;
  25807. const char* qx =
  25808. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25809. const char* qy =
  25810. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25811. const char* d =
  25812. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25813. XMEMSET(&ecc, 0, sizeof(ecc_key));
  25814. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  25815. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  25816. /* Input for signature type ECC */
  25817. sig_type = WC_SIGNATURE_TYPE_ECC;
  25818. key_len = sizeof(ecc_key);
  25819. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  25820. /* Test bad args */
  25821. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  25822. sig_type = (enum wc_SignatureType) 100;
  25823. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  25824. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25825. sig_type = WC_SIGNATURE_TYPE_ECC;
  25826. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25827. key_len = (word32)0;
  25828. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25829. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  25830. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  25831. return EXPECT_RESULT();
  25832. } /* END test_wc_SignatureGetSize_ecc() */
  25833. /* Testing wc_SignatureGetSize() for signature type rsa */
  25834. static int test_wc_SignatureGetSize_rsa(void)
  25835. {
  25836. EXPECT_DECLS;
  25837. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  25838. enum wc_SignatureType sig_type;
  25839. word32 key_len;
  25840. word32 idx = 0;
  25841. RsaKey rsa_key;
  25842. byte* tmp = NULL;
  25843. size_t bytes;
  25844. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  25845. #ifdef USE_CERT_BUFFERS_1024
  25846. bytes = (size_t)sizeof_client_key_der_1024;
  25847. if (bytes < (size_t)sizeof_client_key_der_1024)
  25848. bytes = (size_t)sizeof_client_cert_der_1024;
  25849. #elif defined(USE_CERT_BUFFERS_2048)
  25850. bytes = (size_t)sizeof_client_key_der_2048;
  25851. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25852. bytes = (size_t)sizeof_client_cert_der_2048;
  25853. #else
  25854. bytes = FOURK_BUF;
  25855. #endif
  25856. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  25857. DYNAMIC_TYPE_TMP_BUFFER));
  25858. if (tmp != NULL) {
  25859. #ifdef USE_CERT_BUFFERS_1024
  25860. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  25861. #elif defined(USE_CERT_BUFFERS_2048)
  25862. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  25863. #elif !defined(NO_FILESYSTEM)
  25864. XFILE file = XBADFILE;
  25865. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  25866. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  25867. if (file != XBADFILE)
  25868. XFCLOSE(file);
  25869. }
  25870. #else
  25871. ExpectFail();
  25872. #endif
  25873. }
  25874. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  25875. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  25876. /* Input for signature type RSA */
  25877. sig_type = WC_SIGNATURE_TYPE_RSA;
  25878. key_len = sizeof(RsaKey);
  25879. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  25880. /* Test bad args */
  25881. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  25882. sig_type = (enum wc_SignatureType)100;
  25883. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  25884. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25885. sig_type = WC_SIGNATURE_TYPE_RSA;
  25886. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  25887. key_len = (word32)0;
  25888. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25889. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  25890. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25891. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  25892. return EXPECT_RESULT();
  25893. } /* END test_wc_SignatureGetSize_rsa(void) */
  25894. /*----------------------------------------------------------------------------*
  25895. | hash.h Tests
  25896. *----------------------------------------------------------------------------*/
  25897. static int test_wc_HashInit(void)
  25898. {
  25899. EXPECT_DECLS;
  25900. int i; /* 0 indicates tests passed, 1 indicates failure */
  25901. wc_HashAlg hash;
  25902. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25903. enum wc_HashType enumArray[] = {
  25904. #ifndef NO_MD5
  25905. WC_HASH_TYPE_MD5,
  25906. #endif
  25907. #ifndef NO_SHA
  25908. WC_HASH_TYPE_SHA,
  25909. #endif
  25910. #ifdef WOLFSSL_SHA224
  25911. WC_HASH_TYPE_SHA224,
  25912. #endif
  25913. #ifndef NO_SHA256
  25914. WC_HASH_TYPE_SHA256,
  25915. #endif
  25916. #ifdef WOLFSSL_SHA384
  25917. WC_HASH_TYPE_SHA384,
  25918. #endif
  25919. #ifdef WOLFSSL_SHA512
  25920. WC_HASH_TYPE_SHA512,
  25921. #endif
  25922. };
  25923. /* dynamically finds the length */
  25924. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25925. /* For loop to test various arguments... */
  25926. for (i = 0; i < enumlen; i++) {
  25927. /* check for bad args */
  25928. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25929. wc_HashFree(&hash, enumArray[i]);
  25930. /* check for null ptr */
  25931. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  25932. } /* end of for loop */
  25933. return EXPECT_RESULT();
  25934. } /* end of test_wc_HashInit */
  25935. /*
  25936. * Unit test function for wc_HashSetFlags()
  25937. */
  25938. static int test_wc_HashSetFlags(void)
  25939. {
  25940. EXPECT_DECLS;
  25941. #ifdef WOLFSSL_HASH_FLAGS
  25942. wc_HashAlg hash;
  25943. word32 flags = 0;
  25944. int i, j;
  25945. int notSupportedLen;
  25946. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25947. enum wc_HashType enumArray[] = {
  25948. #ifndef NO_MD5
  25949. WC_HASH_TYPE_MD5,
  25950. #endif
  25951. #ifndef NO_SHA
  25952. WC_HASH_TYPE_SHA,
  25953. #endif
  25954. #ifdef WOLFSSL_SHA224
  25955. WC_HASH_TYPE_SHA224,
  25956. #endif
  25957. #ifndef NO_SHA256
  25958. WC_HASH_TYPE_SHA256,
  25959. #endif
  25960. #ifdef WOLFSSL_SHA384
  25961. WC_HASH_TYPE_SHA384,
  25962. #endif
  25963. #ifdef WOLFSSL_SHA512
  25964. WC_HASH_TYPE_SHA512,
  25965. #endif
  25966. #ifdef WOLFSSL_SHA3
  25967. WC_HASH_TYPE_SHA3_224,
  25968. #endif
  25969. };
  25970. enum wc_HashType notSupported[] = {
  25971. WC_HASH_TYPE_MD5_SHA,
  25972. WC_HASH_TYPE_MD2,
  25973. WC_HASH_TYPE_MD4,
  25974. WC_HASH_TYPE_BLAKE2B,
  25975. WC_HASH_TYPE_BLAKE2S,
  25976. WC_HASH_TYPE_NONE,
  25977. };
  25978. /* dynamically finds the length */
  25979. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25980. /* For loop to test various arguments... */
  25981. for (i = 0; i < enumlen; i++) {
  25982. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25983. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  25984. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25985. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  25986. wc_HashFree(&hash, enumArray[i]);
  25987. }
  25988. /* For loop to test not supported cases */
  25989. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25990. for (j = 0; j < notSupportedLen; j++) {
  25991. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25992. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  25993. BAD_FUNC_ARG);
  25994. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25995. }
  25996. #endif
  25997. return EXPECT_RESULT();
  25998. } /* END test_wc_HashSetFlags */
  25999. /*
  26000. * Unit test function for wc_HashGetFlags()
  26001. */
  26002. static int test_wc_HashGetFlags(void)
  26003. {
  26004. EXPECT_DECLS;
  26005. #ifdef WOLFSSL_HASH_FLAGS
  26006. wc_HashAlg hash;
  26007. word32 flags = 0;
  26008. int i, j;
  26009. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26010. enum wc_HashType enumArray[] = {
  26011. #ifndef NO_MD5
  26012. WC_HASH_TYPE_MD5,
  26013. #endif
  26014. #ifndef NO_SHA
  26015. WC_HASH_TYPE_SHA,
  26016. #endif
  26017. #ifdef WOLFSSL_SHA224
  26018. WC_HASH_TYPE_SHA224,
  26019. #endif
  26020. #ifndef NO_SHA256
  26021. WC_HASH_TYPE_SHA256,
  26022. #endif
  26023. #ifdef WOLFSSL_SHA384
  26024. WC_HASH_TYPE_SHA384,
  26025. #endif
  26026. #ifdef WOLFSSL_SHA512
  26027. WC_HASH_TYPE_SHA512,
  26028. #endif
  26029. #ifdef WOLFSSL_SHA3
  26030. WC_HASH_TYPE_SHA3_224,
  26031. #endif
  26032. };
  26033. enum wc_HashType notSupported[] = {
  26034. WC_HASH_TYPE_MD5_SHA,
  26035. WC_HASH_TYPE_MD2,
  26036. WC_HASH_TYPE_MD4,
  26037. WC_HASH_TYPE_BLAKE2B,
  26038. WC_HASH_TYPE_BLAKE2S,
  26039. WC_HASH_TYPE_NONE,
  26040. };
  26041. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26042. int notSupportedLen;
  26043. /* For loop to test various arguments... */
  26044. for (i = 0; i < enumlen; i++) {
  26045. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26046. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  26047. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26048. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  26049. wc_HashFree(&hash, enumArray[i]);
  26050. }
  26051. /* For loop to test not supported cases */
  26052. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26053. for (j = 0; j < notSupportedLen; j++) {
  26054. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26055. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  26056. BAD_FUNC_ARG);
  26057. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26058. }
  26059. #endif
  26060. return EXPECT_RESULT();
  26061. } /* END test_wc_HashGetFlags */
  26062. /*----------------------------------------------------------------------------*
  26063. | Compatibility Tests
  26064. *----------------------------------------------------------------------------*/
  26065. /*----------------------------------------------------------------------------*
  26066. | ASN.1 Tests
  26067. *----------------------------------------------------------------------------*/
  26068. static int test_wolfSSL_ASN1_BIT_STRING(void)
  26069. {
  26070. EXPECT_DECLS;
  26071. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  26072. ASN1_BIT_STRING* str = NULL;
  26073. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26074. /* Empty data testing. */
  26075. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  26076. ASN1_BIT_STRING_free(str);
  26077. str = NULL;
  26078. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26079. /* Invalid parameter testing. */
  26080. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  26081. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  26082. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  26083. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  26084. /* No bit string - bit is always 0. */
  26085. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  26086. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  26087. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26088. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  26089. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  26090. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  26091. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  26092. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26093. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  26094. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  26095. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  26096. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  26097. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  26098. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  26099. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  26100. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  26101. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  26102. ASN1_BIT_STRING_free(str);
  26103. ASN1_BIT_STRING_free(NULL);
  26104. #endif
  26105. return EXPECT_RESULT();
  26106. }
  26107. static int test_wolfSSL_ASN1_INTEGER(void)
  26108. {
  26109. EXPECT_DECLS;
  26110. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26111. ASN1_INTEGER* a = NULL;
  26112. ASN1_INTEGER* dup = NULL;
  26113. const unsigned char invalidLenDer[] = {
  26114. 0x02, 0x20, 0x00
  26115. };
  26116. const unsigned char longDer[] = {
  26117. 0x02, 0x20,
  26118. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26119. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26120. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26121. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  26122. };
  26123. const unsigned char* p;
  26124. /* Invalid parameter testing. */
  26125. ASN1_INTEGER_free(NULL);
  26126. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  26127. ExpectNotNull(a = ASN1_INTEGER_new());
  26128. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  26129. ASN1_INTEGER_free(dup);
  26130. dup = NULL;
  26131. ASN1_INTEGER_free(a);
  26132. a = NULL;
  26133. p = longDer;
  26134. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  26135. p = longDer;
  26136. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  26137. ExpectPtrNE(p, longDer);
  26138. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  26139. ASN1_INTEGER_free(dup);
  26140. ASN1_INTEGER_free(a);
  26141. #endif
  26142. return EXPECT_RESULT();
  26143. }
  26144. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  26145. {
  26146. EXPECT_DECLS;
  26147. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26148. ASN1_INTEGER* a = NULL;
  26149. ASN1_INTEGER* b = NULL;
  26150. ExpectNotNull(a = ASN1_INTEGER_new());
  26151. ExpectNotNull(b = ASN1_INTEGER_new());
  26152. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  26153. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  26154. /* Invalid parameter testing. */
  26155. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  26156. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  26157. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  26158. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26159. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  26160. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26161. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  26162. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26163. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  26164. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26165. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  26166. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  26167. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26168. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  26169. ASN1_INTEGER_free(b);
  26170. ASN1_INTEGER_free(a);
  26171. #endif
  26172. return EXPECT_RESULT();
  26173. }
  26174. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  26175. {
  26176. EXPECT_DECLS;
  26177. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26178. ASN1_INTEGER* ai = NULL;
  26179. ASN1_INTEGER* ai2 = NULL;
  26180. BIGNUM* bn = NULL;
  26181. BIGNUM* bn2 = NULL;
  26182. ExpectNotNull(ai = ASN1_INTEGER_new());
  26183. ExpectNotNull(bn2 = BN_new());
  26184. /* Invalid parameter testing. */
  26185. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  26186. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  26187. /* at the moment hard setting since no set function */
  26188. if (ai != NULL) {
  26189. ai->data[0] = 0xff; /* No DER encoding. */
  26190. ai->length = 1;
  26191. }
  26192. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26193. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26194. BN_free(bn);
  26195. bn = NULL;
  26196. #else
  26197. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  26198. #endif
  26199. if (ai != NULL) {
  26200. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26201. ai->data[1] = 0x04; /* bad length of integer */
  26202. ai->data[2] = 0x03;
  26203. ai->length = 3;
  26204. }
  26205. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26206. /* Interpreted as a number 0x020403. */
  26207. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26208. BN_free(bn);
  26209. bn = NULL;
  26210. #else
  26211. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  26212. #endif
  26213. if (ai != NULL) {
  26214. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26215. ai->data[1] = 0x01; /* length of integer */
  26216. ai->data[2] = 0x03;
  26217. ai->length = 3;
  26218. }
  26219. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26220. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  26221. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26222. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26223. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26224. if (ai != NULL) {
  26225. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26226. ai->data[1] = 0x02; /* length of integer */
  26227. ai->data[2] = 0x00; /* padding byte to ensure positive */
  26228. ai->data[3] = 0xff;
  26229. ai->length = 4;
  26230. }
  26231. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26232. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26233. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26234. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26235. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26236. if (ai != NULL) {
  26237. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26238. ai->data[1] = 0x01; /* length of integer */
  26239. ai->data[2] = 0x00;
  26240. ai->length = 3;
  26241. }
  26242. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26243. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26244. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26245. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26246. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26247. if (ai != NULL) {
  26248. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26249. ai->data[1] = 0x01; /* length of integer */
  26250. ai->data[2] = 0x01;
  26251. ai->length = 3;
  26252. ai->negative = 1;
  26253. }
  26254. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26255. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26256. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26257. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26258. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26259. BN_free(bn2);
  26260. BN_free(bn);
  26261. ASN1_INTEGER_free(ai2);
  26262. ASN1_INTEGER_free(ai);
  26263. #endif
  26264. return EXPECT_RESULT();
  26265. }
  26266. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  26267. {
  26268. EXPECT_DECLS;
  26269. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26270. ASN1_INTEGER *a = NULL;
  26271. long val;
  26272. ExpectNotNull(a = ASN1_INTEGER_new());
  26273. /* Invalid parameter testing. */
  26274. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  26275. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26276. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  26277. #else
  26278. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  26279. #endif
  26280. ASN1_INTEGER_free(a);
  26281. a = NULL;
  26282. ExpectNotNull(a = ASN1_INTEGER_new());
  26283. val = 0;
  26284. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  26285. ASN1_INTEGER_free(a);
  26286. a = NULL;
  26287. /* 0 */
  26288. ExpectNotNull(a = ASN1_INTEGER_new());
  26289. val = 0;
  26290. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26291. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26292. ASN1_INTEGER_free(a);
  26293. a = NULL;
  26294. /* 40 */
  26295. ExpectNotNull(a = ASN1_INTEGER_new());
  26296. val = 40;
  26297. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26298. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26299. ASN1_INTEGER_free(a);
  26300. a = NULL;
  26301. /* -40 */
  26302. ExpectNotNull(a = ASN1_INTEGER_new());
  26303. val = -40;
  26304. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26305. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26306. ASN1_INTEGER_free(a);
  26307. a = NULL;
  26308. /* 128 */
  26309. ExpectNotNull(a = ASN1_INTEGER_new());
  26310. val = 128;
  26311. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26312. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26313. ASN1_INTEGER_free(a);
  26314. a = NULL;
  26315. /* -128 */
  26316. ExpectNotNull(a = ASN1_INTEGER_new());
  26317. val = -128;
  26318. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26319. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26320. ASN1_INTEGER_free(a);
  26321. a = NULL;
  26322. /* 200 */
  26323. ExpectNotNull(a = ASN1_INTEGER_new());
  26324. val = 200;
  26325. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26326. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26327. ASN1_INTEGER_free(a);
  26328. a = NULL;
  26329. /* int max (2147483647) */
  26330. ExpectNotNull(a = ASN1_INTEGER_new());
  26331. val = 2147483647;
  26332. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26333. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26334. ASN1_INTEGER_free(a);
  26335. a = NULL;
  26336. /* int min (-2147483648) */
  26337. ExpectNotNull(a = ASN1_INTEGER_new());
  26338. val = -2147483647 - 1;
  26339. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26340. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26341. ASN1_INTEGER_free(a);
  26342. a = NULL;
  26343. /* long max positive */
  26344. ExpectNotNull(a = ASN1_INTEGER_new());
  26345. val = (long)(((unsigned long)-1) >> 1);
  26346. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26347. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26348. ASN1_INTEGER_free(a);
  26349. #endif
  26350. return EXPECT_RESULT();
  26351. }
  26352. #if defined(OPENSSL_EXTRA)
  26353. typedef struct ASN1IntTestVector {
  26354. const byte* der;
  26355. const size_t derSz;
  26356. const long value;
  26357. } ASN1IntTestVector;
  26358. #endif
  26359. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  26360. {
  26361. EXPECT_DECLS;
  26362. #if defined(OPENSSL_EXTRA)
  26363. size_t i;
  26364. WOLFSSL_ASN1_INTEGER* a = NULL;
  26365. WOLFSSL_ASN1_INTEGER* b = NULL;
  26366. WOLFSSL_ASN1_INTEGER* c = NULL;
  26367. const byte* p = NULL;
  26368. byte* p2 = NULL;
  26369. byte* reEncoded = NULL;
  26370. int reEncodedSz = 0;
  26371. static const byte zeroDer[] = {
  26372. 0x02, 0x01, 0x00
  26373. };
  26374. static const byte oneDer[] = {
  26375. 0x02, 0x01, 0x01
  26376. };
  26377. static const byte negativeDer[] = {
  26378. 0x02, 0x03, 0xC1, 0x16, 0x0D
  26379. };
  26380. static const byte positiveDer[] = {
  26381. 0x02, 0x03, 0x01, 0x00, 0x01
  26382. };
  26383. static const byte primeDer[] = {
  26384. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  26385. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  26386. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  26387. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  26388. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  26389. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  26390. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  26391. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  26392. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  26393. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  26394. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  26395. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  26396. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  26397. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  26398. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  26399. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  26400. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  26401. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  26402. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  26403. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  26404. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  26405. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  26406. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  26407. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  26408. };
  26409. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  26410. static const ASN1IntTestVector testVectors[] = {
  26411. {zeroDer, sizeof(zeroDer), 0},
  26412. {oneDer, sizeof(oneDer), 1},
  26413. {negativeDer, sizeof(negativeDer), -4123123},
  26414. {positiveDer, sizeof(positiveDer), 65537},
  26415. {primeDer, sizeof(primeDer), 0}
  26416. };
  26417. static const size_t NUM_TEST_VECTORS =
  26418. sizeof(testVectors)/sizeof(testVectors[0]);
  26419. /* Check d2i error conditions */
  26420. /* NULL pointer to input. */
  26421. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  26422. ExpectNull(b);
  26423. /* NULL input. */
  26424. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  26425. ExpectNull(b);
  26426. /* 0 length. */
  26427. p = testVectors[0].der;
  26428. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  26429. ExpectNull(b);
  26430. /* Negative length. */
  26431. p = testVectors[0].der;
  26432. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  26433. ExpectNull(b);
  26434. /* Garbage DER input. */
  26435. p = garbageDer;
  26436. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  26437. ExpectNull(b);
  26438. /* Check i2d error conditions */
  26439. /* NULL input. */
  26440. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  26441. /* 0 length input data buffer (a->length == 0). */
  26442. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  26443. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  26444. if (a != NULL)
  26445. a->data = NULL;
  26446. /* NULL input data buffer. */
  26447. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  26448. if (a != NULL) {
  26449. /* Reset a->data. */
  26450. a->data = a->intData;
  26451. }
  26452. /* Set a to valid value. */
  26453. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  26454. /* NULL output buffer. */
  26455. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  26456. wolfSSL_ASN1_INTEGER_free(a);
  26457. a = NULL;
  26458. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  26459. p = testVectors[i].der;
  26460. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  26461. testVectors[i].derSz));
  26462. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26463. if (testVectors[i].derSz <= sizeof(long)) {
  26464. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  26465. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  26466. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  26467. wolfSSL_ASN1_INTEGER_free(c);
  26468. c = NULL;
  26469. }
  26470. /* Convert to DER without a pre-allocated output buffer. */
  26471. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  26472. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  26473. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  26474. /* Convert to DER with a pre-allocated output buffer. In this case, the
  26475. * output buffer pointer should be incremented just past the end of the
  26476. * encoded data. */
  26477. p2 = reEncoded;
  26478. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  26479. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  26480. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  26481. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  26482. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  26483. reEncoded = NULL;
  26484. wolfSSL_ASN1_INTEGER_free(a);
  26485. a = NULL;
  26486. }
  26487. #endif /* OPENSSL_EXTRA */
  26488. return EXPECT_RESULT();
  26489. }
  26490. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  26491. {
  26492. EXPECT_DECLS;
  26493. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  26494. BIO* bio = NULL;
  26495. BIO* out = NULL;
  26496. BIO* fixed = NULL;
  26497. ASN1_INTEGER* ai = NULL;
  26498. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  26499. char tmp[1024];
  26500. int tmpSz;
  26501. const char expected1[] = "123456";
  26502. const char expected2[] = "112345678912345678901234567890";
  26503. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  26504. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  26505. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  26506. ExpectNotNull(ai = ASN1_INTEGER_new());
  26507. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  26508. /* Invalid parameter testing. */
  26509. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  26510. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  26511. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  26512. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  26513. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  26514. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  26515. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  26516. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  26517. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  26518. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  26519. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  26520. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  26521. /* No data to read from BIO. */
  26522. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  26523. /* read first line */
  26524. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26525. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  26526. XMEMSET(tmp, 0, 1024);
  26527. tmpSz = BIO_read(out, tmp, 1024);
  26528. ExpectIntEQ(tmpSz, 6);
  26529. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  26530. /* fail on second line (not % 2) */
  26531. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  26532. /* read 3rd long line */
  26533. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26534. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  26535. XMEMSET(tmp, 0, 1024);
  26536. tmpSz = BIO_read(out, tmp, 1024);
  26537. ExpectIntEQ(tmpSz, 30);
  26538. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  26539. /* fail on empty line */
  26540. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  26541. BIO_free(bio);
  26542. bio = NULL;
  26543. /* Make long integer, requiring dynamic memory, even longer. */
  26544. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  26545. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26546. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  26547. XMEMSET(tmp, 0, 1024);
  26548. tmpSz = BIO_read(out, tmp, 1024);
  26549. ExpectIntEQ(tmpSz, 48);
  26550. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26551. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  26552. XMEMSET(tmp, 0, 1024);
  26553. tmpSz = BIO_read(out, tmp, 1024);
  26554. ExpectIntEQ(tmpSz, 56);
  26555. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  26556. BIO_free(bio);
  26557. BIO_free(out);
  26558. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26559. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26560. /* Ensure there is 0 bytes available to write into. */
  26561. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  26562. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26563. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26564. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26565. BIO_free(fixed);
  26566. ASN1_INTEGER_free(ai);
  26567. #endif
  26568. return EXPECT_RESULT();
  26569. }
  26570. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  26571. {
  26572. EXPECT_DECLS;
  26573. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26574. ASN1_INTEGER *a = NULL;
  26575. unsigned char *pp = NULL,*tpp = NULL;
  26576. int ret = 0;
  26577. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  26578. /* Invalid parameter testing. */
  26579. /* Set pp to an invalid value. */
  26580. pp = NULL;
  26581. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  26582. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  26583. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  26584. /* 40 */
  26585. if (a != NULL) {
  26586. a->intData[0] = ASN_INTEGER;
  26587. a->intData[1] = 1;
  26588. a->intData[2] = 40;
  26589. }
  26590. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26591. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26592. DYNAMIC_TYPE_TMP_BUFFER));
  26593. tpp = pp;
  26594. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26595. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26596. tpp--;
  26597. ExpectIntEQ(*tpp, 40);
  26598. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26599. pp = NULL;
  26600. /* 128 */
  26601. if (a != NULL) {
  26602. a->intData[0] = ASN_INTEGER;
  26603. a->intData[1] = 1;
  26604. a->intData[2] = 128;
  26605. }
  26606. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26607. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26608. DYNAMIC_TYPE_TMP_BUFFER));
  26609. tpp = pp;
  26610. if (tpp != NULL) {
  26611. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26612. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26613. tpp--;
  26614. ExpectIntEQ(*(tpp--), 128);
  26615. ExpectIntEQ(*tpp, 0);
  26616. }
  26617. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26618. pp = NULL;
  26619. /* -40 */
  26620. if (a != NULL) {
  26621. a->intData[0] = ASN_INTEGER;
  26622. a->intData[1] = 1;
  26623. a->intData[2] = 40;
  26624. a->negative = 1;
  26625. }
  26626. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26627. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26628. DYNAMIC_TYPE_TMP_BUFFER));
  26629. tpp = pp;
  26630. if (tpp != NULL) {
  26631. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26632. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26633. tpp--;
  26634. ExpectIntEQ(*tpp, 216);
  26635. }
  26636. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26637. pp = NULL;
  26638. /* -128 */
  26639. if (a != NULL) {
  26640. a->intData[0] = ASN_INTEGER;
  26641. a->intData[1] = 1;
  26642. a->intData[2] = 128;
  26643. a->negative = 1;
  26644. }
  26645. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26646. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26647. DYNAMIC_TYPE_TMP_BUFFER));
  26648. tpp = pp;
  26649. if (tpp != NULL) {
  26650. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26651. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26652. tpp--;
  26653. ExpectIntEQ(*tpp, 128);
  26654. }
  26655. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26656. pp = NULL;
  26657. /* -200 */
  26658. if (a != NULL) {
  26659. a->intData[0] = ASN_INTEGER;
  26660. a->intData[1] = 1;
  26661. a->intData[2] = 200;
  26662. a->negative = 1;
  26663. }
  26664. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26665. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26666. DYNAMIC_TYPE_TMP_BUFFER));
  26667. tpp = pp;
  26668. if (tpp != NULL) {
  26669. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26670. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26671. tpp--;
  26672. ExpectIntEQ(*(tpp--), 56);
  26673. ExpectIntEQ(*tpp, 255);
  26674. }
  26675. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26676. pp = NULL;
  26677. /* Empty */
  26678. if (a != NULL) {
  26679. a->intData[0] = ASN_INTEGER;
  26680. a->intData[1] = 0;
  26681. a->negative = 0;
  26682. }
  26683. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26684. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26685. DYNAMIC_TYPE_TMP_BUFFER));
  26686. tpp = pp;
  26687. if (tpp != NULL) {
  26688. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26689. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26690. tpp--;
  26691. ExpectIntEQ(*tpp, 0);
  26692. }
  26693. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26694. pp = NULL;
  26695. /* 0 */
  26696. if (a != NULL) {
  26697. a->intData[0] = ASN_INTEGER;
  26698. a->intData[1] = 1;
  26699. a->intData[2] = 0;
  26700. a->negative = 1;
  26701. }
  26702. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26703. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26704. DYNAMIC_TYPE_TMP_BUFFER));
  26705. if (tpp != NULL) {
  26706. tpp = pp;
  26707. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26708. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26709. tpp--;
  26710. ExpectIntEQ(*tpp, 0);
  26711. }
  26712. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26713. pp = NULL;
  26714. /* 0x100 */
  26715. if (a != NULL) {
  26716. a->intData[0] = ASN_INTEGER;
  26717. a->intData[1] = 2;
  26718. a->intData[2] = 0x01;
  26719. a->intData[3] = 0x00;
  26720. a->negative = 0;
  26721. }
  26722. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26723. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26724. DYNAMIC_TYPE_TMP_BUFFER));
  26725. if (tpp != NULL) {
  26726. tpp = pp;
  26727. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26728. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26729. tpp -= 2;
  26730. ExpectIntEQ(tpp[0], 0x01);
  26731. ExpectIntEQ(tpp[1], 0x00);
  26732. }
  26733. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26734. pp = NULL;
  26735. /* -0x8000 => 0x8000 */
  26736. if (a != NULL) {
  26737. a->intData[0] = ASN_INTEGER;
  26738. a->intData[1] = 2;
  26739. a->intData[2] = 0x80;
  26740. a->intData[3] = 0x00;
  26741. a->negative = 1;
  26742. }
  26743. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26744. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26745. DYNAMIC_TYPE_TMP_BUFFER));
  26746. tpp = pp;
  26747. if (tpp != NULL) {
  26748. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26749. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26750. tpp -= 2;
  26751. ExpectIntEQ(tpp[0], 0x80);
  26752. ExpectIntEQ(tpp[1], 0x00);
  26753. }
  26754. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26755. pp = NULL;
  26756. /* -0x8001 => 0xFF7FFF */
  26757. if (a != NULL) {
  26758. a->intData[0] = ASN_INTEGER;
  26759. a->intData[1] = 2;
  26760. a->intData[2] = 0x80;
  26761. a->intData[3] = 0x01;
  26762. a->negative = 1;
  26763. }
  26764. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  26765. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26766. DYNAMIC_TYPE_TMP_BUFFER));
  26767. tpp = pp;
  26768. if (tpp != NULL) {
  26769. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26770. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  26771. tpp -= 3;
  26772. ExpectIntEQ(tpp[0], 0xFF);
  26773. ExpectIntEQ(tpp[1], 0x7F);
  26774. ExpectIntEQ(tpp[2], 0xFF);
  26775. }
  26776. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26777. wolfSSL_ASN1_INTEGER_free(a);
  26778. #endif /* OPENSSL_EXTRA && !NO_ASN */
  26779. return EXPECT_RESULT();
  26780. }
  26781. static int test_wolfSSL_ASN1_OBJECT(void)
  26782. {
  26783. EXPECT_DECLS;
  26784. #if defined(OPENSSL_EXTRA)
  26785. ASN1_OBJECT* a = NULL;
  26786. ASN1_OBJECT s;
  26787. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  26788. /* Invalid parameter testing. */
  26789. ASN1_OBJECT_free(NULL);
  26790. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  26791. /* Test that a static ASN1_OBJECT can be freed. */
  26792. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26793. ASN1_OBJECT_free(&s);
  26794. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26795. ASN1_OBJECT_free(a);
  26796. a = NULL;
  26797. s.obj = der;
  26798. s.objSz = sizeof(der);
  26799. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26800. ASN1_OBJECT_free(a);
  26801. ASN1_OBJECT_free(&s);
  26802. #endif /* OPENSSL_EXTRA */
  26803. return EXPECT_RESULT();
  26804. }
  26805. static int test_wolfSSL_ASN1_get_object(void)
  26806. {
  26807. EXPECT_DECLS;
  26808. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  26809. const unsigned char* derBuf = cliecc_cert_der_256;
  26810. const unsigned char* nullPtr = NULL;
  26811. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  26812. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  26813. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  26814. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  26815. const unsigned char* p;
  26816. unsigned char objDer[8];
  26817. unsigned char* der;
  26818. unsigned char* derPtr;
  26819. int len = sizeof_cliecc_cert_der_256;
  26820. long asnLen = 0;
  26821. int tag = 0;
  26822. int cls = 0;
  26823. ASN1_OBJECT* a = NULL;
  26824. ASN1_OBJECT s;
  26825. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26826. /* Invalid encoding at length. */
  26827. p = objDerInvalidLen;
  26828. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26829. 0x80);
  26830. p = objDerBadLen;
  26831. /* Error = 0x80, Constructed = 0x20 */
  26832. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26833. 0x80 | 0x20);
  26834. /* Read a couple TLV triplets and make sure they match the expected values
  26835. */
  26836. /* SEQUENCE */
  26837. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  26838. ExpectIntEQ(asnLen, 861);
  26839. ExpectIntEQ(tag, 0x10);
  26840. ExpectIntEQ(cls, 0);
  26841. /* SEQUENCE */
  26842. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26843. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26844. ExpectIntEQ(asnLen, 772);
  26845. ExpectIntEQ(tag, 0x10);
  26846. ExpectIntEQ(cls, 0);
  26847. /* [0] */
  26848. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26849. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26850. ExpectIntEQ(asnLen, 3);
  26851. ExpectIntEQ(tag, 0);
  26852. ExpectIntEQ(cls, 0x80);
  26853. /* INTEGER */
  26854. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26855. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26856. ExpectIntEQ(asnLen, 1);
  26857. ExpectIntEQ(tag, 0x2);
  26858. ExpectIntEQ(cls, 0);
  26859. derBuf += asnLen;
  26860. /* INTEGER */
  26861. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26862. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26863. ExpectIntEQ(asnLen, 20);
  26864. ExpectIntEQ(tag, 0x2);
  26865. ExpectIntEQ(cls, 0);
  26866. derBuf += asnLen;
  26867. /* SEQUENCE */
  26868. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26869. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26870. ExpectIntEQ(asnLen, 10);
  26871. ExpectIntEQ(tag, 0x10);
  26872. ExpectIntEQ(cls, 0);
  26873. /* Found OBJECT_ID. */
  26874. /* Invalid parameter testing. */
  26875. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  26876. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  26877. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  26878. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  26879. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  26880. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  26881. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  26882. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  26883. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  26884. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  26885. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26886. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26887. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  26888. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  26889. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  26890. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  26891. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  26892. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26893. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26894. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  26895. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  26896. /* Invalid encoding at length. */
  26897. p = objDerInvalidLen;
  26898. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  26899. p = objDerBadLen;
  26900. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  26901. p = objDerNotObj;
  26902. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  26903. p = objDerNoData;
  26904. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  26905. /* Create an ASN OBJECT from content */
  26906. p = derBuf + 2;
  26907. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  26908. ASN1_OBJECT_free(a);
  26909. a = NULL;
  26910. /* Create an ASN OBJECT from DER */
  26911. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  26912. /* Invalid parameter testing. */
  26913. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  26914. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  26915. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  26916. der = NULL;
  26917. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  26918. derPtr = objDer;
  26919. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  26920. ExpectPtrNE(derPtr, objDer);
  26921. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  26922. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  26923. ASN1_OBJECT_free(a);
  26924. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  26925. return EXPECT_RESULT();
  26926. }
  26927. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  26928. {
  26929. EXPECT_DECLS;
  26930. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26931. ASN1_OBJECT* obj = NULL;
  26932. ASN1_OBJECT* a = NULL;
  26933. BIO *bio = NULL;
  26934. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  26935. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  26936. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  26937. const unsigned char* p;
  26938. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  26939. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  26940. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  26941. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  26942. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  26943. /* No DER encoding in ASN1_OBJECT. */
  26944. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  26945. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26946. ASN1_OBJECT_free(a);
  26947. a = NULL;
  26948. /* DER encoding - not OBJECT_ID */
  26949. p = notObjDer;
  26950. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26951. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26952. ASN1_OBJECT_free(a);
  26953. a = NULL;
  26954. /* Bad length encoding. */
  26955. p = badLenDer;
  26956. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26957. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26958. ASN1_OBJECT_free(a);
  26959. a = NULL;
  26960. /* Good encoding - but unknown. */
  26961. p = goodDer;
  26962. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26963. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26964. ASN1_OBJECT_free(a);
  26965. BIO_free(bio);
  26966. ASN1_OBJECT_free(obj);
  26967. #endif
  26968. return EXPECT_RESULT();
  26969. }
  26970. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  26971. {
  26972. EXPECT_DECLS;
  26973. #if defined(OPENSSL_EXTRA) && \
  26974. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  26975. char buf[50] = {0};
  26976. ASN1_OBJECT* obj;
  26977. const char* oid = "2.5.29.19";
  26978. const char* ln = "X509v3 Basic Constraints";
  26979. obj = NULL;
  26980. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  26981. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  26982. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  26983. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  26984. XMEMSET(buf, 0, sizeof(buf));
  26985. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  26986. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  26987. ASN1_OBJECT_free(obj);
  26988. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  26989. return EXPECT_RESULT();
  26990. }
  26991. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  26992. {
  26993. EXPECT_DECLS;
  26994. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  26995. WOLFSSL_STACK* sk = NULL;
  26996. WOLFSSL_ASN1_OBJECT* obj;
  26997. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26998. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26999. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27000. sk = NULL;
  27001. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27002. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  27003. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  27004. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  27005. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27006. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  27007. sk = NULL;
  27008. /* obj freed in pop_free call. */
  27009. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27010. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27011. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27012. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  27013. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27014. wolfSSL_ASN1_OBJECT_free(obj);
  27015. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  27016. return EXPECT_RESULT();
  27017. }
  27018. static int test_wolfSSL_ASN1_STRING(void)
  27019. {
  27020. EXPECT_DECLS;
  27021. #if defined(OPENSSL_EXTRA)
  27022. ASN1_STRING* str = NULL;
  27023. ASN1_STRING* c = NULL;
  27024. const char data[] = "hello wolfSSL";
  27025. const char data2[] = "Same len data";
  27026. const char longData[] =
  27027. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  27028. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27029. ASN1_STRING_free(str);
  27030. str = NULL;
  27031. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27032. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  27033. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  27034. /* Check setting to NULL works. */
  27035. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  27036. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  27037. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27038. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  27039. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  27040. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  27041. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  27042. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  27043. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  27044. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  27045. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  27046. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  27047. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  27048. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27049. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  27050. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  27051. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  27052. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  27053. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27054. /* Check setting back to smaller size frees dynamic data. */
  27055. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27056. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  27057. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  27058. ExpectNull(ASN1_STRING_get0_data(NULL));
  27059. ExpectNotNull(ASN1_STRING_get0_data(str));
  27060. ExpectNull(ASN1_STRING_data(NULL));
  27061. ExpectNotNull(ASN1_STRING_data(str));
  27062. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  27063. ExpectIntGT(ASN1_STRING_length(str), 0);
  27064. ASN1_STRING_free(c);
  27065. ASN1_STRING_free(str);
  27066. ASN1_STRING_free(NULL);
  27067. #ifndef NO_WOLFSSL_STUB
  27068. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  27069. #endif
  27070. #endif
  27071. return EXPECT_RESULT();
  27072. }
  27073. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  27074. {
  27075. EXPECT_DECLS;
  27076. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  27077. !defined(NO_FILESYSTEM)
  27078. WOLFSSL_X509* x509 = NULL;
  27079. WOLFSSL_X509_NAME* subject = NULL;
  27080. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  27081. WOLFSSL_ASN1_STRING* a = NULL;
  27082. FILE* file = XBADFILE;
  27083. int idx = 0;
  27084. char targetOutput[16] = "www.wolfssl.com";
  27085. unsigned char* actual_output = NULL;
  27086. int len = 0;
  27087. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27088. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27089. if (file != NULL)
  27090. fclose(file);
  27091. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  27092. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  27093. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  27094. NID_commonName, -1)), 5);
  27095. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  27096. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  27097. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  27098. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  27099. a = NULL;
  27100. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  27101. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  27102. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  27103. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  27104. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  27105. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  27106. wolfSSL_X509_free(x509);
  27107. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27108. ExpectNotNull(a = ASN1_STRING_new());
  27109. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  27110. ASN1_STRING_free(a);
  27111. #endif
  27112. return EXPECT_RESULT();
  27113. }
  27114. static int test_wolfSSL_i2s_ASN1_STRING(void)
  27115. {
  27116. EXPECT_DECLS;
  27117. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27118. WOLFSSL_ASN1_STRING* str = NULL;
  27119. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  27120. char* ret = NULL;
  27121. ExpectNotNull(str = ASN1_STRING_new());
  27122. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  27123. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27124. ret = NULL;
  27125. /* No data. */
  27126. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27127. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27128. ret = NULL;
  27129. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  27130. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27131. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27132. ret = NULL;
  27133. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  27134. /* No type. */
  27135. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27136. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27137. ASN1_STRING_free(str);
  27138. #endif
  27139. return EXPECT_RESULT();
  27140. }
  27141. static int test_wolfSSL_ASN1_STRING_canon(void)
  27142. {
  27143. EXPECT_DECLS;
  27144. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  27145. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  27146. defined(OPENSSL_EXTRA_X509_SMALL))
  27147. WOLFSSL_ASN1_STRING* orig = NULL;
  27148. WOLFSSL_ASN1_STRING* canon = NULL;
  27149. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  27150. const char* whitespaceOnly = "\t\r\n";
  27151. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  27152. const char* canonData = "\x01 \x02 \xff tt";
  27153. const char longData[] =
  27154. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  27155. ExpectNotNull(orig = ASN1_STRING_new());
  27156. ExpectNotNull(canon = ASN1_STRING_new());
  27157. /* Invalid parameter testing. */
  27158. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  27159. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  27160. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  27161. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27162. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27163. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  27164. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27165. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27166. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  27167. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27168. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27169. ASN1_STRING_free(orig);
  27170. orig = NULL;
  27171. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  27172. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  27173. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27174. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  27175. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27176. ASN1_STRING_free(orig);
  27177. orig = NULL;
  27178. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  27179. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  27180. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27181. ASN1_STRING_free(orig);
  27182. orig = NULL;
  27183. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  27184. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27185. ASN1_STRING_free(orig);
  27186. ASN1_STRING_free(canon);
  27187. #endif
  27188. #endif
  27189. return EXPECT_RESULT();
  27190. }
  27191. static int test_wolfSSL_ASN1_STRING_print(void)
  27192. {
  27193. EXPECT_DECLS;
  27194. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  27195. !defined(NO_BIO)
  27196. ASN1_STRING* asnStr = NULL;
  27197. const char HELLO_DATA[]= \
  27198. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  27199. #define MAX_UNPRINTABLE_CHAR 32
  27200. #define MAX_BUF 255
  27201. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  27202. unsigned char expected[sizeof(unprintableData)+1];
  27203. unsigned char rbuf[MAX_BUF];
  27204. BIO *bio = NULL;
  27205. int p_len;
  27206. int i;
  27207. /* setup */
  27208. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  27209. unprintableData[i] = HELLO_DATA[i];
  27210. expected[i] = HELLO_DATA[i];
  27211. }
  27212. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  27213. unprintableData[sizeof(HELLO_DATA)+i] = i;
  27214. if (i == (int)'\n' || i == (int)'\r')
  27215. expected[sizeof(HELLO_DATA)+i] = i;
  27216. else
  27217. expected[sizeof(HELLO_DATA)+i] = '.';
  27218. }
  27219. unprintableData[sizeof(unprintableData)-1] = '\0';
  27220. expected[sizeof(expected)-1] = '\0';
  27221. XMEMSET(rbuf, 0, MAX_BUF);
  27222. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27223. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  27224. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27225. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  27226. (int)sizeof(unprintableData)), 1);
  27227. /* test */
  27228. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  27229. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  27230. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  27231. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  27232. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  27233. ExpectStrEQ((char*)rbuf, (const char*)expected);
  27234. BIO_free(bio);
  27235. bio = NULL;
  27236. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27237. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  27238. /* Ensure there is 0 bytes available to write into. */
  27239. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  27240. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27241. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  27242. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27243. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  27244. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27245. BIO_free(bio);
  27246. ASN1_STRING_free(asnStr);
  27247. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  27248. return EXPECT_RESULT();
  27249. }
  27250. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  27251. {
  27252. EXPECT_DECLS;
  27253. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27254. ASN1_STRING* asn_str = NULL;
  27255. const char data[] = "Hello wolfSSL!";
  27256. ASN1_STRING* esc_str = NULL;
  27257. const char esc_data[] = "a+;<>";
  27258. ASN1_STRING* neg_int = NULL;
  27259. const char neg_int_data[] = "\xff";
  27260. ASN1_STRING* neg_enum = NULL;
  27261. const char neg_enum_data[] = "\xff";
  27262. BIO *bio = NULL;
  27263. BIO *fixed = NULL;
  27264. unsigned long flags;
  27265. int p_len;
  27266. unsigned char rbuf[255];
  27267. /* setup */
  27268. XMEMSET(rbuf, 0, 255);
  27269. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27270. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  27271. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27272. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27273. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  27274. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27275. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  27276. sizeof(esc_data)), 1);
  27277. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  27278. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  27279. sizeof(neg_int_data) - 1), 1);
  27280. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  27281. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  27282. sizeof(neg_enum_data) - 1), 1);
  27283. /* Invalid parameter testing. */
  27284. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  27285. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  27286. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  27287. /* no flags */
  27288. XMEMSET(rbuf, 0, 255);
  27289. flags = 0;
  27290. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  27291. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  27292. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  27293. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27294. /* Ensure there is 0 bytes available to write into. */
  27295. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27296. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27297. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27298. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27299. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  27300. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27301. /* RFC2253 Escape */
  27302. XMEMSET(rbuf, 0, 255);
  27303. flags = ASN1_STRFLGS_ESC_2253;
  27304. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  27305. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  27306. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  27307. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27308. /* Ensure there is 0 bytes available to write into. */
  27309. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27310. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27311. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27312. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27313. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  27314. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27315. /* Show type */
  27316. XMEMSET(rbuf, 0, 255);
  27317. flags = ASN1_STRFLGS_SHOW_TYPE;
  27318. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  27319. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  27320. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  27321. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27322. /* Ensure there is 0 bytes available to write into. */
  27323. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27324. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27325. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27326. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27327. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  27328. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27329. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  27330. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27331. /* Dump All */
  27332. XMEMSET(rbuf, 0, 255);
  27333. flags = ASN1_STRFLGS_DUMP_ALL;
  27334. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  27335. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  27336. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  27337. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27338. /* Ensure there is 0 bytes available to write into. */
  27339. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27340. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27341. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27342. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27343. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  27344. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27345. /* Dump Der */
  27346. XMEMSET(rbuf, 0, 255);
  27347. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  27348. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  27349. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  27350. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  27351. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27352. /* Ensure there is 0 bytes available to write into. */
  27353. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27354. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27355. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27356. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27357. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  27358. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27359. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  27360. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27361. /* Dump All + Show type */
  27362. XMEMSET(rbuf, 0, 255);
  27363. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27364. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  27365. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  27366. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  27367. /* Dump All + Show type - Negative Integer. */
  27368. XMEMSET(rbuf, 0, 255);
  27369. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27370. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  27371. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  27372. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  27373. /* Dump All + Show type - Negative Enumerated. */
  27374. XMEMSET(rbuf, 0, 255);
  27375. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27376. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  27377. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  27378. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  27379. BIO_free(fixed);
  27380. BIO_free(bio);
  27381. ASN1_STRING_free(asn_str);
  27382. ASN1_STRING_free(esc_str);
  27383. ASN1_STRING_free(neg_int);
  27384. ASN1_STRING_free(neg_enum);
  27385. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  27386. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  27387. #endif
  27388. return EXPECT_RESULT();
  27389. }
  27390. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  27391. {
  27392. EXPECT_DECLS;
  27393. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  27394. ASN1_STRING* asn1str_test = NULL;
  27395. ASN1_STRING* asn1str_answer = NULL;
  27396. /* Each character is encoded using 4 bytes */
  27397. char input[] = {
  27398. 0, 0, 0, 'T',
  27399. 0, 0, 0, 'e',
  27400. 0, 0, 0, 's',
  27401. 0, 0, 0, 't',
  27402. };
  27403. char output[] = "Test";
  27404. char badInput[] = {
  27405. 1, 0, 0, 'T',
  27406. 0, 1, 0, 'e',
  27407. 0, 0, 1, 's',
  27408. };
  27409. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  27410. /* Test wrong type. */
  27411. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27412. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27413. ASN1_STRING_free(asn1str_test);
  27414. asn1str_test = NULL;
  27415. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  27416. /* Test bad length. */
  27417. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  27418. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27419. /* Test bad input. */
  27420. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  27421. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27422. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  27423. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27424. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  27425. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27426. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  27427. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  27428. ExpectNotNull(
  27429. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  27430. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  27431. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  27432. ASN1_STRING_free(asn1str_test);
  27433. ASN1_STRING_free(asn1str_answer);
  27434. #endif /* OPENSSL_ALL && !NO_ASN */
  27435. return EXPECT_RESULT();
  27436. }
  27437. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27438. {
  27439. EXPECT_DECLS;
  27440. #if defined(OPENSSL_EXTRA)
  27441. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  27442. unsigned char nullstr[32];
  27443. XMEMSET(nullstr, 0, 32);
  27444. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27445. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  27446. if (asn1_gtime != NULL) {
  27447. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27448. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27449. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  27450. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27451. }
  27452. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  27453. #endif /* OPENSSL_EXTRA */
  27454. return EXPECT_RESULT();
  27455. }
  27456. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  27457. {
  27458. EXPECT_DECLS;
  27459. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27460. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  27461. BIO* bio = NULL;
  27462. unsigned char buf[24];
  27463. int i;
  27464. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27465. BIO_set_write_buf_size(bio, 24);
  27466. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  27467. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  27468. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  27469. /* Type not set. */
  27470. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  27471. gtime.type = V_ASN1_GENERALIZEDTIME;
  27472. /* Invalid parameters testing. */
  27473. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  27474. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  27475. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  27476. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  27477. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  27478. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  27479. BIO_free(bio);
  27480. bio = NULL;
  27481. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27482. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  27483. /* Ensure there is 0 bytes available to write into. */
  27484. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  27485. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  27486. for (i = 1; i < 20; i++) {
  27487. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  27488. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  27489. }
  27490. BIO_free(bio);
  27491. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  27492. #endif /* OPENSSL_EXTRA */
  27493. return EXPECT_RESULT();
  27494. }
  27495. static int test_wolfSSL_ASN1_TIME(void)
  27496. {
  27497. EXPECT_DECLS;
  27498. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27499. WOLFSSL_ASN1_TIME* asn_time = NULL;
  27500. unsigned char *data;
  27501. ExpectNotNull(asn_time = ASN1_TIME_new());
  27502. #ifndef NO_WOLFSSL_STUB
  27503. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  27504. #endif
  27505. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  27506. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  27507. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  27508. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  27509. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  27510. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  27511. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  27512. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  27513. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  27514. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  27515. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  27516. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  27517. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  27518. ASN1_TIME_free(asn_time);
  27519. ASN1_TIME_free(NULL);
  27520. #endif
  27521. return EXPECT_RESULT();
  27522. }
  27523. static int test_wolfSSL_ASN1_TIME_to_string(void)
  27524. {
  27525. EXPECT_DECLS;
  27526. #ifndef NO_ASN_TIME
  27527. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27528. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  27529. WOLFSSL_ASN1_TIME* t = NULL;
  27530. char buf[ASN_GENERALIZED_TIME_SIZE];
  27531. ExpectNotNull((t = ASN1_TIME_new()));
  27532. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  27533. /* Invalid parameter testing. */
  27534. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  27535. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  27536. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  27537. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  27538. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  27539. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  27540. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  27541. /* Buffer needs to be longer than minimum of 5 characters. */
  27542. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  27543. ASN1_TIME_free(t);
  27544. #endif
  27545. #endif /* NO_ASN_TIME */
  27546. return EXPECT_RESULT();
  27547. }
  27548. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27549. {
  27550. EXPECT_DECLS;
  27551. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27552. ASN1_TIME* fromTime = NULL;
  27553. ASN1_TIME* closeToTime = NULL;
  27554. ASN1_TIME* toTime = NULL;
  27555. ASN1_TIME* invalidTime = NULL;
  27556. int daysDiff = 0;
  27557. int secsDiff = 0;
  27558. ExpectNotNull((fromTime = ASN1_TIME_new()));
  27559. /* Feb 22, 2003, 21:15:15 */
  27560. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  27561. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  27562. /* Feb 22, 2003, 21:16:15 */
  27563. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  27564. ExpectNotNull((toTime = ASN1_TIME_new()));
  27565. /* Dec 19, 2010, 18:10:11 */
  27566. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  27567. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  27568. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  27569. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  27570. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  27571. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  27572. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27573. /* Error conditions. */
  27574. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  27575. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  27576. /* If both times are NULL, difference is 0. */
  27577. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  27578. ExpectIntEQ(daysDiff, 0);
  27579. ExpectIntEQ(secsDiff, 0);
  27580. /* If one time is NULL, it defaults to the current time. */
  27581. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  27582. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  27583. /* Normal operation. Both times non-NULL. */
  27584. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27585. ExpectIntEQ(daysDiff, 2856);
  27586. ExpectIntEQ(secsDiff, 75296);
  27587. /* Swapping the times should return negative values. */
  27588. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  27589. ExpectIntEQ(daysDiff, -2856);
  27590. ExpectIntEQ(secsDiff, -75296);
  27591. /* Compare with invalid time string. */
  27592. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  27593. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  27594. /* Compare with days difference of 0. */
  27595. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  27596. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  27597. /* Days and seconds differences not 0. */
  27598. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27599. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27600. /* Same time. */
  27601. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27602. /* Compare regression test: No seconds difference, just difference in days.
  27603. */
  27604. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27605. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27606. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27607. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27608. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27609. /* Edge case with Unix epoch. */
  27610. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27611. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27612. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27613. ExpectIntEQ(daysDiff, 3652);
  27614. ExpectIntEQ(secsDiff, 0);
  27615. /* Edge case with year > 2038 (year 2038 problem). */
  27616. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27617. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27618. ExpectIntEQ(daysDiff, 2932896);
  27619. ExpectIntEQ(secsDiff, 86399);
  27620. ASN1_TIME_free(fromTime);
  27621. ASN1_TIME_free(closeToTime);
  27622. ASN1_TIME_free(toTime);
  27623. ASN1_TIME_free(invalidTime);
  27624. #endif
  27625. return EXPECT_RESULT();
  27626. }
  27627. static int test_wolfSSL_ASN1_TIME_adj(void)
  27628. {
  27629. EXPECT_DECLS;
  27630. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  27631. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  27632. const int year = 365*24*60*60;
  27633. const int day = 24*60*60;
  27634. const int hour = 60*60;
  27635. const int mini = 60;
  27636. const byte asn_utc_time = ASN_UTC_TIME;
  27637. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27638. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  27639. #endif
  27640. WOLFSSL_ASN1_TIME* asn_time = NULL;
  27641. WOLFSSL_ASN1_TIME* s = NULL;
  27642. int offset_day;
  27643. long offset_sec;
  27644. char date_str[CTC_DATE_SIZE + 1];
  27645. time_t t;
  27646. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  27647. /* UTC notation test */
  27648. /* 2000/2/15 20:30:00 */
  27649. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  27650. offset_day = 7;
  27651. offset_sec = 45 * mini;
  27652. /* offset_sec = -45 * min;*/
  27653. ExpectNotNull(asn_time =
  27654. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  27655. ExpectTrue(asn_time->type == asn_utc_time);
  27656. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27657. CTC_DATE_SIZE));
  27658. date_str[CTC_DATE_SIZE] = '\0';
  27659. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  27660. /* negative offset */
  27661. offset_sec = -45 * mini;
  27662. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  27663. ExpectNotNull(asn_time);
  27664. ExpectTrue(asn_time->type == asn_utc_time);
  27665. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27666. CTC_DATE_SIZE));
  27667. date_str[CTC_DATE_SIZE] = '\0';
  27668. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  27669. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27670. s = NULL;
  27671. XMEMSET(date_str, 0, sizeof(date_str));
  27672. /* Generalized time will overflow time_t if not long */
  27673. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27674. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  27675. DYNAMIC_TYPE_OPENSSL);
  27676. /* GeneralizedTime notation test */
  27677. /* 2055/03/01 09:00:00 */
  27678. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  27679. offset_day = 12;
  27680. offset_sec = 10 * mini;
  27681. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27682. offset_sec));
  27683. ExpectTrue(asn_time->type == asn_gen_time);
  27684. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27685. CTC_DATE_SIZE));
  27686. date_str[CTC_DATE_SIZE] = '\0';
  27687. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  27688. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27689. s = NULL;
  27690. XMEMSET(date_str, 0, sizeof(date_str));
  27691. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  27692. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  27693. s = NULL;
  27694. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  27695. offset_day = 7;
  27696. offset_sec = 45 * mini;
  27697. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27698. offset_sec));
  27699. ExpectTrue(asn_time->type == asn_utc_time);
  27700. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27701. CTC_DATE_SIZE));
  27702. date_str[CTC_DATE_SIZE] = '\0';
  27703. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27704. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27705. asn_time = NULL;
  27706. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  27707. offset_sec));
  27708. ExpectTrue(asn_time->type == asn_utc_time);
  27709. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27710. CTC_DATE_SIZE));
  27711. date_str[CTC_DATE_SIZE] = '\0';
  27712. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27713. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27714. #endif
  27715. return EXPECT_RESULT();
  27716. }
  27717. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  27718. {
  27719. EXPECT_DECLS;
  27720. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27721. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27722. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  27723. ASN1_TIME asnTime;
  27724. struct tm tm;
  27725. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  27726. XMEMSET(&tm, 0, sizeof(struct tm));
  27727. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27728. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  27729. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  27730. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27731. ExpectIntEQ(tm.tm_sec, 15);
  27732. ExpectIntEQ(tm.tm_min, 15);
  27733. ExpectIntEQ(tm.tm_hour, 21);
  27734. ExpectIntEQ(tm.tm_mday, 22);
  27735. ExpectIntEQ(tm.tm_mon, 1);
  27736. ExpectIntEQ(tm.tm_year, 100);
  27737. ExpectIntEQ(tm.tm_isdst, 0);
  27738. #ifdef XMKTIME
  27739. ExpectIntEQ(tm.tm_wday, 2);
  27740. ExpectIntEQ(tm.tm_yday, 52);
  27741. #endif
  27742. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  27743. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27744. ExpectIntEQ(tm.tm_year, 50);
  27745. /* Get current time. */
  27746. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  27747. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  27748. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27749. /* 0 length. */
  27750. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27751. /* No type. */
  27752. asnTime.length = 1;
  27753. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27754. /* Not UTCTIME length. */
  27755. asnTime.type = V_ASN1_UTCTIME;
  27756. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27757. /* Not GENERALIZEDTIME length. */
  27758. asnTime.type = V_ASN1_GENERALIZEDTIME;
  27759. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27760. /* Not Zulu timezone. */
  27761. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  27762. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27763. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  27764. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27765. #ifdef XMKTIME
  27766. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  27767. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27768. ExpectIntEQ(tm.tm_sec, 47);
  27769. ExpectIntEQ(tm.tm_min, 22);
  27770. ExpectIntEQ(tm.tm_hour, 21);
  27771. ExpectIntEQ(tm.tm_mday, 12);
  27772. ExpectIntEQ(tm.tm_mon, 4);
  27773. ExpectIntEQ(tm.tm_year, 123);
  27774. ExpectIntEQ(tm.tm_wday, 5);
  27775. ExpectIntEQ(tm.tm_yday, 131);
  27776. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  27777. fields are zeroed out as expected. */
  27778. ExpectIntEQ(tm.tm_isdst, 0);
  27779. #endif
  27780. #endif
  27781. return EXPECT_RESULT();
  27782. }
  27783. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  27784. {
  27785. EXPECT_DECLS;
  27786. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27787. WOLFSSL_ASN1_TIME *t = NULL;
  27788. WOLFSSL_ASN1_TIME *out = NULL;
  27789. WOLFSSL_ASN1_TIME *gtime = NULL;
  27790. int tlen = 0;
  27791. unsigned char *data = NULL;
  27792. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27793. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  27794. /* type not set. */
  27795. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27796. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27797. t = NULL;
  27798. /* UTC Time test */
  27799. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27800. if (t != NULL) {
  27801. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  27802. t->type = ASN_UTC_TIME;
  27803. t->length = ASN_UTC_TIME_SIZE;
  27804. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27805. }
  27806. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  27807. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  27808. out = NULL;
  27809. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27810. wolfSSL_ASN1_TIME_free(gtime);
  27811. gtime = NULL;
  27812. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  27813. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27814. ExpectPtrEq(gtime, out);
  27815. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27816. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27817. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27818. /* Generalized Time test */
  27819. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27820. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27821. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27822. if (t != NULL) {
  27823. t->type = ASN_GENERALIZED_TIME;
  27824. t->length = ASN_GENERALIZED_TIME_SIZE;
  27825. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  27826. }
  27827. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  27828. ASN_GENERALIZED_TIME_SIZE);
  27829. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  27830. "20050727123456Z");
  27831. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27832. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27833. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27834. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27835. /* UTC Time to Generalized Time 1900's test */
  27836. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27837. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27838. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27839. if (t != NULL) {
  27840. t->type = ASN_UTC_TIME;
  27841. t->length = ASN_UTC_TIME_SIZE;
  27842. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  27843. }
  27844. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27845. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27846. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27847. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  27848. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27849. /* Null parameter test */
  27850. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27851. gtime = NULL;
  27852. out = NULL;
  27853. if (t != NULL) {
  27854. t->type = ASN_UTC_TIME;
  27855. t->length = ASN_UTC_TIME_SIZE;
  27856. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27857. }
  27858. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  27859. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27860. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27861. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27862. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27863. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27864. #endif
  27865. return EXPECT_RESULT();
  27866. }
  27867. static int test_wolfSSL_ASN1_TIME_print(void)
  27868. {
  27869. EXPECT_DECLS;
  27870. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  27871. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27872. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27873. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  27874. !defined(NO_ASN_TIME)
  27875. BIO* bio = NULL;
  27876. BIO* fixed = NULL;
  27877. X509* x509 = NULL;
  27878. const unsigned char* der = client_cert_der_2048;
  27879. ASN1_TIME* notAfter = NULL;
  27880. ASN1_TIME* notBefore = NULL;
  27881. unsigned char buf[25];
  27882. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27883. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27884. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27885. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27886. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  27887. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  27888. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  27889. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  27890. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  27891. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27892. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  27893. /* Test BIO_write fails. */
  27894. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27895. /* Ensure there is 0 bytes available to write into. */
  27896. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  27897. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27898. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27899. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27900. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  27901. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27902. /* create a bad time and test results */
  27903. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  27904. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  27905. if (EXPECT_SUCCESS()) {
  27906. notAfter->data[8] = 0;
  27907. notAfter->data[3] = 0;
  27908. }
  27909. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  27910. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27911. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27912. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  27913. BIO_free(bio);
  27914. BIO_free(fixed);
  27915. X509_free(x509);
  27916. #endif
  27917. return EXPECT_RESULT();
  27918. }
  27919. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27920. {
  27921. EXPECT_DECLS;
  27922. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27923. BIO* bio = NULL;
  27924. ASN1_UTCTIME* utc = NULL;
  27925. unsigned char buf[25];
  27926. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27927. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27928. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  27929. /* Valid date */
  27930. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27931. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27932. DYNAMIC_TYPE_ASN1));
  27933. if (utc != NULL) {
  27934. utc->type = ASN_UTC_TIME;
  27935. utc->length = ASN_UTC_TIME_SIZE;
  27936. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27937. }
  27938. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  27939. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  27940. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  27941. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27942. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27943. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27944. XMEMSET(buf, 0, sizeof(buf));
  27945. BIO_free(bio);
  27946. bio = NULL;
  27947. /* Invalid format */
  27948. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27949. if (utc != NULL) {
  27950. utc->type = ASN_UTC_TIME;
  27951. utc->length = ASN_UTC_TIME_SIZE;
  27952. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27953. }
  27954. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27955. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27956. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27957. /* Invalid type */
  27958. if (utc != NULL) {
  27959. utc->type = ASN_GENERALIZED_TIME;
  27960. utc->length = ASN_GENERALIZED_TIME_SIZE;
  27961. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  27962. }
  27963. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27964. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27965. BIO_free(bio);
  27966. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27967. return EXPECT_RESULT();
  27968. }
  27969. static int test_wolfSSL_ASN1_TYPE(void)
  27970. {
  27971. EXPECT_DECLS;
  27972. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  27973. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  27974. WOLFSSL_ASN1_TYPE* t = NULL;
  27975. WOLFSSL_ASN1_OBJECT* obj = NULL;
  27976. #ifndef NO_ASN_TIME
  27977. WOLFSSL_ASN1_TIME* time = NULL;
  27978. #endif
  27979. WOLFSSL_ASN1_STRING* str = NULL;
  27980. unsigned char data[] = { 0x00 };
  27981. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  27982. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27983. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  27984. wolfSSL_ASN1_TYPE_free(t);
  27985. t = NULL;
  27986. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27987. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  27988. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  27989. wolfSSL_ASN1_TYPE_free(t);
  27990. t = NULL;
  27991. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27992. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27993. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  27994. wolfSSL_ASN1_TYPE_free(t);
  27995. t = NULL;
  27996. #ifndef NO_ASN_TIME
  27997. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27998. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27999. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  28000. wolfSSL_ASN1_TYPE_free(t);
  28001. t = NULL;
  28002. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28003. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28004. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  28005. wolfSSL_ASN1_TYPE_free(t);
  28006. t = NULL;
  28007. #endif
  28008. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28009. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28010. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  28011. wolfSSL_ASN1_TYPE_free(t);
  28012. t = NULL;
  28013. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28014. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28015. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  28016. wolfSSL_ASN1_TYPE_free(t);
  28017. t = NULL;
  28018. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28019. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28020. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  28021. wolfSSL_ASN1_TYPE_free(t);
  28022. t = NULL;
  28023. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28024. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28025. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  28026. wolfSSL_ASN1_TYPE_free(t);
  28027. t = NULL;
  28028. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28029. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28030. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  28031. wolfSSL_ASN1_TYPE_free(t);
  28032. t = NULL;
  28033. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28034. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28035. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  28036. wolfSSL_ASN1_TYPE_free(t);
  28037. t = NULL;
  28038. #endif
  28039. return EXPECT_RESULT();
  28040. }
  28041. /* Testing code used in dpp.c in hostap */
  28042. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28043. typedef struct {
  28044. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  28045. * as an OID identifying the curve */
  28046. X509_ALGOR *alg;
  28047. /* Compressed format public key per ANSI X9.63 */
  28048. ASN1_BIT_STRING *pub_key;
  28049. } DPP_BOOTSTRAPPING_KEY;
  28050. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  28051. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  28052. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  28053. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  28054. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  28055. typedef struct {
  28056. ASN1_INTEGER *integer;
  28057. } TEST_ASN1;
  28058. ASN1_SEQUENCE(TEST_ASN1) = {
  28059. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  28060. } ASN1_SEQUENCE_END(TEST_ASN1)
  28061. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  28062. typedef struct {
  28063. ASN1_OCTET_STRING *octet_string;
  28064. } TEST_FAIL_ASN1;
  28065. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  28066. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  28067. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  28068. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  28069. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  28070. #endif
  28071. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  28072. {
  28073. EXPECT_DECLS;
  28074. /* Testing code used in dpp.c in hostap */
  28075. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28076. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28077. EC_KEY *eckey = NULL;
  28078. EVP_PKEY *key = NULL;
  28079. size_t len = 0;
  28080. unsigned char *der = NULL;
  28081. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  28082. const unsigned char *in = ecc_clikey_der_256;
  28083. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  28084. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  28085. const EC_GROUP *group = NULL;
  28086. const EC_POINT *point = NULL;
  28087. int nid;
  28088. TEST_ASN1 *test_asn1 = NULL;
  28089. TEST_FAIL_ASN1 test_fail_asn1;
  28090. const unsigned char badObjDer[] = { 0x06, 0x00 };
  28091. const unsigned char goodObjDer[] = {
  28092. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  28093. };
  28094. WOLFSSL_ASN1_ITEM emptyTemplate;
  28095. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  28096. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  28097. der = NULL;
  28098. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  28099. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  28100. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28101. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  28102. (long)sizeof_ecc_clikey_der_256));
  28103. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  28104. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  28105. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  28106. nid = EC_GROUP_get_curve_name(group);
  28107. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  28108. group_obj = OBJ_nid2obj(nid);
  28109. if ((ec_obj != NULL) && (group_obj != NULL)) {
  28110. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  28111. group_obj), 1);
  28112. if (EXPECT_SUCCESS()) {
  28113. ec_obj = NULL;
  28114. group_obj = NULL;
  28115. }
  28116. }
  28117. wolfSSL_ASN1_OBJECT_free(group_obj);
  28118. wolfSSL_ASN1_OBJECT_free(ec_obj);
  28119. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  28120. #ifdef HAVE_COMP_KEY
  28121. ExpectIntGT((len = EC_POINT_point2oct(
  28122. group, point, POINT_CONVERSION_COMPRESSED,
  28123. NULL, 0, NULL)), 0);
  28124. #else
  28125. ExpectIntGT((len = EC_POINT_point2oct(
  28126. group, point, POINT_CONVERSION_UNCOMPRESSED,
  28127. NULL, 0, NULL)), 0);
  28128. #endif
  28129. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  28130. #ifdef HAVE_COMP_KEY
  28131. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  28132. der, len-1, NULL), 0);
  28133. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  28134. der, len, NULL), len);
  28135. #else
  28136. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  28137. der, len-1, NULL), 0);
  28138. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  28139. der, len, NULL), len);
  28140. #endif
  28141. if (EXPECT_SUCCESS()) {
  28142. bootstrap->pub_key->data = der;
  28143. bootstrap->pub_key->length = (int)len;
  28144. /* Not actually used */
  28145. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  28146. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  28147. }
  28148. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  28149. der = NULL;
  28150. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28151. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28152. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28153. EVP_PKEY_free(key);
  28154. EC_KEY_free(eckey);
  28155. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  28156. bootstrap = NULL;
  28157. DPP_BOOTSTRAPPING_KEY_free(NULL);
  28158. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  28159. * a NULL and an empty BIT_STRING. */
  28160. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  28161. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  28162. if (EXPECT_SUCCESS()) {
  28163. bootstrap->alg->algorithm->obj = badObjDer;
  28164. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  28165. }
  28166. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  28167. if (EXPECT_SUCCESS()) {
  28168. bootstrap->alg->parameter->type = V_ASN1_NULL;
  28169. bootstrap->alg->parameter->value.ptr = NULL;
  28170. bootstrap->pub_key->data = NULL;
  28171. bootstrap->pub_key->length = 0;
  28172. /* Not actually used */
  28173. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  28174. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  28175. }
  28176. /* Encode with bad OBJECT_ID. */
  28177. der = NULL;
  28178. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28179. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  28180. if (EXPECT_SUCCESS()) {
  28181. bootstrap->alg->algorithm->obj = goodObjDer;
  28182. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  28183. bootstrap->alg->algorithm->grp = 2;
  28184. }
  28185. der = NULL;
  28186. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  28187. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  28188. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28189. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  28190. /* Test integer */
  28191. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  28192. der = NULL;
  28193. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  28194. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  28195. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28196. TEST_ASN1_free(test_asn1);
  28197. /* Test integer cases. */
  28198. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  28199. TEST_ASN1_free(NULL);
  28200. /* Test error cases. */
  28201. ExpectNull(TEST_FAIL_ASN1_new());
  28202. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  28203. TEST_FAIL_ASN1_free(NULL);
  28204. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  28205. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  28206. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  28207. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  28208. return EXPECT_RESULT();
  28209. }
  28210. static int test_wolfSSL_lhash(void)
  28211. {
  28212. EXPECT_DECLS;
  28213. #ifdef OPENSSL_ALL
  28214. const char testStr[] = "Like a true nature's child\n"
  28215. "We were born\n"
  28216. "Born to be wild";
  28217. #ifdef NO_SHA
  28218. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  28219. #else
  28220. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  28221. #endif
  28222. #endif
  28223. return EXPECT_RESULT();
  28224. }
  28225. static int test_wolfSSL_X509_NAME(void)
  28226. {
  28227. EXPECT_DECLS;
  28228. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  28229. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28230. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  28231. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  28232. defined(OPENSSL_EXTRA))
  28233. X509* x509 = NULL;
  28234. const unsigned char* c = NULL;
  28235. unsigned char buf[4096];
  28236. int bytes = 0;
  28237. XFILE f = XBADFILE;
  28238. const X509_NAME* a = NULL;
  28239. const X509_NAME* b = NULL;
  28240. X509_NAME* d2i_name = NULL;
  28241. int sz = 0;
  28242. unsigned char* tmp = NULL;
  28243. char file[] = "./certs/ca-cert.der";
  28244. #ifndef OPENSSL_EXTRA_X509_SMALL
  28245. byte empty[] = { /* CN=empty emailAddress= */
  28246. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  28247. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  28248. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  28249. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  28250. 0x01, 0x16, 0x00
  28251. };
  28252. #endif
  28253. #ifndef OPENSSL_EXTRA_X509_SMALL
  28254. /* test compile of deprecated function, returns 0 */
  28255. ExpectIntEQ(CRYPTO_thread_id(), 0);
  28256. #endif
  28257. ExpectNotNull(a = X509_NAME_new());
  28258. X509_NAME_free((X509_NAME*)a);
  28259. a = NULL;
  28260. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  28261. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  28262. if (f != XBADFILE)
  28263. XFCLOSE(f);
  28264. c = buf;
  28265. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  28266. /* test cmp function */
  28267. ExpectNotNull(a = X509_get_issuer_name(x509));
  28268. ExpectNotNull(b = X509_get_subject_name(x509));
  28269. #ifndef OPENSSL_EXTRA_X509_SMALL
  28270. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  28271. #endif
  28272. tmp = buf;
  28273. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  28274. if (sz > 0 && tmp == buf) {
  28275. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  28276. __LINE__);
  28277. fprintf(stderr, " Expected pointer to be incremented\n");
  28278. abort();
  28279. }
  28280. #ifndef OPENSSL_EXTRA_X509_SMALL
  28281. tmp = buf;
  28282. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  28283. #endif
  28284. /* if output parameter is NULL, should still return required size. */
  28285. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  28286. /* retry but with the function creating a buffer */
  28287. tmp = NULL;
  28288. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  28289. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  28290. tmp = NULL;
  28291. #ifdef WOLFSSL_CERT_NAME_ALL
  28292. /* test for givenName and name */
  28293. {
  28294. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  28295. const byte gName[] = "test-given-name";
  28296. const byte name[] = "test-name";
  28297. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  28298. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  28299. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  28300. 1);
  28301. wolfSSL_X509_NAME_ENTRY_free(entry);
  28302. entry = NULL;
  28303. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  28304. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  28305. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  28306. 1);
  28307. wolfSSL_X509_NAME_ENTRY_free(entry);
  28308. tmp = NULL;
  28309. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  28310. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  28311. }
  28312. #endif
  28313. b = NULL;
  28314. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  28315. #ifndef OPENSSL_EXTRA_X509_SMALL
  28316. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  28317. #endif
  28318. X509_NAME_free((X509_NAME*)b);
  28319. X509_NAME_free(d2i_name);
  28320. d2i_name = NULL;
  28321. X509_free(x509);
  28322. #ifndef OPENSSL_EXTRA_X509_SMALL
  28323. /* test with an empty domain component */
  28324. tmp = empty;
  28325. sz = sizeof(empty);
  28326. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  28327. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  28328. /* size of empty emailAddress will be 0 */
  28329. tmp = buf;
  28330. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  28331. (char*)tmp, sizeof(buf)), 0);
  28332. /* should contain no organization name */
  28333. tmp = buf;
  28334. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  28335. (char*)tmp, sizeof(buf)), -1);
  28336. X509_NAME_free(d2i_name);
  28337. #endif
  28338. #endif
  28339. return EXPECT_RESULT();
  28340. }
  28341. static int test_wolfSSL_X509_NAME_hash(void)
  28342. {
  28343. EXPECT_DECLS;
  28344. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28345. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  28346. BIO* bio = NULL;
  28347. X509* x509 = NULL;
  28348. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  28349. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28350. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  28351. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  28352. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  28353. X509_free(x509);
  28354. BIO_free(bio);
  28355. #endif
  28356. return EXPECT_RESULT();
  28357. }
  28358. static int test_wolfSSL_X509_NAME_print_ex(void)
  28359. {
  28360. EXPECT_DECLS;
  28361. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  28362. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  28363. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  28364. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  28365. !defined(NO_BIO) && !defined(NO_RSA)
  28366. int memSz = 0;
  28367. byte* mem = NULL;
  28368. BIO* bio = NULL;
  28369. BIO* membio = NULL;
  28370. X509* x509 = NULL;
  28371. X509_NAME* name = NULL;
  28372. const char* expNormal = "C=US, CN=wolfssl.com";
  28373. const char* expReverse = "CN=wolfssl.com, C=US";
  28374. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  28375. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  28376. const char* expRFC5523 =
  28377. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  28378. /* Test with real cert (svrCertFile) first */
  28379. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  28380. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28381. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  28382. ExpectNotNull(name = X509_get_subject_name(x509));
  28383. /* Test without flags */
  28384. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28385. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28386. BIO_free(membio);
  28387. membio = NULL;
  28388. /* Test flag: XN_FLAG_RFC2253 */
  28389. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28390. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28391. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28392. BIO_free(membio);
  28393. membio = NULL;
  28394. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  28395. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28396. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28397. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28398. BIO_free(membio);
  28399. membio = NULL;
  28400. X509_free(x509);
  28401. BIO_free(bio);
  28402. name = NULL;
  28403. /* Test normal case without escaped characters */
  28404. {
  28405. /* Create name: "/C=US/CN=wolfssl.com" */
  28406. ExpectNotNull(name = X509_NAME_new());
  28407. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  28408. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  28409. WOLFSSL_SUCCESS);
  28410. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  28411. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  28412. WOLFSSL_SUCCESS);
  28413. /* Test without flags */
  28414. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28415. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28416. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28417. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  28418. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  28419. BIO_free(membio);
  28420. membio = NULL;
  28421. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  28422. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28423. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28424. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28425. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28426. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  28427. BIO_free(membio);
  28428. membio = NULL;
  28429. /* Test flags: XN_FLAG_DN_REV - reversed */
  28430. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28431. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28432. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28433. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28434. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  28435. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  28436. BIO_free(membio);
  28437. membio = NULL;
  28438. X509_NAME_free(name);
  28439. name = NULL;
  28440. }
  28441. /* Test RFC2253 characters are escaped with backslashes */
  28442. {
  28443. ExpectNotNull(name = X509_NAME_new());
  28444. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  28445. /* space at beginning and end, and: ,+"\ */
  28446. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  28447. WOLFSSL_SUCCESS);
  28448. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  28449. /* # at beginning, and: <>;*/
  28450. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  28451. WOLFSSL_SUCCESS);
  28452. /* Test without flags */
  28453. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28454. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28455. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28456. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  28457. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  28458. XSTRLEN(expNotEscaped)), 0);
  28459. BIO_free(membio);
  28460. membio = NULL;
  28461. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  28462. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28463. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28464. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28465. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28466. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  28467. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  28468. BIO_free(membio);
  28469. membio = NULL;
  28470. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  28471. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28472. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28473. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28474. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28475. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  28476. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  28477. XSTRLEN(expNotEscapedRev)), 0);
  28478. BIO_free(membio);
  28479. X509_NAME_free(name);
  28480. }
  28481. #endif
  28482. return EXPECT_RESULT();
  28483. }
  28484. #ifndef NO_BIO
  28485. static int test_wolfSSL_X509_INFO_multiple_info(void)
  28486. {
  28487. EXPECT_DECLS;
  28488. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  28489. STACK_OF(X509_INFO) *info_stack = NULL;
  28490. X509_INFO *info = NULL;
  28491. int len;
  28492. int i;
  28493. const char* files[] = {
  28494. cliCertFile,
  28495. cliKeyFile,
  28496. /* This needs to be the order as svrCertFile contains the
  28497. * intermediate cert as well. */
  28498. svrKeyFile,
  28499. svrCertFile,
  28500. NULL,
  28501. };
  28502. const char** curFile;
  28503. BIO *fileBIO = NULL;
  28504. BIO *concatBIO = NULL;
  28505. byte tmp[FOURK_BUF];
  28506. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  28507. * to group objects together. */
  28508. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  28509. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  28510. int fileLen = 0;
  28511. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  28512. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  28513. if (EXPECT_SUCCESS()) {
  28514. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  28515. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  28516. fileLen -= len;
  28517. if (EXPECT_FAIL())
  28518. break;
  28519. }
  28520. /* Make sure we read the entire file */
  28521. ExpectIntEQ(fileLen, 0);
  28522. }
  28523. BIO_free(fileBIO);
  28524. fileBIO = NULL;
  28525. }
  28526. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  28527. NULL));
  28528. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  28529. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  28530. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28531. ExpectNotNull(info->x509);
  28532. ExpectNull(info->crl);
  28533. if (i != 0) {
  28534. ExpectNotNull(info->x_pkey);
  28535. ExpectIntEQ(X509_check_private_key(info->x509,
  28536. info->x_pkey->dec_pkey), 1);
  28537. }
  28538. else {
  28539. ExpectNull(info->x_pkey);
  28540. }
  28541. }
  28542. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28543. BIO_free(concatBIO);
  28544. #endif
  28545. return EXPECT_RESULT();
  28546. }
  28547. #endif
  28548. #ifndef NO_BIO
  28549. static int test_wolfSSL_X509_INFO(void)
  28550. {
  28551. EXPECT_DECLS;
  28552. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  28553. STACK_OF(X509_INFO) *info_stack = NULL;
  28554. X509_INFO *info = NULL;
  28555. BIO *cert = NULL;
  28556. int i;
  28557. /* PEM in hex format to avoid null terminator */
  28558. byte data[] = {
  28559. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  28560. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  28561. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  28562. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  28563. 0x2d, 0x2d, 0x2d
  28564. };
  28565. /* PEM in hex format to avoid null terminator */
  28566. byte data2[] = {
  28567. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  28568. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  28569. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  28570. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  28571. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  28572. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  28573. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  28574. };
  28575. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28576. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28577. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  28578. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28579. ExpectNotNull(info->x509);
  28580. ExpectNull(info->crl);
  28581. ExpectNull(info->x_pkey);
  28582. }
  28583. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28584. info_stack = NULL;
  28585. BIO_free(cert);
  28586. cert = NULL;
  28587. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28588. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28589. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28590. info_stack = NULL;
  28591. BIO_free(cert);
  28592. cert = NULL;
  28593. /* This case should fail due to invalid input. */
  28594. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28595. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  28596. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28597. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28598. info_stack = NULL;
  28599. BIO_free(cert);
  28600. cert = NULL;
  28601. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28602. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  28603. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28604. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28605. BIO_free(cert);
  28606. #endif
  28607. return EXPECT_RESULT();
  28608. }
  28609. #endif
  28610. static int test_wolfSSL_X509_subject_name_hash(void)
  28611. {
  28612. EXPECT_DECLS;
  28613. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28614. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28615. X509* x509 = NULL;
  28616. X509_NAME* subjectName = NULL;
  28617. unsigned long ret1 = 0;
  28618. unsigned long ret2 = 0;
  28619. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28620. SSL_FILETYPE_PEM));
  28621. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  28622. /* These two
  28623. * - X509_subject_name_hash(x509)
  28624. * - X509_NAME_hash(X509_get_subject_name(x509))
  28625. * should give the same hash, if !defined(NO_SHA) is true. */
  28626. ret1 = X509_subject_name_hash(x509);
  28627. ExpectIntNE(ret1, 0);
  28628. #if !defined(NO_SHA)
  28629. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  28630. ExpectIntNE(ret2, 0);
  28631. ExpectIntEQ(ret1, ret2);
  28632. #else
  28633. (void) ret2;
  28634. #endif
  28635. X509_free(x509);
  28636. #endif
  28637. return EXPECT_RESULT();
  28638. }
  28639. static int test_wolfSSL_X509_issuer_name_hash(void)
  28640. {
  28641. EXPECT_DECLS;
  28642. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28643. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28644. X509* x509 = NULL;
  28645. X509_NAME* issuertName = NULL;
  28646. unsigned long ret1 = 0;
  28647. unsigned long ret2 = 0;
  28648. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28649. SSL_FILETYPE_PEM));
  28650. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  28651. /* These two
  28652. * - X509_issuer_name_hash(x509)
  28653. * - X509_NAME_hash(X509_get_issuer_name(x509))
  28654. * should give the same hash, if !defined(NO_SHA) is true. */
  28655. ret1 = X509_issuer_name_hash(x509);
  28656. ExpectIntNE(ret1, 0);
  28657. #if !defined(NO_SHA)
  28658. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  28659. ExpectIntNE(ret2, 0);
  28660. ExpectIntEQ(ret1, ret2);
  28661. #else
  28662. (void) ret2;
  28663. #endif
  28664. X509_free(x509);
  28665. #endif
  28666. return EXPECT_RESULT();
  28667. }
  28668. static int test_wolfSSL_X509_check_host(void)
  28669. {
  28670. EXPECT_DECLS;
  28671. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28672. && !defined(NO_SHA) && !defined(NO_RSA)
  28673. X509* x509 = NULL;
  28674. const char altName[] = "example.com";
  28675. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28676. SSL_FILETYPE_PEM));
  28677. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  28678. WOLFSSL_SUCCESS);
  28679. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  28680. WOLFSSL_FAILURE);
  28681. X509_free(x509);
  28682. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  28683. WOLFSSL_FAILURE);
  28684. #endif
  28685. return EXPECT_RESULT();
  28686. }
  28687. static int test_wolfSSL_X509_check_email(void)
  28688. {
  28689. EXPECT_DECLS;
  28690. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  28691. X509* x509 = NULL;
  28692. const char goodEmail[] = "info@wolfssl.com";
  28693. const char badEmail[] = "disinfo@wolfssl.com";
  28694. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28695. SSL_FILETYPE_PEM));
  28696. /* Should fail on non-matching email address */
  28697. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  28698. WOLFSSL_FAILURE);
  28699. /* Should succeed on matching email address */
  28700. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  28701. WOLFSSL_SUCCESS);
  28702. /* Should compute length internally when not provided */
  28703. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  28704. WOLFSSL_SUCCESS);
  28705. /* Should fail when email address is NULL */
  28706. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  28707. WOLFSSL_FAILURE);
  28708. X509_free(x509);
  28709. /* Should fail when x509 is NULL */
  28710. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  28711. WOLFSSL_FAILURE);
  28712. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  28713. return EXPECT_RESULT();
  28714. }
  28715. static int test_wc_PemToDer(void)
  28716. {
  28717. EXPECT_DECLS;
  28718. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28719. int ret;
  28720. DerBuffer* pDer = NULL;
  28721. const char* ca_cert = "./certs/server-cert.pem";
  28722. byte* cert_buf = NULL;
  28723. size_t cert_sz = 0;
  28724. int eccKey = 0;
  28725. EncryptedInfo info;
  28726. XMEMSET(&info, 0, sizeof(info));
  28727. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28728. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  28729. &info, &eccKey), 0);
  28730. wc_FreeDer(&pDer);
  28731. pDer = NULL;
  28732. if (cert_buf != NULL) {
  28733. free(cert_buf);
  28734. cert_buf = NULL;
  28735. }
  28736. #ifdef HAVE_ECC
  28737. {
  28738. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  28739. byte key_buf[256] = {0};
  28740. /* Test fail of loading a key with cert type */
  28741. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  28742. key_buf[0] = '\n';
  28743. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  28744. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  28745. &pDer, NULL, &info, &eccKey)), 0);
  28746. #ifdef OPENSSL_EXTRA
  28747. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  28748. &pDer, NULL, &info, &eccKey)), 0);
  28749. #endif
  28750. wc_FreeDer(&pDer);
  28751. if (cert_buf != NULL)
  28752. free(cert_buf);
  28753. }
  28754. #endif
  28755. #endif
  28756. return EXPECT_RESULT();
  28757. }
  28758. static int test_wc_AllocDer(void)
  28759. {
  28760. EXPECT_DECLS;
  28761. #if !defined(NO_CERTS)
  28762. DerBuffer* pDer = NULL;
  28763. word32 testSize = 1024;
  28764. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  28765. BAD_FUNC_ARG);
  28766. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  28767. ExpectNotNull(pDer);
  28768. wc_FreeDer(&pDer);
  28769. #endif
  28770. return EXPECT_RESULT();
  28771. }
  28772. static int test_wc_CertPemToDer(void)
  28773. {
  28774. EXPECT_DECLS;
  28775. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28776. const char* ca_cert = "./certs/ca-cert.pem";
  28777. byte* cert_buf = NULL;
  28778. size_t cert_sz = 0;
  28779. size_t cert_dersz = 0;
  28780. byte* cert_der = NULL;
  28781. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28782. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28783. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28784. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28785. (int)cert_dersz, CERT_TYPE), 0);
  28786. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  28787. BAD_FUNC_ARG);
  28788. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  28789. BAD_FUNC_ARG);
  28790. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  28791. BAD_FUNC_ARG);
  28792. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  28793. CERT_TYPE), BAD_FUNC_ARG);
  28794. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  28795. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28796. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  28797. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28798. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  28799. CERT_TYPE), BAD_FUNC_ARG);
  28800. if (cert_der != NULL)
  28801. free(cert_der);
  28802. if (cert_buf != NULL)
  28803. free(cert_buf);
  28804. #endif
  28805. return EXPECT_RESULT();
  28806. }
  28807. static int test_wc_KeyPemToDer(void)
  28808. {
  28809. EXPECT_DECLS;
  28810. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28811. int ret = 0;
  28812. const byte cert_buf[] = \
  28813. "-----BEGIN PRIVATE KEY-----\n"
  28814. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  28815. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  28816. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  28817. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  28818. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  28819. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  28820. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  28821. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  28822. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  28823. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  28824. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  28825. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  28826. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  28827. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  28828. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  28829. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  28830. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  28831. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  28832. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  28833. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  28834. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  28835. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  28836. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  28837. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  28838. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  28839. "AQue5o6oVfhgLiJzMdo/77gw\n"
  28840. "-----END PRIVATE KEY-----\n";
  28841. const int cert_sz = sizeof(cert_buf);
  28842. const char cert_pw[] = "password";
  28843. int cert_dersz = 0;
  28844. byte* cert_der = NULL;
  28845. /* Bad arg: Cert buffer is NULL */
  28846. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  28847. BAD_FUNC_ARG);
  28848. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  28849. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  28850. BAD_FUNC_ARG);
  28851. /* Test normal operation */
  28852. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28853. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28854. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28855. cert_pw), 0);
  28856. ExpectIntLE(ret, cert_sz);
  28857. if (cert_der != NULL) {
  28858. free(cert_der);
  28859. cert_der = NULL;
  28860. }
  28861. /* Test NULL for DER buffer to return needed DER buffer size */
  28862. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  28863. ExpectIntLE(ret, cert_sz);
  28864. if (EXPECT_SUCCESS())
  28865. cert_dersz = ret;
  28866. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28867. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28868. cert_pw), 0);
  28869. ExpectIntLE(ret, cert_sz);
  28870. if (cert_der != NULL)
  28871. free(cert_der);
  28872. #endif
  28873. return EXPECT_RESULT();
  28874. }
  28875. static int test_wc_PubKeyPemToDer(void)
  28876. {
  28877. EXPECT_DECLS;
  28878. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  28879. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28880. int ret = 0;
  28881. const char* key = "./certs/ecc-client-keyPub.pem";
  28882. byte* cert_buf = NULL;
  28883. size_t cert_sz = 0, cert_dersz = 0;
  28884. byte* cert_der = NULL;
  28885. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  28886. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  28887. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  28888. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28889. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28890. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28891. (int)cert_dersz), 0);
  28892. if (cert_der != NULL) {
  28893. free(cert_der);
  28894. cert_der = NULL;
  28895. }
  28896. /* Test NULL for DER buffer to return needed DER buffer size */
  28897. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  28898. ExpectIntLE(ret, cert_sz);
  28899. cert_dersz = ret;
  28900. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28901. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28902. (int)cert_dersz), 0);
  28903. if (cert_der != NULL) {
  28904. free(cert_der);
  28905. }
  28906. if (cert_buf != NULL) {
  28907. free(cert_buf);
  28908. }
  28909. #endif
  28910. return EXPECT_RESULT();
  28911. }
  28912. static int test_wc_PemPubKeyToDer(void)
  28913. {
  28914. EXPECT_DECLS;
  28915. #if !defined(NO_FILESYSTEM) && \
  28916. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28917. const char* key = "./certs/ecc-client-keyPub.pem";
  28918. size_t cert_dersz = 1024;
  28919. byte* cert_der = NULL;
  28920. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  28921. BAD_FUNC_ARG);
  28922. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28923. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  28924. if (cert_der != NULL) {
  28925. free(cert_der);
  28926. }
  28927. #endif
  28928. return EXPECT_RESULT();
  28929. }
  28930. static int test_wc_GetPubKeyDerFromCert(void)
  28931. {
  28932. EXPECT_DECLS;
  28933. #if !defined(NO_RSA) || defined(HAVE_ECC)
  28934. int ret;
  28935. word32 idx = 0;
  28936. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28937. word32 keyDerSz = (word32)sizeof(keyDer);
  28938. DecodedCert decoded;
  28939. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28940. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  28941. word32 certBufSz = sizeof(certBuf);
  28942. #endif
  28943. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  28944. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28945. XFILE fp = XBADFILE;
  28946. #endif
  28947. #ifndef NO_RSA
  28948. RsaKey rsaKey;
  28949. #if defined(USE_CERT_BUFFERS_2048)
  28950. byte* rsaCertDer = (byte*)client_cert_der_2048;
  28951. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  28952. #elif defined(USE_CERT_BUFFERS_1024)
  28953. byte* rsaCertDer = (byte*)client_cert_der_1024;
  28954. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  28955. #else
  28956. unsigned char rsaCertDer[TWOK_BUF];
  28957. word32 rsaCertDerSz;
  28958. #endif
  28959. #endif
  28960. #ifdef HAVE_ECC
  28961. ecc_key eccKey;
  28962. #if defined(USE_CERT_BUFFERS_256)
  28963. byte* eccCert = (byte*)cliecc_cert_der_256;
  28964. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  28965. #else
  28966. unsigned char eccCert[ONEK_BUF];
  28967. word32 eccCertSz;
  28968. XFILE fp2 = XBADFILE;
  28969. #endif
  28970. #endif
  28971. #ifndef NO_RSA
  28972. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  28973. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  28974. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  28975. fp), 0);
  28976. if (fp != XBADFILE) {
  28977. XFCLOSE(fp);
  28978. fp = XBADFILE;
  28979. }
  28980. #endif
  28981. /* good test case - RSA DER cert */
  28982. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  28983. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28984. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28985. ExpectIntGT(keyDerSz, 0);
  28986. /* sanity check, verify we can import DER public key */
  28987. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28988. ExpectIntEQ(ret, 0);
  28989. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28990. if (ret == 0) {
  28991. wc_FreeRsaKey(&rsaKey);
  28992. }
  28993. /* test LENGTH_ONLY_E case */
  28994. keyDerSz = 0;
  28995. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28996. LENGTH_ONLY_E);
  28997. ExpectIntGT(keyDerSz, 0);
  28998. /* bad args: DecodedCert NULL */
  28999. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  29000. /* bad args: output key buff size */
  29001. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  29002. /* bad args: zero size output key buffer */
  29003. keyDerSz = 0;
  29004. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29005. BAD_FUNC_ARG);
  29006. wc_FreeDecodedCert(&decoded);
  29007. /* Certificate Request Tests */
  29008. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29009. {
  29010. XMEMSET(certBuf, 0, sizeof(certBuf));
  29011. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  29012. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  29013. if (fp != XBADFILE) {
  29014. XFCLOSE(fp);
  29015. }
  29016. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  29017. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  29018. /* good test case - RSA DER certificate request */
  29019. keyDerSz = sizeof(keyDer);
  29020. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29021. 0);
  29022. ExpectIntGT(keyDerSz, 0);
  29023. /* sanity check, verify we can import DER public key */
  29024. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29025. ExpectIntEQ(ret, 0);
  29026. idx = 0;
  29027. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29028. if (ret == 0) {
  29029. wc_FreeRsaKey(&rsaKey);
  29030. }
  29031. wc_FreeDecodedCert(&decoded);
  29032. }
  29033. #endif /* WOLFSSL_CERT_REQ */
  29034. #endif /* NO_RSA */
  29035. #ifdef HAVE_ECC
  29036. #ifndef USE_CERT_BUFFERS_256
  29037. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  29038. XBADFILE);
  29039. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  29040. if (fp2 != XBADFILE) {
  29041. XFCLOSE(fp2);
  29042. }
  29043. #endif
  29044. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  29045. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29046. /* good test case - ECC */
  29047. XMEMSET(keyDer, 0, sizeof(keyDer));
  29048. keyDerSz = sizeof(keyDer);
  29049. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29050. ExpectIntGT(keyDerSz, 0);
  29051. /* sanity check, verify we can import DER public key */
  29052. ret = wc_ecc_init(&eccKey);
  29053. ExpectIntEQ(ret, 0);
  29054. idx = 0; /* reset idx to 0, used above in RSA case */
  29055. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  29056. if (ret == 0) {
  29057. wc_ecc_free(&eccKey);
  29058. }
  29059. /* test LENGTH_ONLY_E case */
  29060. keyDerSz = 0;
  29061. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29062. LENGTH_ONLY_E);
  29063. ExpectIntGT(keyDerSz, 0);
  29064. wc_FreeDecodedCert(&decoded);
  29065. #endif
  29066. #endif /* !NO_RSA || HAVE_ECC */
  29067. return EXPECT_RESULT();
  29068. }
  29069. static int test_wc_CheckCertSigPubKey(void)
  29070. {
  29071. EXPECT_DECLS;
  29072. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29073. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  29074. int ret = 0;
  29075. const char* ca_cert = "./certs/ca-cert.pem";
  29076. byte* cert_buf = NULL;
  29077. size_t cert_sz = 0;
  29078. byte* cert_der = NULL;
  29079. word32 cert_dersz = 0;
  29080. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29081. word32 keyDerSz = (word32)sizeof(keyDer);
  29082. DecodedCert decoded;
  29083. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29084. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  29085. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29086. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29087. (int)cert_dersz, CERT_TYPE), 0);
  29088. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  29089. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29090. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29091. ExpectIntGT(keyDerSz, 0);
  29092. /* Good test case. */
  29093. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  29094. keyDerSz, RSAk), 0);
  29095. /* No certificate. */
  29096. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  29097. ECDSAk), BAD_FUNC_ARG);
  29098. /* Bad cert size. */
  29099. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  29100. RSAk), 0);
  29101. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  29102. /* No public key. */
  29103. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  29104. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  29105. /* Bad public key size. */
  29106. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  29107. RSAk), BAD_FUNC_ARG);
  29108. /* Wrong aglo. */
  29109. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  29110. keyDerSz, ECDSAk), ASN_PARSE_E);
  29111. wc_FreeDecodedCert(&decoded);
  29112. if (cert_der != NULL)
  29113. free(cert_der);
  29114. if (cert_buf != NULL)
  29115. free(cert_buf);
  29116. #endif
  29117. return EXPECT_RESULT();
  29118. }
  29119. static int test_wolfSSL_certs(void)
  29120. {
  29121. EXPECT_DECLS;
  29122. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29123. !defined(NO_RSA)
  29124. X509* x509ext = NULL;
  29125. #ifdef OPENSSL_ALL
  29126. X509* x509 = NULL;
  29127. WOLFSSL_X509_EXTENSION* ext = NULL;
  29128. ASN1_OBJECT* obj = NULL;
  29129. #endif
  29130. WOLFSSL* ssl = NULL;
  29131. WOLFSSL_CTX* ctx = NULL;
  29132. STACK_OF(ASN1_OBJECT)* sk = NULL;
  29133. ASN1_STRING* asn1_str = NULL;
  29134. AUTHORITY_KEYID* akey = NULL;
  29135. BASIC_CONSTRAINTS* bc = NULL;
  29136. int crit = 0;
  29137. #ifndef NO_WOLFSSL_SERVER
  29138. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  29139. #else
  29140. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  29141. #endif
  29142. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29143. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29144. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29145. #if !defined(NO_CHECK_PRIVATE_KEY)
  29146. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  29147. #endif
  29148. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29149. #if !defined(NO_CHECK_PRIVATE_KEY)
  29150. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  29151. #endif
  29152. ExpectNotNull(ssl = SSL_new(ctx));
  29153. #if !defined(NO_CHECK_PRIVATE_KEY)
  29154. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29155. #endif
  29156. #ifdef HAVE_PK_CALLBACKS
  29157. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  29158. #endif /* HAVE_PK_CALLBACKS */
  29159. /* create and use x509 */
  29160. #ifdef OPENSSL_ALL
  29161. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29162. WOLFSSL_FILETYPE_PEM));
  29163. #endif
  29164. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  29165. WOLFSSL_FILETYPE_PEM));
  29166. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  29167. #if !defined(NO_CHECK_PRIVATE_KEY)
  29168. /* with loading in a new cert the check on private key should now fail */
  29169. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29170. #endif
  29171. #if defined(USE_CERT_BUFFERS_2048)
  29172. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  29173. (unsigned char*)server_cert_der_2048,
  29174. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  29175. #endif
  29176. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  29177. /************* Get Digest of Certificate ******************/
  29178. {
  29179. byte digest[64]; /* max digest size */
  29180. word32 digestSz;
  29181. XMEMSET(digest, 0, sizeof(digest));
  29182. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  29183. WOLFSSL_SUCCESS);
  29184. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  29185. WOLFSSL_SUCCESS);
  29186. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  29187. WOLFSSL_FAILURE);
  29188. }
  29189. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  29190. /* test and checkout X509 extensions */
  29191. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  29192. NID_basic_constraints, &crit, NULL));
  29193. ExpectIntEQ(crit, 0);
  29194. #ifdef OPENSSL_ALL
  29195. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  29196. X509_EXTENSION_free(ext);
  29197. ext = NULL;
  29198. ExpectNotNull(ext = X509_EXTENSION_new());
  29199. X509_EXTENSION_set_critical(ext, 1);
  29200. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  29201. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  29202. ASN1_OBJECT_free(obj);
  29203. obj = NULL;
  29204. X509_EXTENSION_free(ext);
  29205. ext = NULL;
  29206. ExpectNotNull(ext = X509_EXTENSION_new());
  29207. X509_EXTENSION_set_critical(ext, 0);
  29208. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  29209. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  29210. NULL);
  29211. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  29212. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  29213. * and X509_get_ext_d2i has created new */
  29214. asn1_str = NULL;
  29215. X509_EXTENSION_free(ext);
  29216. ext = NULL;
  29217. #endif
  29218. BASIC_CONSTRAINTS_free(bc);
  29219. bc = NULL;
  29220. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  29221. NID_key_usage, &crit, NULL));
  29222. ExpectIntEQ(crit, 1);
  29223. ExpectIntEQ(asn1_str->type, NID_key_usage);
  29224. #ifdef OPENSSL_ALL
  29225. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  29226. X509_EXTENSION_free(ext);
  29227. ext = NULL;
  29228. #endif
  29229. ASN1_STRING_free(asn1_str);
  29230. asn1_str = NULL;
  29231. #ifdef OPENSSL_ALL
  29232. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  29233. NID_ext_key_usage, &crit, NULL));
  29234. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  29235. X509_EXTENSION_free(ext);
  29236. ext = NULL;
  29237. EXTENDED_KEY_USAGE_free(sk);
  29238. sk = NULL;
  29239. #else
  29240. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  29241. &crit, NULL);
  29242. ExpectNull(sk);
  29243. #endif
  29244. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  29245. NID_authority_key_identifier, &crit, NULL));
  29246. #ifdef OPENSSL_ALL
  29247. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  29248. akey));
  29249. X509_EXTENSION_free(ext);
  29250. ext = NULL;
  29251. #endif
  29252. wolfSSL_AUTHORITY_KEYID_free(akey);
  29253. akey = NULL;
  29254. /* NID not yet supported */
  29255. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29256. NID_private_key_usage_period, &crit, NULL));
  29257. ExpectIntEQ(crit, -1);
  29258. sk_ASN1_OBJECT_free(sk);
  29259. sk = NULL;
  29260. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  29261. NID_subject_alt_name, &crit, NULL));
  29262. {
  29263. int i;
  29264. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  29265. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  29266. ExpectIntEQ(gen->type, GEN_DNS);
  29267. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  29268. }
  29269. }
  29270. sk_GENERAL_NAME_free(sk);
  29271. sk = NULL;
  29272. /* NID not yet supported */
  29273. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29274. NID_issuer_alt_name, &crit, NULL));
  29275. ExpectIntEQ(crit, -1);
  29276. sk_ASN1_OBJECT_free(sk);
  29277. sk = NULL;
  29278. /* NID not yet supported */
  29279. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29280. NID_info_access, &crit, NULL));
  29281. sk_ASN1_OBJECT_free(sk);
  29282. sk = NULL;
  29283. /* NID not yet supported */
  29284. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29285. NID_sinfo_access, &crit, NULL));
  29286. ExpectIntEQ(crit, -1);
  29287. sk_ASN1_OBJECT_free(sk);
  29288. sk = NULL;
  29289. /* NID not yet supported */
  29290. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29291. NID_name_constraints, &crit, NULL));
  29292. ExpectIntEQ(crit, -1);
  29293. sk_ASN1_OBJECT_free(sk);
  29294. sk = NULL;
  29295. /* no cert policy set */
  29296. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29297. NID_certificate_policies, &crit, NULL));
  29298. sk_ASN1_OBJECT_free(sk);
  29299. sk = NULL;
  29300. /* NID not yet supported */
  29301. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29302. NID_policy_mappings, &crit, NULL));
  29303. ExpectIntEQ(crit, -1);
  29304. sk_ASN1_OBJECT_free(sk);
  29305. sk = NULL;
  29306. /* NID not yet supported */
  29307. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29308. NID_policy_constraints, &crit, NULL));
  29309. ExpectIntEQ(crit, -1);
  29310. sk_ASN1_OBJECT_free(sk);
  29311. sk = NULL;
  29312. /* NID not yet supported */
  29313. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29314. NID_inhibit_any_policy, &crit, NULL));
  29315. ExpectIntEQ(crit, -1);
  29316. sk_ASN1_OBJECT_free(sk);
  29317. sk = NULL;
  29318. /* NID not yet supported */
  29319. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29320. NID_tlsfeature, &crit, NULL));
  29321. ExpectIntEQ(crit, -1);
  29322. sk_ASN1_OBJECT_free(sk);
  29323. sk = NULL;
  29324. /* test invalid cases */
  29325. crit = 0;
  29326. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  29327. NULL));
  29328. ExpectIntEQ(crit, -1);
  29329. /* NULL passed for criticality. */
  29330. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  29331. NID_tlsfeature, NULL, NULL));
  29332. ExpectIntEQ(SSL_get_hit(ssl), 0);
  29333. #ifdef OPENSSL_ALL
  29334. X509_free(x509);
  29335. #endif
  29336. X509_free(x509ext);
  29337. SSL_free(ssl);
  29338. SSL_CTX_free(ctx);
  29339. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  29340. return EXPECT_RESULT();
  29341. }
  29342. static int test_wolfSSL_X509_check_private_key(void)
  29343. {
  29344. EXPECT_DECLS;
  29345. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  29346. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  29347. X509* x509 = NULL;
  29348. EVP_PKEY* pkey = NULL;
  29349. const byte* key;
  29350. /* Check with correct key */
  29351. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  29352. SSL_FILETYPE_PEM)));
  29353. key = client_key_der_2048;
  29354. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  29355. (long)sizeof_client_key_der_2048));
  29356. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  29357. EVP_PKEY_free(pkey);
  29358. pkey = NULL;
  29359. /* Check with wrong key */
  29360. key = server_key_der_2048;
  29361. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  29362. (long)sizeof_server_key_der_2048));
  29363. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  29364. /* test for incorrect parameter */
  29365. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  29366. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  29367. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  29368. EVP_PKEY_free(pkey);
  29369. X509_free(x509);
  29370. #endif
  29371. return EXPECT_RESULT();
  29372. }
  29373. static int test_wolfSSL_private_keys(void)
  29374. {
  29375. EXPECT_DECLS;
  29376. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29377. !defined(NO_FILESYSTEM)
  29378. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29379. WOLFSSL* ssl = NULL;
  29380. WOLFSSL_CTX* ctx = NULL;
  29381. EVP_PKEY* pkey = NULL;
  29382. OpenSSL_add_all_digests();
  29383. OpenSSL_add_all_algorithms();
  29384. #ifndef NO_RSA
  29385. #ifndef NO_WOLFSSL_SERVER
  29386. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29387. #else
  29388. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29389. #endif
  29390. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  29391. /* Have to load a cert before you can check the private key against that
  29392. * certificates public key! */
  29393. #if !defined(NO_CHECK_PRIVATE_KEY)
  29394. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  29395. #endif
  29396. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  29397. #if !defined(NO_CHECK_PRIVATE_KEY)
  29398. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29399. #endif
  29400. ExpectNotNull(ssl = SSL_new(ctx));
  29401. #if !defined(NO_CHECK_PRIVATE_KEY)
  29402. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29403. #endif
  29404. #ifdef USE_CERT_BUFFERS_2048
  29405. {
  29406. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29407. unsigned char buf[FOURK_BUF];
  29408. word32 bufSz;
  29409. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  29410. (unsigned char*)client_key_der_2048,
  29411. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  29412. #if !defined(NO_CHECK_PRIVATE_KEY)
  29413. /* Should mismatch now that a different private key loaded */
  29414. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29415. #endif
  29416. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  29417. (unsigned char*)server_key,
  29418. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  29419. #if !defined(NO_CHECK_PRIVATE_KEY)
  29420. /* After loading back in DER format of original key, should match */
  29421. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29422. #endif
  29423. /* test loading private key to the WOLFSSL_CTX */
  29424. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  29425. (unsigned char*)client_key_der_2048,
  29426. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  29427. #if !defined(NO_CHECK_PRIVATE_KEY)
  29428. /* Should mismatch now that a different private key loaded */
  29429. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29430. #endif
  29431. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  29432. (unsigned char*)server_key,
  29433. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  29434. #if !defined(NO_CHECK_PRIVATE_KEY)
  29435. /* After loading back in DER format of original key, should match */
  29436. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29437. #endif
  29438. /* pkey not set yet, expecting to fail */
  29439. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  29440. /* set PKEY and test again */
  29441. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29442. &server_key, (long)sizeof_server_key_der_2048));
  29443. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  29444. /* reuse PKEY structure and test
  29445. * this should be checked with a memory management sanity checker */
  29446. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  29447. server_key = (const unsigned char*)server_key_der_2048;
  29448. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29449. &server_key, (long)sizeof_server_key_der_2048));
  29450. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  29451. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  29452. bufSz = FOURK_BUF;
  29453. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  29454. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  29455. RSAk, NULL, 0)), 0);
  29456. server_key = (const unsigned char*)buf;
  29457. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  29458. (long)bufSz));
  29459. }
  29460. #endif
  29461. EVP_PKEY_free(pkey);
  29462. pkey = NULL;
  29463. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  29464. ssl = NULL;
  29465. SSL_CTX_free(ctx);
  29466. ctx = NULL;
  29467. #endif /* end of RSA private key match tests */
  29468. #ifdef HAVE_ECC
  29469. #ifndef NO_WOLFSSL_SERVER
  29470. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29471. #else
  29472. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29473. #endif
  29474. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  29475. WOLFSSL_FILETYPE_PEM));
  29476. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  29477. WOLFSSL_FILETYPE_PEM));
  29478. ExpectNotNull(ssl = SSL_new(ctx));
  29479. #if !defined(NO_CHECK_PRIVATE_KEY)
  29480. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29481. #endif
  29482. SSL_free(ssl);
  29483. ssl = NULL;
  29484. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  29485. WOLFSSL_FILETYPE_PEM));
  29486. ExpectNotNull(ssl = SSL_new(ctx));
  29487. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  29488. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29489. #endif
  29490. SSL_free(ssl);
  29491. ssl = NULL;
  29492. SSL_CTX_free(ctx);
  29493. ctx = NULL;
  29494. #endif /* end of ECC private key match tests */
  29495. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  29496. #ifndef NO_WOLFSSL_SERVER
  29497. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29498. #else
  29499. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29500. #endif
  29501. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  29502. WOLFSSL_FILETYPE_PEM));
  29503. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  29504. WOLFSSL_FILETYPE_PEM));
  29505. ExpectNotNull(ssl = SSL_new(ctx));
  29506. #if !defined(NO_CHECK_PRIVATE_KEY)
  29507. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29508. #endif
  29509. SSL_free(ssl);
  29510. ssl = NULL;
  29511. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  29512. WOLFSSL_FILETYPE_PEM));
  29513. ExpectNotNull(ssl = SSL_new(ctx));
  29514. #if !defined(NO_CHECK_PRIVATE_KEY)
  29515. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29516. #endif
  29517. SSL_free(ssl);
  29518. ssl = NULL;
  29519. SSL_CTX_free(ctx);
  29520. ctx = NULL;
  29521. #endif /* end of Ed25519 private key match tests */
  29522. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  29523. #ifndef NO_WOLFSSL_SERVER
  29524. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29525. #else
  29526. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29527. #endif
  29528. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  29529. WOLFSSL_FILETYPE_PEM));
  29530. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  29531. WOLFSSL_FILETYPE_PEM));
  29532. ExpectNotNull(ssl = SSL_new(ctx));
  29533. #if !defined(NO_CHECK_PRIVATE_KEY)
  29534. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29535. #endif
  29536. SSL_free(ssl);
  29537. ssl = NULL;
  29538. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  29539. WOLFSSL_FILETYPE_PEM));
  29540. ExpectNotNull(ssl = SSL_new(ctx));
  29541. #if !defined(NO_CHECK_PRIVATE_KEY)
  29542. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29543. #endif
  29544. SSL_free(ssl);
  29545. ssl = NULL;
  29546. SSL_CTX_free(ctx);
  29547. ctx = NULL;
  29548. #endif /* end of Ed448 private key match tests */
  29549. EVP_cleanup();
  29550. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  29551. CONF_modules_free();
  29552. ENGINE_cleanup();
  29553. CONF_modules_unload();
  29554. (void)ssl;
  29555. (void)ctx;
  29556. (void)pkey;
  29557. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29558. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29559. return EXPECT_RESULT();
  29560. }
  29561. static int test_wolfSSL_PEM_read_PrivateKey(void)
  29562. {
  29563. EXPECT_DECLS;
  29564. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29565. && !defined(NO_FILESYSTEM)
  29566. XFILE file = XBADFILE;
  29567. const char* fname = "./certs/server-key.pem";
  29568. EVP_PKEY* pkey = NULL;
  29569. RSA* rsa = NULL;
  29570. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  29571. unsigned char* sig = NULL;
  29572. size_t sigLen = 0;
  29573. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  29574. size_t tbsLen = sizeof(tbs);
  29575. /* Check error case. */
  29576. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  29577. /* Read in an RSA key. */
  29578. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29579. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  29580. if (file != XBADFILE)
  29581. XFCLOSE(file);
  29582. /* Make sure the key is usable by signing some data with it. */
  29583. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  29584. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  29585. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  29586. DYNAMIC_TYPE_TMP_BUFFER));
  29587. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  29588. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  29589. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  29590. WOLFSSL_SUCCESS);
  29591. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29592. EVP_PKEY_CTX_free(ctx);
  29593. EVP_PKEY_free(pkey);
  29594. #endif
  29595. return EXPECT_RESULT();
  29596. }
  29597. static int test_wolfSSL_PEM_read_PUBKEY(void)
  29598. {
  29599. EXPECT_DECLS;
  29600. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29601. && !defined(NO_FILESYSTEM)
  29602. XFILE file = XBADFILE;
  29603. const char* fname = "./certs/client-keyPub.pem";
  29604. EVP_PKEY* pkey = NULL;
  29605. /* Check error case. */
  29606. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  29607. /* Read in an RSA key. */
  29608. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29609. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  29610. EVP_PKEY_free(pkey);
  29611. if (file != XBADFILE)
  29612. XFCLOSE(file);
  29613. #endif
  29614. return EXPECT_RESULT();
  29615. }
  29616. static int test_wolfSSL_PEM_PrivateKey(void)
  29617. {
  29618. EXPECT_DECLS;
  29619. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29620. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  29621. #ifndef NO_BIO
  29622. BIO* bio = NULL;
  29623. #endif
  29624. EVP_PKEY* pkey = NULL;
  29625. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29626. #ifndef NO_BIO
  29627. /* test creating new EVP_PKEY with bad arg */
  29628. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  29629. /* test loading RSA key using BIO */
  29630. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29631. {
  29632. XFILE file = XBADFILE;
  29633. const char* fname = "./certs/server-key.pem";
  29634. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  29635. size_t sz = 0;
  29636. byte* buf = NULL;
  29637. EVP_PKEY* pkey2 = NULL;
  29638. EVP_PKEY* pkey3 = NULL;
  29639. RSA* rsa_key = NULL;
  29640. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29641. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29642. ExpectIntGT(sz = XFTELL(file), 0);
  29643. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29644. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29645. if (buf != NULL) {
  29646. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29647. }
  29648. if (file != XBADFILE) {
  29649. XFCLOSE(file);
  29650. file = XBADFILE;
  29651. }
  29652. /* Test using BIO new mem and loading PEM private key */
  29653. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29654. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29655. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29656. buf = NULL;
  29657. BIO_free(bio);
  29658. bio = NULL;
  29659. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29660. if (pkey2 != NULL) {
  29661. pkey2->type = EVP_PKEY_RSA;
  29662. }
  29663. /* Test parameter copy */
  29664. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  29665. EVP_PKEY_free(pkey2);
  29666. EVP_PKEY_free(pkey);
  29667. pkey = NULL;
  29668. /* Qt unit test case : rsa pkcs8 key */
  29669. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  29670. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29671. ExpectIntGT(sz = XFTELL(file), 0);
  29672. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29673. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29674. if (buf) {
  29675. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29676. }
  29677. if (file != XBADFILE) {
  29678. XFCLOSE(file);
  29679. file = XBADFILE;
  29680. }
  29681. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29682. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29683. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29684. buf = NULL;
  29685. BIO_free(bio);
  29686. bio = NULL;
  29687. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29688. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  29689. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  29690. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29691. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29692. #else
  29693. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29694. #endif
  29695. RSA_free(rsa_key);
  29696. EVP_PKEY_free(pkey3);
  29697. EVP_PKEY_free(pkey);
  29698. pkey = NULL;
  29699. }
  29700. #endif
  29701. /* test loading ECC key using BIO */
  29702. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29703. {
  29704. XFILE file = XBADFILE;
  29705. const char* fname = "./certs/ecc-key.pem";
  29706. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  29707. size_t sz = 0;
  29708. byte* buf = NULL;
  29709. EVP_PKEY* pkey2 = NULL;
  29710. EVP_PKEY* pkey3 = NULL;
  29711. EC_KEY* ec_key = NULL;
  29712. int nid = 0;
  29713. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29714. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29715. ExpectIntGT(sz = XFTELL(file), 0);
  29716. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29717. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29718. if (buf) {
  29719. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29720. }
  29721. if (file != XBADFILE) {
  29722. XFCLOSE(file);
  29723. file = XBADFILE;
  29724. }
  29725. /* Test using BIO new mem and loading PEM private key */
  29726. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29727. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29728. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29729. buf = NULL;
  29730. BIO_free(bio);
  29731. bio = NULL;
  29732. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29733. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29734. if (pkey2 != NULL) {
  29735. pkey2->type = EVP_PKEY_EC;
  29736. }
  29737. /* Test parameter copy */
  29738. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  29739. /* Qt unit test case 1*/
  29740. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29741. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29742. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29743. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29744. #else
  29745. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29746. #endif
  29747. /* Test default digest */
  29748. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  29749. ExpectIntEQ(nid, NID_sha256);
  29750. EC_KEY_free(ec_key);
  29751. ec_key = NULL;
  29752. EVP_PKEY_free(pkey3);
  29753. pkey3 = NULL;
  29754. EVP_PKEY_free(pkey2);
  29755. pkey2 = NULL;
  29756. EVP_PKEY_free(pkey);
  29757. pkey = NULL;
  29758. /* Qt unit test case ec pkcs8 key */
  29759. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  29760. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29761. ExpectIntGT(sz = XFTELL(file), 0);
  29762. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29763. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29764. if (buf) {
  29765. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29766. }
  29767. if (file != XBADFILE) {
  29768. XFCLOSE(file);
  29769. file = XBADFILE;
  29770. }
  29771. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29772. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29773. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29774. buf = NULL;
  29775. BIO_free(bio);
  29776. bio = NULL;
  29777. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29778. /* Qt unit test case */
  29779. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29780. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29781. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29782. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29783. #else
  29784. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29785. #endif
  29786. EC_KEY_free(ec_key);
  29787. EVP_PKEY_free(pkey3);
  29788. EVP_PKEY_free(pkey);
  29789. pkey = NULL;
  29790. }
  29791. #endif
  29792. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  29793. defined(WOLFSSL_CERT_GEN))
  29794. {
  29795. #define BIO_PEM_TEST_CHAR 'a'
  29796. EVP_PKEY* pkey2 = NULL;
  29797. unsigned char extra[10];
  29798. int i;
  29799. BIO* pub_bio = NULL;
  29800. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  29801. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29802. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  29803. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29804. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  29805. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  29806. &server_key, (long)sizeof_server_key_der_2048));
  29807. ExpectNull(pkey);
  29808. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29809. &server_key, (long)sizeof_server_key_der_2048));
  29810. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  29811. NULL), WOLFSSL_FAILURE);
  29812. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29813. NULL), WOLFSSL_FAILURE);
  29814. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  29815. NULL), WOLFSSL_SUCCESS);
  29816. ExpectIntGT(BIO_pending(bio), 0);
  29817. ExpectIntEQ(BIO_pending(bio), 1679);
  29818. /* Check if the pubkey API writes only the public key */
  29819. #ifdef WOLFSSL_KEY_GEN
  29820. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  29821. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  29822. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  29823. ExpectIntGT(BIO_pending(pub_bio), 0);
  29824. /* Previously both the private key and the pubkey calls would write
  29825. * out the private key and the PEM header was the only difference.
  29826. * The public PEM should be significantly shorter than the
  29827. * private key versison. */
  29828. ExpectIntEQ(BIO_pending(pub_bio), 451);
  29829. #endif
  29830. /* test creating new EVP_PKEY with good args */
  29831. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29832. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  29833. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  29834. /* test of reuse of EVP_PKEY */
  29835. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29836. ExpectIntEQ(BIO_pending(bio), 0);
  29837. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  29838. SSL_SUCCESS);
  29839. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  29840. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29841. ExpectNotNull(pkey);
  29842. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  29843. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  29844. }
  29845. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  29846. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  29847. for (i = 0; i < 10; i++) {
  29848. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  29849. }
  29850. BIO_free(pub_bio);
  29851. BIO_free(bio);
  29852. bio = NULL;
  29853. EVP_PKEY_free(pkey);
  29854. pkey = NULL;
  29855. EVP_PKEY_free(pkey2);
  29856. }
  29857. #endif
  29858. /* key is DES encrypted */
  29859. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  29860. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  29861. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  29862. {
  29863. XFILE f = XBADFILE;
  29864. wc_pem_password_cb* passwd_cb = NULL;
  29865. void* passwd_cb_userdata;
  29866. SSL_CTX* ctx = NULL;
  29867. char passwd[] = "bad password";
  29868. #ifndef WOLFSSL_NO_TLS12
  29869. #ifndef NO_WOLFSSL_SERVER
  29870. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29871. #else
  29872. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29873. #endif
  29874. #else
  29875. #ifndef NO_WOLFSSL_SERVER
  29876. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29877. #else
  29878. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29879. #endif
  29880. #endif
  29881. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29882. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  29883. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  29884. ExpectNull(passwd_cb_userdata =
  29885. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29886. /* fail case with password call back */
  29887. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  29888. (void*)passwd));
  29889. BIO_free(bio);
  29890. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29891. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29892. (void*)passwd));
  29893. BIO_free(bio);
  29894. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  29895. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  29896. if ((bio == NULL) && (f != XBADFILE)) {
  29897. XFCLOSE(f);
  29898. }
  29899. /* use callback that works */
  29900. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29901. (void*)"yassl123"));
  29902. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29903. EVP_PKEY_free(pkey);
  29904. pkey = NULL;
  29905. BIO_free(bio);
  29906. bio = NULL;
  29907. SSL_CTX_free(ctx);
  29908. }
  29909. #endif /* !defined(NO_DES3) */
  29910. #endif /* !NO_BIO */
  29911. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29912. {
  29913. unsigned char buf[2048];
  29914. size_t bytes = 0;
  29915. XFILE f = XBADFILE;
  29916. SSL_CTX* ctx = NULL;
  29917. #ifndef WOLFSSL_NO_TLS12
  29918. #ifndef NO_WOLFSSL_SERVER
  29919. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29920. #else
  29921. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29922. #endif
  29923. #else
  29924. #ifndef NO_WOLFSSL_SERVER
  29925. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29926. #else
  29927. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29928. #endif
  29929. #endif
  29930. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  29931. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  29932. if (f != XBADFILE)
  29933. XFCLOSE(f);
  29934. server_key = buf;
  29935. pkey = NULL;
  29936. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  29937. ExpectNull(pkey);
  29938. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  29939. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29940. EVP_PKEY_free(pkey);
  29941. pkey = NULL;
  29942. SSL_CTX_free(ctx);
  29943. server_key = NULL;
  29944. }
  29945. #endif
  29946. #ifndef NO_BIO
  29947. (void)bio;
  29948. #endif
  29949. (void)pkey;
  29950. (void)server_key;
  29951. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  29952. return EXPECT_RESULT();
  29953. }
  29954. static int test_wolfSSL_PEM_file_RSAKey(void)
  29955. {
  29956. EXPECT_DECLS;
  29957. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29958. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29959. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29960. RSA* rsa = NULL;
  29961. XFILE fp = XBADFILE;
  29962. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  29963. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  29964. if (fp != XBADFILE)
  29965. XFCLOSE(fp);
  29966. ExpectIntEQ(RSA_size(rsa), 256);
  29967. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  29968. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  29969. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  29970. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  29971. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29972. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  29973. RSA_free(rsa);
  29974. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29975. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29976. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29977. return EXPECT_RESULT();
  29978. }
  29979. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  29980. {
  29981. EXPECT_DECLS;
  29982. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  29983. !defined(NO_FILESYSTEM) && \
  29984. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  29985. RSA* rsa = NULL;
  29986. XFILE f = NULL;
  29987. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  29988. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29989. ExpectIntEQ(RSA_size(rsa), 256);
  29990. if (f != XBADFILE) {
  29991. XFCLOSE(f);
  29992. f = XBADFILE;
  29993. }
  29994. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  29995. NULL), WOLFSSL_FAILURE);
  29996. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  29997. NULL), WOLFSSL_FAILURE);
  29998. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  29999. WOLFSSL_SUCCESS);
  30000. RSA_free(rsa);
  30001. #ifdef HAVE_ECC
  30002. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  30003. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  30004. if (f != XBADFILE)
  30005. XFCLOSE(f);
  30006. #endif /* HAVE_ECC */
  30007. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30008. return EXPECT_RESULT();
  30009. }
  30010. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  30011. {
  30012. EXPECT_DECLS;
  30013. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30014. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30015. XFILE file = XBADFILE;
  30016. const char* fname = "./certs/client-keyPub.pem";
  30017. RSA *rsa = NULL;
  30018. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  30019. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30020. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  30021. ExpectIntEQ(RSA_size(rsa), 256);
  30022. RSA_free(rsa);
  30023. if (file != XBADFILE)
  30024. XFCLOSE(file);
  30025. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30026. return EXPECT_RESULT();
  30027. }
  30028. #ifndef NO_BIO
  30029. static int test_wolfSSL_PEM_bio_RSAKey(void)
  30030. {
  30031. EXPECT_DECLS;
  30032. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30033. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  30034. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  30035. RSA* rsa = NULL;
  30036. BIO* bio = NULL;
  30037. /* PrivateKey */
  30038. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  30039. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  30040. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  30041. ExpectNotNull(rsa);
  30042. ExpectIntEQ(RSA_size(rsa), 256);
  30043. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  30044. NULL), WOLFSSL_FAILURE);
  30045. BIO_free(bio);
  30046. bio = NULL;
  30047. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30048. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  30049. NULL), WOLFSSL_SUCCESS);
  30050. BIO_free(bio);
  30051. bio = NULL;
  30052. RSA_free(rsa);
  30053. rsa = NULL;
  30054. /* PUBKEY */
  30055. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  30056. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  30057. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30058. ExpectIntEQ(RSA_size(rsa), 256);
  30059. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30060. BIO_free(bio);
  30061. bio = NULL;
  30062. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30063. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  30064. BIO_free(bio);
  30065. bio = NULL;
  30066. RSA_free(rsa);
  30067. rsa = NULL;
  30068. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  30069. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  30070. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30071. BIO_free(bio);
  30072. bio = NULL;
  30073. RSA_free(rsa);
  30074. rsa = NULL;
  30075. #ifdef HAVE_ECC
  30076. /* ensure that non-rsa keys do not work */
  30077. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  30078. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30079. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30080. BIO_free(bio);
  30081. bio = NULL;
  30082. RSA_free(rsa);
  30083. rsa = NULL;
  30084. #endif /* HAVE_ECC */
  30085. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30086. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  30087. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  30088. return EXPECT_RESULT();
  30089. }
  30090. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  30091. {
  30092. EXPECT_DECLS;
  30093. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30094. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30095. RSA* rsa = NULL;
  30096. RSA* rsa_dup = NULL;
  30097. BIO* bio = NULL;
  30098. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  30099. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30100. ExpectIntEQ(RSA_size(rsa), 256);
  30101. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  30102. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  30103. /* Test duplicating empty key. */
  30104. ExpectNotNull(rsa_dup = RSA_new());
  30105. ExpectNull(RSAPublicKey_dup(rsa_dup));
  30106. RSA_free(rsa_dup);
  30107. rsa_dup = NULL;
  30108. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  30109. ExpectPtrNE(rsa_dup, rsa);
  30110. #endif
  30111. /* test if valgrind complains about unreleased memory */
  30112. RSA_up_ref(rsa);
  30113. RSA_free(rsa);
  30114. BIO_free(bio);
  30115. bio = NULL;
  30116. RSA_free(rsa);
  30117. rsa = NULL;
  30118. RSA_free(rsa_dup);
  30119. rsa_dup = NULL;
  30120. #ifdef HAVE_ECC
  30121. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  30122. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30123. BIO_free(bio);
  30124. #endif /* HAVE_ECC */
  30125. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30126. return EXPECT_RESULT();
  30127. }
  30128. static int test_wolfSSL_PEM_bio_DSAKey(void)
  30129. {
  30130. EXPECT_DECLS;
  30131. #ifndef HAVE_SELFTEST
  30132. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  30133. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  30134. DSA* dsa = NULL;
  30135. BIO* bio = NULL;
  30136. /* PrivateKey */
  30137. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  30138. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  30139. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  30140. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  30141. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  30142. NULL), WOLFSSL_FAILURE);
  30143. BIO_free(bio);
  30144. bio = NULL;
  30145. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30146. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  30147. NULL), WOLFSSL_SUCCESS);
  30148. BIO_free(bio);
  30149. bio = NULL;
  30150. DSA_free(dsa);
  30151. dsa = NULL;
  30152. /* PUBKEY */
  30153. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  30154. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  30155. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  30156. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  30157. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30158. BIO_free(bio);
  30159. bio = NULL;
  30160. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30161. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  30162. BIO_free(bio);
  30163. bio = NULL;
  30164. DSA_free(dsa);
  30165. dsa = NULL;
  30166. #ifdef HAVE_ECC
  30167. /* ensure that non-dsa keys do not work */
  30168. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  30169. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  30170. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  30171. BIO_free(bio);
  30172. bio = NULL;
  30173. DSA_free(dsa);
  30174. dsa = NULL;
  30175. #endif /* HAVE_ECC */
  30176. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  30177. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  30178. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  30179. #endif /* HAVE_SELFTEST */
  30180. return EXPECT_RESULT();
  30181. }
  30182. static int test_wolfSSL_PEM_bio_ECKey(void)
  30183. {
  30184. EXPECT_DECLS;
  30185. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30186. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  30187. EC_KEY* ec = NULL;
  30188. EC_KEY* ec2;
  30189. BIO* bio = NULL;
  30190. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  30191. unsigned char* pem = NULL;
  30192. int pLen;
  30193. #endif
  30194. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  30195. "MAA=\n"
  30196. "-----END PUBLIC KEY-----";
  30197. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  30198. "MAA=\n"
  30199. "-----END EC PRIVATE KEY-----";
  30200. /* PrivateKey */
  30201. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  30202. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  30203. ec2 = NULL;
  30204. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  30205. ExpectIntEQ(ec == ec2, 1);
  30206. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  30207. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  30208. NULL), WOLFSSL_FAILURE);
  30209. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  30210. NULL), WOLFSSL_FAILURE);
  30211. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  30212. WOLFSSL_FAILURE);
  30213. BIO_free(bio);
  30214. bio = NULL;
  30215. /* Public key data - fail. */
  30216. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  30217. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  30218. BIO_free(bio);
  30219. bio = NULL;
  30220. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30221. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  30222. NULL), WOLFSSL_SUCCESS);
  30223. BIO_free(bio);
  30224. bio = NULL;
  30225. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  30226. NULL),WOLFSSL_FAILURE);
  30227. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  30228. WOLFSSL_FAILURE);
  30229. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  30230. WOLFSSL_FAILURE);
  30231. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  30232. WOLFSSL_SUCCESS);
  30233. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  30234. NULL), 0);
  30235. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  30236. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  30237. NULL), 0);
  30238. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  30239. NULL), 0);
  30240. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  30241. &pLen), 0);
  30242. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  30243. &pLen), 0);
  30244. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  30245. &pLen), 0);
  30246. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  30247. NULL), 0);
  30248. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  30249. &pLen), 1);
  30250. ExpectIntGT(pLen, 0);
  30251. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30252. #endif
  30253. EC_KEY_free(ec);
  30254. ec = NULL;
  30255. /* PUBKEY */
  30256. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  30257. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  30258. ec2 = NULL;
  30259. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  30260. ExpectIntEQ(ec == ec2, 1);
  30261. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  30262. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30263. BIO_free(bio);
  30264. bio = NULL;
  30265. /* Test 0x30, 0x00 fails. */
  30266. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  30267. sizeof(ec_key_bad_1)));
  30268. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  30269. BIO_free(bio);
  30270. bio = NULL;
  30271. /* Private key data - fail. */
  30272. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  30273. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  30274. BIO_free(bio);
  30275. bio = NULL;
  30276. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30277. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  30278. BIO_free(bio);
  30279. bio = NULL;
  30280. /* Same test as above, but with a file pointer rather than a BIO. */
  30281. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30282. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  30283. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  30284. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  30285. EC_KEY_free(ec);
  30286. ec = NULL;
  30287. #ifndef NO_RSA
  30288. /* ensure that non-ec keys do not work */
  30289. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  30290. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  30291. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  30292. BIO_free(bio);
  30293. bio = NULL;
  30294. EC_KEY_free(ec);
  30295. ec = NULL;
  30296. #endif /* !NO_RSA */
  30297. /* Test 0x30, 0x00 fails. */
  30298. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  30299. sizeof(ec_priv_key_bad_1)));
  30300. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  30301. BIO_free(bio);
  30302. bio = NULL;
  30303. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30304. return EXPECT_RESULT();
  30305. }
  30306. static int test_wolfSSL_PEM_PUBKEY(void)
  30307. {
  30308. EXPECT_DECLS;
  30309. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  30310. BIO* bio = NULL;
  30311. EVP_PKEY* pkey = NULL;
  30312. /* test creating new EVP_PKEY with bad arg */
  30313. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  30314. /* test loading ECC key using BIO */
  30315. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30316. {
  30317. XFILE file = XBADFILE;
  30318. const char* fname = "./certs/ecc-client-keyPub.pem";
  30319. size_t sz;
  30320. byte* buf = NULL;
  30321. EVP_PKEY* pkey2 = NULL;
  30322. EC_KEY* ec_key = NULL;
  30323. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30324. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  30325. ExpectIntGT(sz = XFTELL(file), 0);
  30326. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  30327. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30328. if (buf != NULL) {
  30329. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30330. }
  30331. if (file != XBADFILE) {
  30332. XFCLOSE(file);
  30333. }
  30334. /* Test using BIO new mem and loading PEM private key */
  30335. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30336. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  30337. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30338. BIO_free(bio);
  30339. bio = NULL;
  30340. /* Qt unit test case*/
  30341. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30342. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30343. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  30344. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30345. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  30346. #else
  30347. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  30348. #endif
  30349. EC_KEY_free(ec_key);
  30350. EVP_PKEY_free(pkey2);
  30351. EVP_PKEY_free(pkey);
  30352. pkey = NULL;
  30353. }
  30354. #endif
  30355. (void)bio;
  30356. (void)pkey;
  30357. #endif
  30358. return EXPECT_RESULT();
  30359. }
  30360. #endif /* !NO_BIO */
  30361. static int test_DSA_do_sign_verify(void)
  30362. {
  30363. EXPECT_DECLS;
  30364. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  30365. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30366. !defined(NO_DSA)
  30367. unsigned char digest[WC_SHA_DIGEST_SIZE];
  30368. DSA_SIG* sig = NULL;
  30369. DSA* dsa = NULL;
  30370. word32 bytes;
  30371. byte sigBin[DSA_SIG_SIZE];
  30372. int dsacheck;
  30373. #ifdef USE_CERT_BUFFERS_1024
  30374. byte tmp[ONEK_BUF];
  30375. XMEMSET(tmp, 0, sizeof(tmp));
  30376. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  30377. bytes = sizeof_dsa_key_der_1024;
  30378. #elif defined(USE_CERT_BUFFERS_2048)
  30379. byte tmp[TWOK_BUF];
  30380. XMEMSET(tmp, 0, sizeof(tmp));
  30381. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  30382. bytes = sizeof_dsa_key_der_2048;
  30383. #else
  30384. byte tmp[TWOK_BUF];
  30385. XFILE fp = XBADFILE;
  30386. XMEMSET(tmp, 0, sizeof(tmp));
  30387. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  30388. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  30389. if (fp != XBADFILE)
  30390. XFCLOSE(fp);
  30391. #endif /* END USE_CERT_BUFFERS_1024 */
  30392. XMEMSET(digest, 202, sizeof(digest));
  30393. ExpectNotNull(dsa = DSA_new());
  30394. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  30395. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  30396. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  30397. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  30398. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  30399. DSA_SIG_free(sig);
  30400. DSA_free(dsa);
  30401. #endif
  30402. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  30403. return EXPECT_RESULT();
  30404. }
  30405. static int test_wolfSSL_tmp_dh(void)
  30406. {
  30407. EXPECT_DECLS;
  30408. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30409. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  30410. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30411. byte buff[6000];
  30412. char file[] = "./certs/dsaparams.pem";
  30413. XFILE f = XBADFILE;
  30414. int bytes = 0;
  30415. DSA* dsa = NULL;
  30416. DH* dh = NULL;
  30417. #if defined(WOLFSSL_DH_EXTRA) && \
  30418. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30419. DH* dh2 = NULL;
  30420. #endif
  30421. BIO* bio = NULL;
  30422. SSL* ssl = NULL;
  30423. SSL_CTX* ctx = NULL;
  30424. #ifndef NO_WOLFSSL_SERVER
  30425. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30426. #else
  30427. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30428. #endif
  30429. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30430. WOLFSSL_FILETYPE_PEM));
  30431. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30432. WOLFSSL_FILETYPE_PEM));
  30433. ExpectNotNull(ssl = SSL_new(ctx));
  30434. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  30435. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  30436. if (f != XBADFILE)
  30437. XFCLOSE(f);
  30438. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  30439. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  30440. ExpectNotNull(dsa);
  30441. dh = wolfSSL_DSA_dup_DH(dsa);
  30442. ExpectNotNull(dh);
  30443. #if defined(WOLFSSL_DH_EXTRA) && \
  30444. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30445. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  30446. #endif
  30447. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  30448. #ifndef NO_WOLFSSL_SERVER
  30449. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  30450. #else
  30451. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  30452. #endif
  30453. BIO_free(bio);
  30454. DSA_free(dsa);
  30455. DH_free(dh);
  30456. dh = NULL;
  30457. #if defined(WOLFSSL_DH_EXTRA) && \
  30458. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30459. DH_free(dh2);
  30460. dh2 = NULL;
  30461. #endif
  30462. SSL_free(ssl);
  30463. SSL_CTX_free(ctx);
  30464. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30465. #endif
  30466. return EXPECT_RESULT();
  30467. }
  30468. static int test_wolfSSL_ctrl(void)
  30469. {
  30470. EXPECT_DECLS;
  30471. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  30472. byte buff[6000];
  30473. BIO* bio = NULL;
  30474. int bytes;
  30475. BUF_MEM* ptr = NULL;
  30476. XMEMSET(buff, 0, sizeof(buff));
  30477. bytes = sizeof(buff);
  30478. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  30479. ExpectNotNull(BIO_s_socket());
  30480. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  30481. /* needs tested after stubs filled out @TODO
  30482. SSL_ctrl
  30483. SSL_CTX_ctrl
  30484. */
  30485. BIO_free(bio);
  30486. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  30487. return EXPECT_RESULT();
  30488. }
  30489. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  30490. {
  30491. EXPECT_DECLS;
  30492. #ifdef OPENSSL_EXTRA
  30493. static const unsigned char pw[] = "password";
  30494. static const int pwSz = sizeof(pw) - 1;
  30495. size_t checkPwSz = 0;
  30496. const unsigned char* checkPw = NULL;
  30497. WOLFSSL_EVP_PKEY* key = NULL;
  30498. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  30499. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  30500. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  30501. pwSz));
  30502. if (key != NULL) {
  30503. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  30504. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  30505. ExpectIntEQ(key->pkey_sz, pwSz);
  30506. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  30507. }
  30508. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  30509. ExpectIntEQ((int)checkPwSz, pwSz);
  30510. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  30511. wolfSSL_EVP_PKEY_free(key);
  30512. key = NULL;
  30513. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  30514. 0));
  30515. ExpectIntEQ(key->pkey_sz, 0);
  30516. if (EXPECT_SUCCESS()) {
  30517. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  30518. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  30519. }
  30520. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30521. ExpectIntEQ((int)checkPwSz, 0);
  30522. wolfSSL_EVP_PKEY_free(key);
  30523. key = NULL;
  30524. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  30525. 0));
  30526. ExpectIntEQ(key->pkey_sz, 0);
  30527. if (EXPECT_SUCCESS()) {
  30528. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  30529. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  30530. }
  30531. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30532. ExpectIntEQ((int)checkPwSz, 0);
  30533. wolfSSL_EVP_PKEY_free(key);
  30534. key = NULL;
  30535. #endif /* OPENSSL_EXTRA */
  30536. return EXPECT_RESULT();
  30537. }
  30538. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  30539. {
  30540. EXPECT_DECLS;
  30541. #ifdef OPENSSL_EXTRA
  30542. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  30543. const char *priv = "ABCDEFGHIJKLMNOP";
  30544. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  30545. WOLFSSL_EVP_PKEY* key = NULL;
  30546. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30547. NULL, NULL, AES_128_KEY_SIZE, cipher));
  30548. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30549. NULL, (const unsigned char *)priv, 0, cipher));
  30550. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30551. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  30552. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30553. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  30554. wolfSSL_EVP_PKEY_free(key);
  30555. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  30556. #endif /* OPENSSL_EXTRA */
  30557. return EXPECT_RESULT();
  30558. }
  30559. static int test_wolfSSL_EVP_Digest(void)
  30560. {
  30561. EXPECT_DECLS;
  30562. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30563. const char* in = "abc";
  30564. int inLen = (int)XSTRLEN(in);
  30565. byte out[WC_SHA256_DIGEST_SIZE];
  30566. unsigned int outLen;
  30567. const char* expOut =
  30568. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  30569. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  30570. "\x15\xAD";
  30571. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  30572. "SHA256", NULL), 1);
  30573. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  30574. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  30575. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  30576. return EXPECT_RESULT();
  30577. }
  30578. static int test_wolfSSL_EVP_Digest_all(void)
  30579. {
  30580. EXPECT_DECLS;
  30581. #ifdef OPENSSL_EXTRA
  30582. const char* digests[] = {
  30583. #ifndef NO_MD5
  30584. "MD5",
  30585. #endif
  30586. #ifndef NO_SHA
  30587. "SHA",
  30588. #endif
  30589. #ifdef WOLFSSL_SHA224
  30590. "SHA224",
  30591. #endif
  30592. #ifndef NO_SHA256
  30593. "SHA256",
  30594. #endif
  30595. #ifdef WOLFSSL_SHA384
  30596. "SHA384",
  30597. #endif
  30598. #ifdef WOLFSSL_SHA512
  30599. "SHA512",
  30600. #endif
  30601. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  30602. "SHA512_224",
  30603. #endif
  30604. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  30605. "SHA512_256",
  30606. #endif
  30607. #ifdef WOLFSSL_SHA3
  30608. #ifndef WOLFSSL_NOSHA3_224
  30609. "SHA3_224",
  30610. #endif
  30611. #ifndef WOLFSSL_NOSHA3_256
  30612. "SHA3_256",
  30613. #endif
  30614. "SHA3_384",
  30615. #ifndef WOLFSSL_NOSHA3_512
  30616. "SHA3_512",
  30617. #endif
  30618. #endif /* WOLFSSL_SHA3 */
  30619. NULL
  30620. };
  30621. const char** d;
  30622. const unsigned char in[] = "abc";
  30623. int inLen = XSTR_SIZEOF(in);
  30624. byte out[WC_MAX_DIGEST_SIZE];
  30625. unsigned int outLen;
  30626. for (d = digests; *d != NULL; d++) {
  30627. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  30628. ExpectIntGT(outLen, 0);
  30629. ExpectIntEQ(EVP_MD_size(*d), outLen);
  30630. }
  30631. #endif
  30632. return EXPECT_RESULT();
  30633. }
  30634. static int test_wolfSSL_EVP_MD_size(void)
  30635. {
  30636. EXPECT_DECLS;
  30637. #ifdef OPENSSL_EXTRA
  30638. WOLFSSL_EVP_MD_CTX mdCtx;
  30639. #ifdef WOLFSSL_SHA3
  30640. #ifndef WOLFSSL_NOSHA3_224
  30641. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30642. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  30643. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  30644. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  30645. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30646. #endif
  30647. #ifndef WOLFSSL_NOSHA3_256
  30648. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30649. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  30650. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  30651. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  30652. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30653. #endif
  30654. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30655. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  30656. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  30657. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  30658. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30659. #ifndef WOLFSSL_NOSHA3_512
  30660. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30661. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  30662. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  30663. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  30664. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30665. #endif
  30666. #endif /* WOLFSSL_SHA3 */
  30667. #ifndef NO_SHA256
  30668. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30669. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  30670. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30671. WC_SHA256_DIGEST_SIZE);
  30672. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30673. WC_SHA256_BLOCK_SIZE);
  30674. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  30675. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  30676. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30677. #endif
  30678. #ifndef NO_MD5
  30679. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30680. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  30681. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30682. WC_MD5_DIGEST_SIZE);
  30683. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30684. WC_MD5_BLOCK_SIZE);
  30685. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  30686. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  30687. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30688. #endif
  30689. #ifdef WOLFSSL_SHA224
  30690. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30691. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  30692. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30693. WC_SHA224_DIGEST_SIZE);
  30694. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30695. WC_SHA224_BLOCK_SIZE);
  30696. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  30697. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  30698. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30699. #endif
  30700. #ifdef WOLFSSL_SHA384
  30701. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30702. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  30703. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30704. WC_SHA384_DIGEST_SIZE);
  30705. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30706. WC_SHA384_BLOCK_SIZE);
  30707. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  30708. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  30709. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30710. #endif
  30711. #ifdef WOLFSSL_SHA512
  30712. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30713. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  30714. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30715. WC_SHA512_DIGEST_SIZE);
  30716. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30717. WC_SHA512_BLOCK_SIZE);
  30718. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  30719. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  30720. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30721. #endif
  30722. #ifndef NO_SHA
  30723. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30724. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  30725. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30726. WC_SHA_DIGEST_SIZE);
  30727. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30728. WC_SHA_BLOCK_SIZE);
  30729. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30730. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30731. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30732. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30733. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  30734. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30735. WC_SHA_DIGEST_SIZE);
  30736. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30737. WC_SHA_BLOCK_SIZE);
  30738. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30739. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30740. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30741. #endif
  30742. /* error case */
  30743. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30744. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  30745. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30746. BAD_FUNC_ARG);
  30747. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  30748. /* Cleanup is valid on uninit'ed struct */
  30749. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30750. #endif /* OPENSSL_EXTRA */
  30751. return EXPECT_RESULT();
  30752. }
  30753. static int test_wolfSSL_EVP_MD_pkey_type(void)
  30754. {
  30755. EXPECT_DECLS;
  30756. #ifdef OPENSSL_EXTRA
  30757. const WOLFSSL_EVP_MD* md;
  30758. #ifndef NO_MD5
  30759. ExpectNotNull(md = EVP_md5());
  30760. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  30761. #endif
  30762. #ifndef NO_SHA
  30763. ExpectNotNull(md = EVP_sha1());
  30764. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  30765. #endif
  30766. #ifdef WOLFSSL_SHA224
  30767. ExpectNotNull(md = EVP_sha224());
  30768. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  30769. #endif
  30770. ExpectNotNull(md = EVP_sha256());
  30771. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  30772. #ifdef WOLFSSL_SHA384
  30773. ExpectNotNull(md = EVP_sha384());
  30774. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  30775. #endif
  30776. #ifdef WOLFSSL_SHA512
  30777. ExpectNotNull(md = EVP_sha512());
  30778. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  30779. #endif
  30780. #endif
  30781. return EXPECT_RESULT();
  30782. }
  30783. #ifdef OPENSSL_EXTRA
  30784. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  30785. size_t testKeySz, const char* testData, size_t testDataSz,
  30786. const byte* testResult, size_t testResultSz)
  30787. {
  30788. EXPECT_DECLS;
  30789. unsigned char check[WC_MAX_DIGEST_SIZE];
  30790. size_t checkSz = -1;
  30791. WOLFSSL_EVP_PKEY* key = NULL;
  30792. WOLFSSL_EVP_MD_CTX mdCtx;
  30793. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  30794. testKey, (int)testKeySz));
  30795. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30796. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30797. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30798. (unsigned int)testDataSz), 1);
  30799. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30800. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30801. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30802. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30803. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30804. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30805. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30806. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30807. (unsigned int)testDataSz), 1);
  30808. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30809. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30810. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30811. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30812. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30813. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30814. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30815. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30816. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30817. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30818. (unsigned int)testDataSz - 4), 1);
  30819. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30820. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30821. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30822. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30823. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30824. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30825. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30826. (unsigned int)testDataSz - 4), 1);
  30827. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30828. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30829. wolfSSL_EVP_PKEY_free(key);
  30830. return EXPECT_RESULT();
  30831. }
  30832. #endif
  30833. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  30834. {
  30835. EXPECT_DECLS;
  30836. #ifdef OPENSSL_EXTRA
  30837. static const unsigned char testKey[] =
  30838. {
  30839. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30840. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30841. 0x0b, 0x0b, 0x0b, 0x0b
  30842. };
  30843. static const char testData[] = "Hi There";
  30844. #ifdef WOLFSSL_SHA224
  30845. static const unsigned char testResultSha224[] =
  30846. {
  30847. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  30848. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  30849. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  30850. 0x53, 0x68, 0x4b, 0x22
  30851. };
  30852. #endif
  30853. #ifndef NO_SHA256
  30854. static const unsigned char testResultSha256[] =
  30855. {
  30856. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  30857. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  30858. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  30859. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  30860. };
  30861. #endif
  30862. #ifdef WOLFSSL_SHA384
  30863. static const unsigned char testResultSha384[] =
  30864. {
  30865. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  30866. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  30867. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  30868. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  30869. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  30870. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  30871. };
  30872. #endif
  30873. #ifdef WOLFSSL_SHA512
  30874. static const unsigned char testResultSha512[] =
  30875. {
  30876. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  30877. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  30878. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  30879. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  30880. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  30881. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  30882. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  30883. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  30884. };
  30885. #endif
  30886. #ifdef WOLFSSL_SHA3
  30887. #ifndef WOLFSSL_NOSHA3_224
  30888. static const unsigned char testResultSha3_224[] =
  30889. {
  30890. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  30891. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  30892. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  30893. 0xf3, 0xc8, 0x60, 0xf7
  30894. };
  30895. #endif
  30896. #ifndef WOLFSSL_NOSHA3_256
  30897. static const unsigned char testResultSha3_256[] =
  30898. {
  30899. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  30900. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  30901. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  30902. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  30903. };
  30904. #endif
  30905. #ifndef WOLFSSL_NOSHA3_384
  30906. static const unsigned char testResultSha3_384[] =
  30907. {
  30908. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  30909. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  30910. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  30911. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  30912. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  30913. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  30914. };
  30915. #endif
  30916. #ifndef WOLFSSL_NOSHA3_512
  30917. static const unsigned char testResultSha3_512[] =
  30918. {
  30919. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  30920. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  30921. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  30922. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  30923. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  30924. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  30925. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  30926. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  30927. };
  30928. #endif
  30929. #endif
  30930. #ifndef NO_SHA256
  30931. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  30932. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  30933. sizeof(testResultSha256)), TEST_SUCCESS);
  30934. #endif
  30935. #ifdef WOLFSSL_SHA224
  30936. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  30937. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  30938. sizeof(testResultSha224)), TEST_SUCCESS);
  30939. #endif
  30940. #ifdef WOLFSSL_SHA384
  30941. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  30942. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  30943. sizeof(testResultSha384)), TEST_SUCCESS);
  30944. #endif
  30945. #ifdef WOLFSSL_SHA512
  30946. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  30947. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  30948. sizeof(testResultSha512)), TEST_SUCCESS);
  30949. #endif
  30950. #ifdef WOLFSSL_SHA3
  30951. #ifndef WOLFSSL_NOSHA3_224
  30952. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  30953. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  30954. sizeof(testResultSha3_224)), TEST_SUCCESS);
  30955. #endif
  30956. #ifndef WOLFSSL_NOSHA3_256
  30957. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  30958. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  30959. sizeof(testResultSha3_256)), TEST_SUCCESS);
  30960. #endif
  30961. #ifndef WOLFSSL_NOSHA3_384
  30962. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  30963. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  30964. sizeof(testResultSha3_384)), TEST_SUCCESS);
  30965. #endif
  30966. #ifndef WOLFSSL_NOSHA3_512
  30967. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  30968. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  30969. sizeof(testResultSha3_512)), TEST_SUCCESS);
  30970. #endif
  30971. #endif
  30972. #endif /* OPENSSL_EXTRA */
  30973. return EXPECT_RESULT();
  30974. }
  30975. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  30976. {
  30977. EXPECT_DECLS;
  30978. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  30979. WOLFSSL_EVP_PKEY* privKey = NULL;
  30980. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30981. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  30982. const char testData[] = "Hi There";
  30983. WOLFSSL_EVP_MD_CTX mdCtx;
  30984. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  30985. int ret;
  30986. size_t checkSz = -1;
  30987. int sz = 2048 / 8;
  30988. const unsigned char* cp;
  30989. const unsigned char* p;
  30990. unsigned char check[2048/8];
  30991. size_t i;
  30992. int paddings[] = {
  30993. RSA_PKCS1_PADDING,
  30994. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  30995. RSA_PKCS1_PSS_PADDING,
  30996. #endif
  30997. };
  30998. cp = client_key_der_2048;
  30999. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  31000. sizeof_client_key_der_2048)));
  31001. p = client_keypub_der_2048;
  31002. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  31003. sizeof_client_keypub_der_2048)));
  31004. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31005. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  31006. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31007. NULL, privKey), 1);
  31008. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31009. (unsigned int)XSTRLEN(testData)), 1);
  31010. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31011. ExpectIntEQ((int)checkSz, sz);
  31012. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31013. ExpectIntEQ((int)checkSz,sz);
  31014. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  31015. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  31016. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  31017. ExpectIntEQ(ret, 1);
  31018. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31019. ExpectIntEQ(ret, 1);
  31020. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31021. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31022. NULL, pubKey), 1);
  31023. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31024. (unsigned int)XSTRLEN(testData)),
  31025. 1);
  31026. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31027. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31028. ExpectIntEQ(ret, 1);
  31029. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31030. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31031. NULL, privKey), 1);
  31032. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31033. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31034. ExpectIntEQ((int)checkSz, sz);
  31035. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31036. ExpectIntEQ((int)checkSz, sz);
  31037. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31038. (unsigned int)XSTRLEN(testData) - 4), 1);
  31039. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31040. ExpectIntEQ((int)checkSz, sz);
  31041. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31042. ExpectIntEQ(ret, 1);
  31043. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31044. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31045. NULL, pubKey), 1);
  31046. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31047. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31048. (unsigned int)XSTRLEN(testData) - 4),
  31049. 1);
  31050. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31051. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31052. ExpectIntEQ(ret, 1);
  31053. /* Check all signing padding types */
  31054. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  31055. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31056. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  31057. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  31058. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  31059. paddings[i]), 1);
  31060. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31061. (unsigned int)XSTRLEN(testData)), 1);
  31062. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31063. ExpectIntEQ((int)checkSz, sz);
  31064. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31065. ExpectIntEQ((int)checkSz,sz);
  31066. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31067. ExpectIntEQ(ret, 1);
  31068. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31069. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  31070. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  31071. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  31072. paddings[i]), 1);
  31073. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31074. (unsigned int)XSTRLEN(testData)), 1);
  31075. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31076. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31077. ExpectIntEQ(ret, 1);
  31078. }
  31079. wolfSSL_EVP_PKEY_free(pubKey);
  31080. wolfSSL_EVP_PKEY_free(privKey);
  31081. #endif
  31082. return EXPECT_RESULT();
  31083. }
  31084. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  31085. {
  31086. EXPECT_DECLS;
  31087. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  31088. WOLFSSL_EVP_PKEY* privKey = NULL;
  31089. WOLFSSL_EVP_PKEY* pubKey = NULL;
  31090. const char testData[] = "Hi There";
  31091. WOLFSSL_EVP_MD_CTX mdCtx;
  31092. int ret;
  31093. size_t checkSz = -1;
  31094. const unsigned char* cp;
  31095. const unsigned char* p;
  31096. unsigned char check[2048/8];
  31097. cp = ecc_clikey_der_256;
  31098. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  31099. sizeof_ecc_clikey_der_256));
  31100. p = ecc_clikeypub_der_256;
  31101. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  31102. sizeof_ecc_clikeypub_der_256)));
  31103. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31104. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31105. NULL, privKey), 1);
  31106. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31107. (unsigned int)XSTRLEN(testData)), 1);
  31108. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31109. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31110. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31111. ExpectIntEQ(ret, 1);
  31112. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31113. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31114. NULL, pubKey), 1);
  31115. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31116. (unsigned int)XSTRLEN(testData)),
  31117. 1);
  31118. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31119. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31120. ExpectIntEQ(ret, 1);
  31121. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31122. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31123. NULL, privKey), 1);
  31124. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31125. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31126. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31127. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31128. (unsigned int)XSTRLEN(testData) - 4), 1);
  31129. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31130. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31131. ExpectIntEQ(ret, 1);
  31132. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31133. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31134. NULL, pubKey), 1);
  31135. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31136. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31137. (unsigned int)XSTRLEN(testData) - 4),
  31138. 1);
  31139. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31140. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31141. ExpectIntEQ(ret, 1);
  31142. wolfSSL_EVP_PKEY_free(pubKey);
  31143. wolfSSL_EVP_PKEY_free(privKey);
  31144. #endif
  31145. return EXPECT_RESULT();
  31146. }
  31147. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  31148. {
  31149. EXPECT_DECLS;
  31150. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31151. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  31152. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31153. char caFile[] = "./certs/client-ca.pem";
  31154. char clientFile[] = "./certs/client-cert.pem";
  31155. SSL_CTX* ctx = NULL;
  31156. X509* x509 = NULL;
  31157. BIO *bio = NULL;
  31158. X509 *cert = NULL;
  31159. X509 *ca = NULL;
  31160. STACK_OF(X509) *chain = NULL;
  31161. STACK_OF(X509) *chain2 = NULL;
  31162. #ifndef NO_WOLFSSL_SERVER
  31163. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31164. #else
  31165. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31166. #endif
  31167. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  31168. WOLFSSL_FILETYPE_PEM));
  31169. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  31170. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  31171. WOLFSSL_FILETYPE_PEM));
  31172. /* additional test of getting EVP_PKEY key size from X509
  31173. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  31174. * allowed with user RSA */
  31175. {
  31176. EVP_PKEY* pkey = NULL;
  31177. #if defined(HAVE_ECC)
  31178. X509* ecX509 = NULL;
  31179. #endif /* HAVE_ECC */
  31180. ExpectNotNull(pkey = X509_get_pubkey(x509));
  31181. /* current RSA key is 2048 bit (256 bytes) */
  31182. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  31183. EVP_PKEY_free(pkey);
  31184. pkey = NULL;
  31185. #if defined(HAVE_ECC)
  31186. #if defined(USE_CERT_BUFFERS_256)
  31187. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  31188. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  31189. SSL_FILETYPE_ASN1));
  31190. #else
  31191. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  31192. cliEccCertFile, SSL_FILETYPE_PEM));
  31193. #endif
  31194. pkey = X509_get_pubkey(ecX509);
  31195. ExpectNotNull(pkey);
  31196. /* current ECC key is 256 bit (32 bytes) */
  31197. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  31198. X509_free(ecX509);
  31199. ecX509 = NULL;
  31200. EVP_PKEY_free(pkey);
  31201. pkey = NULL;
  31202. #endif /* HAVE_ECC */
  31203. }
  31204. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  31205. if (EXPECT_SUCCESS()) {
  31206. x509 = NULL;
  31207. }
  31208. #ifdef WOLFSSL_ENCRYPTED_KEYS
  31209. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  31210. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31211. #endif
  31212. SSL_CTX_free(ctx);
  31213. ctx = NULL;
  31214. #ifndef NO_WOLFSSL_SERVER
  31215. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31216. #else
  31217. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31218. #endif
  31219. /* Test haproxy use case */
  31220. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  31221. /* Read Certificate */
  31222. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  31223. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  31224. ExpectNotNull(chain = sk_X509_new_null());
  31225. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  31226. if (EXPECT_SUCCESS()) {
  31227. ca = NULL;
  31228. }
  31229. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  31230. ExpectNotNull(ca = sk_X509_shift(chain2));
  31231. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  31232. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  31233. if (EXPECT_SUCCESS()) {
  31234. ca = NULL;
  31235. }
  31236. BIO_free(bio);
  31237. X509_free(cert);
  31238. X509_free(ca);
  31239. X509_free(x509);
  31240. sk_X509_pop_free(chain, X509_free);
  31241. sk_X509_pop_free(chain2, X509_free);
  31242. SSL_CTX_free(ctx);
  31243. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31244. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31245. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  31246. return EXPECT_RESULT();
  31247. }
  31248. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  31249. static int test_wolfSSL_ERR_peek_last_error_line(void)
  31250. {
  31251. EXPECT_DECLS;
  31252. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31253. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31254. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  31255. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  31256. callback_functions client_cb;
  31257. callback_functions server_cb;
  31258. int line = 0;
  31259. int flag = ERR_TXT_STRING;
  31260. const char* file = NULL;
  31261. const char* data = NULL;
  31262. /* create a failed connection and inspect the error */
  31263. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31264. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31265. client_cb.method = wolfTLSv1_1_client_method;
  31266. server_cb.method = wolfTLSv1_2_server_method;
  31267. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  31268. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  31269. ExpectNotNull(data);
  31270. /* check clearing error state */
  31271. ERR_remove_state(0);
  31272. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  31273. ERR_peek_last_error_line(NULL, &line);
  31274. ExpectIntEQ(line, 0);
  31275. ERR_peek_last_error_line(&file, NULL);
  31276. ExpectNull(file);
  31277. /* retry connection to fill error queue */
  31278. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31279. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31280. client_cb.method = wolfTLSv1_1_client_method;
  31281. server_cb.method = wolfTLSv1_2_server_method;
  31282. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  31283. /* check that error code was stored */
  31284. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  31285. ERR_peek_last_error_line(NULL, &line);
  31286. ExpectIntNE(line, 0);
  31287. ERR_peek_last_error_line(&file, NULL);
  31288. ExpectNotNull(file);
  31289. fprintf(stderr, "\nTesting error print out\n");
  31290. ERR_print_errors_fp(stderr);
  31291. fprintf(stderr, "Done testing print out\n\n");
  31292. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  31293. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  31294. return EXPECT_RESULT();
  31295. }
  31296. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  31297. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31298. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31299. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  31300. {
  31301. (void) ok;
  31302. (void) ctx;
  31303. fprintf(stderr, "ENTER verify_cb\n");
  31304. return SSL_SUCCESS;
  31305. }
  31306. #endif
  31307. static int test_wolfSSL_X509_Name_canon(void)
  31308. {
  31309. EXPECT_DECLS;
  31310. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31311. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  31312. defined(WOLFSSL_CERT_GEN) && \
  31313. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  31314. const long ex_hash1 = 0x0fdb2da4;
  31315. const long ex_hash2 = 0x9f3e8c9e;
  31316. X509_NAME *name = NULL;
  31317. X509 *x509 = NULL;
  31318. XFILE file = XBADFILE;
  31319. unsigned long hash = 0;
  31320. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  31321. byte *pbuf = NULL;
  31322. word32 len = 0;
  31323. (void) ex_hash2;
  31324. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  31325. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  31326. ExpectNotNull(name = X509_get_issuer_name(x509));
  31327. /* When output buffer is NULL, should return necessary output buffer
  31328. * length.*/
  31329. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  31330. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  31331. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  31332. hash = (((unsigned long)digest[3] << 24) |
  31333. ((unsigned long)digest[2] << 16) |
  31334. ((unsigned long)digest[1] << 8) |
  31335. ((unsigned long)digest[0]));
  31336. ExpectIntEQ(hash, ex_hash1);
  31337. if (file != XBADFILE) {
  31338. XFCLOSE(file);
  31339. file = XBADFILE;
  31340. }
  31341. X509_free(x509);
  31342. x509 = NULL;
  31343. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  31344. pbuf = NULL;
  31345. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  31346. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  31347. ExpectNotNull(name = X509_get_issuer_name(x509));
  31348. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  31349. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  31350. hash = (((unsigned long)digest[3] << 24) |
  31351. ((unsigned long)digest[2] << 16) |
  31352. ((unsigned long)digest[1] << 8) |
  31353. ((unsigned long)digest[0]));
  31354. ExpectIntEQ(hash, ex_hash2);
  31355. if (file != XBADFILE)
  31356. XFCLOSE(file);
  31357. X509_free(x509);
  31358. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  31359. #endif
  31360. return EXPECT_RESULT();
  31361. }
  31362. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  31363. {
  31364. EXPECT_DECLS;
  31365. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  31366. const int MAX_DIR = 4;
  31367. const char paths[][32] = {
  31368. "./certs/ed25519",
  31369. "./certs/ecc",
  31370. "./certs/crl",
  31371. "./certs/",
  31372. };
  31373. char CertCrl_path[MAX_FILENAME_SZ];
  31374. char *p;
  31375. X509_STORE* str = NULL;
  31376. X509_LOOKUP* lookup = NULL;
  31377. WOLFSSL_STACK* sk = NULL;
  31378. int len, total_len, i;
  31379. (void)sk;
  31380. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  31381. /* illegal string */
  31382. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31383. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31384. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  31385. SSL_FILETYPE_PEM,NULL), 0);
  31386. /* free store */
  31387. X509_STORE_free(str);
  31388. str = NULL;
  31389. /* short folder string */
  31390. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31391. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31392. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  31393. SSL_FILETYPE_PEM,NULL), 1);
  31394. #if defined(WOLFSSL_INT_H)
  31395. /* only available when including internal.h */
  31396. ExpectNotNull(sk = lookup->dirs->dir_entry);
  31397. #endif
  31398. /* free store */
  31399. X509_STORE_free(str);
  31400. str = NULL;
  31401. /* typical function check */
  31402. p = &CertCrl_path[0];
  31403. total_len = 0;
  31404. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  31405. len = (int)XSTRLEN((const char*)&paths[i]);
  31406. total_len += len;
  31407. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  31408. p += len;
  31409. if (i != 0) *(p++) = SEPARATOR_CHAR;
  31410. }
  31411. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31412. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31413. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  31414. SSL_FILETYPE_PEM,NULL), 1);
  31415. #if defined(WOLFSSL_INT_H)
  31416. /* only available when including internal.h */
  31417. ExpectNotNull(sk = lookup->dirs->dir_entry);
  31418. #endif
  31419. X509_STORE_free(str);
  31420. #endif
  31421. return EXPECT_RESULT();
  31422. }
  31423. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  31424. {
  31425. EXPECT_DECLS;
  31426. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31427. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31428. defined(WOLFSSL_SIGNER_DER_CERT)
  31429. X509_STORE_CTX* ctx = NULL;
  31430. X509_STORE* str = NULL;
  31431. X509_LOOKUP* lookup = NULL;
  31432. X509* cert1 = NULL;
  31433. X509* x509Ca = NULL;
  31434. X509* x509Svr = NULL;
  31435. X509* issuer = NULL;
  31436. WOLFSSL_STACK* sk = NULL;
  31437. X509_NAME* caName = NULL;
  31438. X509_NAME* issuerName = NULL;
  31439. XFILE file1 = XBADFILE;
  31440. int i;
  31441. int cert_count = 0;
  31442. int cmp;
  31443. char der[] = "certs/ca-cert.der";
  31444. #ifdef HAVE_CRL
  31445. char pem[][100] = {
  31446. "./certs/crl/crl.pem",
  31447. "./certs/crl/crl2.pem",
  31448. "./certs/crl/caEccCrl.pem",
  31449. "./certs/crl/eccCliCRL.pem",
  31450. "./certs/crl/eccSrvCRL.pem",
  31451. ""
  31452. };
  31453. #endif
  31454. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  31455. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  31456. if (file1 != XBADFILE)
  31457. XFCLOSE(file1);
  31458. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31459. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31460. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31461. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  31462. SSL_FILETYPE_PEM,NULL), 1);
  31463. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  31464. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  31465. /* check if CA cert is loaded into the store */
  31466. for (i = 0; i < cert_count; i++) {
  31467. x509Ca = sk_X509_value(sk, i);
  31468. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  31469. }
  31470. ExpectNotNull((x509Svr =
  31471. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31472. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  31473. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  31474. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  31475. ExpectNotNull(issuer);
  31476. caName = X509_get_subject_name(x509Ca);
  31477. ExpectNotNull(caName);
  31478. issuerName = X509_get_subject_name(issuer);
  31479. ExpectNotNull(issuerName);
  31480. cmp = X509_NAME_cmp(caName, issuerName);
  31481. ExpectIntEQ(cmp, 0);
  31482. /* load der format */
  31483. X509_free(issuer);
  31484. issuer = NULL;
  31485. X509_STORE_CTX_free(ctx);
  31486. ctx = NULL;
  31487. X509_STORE_free(str);
  31488. str = NULL;
  31489. sk_X509_pop_free(sk, NULL);
  31490. sk = NULL;
  31491. X509_free(x509Svr);
  31492. x509Svr = NULL;
  31493. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31494. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31495. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  31496. SSL_FILETYPE_ASN1,NULL), 1);
  31497. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  31498. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  31499. /* check if CA cert is loaded into the store */
  31500. for (i = 0; i < cert_count; i++) {
  31501. x509Ca = sk_X509_value(sk, i);
  31502. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  31503. }
  31504. X509_STORE_free(str);
  31505. str = NULL;
  31506. sk_X509_pop_free(sk, NULL);
  31507. sk = NULL;
  31508. X509_free(cert1);
  31509. cert1 = NULL;
  31510. #ifdef HAVE_CRL
  31511. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  31512. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31513. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  31514. SSL_FILETYPE_PEM,NULL), 1);
  31515. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  31516. "certs/server-revoked-cert.pem",
  31517. SSL_FILETYPE_PEM,NULL), 1);
  31518. if (str) {
  31519. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  31520. WOLFSSL_FILETYPE_PEM), 1);
  31521. /* since store hasn't yet known the revoked cert*/
  31522. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31523. "certs/server-revoked-cert.pem",
  31524. WOLFSSL_FILETYPE_PEM), 1);
  31525. }
  31526. for (i = 0; pem[i][0] != '\0'; i++)
  31527. {
  31528. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  31529. SSL_FILETYPE_PEM, NULL), 1);
  31530. }
  31531. if (str) {
  31532. /* since store knows crl list */
  31533. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31534. "certs/server-revoked-cert.pem",
  31535. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  31536. }
  31537. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  31538. X509_STORE_free(str);
  31539. #endif
  31540. #endif
  31541. return EXPECT_RESULT();
  31542. }
  31543. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  31544. {
  31545. int res = TEST_SKIPPED;
  31546. #if defined(OPENSSL_EXTRA)
  31547. X509_STORE_CTX_cleanup(NULL);
  31548. X509_STORE_CTX_trusted_stack(NULL, NULL);
  31549. res = TEST_SUCCESS;
  31550. #endif
  31551. return res;
  31552. }
  31553. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  31554. {
  31555. EXPECT_DECLS;
  31556. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31557. X509_STORE_CTX* ctx = NULL;
  31558. X509_STORE* str = NULL;
  31559. X509* x509Ca = NULL;
  31560. X509* x509Svr = NULL;
  31561. X509* issuer = NULL;
  31562. X509_NAME* caName = NULL;
  31563. X509_NAME* issuerName = NULL;
  31564. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31565. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31566. ExpectNotNull((x509Ca =
  31567. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  31568. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  31569. ExpectNotNull((x509Svr =
  31570. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31571. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  31572. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  31573. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  31574. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  31575. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  31576. #ifdef WOLFSSL_SIGNER_DER_CERT
  31577. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  31578. #endif
  31579. X509_free(issuer);
  31580. X509_STORE_CTX_free(ctx);
  31581. X509_free(x509Svr);
  31582. X509_STORE_free(str);
  31583. X509_free(x509Ca);
  31584. #endif
  31585. return EXPECT_RESULT();
  31586. }
  31587. static int test_wolfSSL_PKCS7_certs(void)
  31588. {
  31589. EXPECT_DECLS;
  31590. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  31591. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  31592. STACK_OF(X509)* sk = NULL;
  31593. STACK_OF(X509_INFO)* info_sk = NULL;
  31594. PKCS7 *p7 = NULL;
  31595. BIO* bio = NULL;
  31596. const byte* p = NULL;
  31597. int buflen = 0;
  31598. int i;
  31599. /* Test twice. Once with d2i and once without to test
  31600. * that everything is free'd correctly. */
  31601. for (i = 0; i < 2; i++) {
  31602. ExpectNotNull(p7 = PKCS7_new());
  31603. if (p7 != NULL) {
  31604. p7->version = 1;
  31605. #ifdef NO_SHA
  31606. p7->hashOID = SHA256h;
  31607. #else
  31608. p7->hashOID = SHAh;
  31609. #endif
  31610. }
  31611. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  31612. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  31613. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  31614. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  31615. ExpectNotNull(sk = sk_X509_new_null());
  31616. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  31617. X509_INFO* info = NULL;
  31618. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  31619. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  31620. if (EXPECT_SUCCESS() && (info != NULL)) {
  31621. info->x509 = NULL;
  31622. }
  31623. X509_INFO_free(info);
  31624. }
  31625. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  31626. info_sk = NULL;
  31627. BIO_free(bio);
  31628. bio = NULL;
  31629. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  31630. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  31631. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  31632. sk_X509_pop_free(sk, X509_free);
  31633. }
  31634. sk = NULL;
  31635. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  31636. if (i == 0) {
  31637. PKCS7_free(p7);
  31638. p7 = NULL;
  31639. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  31640. if (p7 != NULL) {
  31641. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  31642. * them */
  31643. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  31644. }
  31645. /* PKCS7_free free's the certs */
  31646. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  31647. }
  31648. BIO_free(bio);
  31649. bio = NULL;
  31650. PKCS7_free(p7);
  31651. p7 = NULL;
  31652. }
  31653. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31654. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  31655. return EXPECT_RESULT();
  31656. }
  31657. static int test_wolfSSL_X509_STORE_CTX(void)
  31658. {
  31659. EXPECT_DECLS;
  31660. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31661. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31662. X509_STORE_CTX* ctx = NULL;
  31663. X509_STORE* str = NULL;
  31664. X509* x509 = NULL;
  31665. #ifdef OPENSSL_ALL
  31666. X509* x5092 = NULL;
  31667. STACK_OF(X509) *sk = NULL;
  31668. STACK_OF(X509) *sk2 = NULL;
  31669. STACK_OF(X509) *sk3 = NULL;
  31670. #endif
  31671. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31672. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31673. ExpectNotNull((x509 =
  31674. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31675. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  31676. #ifdef OPENSSL_ALL
  31677. /* sk_X509_new only in OPENSSL_ALL */
  31678. sk = sk_X509_new_null();
  31679. ExpectNotNull(sk);
  31680. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  31681. #else
  31682. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  31683. #endif
  31684. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  31685. X509_STORE_CTX_set_error(ctx, -5);
  31686. X509_STORE_CTX_set_error(NULL, -5);
  31687. X509_STORE_CTX_free(ctx);
  31688. ctx = NULL;
  31689. #ifdef OPENSSL_ALL
  31690. sk_X509_pop_free(sk, NULL);
  31691. sk = NULL;
  31692. #endif
  31693. X509_STORE_free(str);
  31694. str = NULL;
  31695. X509_free(x509);
  31696. x509 = NULL;
  31697. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31698. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  31699. X509_STORE_CTX_free(ctx);
  31700. ctx = NULL;
  31701. #ifdef OPENSSL_ALL
  31702. /* test X509_STORE_CTX_get(1)_chain */
  31703. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  31704. SSL_FILETYPE_PEM)));
  31705. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  31706. SSL_FILETYPE_PEM)));
  31707. ExpectNotNull((sk = sk_X509_new_null()));
  31708. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  31709. if (EXPECT_FAIL()) {
  31710. X509_free(x509);
  31711. x509 = NULL;
  31712. }
  31713. ExpectNotNull((str = X509_STORE_new()));
  31714. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  31715. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  31716. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  31717. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  31718. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  31719. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  31720. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  31721. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  31722. X509_STORE_CTX_free(ctx);
  31723. ctx = NULL;
  31724. X509_STORE_free(str);
  31725. str = NULL;
  31726. /* CTX certs not freed yet */
  31727. X509_free(x5092);
  31728. x5092 = NULL;
  31729. sk_X509_pop_free(sk, NULL);
  31730. sk = NULL;
  31731. /* sk3 is dup so free here */
  31732. sk_X509_pop_free(sk3, NULL);
  31733. sk3 = NULL;
  31734. #endif
  31735. /* test X509_STORE_CTX_get/set_ex_data */
  31736. {
  31737. int i = 0, tmpData = 5;
  31738. void* tmpDataRet;
  31739. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31740. #ifdef HAVE_EX_DATA
  31741. for (i = 0; i < MAX_EX_DATA; i++) {
  31742. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31743. WOLFSSL_SUCCESS);
  31744. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31745. ExpectNotNull(tmpDataRet);
  31746. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31747. }
  31748. #else
  31749. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31750. WOLFSSL_FAILURE);
  31751. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31752. ExpectNull(tmpDataRet);
  31753. #endif
  31754. X509_STORE_CTX_free(ctx);
  31755. ctx = NULL;
  31756. }
  31757. /* test X509_STORE_get/set_ex_data */
  31758. {
  31759. int i = 0, tmpData = 99;
  31760. void* tmpDataRet;
  31761. ExpectNotNull(str = X509_STORE_new());
  31762. #ifdef HAVE_EX_DATA
  31763. for (i = 0; i < MAX_EX_DATA; i++) {
  31764. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31765. WOLFSSL_SUCCESS);
  31766. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31767. ExpectNotNull(tmpDataRet);
  31768. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31769. }
  31770. #else
  31771. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31772. WOLFSSL_FAILURE);
  31773. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31774. ExpectNull(tmpDataRet);
  31775. #endif
  31776. X509_STORE_free(str);
  31777. str = NULL;
  31778. }
  31779. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31780. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31781. return EXPECT_RESULT();
  31782. }
  31783. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31784. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  31785. STACK_OF(X509)* chain)
  31786. {
  31787. EXPECT_DECLS;
  31788. XFILE fp = XBADFILE;
  31789. X509* cert = NULL;
  31790. ExpectTrue((fp = XFOPEN(filename, "rb"))
  31791. != XBADFILE);
  31792. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31793. if (fp != XBADFILE) {
  31794. XFCLOSE(fp);
  31795. fp = XBADFILE;
  31796. }
  31797. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  31798. if (EXPECT_FAIL())
  31799. X509_free(cert);
  31800. return EXPECT_RESULT();
  31801. }
  31802. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  31803. int err, int loadCA)
  31804. {
  31805. EXPECT_DECLS;
  31806. X509_STORE_CTX* ctx = NULL;
  31807. X509_STORE* str = NULL;
  31808. XFILE fp = XBADFILE;
  31809. X509* cert = NULL;
  31810. STACK_OF(X509)* untrusted = NULL;
  31811. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  31812. != XBADFILE);
  31813. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31814. if (fp != XBADFILE) {
  31815. XFCLOSE(fp);
  31816. fp = XBADFILE;
  31817. }
  31818. ExpectNotNull(str = X509_STORE_new());
  31819. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31820. ExpectNotNull(untrusted = sk_X509_new_null());
  31821. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  31822. if (loadCA) {
  31823. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  31824. 1);
  31825. }
  31826. for (; *filenames; filenames++) {
  31827. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  31828. untrusted), TEST_SUCCESS);
  31829. }
  31830. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  31831. ExpectIntEQ(X509_verify_cert(ctx), ret);
  31832. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  31833. X509_free(cert);
  31834. X509_STORE_free(str);
  31835. X509_STORE_CTX_free(ctx);
  31836. sk_X509_pop_free(untrusted, NULL);
  31837. return EXPECT_RESULT();
  31838. }
  31839. #endif
  31840. static int test_X509_STORE_untrusted(void)
  31841. {
  31842. EXPECT_DECLS;
  31843. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31844. const char* untrusted1[] = {
  31845. "./certs/intermediate/ca-int2-cert.pem",
  31846. NULL
  31847. };
  31848. const char* untrusted2[] = {
  31849. "./certs/intermediate/ca-int-cert.pem",
  31850. "./certs/intermediate/ca-int2-cert.pem",
  31851. NULL
  31852. };
  31853. const char* untrusted3[] = {
  31854. "./certs/intermediate/ca-int-cert.pem",
  31855. "./certs/intermediate/ca-int2-cert.pem",
  31856. "./certs/ca-cert.pem",
  31857. NULL
  31858. };
  31859. /* Adding unrelated certs that should be ignored */
  31860. const char* untrusted4[] = {
  31861. "./certs/client-ca.pem",
  31862. "./certs/intermediate/ca-int-cert.pem",
  31863. "./certs/server-cert.pem",
  31864. "./certs/intermediate/ca-int2-cert.pem",
  31865. NULL
  31866. };
  31867. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  31868. * to loaded CA. */
  31869. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  31870. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  31871. /* Succeeds because path to loaded CA is available. */
  31872. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  31873. TEST_SUCCESS);
  31874. /* Fails because root CA is in the untrusted stack */
  31875. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  31876. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  31877. /* Succeeds because path to loaded CA is available. */
  31878. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  31879. TEST_SUCCESS);
  31880. #endif
  31881. return EXPECT_RESULT();
  31882. }
  31883. static int test_wolfSSL_X509_STORE_set_flags(void)
  31884. {
  31885. EXPECT_DECLS;
  31886. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31887. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31888. X509_STORE* store = NULL;
  31889. X509* x509 = NULL;
  31890. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  31891. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  31892. WOLFSSL_FILETYPE_PEM)));
  31893. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  31894. #ifdef HAVE_CRL
  31895. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31896. WOLFSSL_SUCCESS);
  31897. #else
  31898. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31899. NOT_COMPILED_IN);
  31900. #endif
  31901. wolfSSL_X509_free(x509);
  31902. wolfSSL_X509_STORE_free(store);
  31903. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  31904. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31905. return EXPECT_RESULT();
  31906. }
  31907. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  31908. {
  31909. EXPECT_DECLS;
  31910. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  31911. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31912. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  31913. WOLFSSL_X509_STORE* store = NULL;
  31914. WOLFSSL_X509_LOOKUP* lookup = NULL;
  31915. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  31916. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  31917. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  31918. X509_FILETYPE_PEM), 1);
  31919. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  31920. X509_FILETYPE_PEM), 1);
  31921. if (store != NULL) {
  31922. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  31923. WOLFSSL_FILETYPE_PEM), 1);
  31924. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31925. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  31926. }
  31927. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  31928. X509_FILETYPE_PEM), 1);
  31929. if (store != NULL) {
  31930. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31931. WOLFSSL_FILETYPE_PEM), 1);
  31932. }
  31933. wolfSSL_X509_STORE_free(store);
  31934. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  31935. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31936. return EXPECT_RESULT();
  31937. }
  31938. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  31939. {
  31940. EXPECT_DECLS;
  31941. #if defined(OPENSSL_EXTRA)
  31942. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  31943. time_t c_time;
  31944. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  31945. c_time = 365*24*60*60;
  31946. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  31947. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  31948. WOLFSSL_USE_CHECK_TIME);
  31949. ExpectTrue(ctx->param->check_time == c_time);
  31950. wolfSSL_X509_STORE_CTX_free(ctx);
  31951. #endif /* OPENSSL_EXTRA */
  31952. return EXPECT_RESULT();
  31953. }
  31954. static int test_wolfSSL_CTX_get0_set1_param(void)
  31955. {
  31956. EXPECT_DECLS;
  31957. #if defined(OPENSSL_EXTRA)
  31958. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31959. SSL_CTX* ctx = NULL;
  31960. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31961. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  31962. char testIPv4[] = "127.0.0.1";
  31963. char testhostName[] = "foo.hoge.com";
  31964. #ifndef NO_WOLFSSL_SERVER
  31965. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31966. #else
  31967. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31968. #endif
  31969. ExpectNull(SSL_CTX_get0_param(NULL));
  31970. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  31971. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  31972. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  31973. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  31974. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  31975. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  31976. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  31977. WOLFSSL_SUCCESS);
  31978. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  31979. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  31980. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  31981. (int)XSTRLEN(testhostName)));
  31982. ExpectIntEQ(0x01, pParam->hostFlags);
  31983. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31984. /* test for incorrect parameter */
  31985. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  31986. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  31987. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  31988. SSL_CTX_free(ctx);
  31989. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  31990. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31991. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31992. return EXPECT_RESULT();
  31993. }
  31994. static int test_wolfSSL_get0_param(void)
  31995. {
  31996. EXPECT_DECLS;
  31997. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31998. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31999. SSL_CTX* ctx = NULL;
  32000. SSL* ssl = NULL;
  32001. #ifndef NO_WOLFSSL_SERVER
  32002. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32003. #else
  32004. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32005. #endif
  32006. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32007. SSL_FILETYPE_PEM));
  32008. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32009. ExpectNotNull(ssl = SSL_new(ctx));
  32010. ExpectNotNull(SSL_get0_param(ssl));
  32011. SSL_free(ssl);
  32012. SSL_CTX_free(ctx);
  32013. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32014. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  32015. return EXPECT_RESULT();
  32016. }
  32017. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  32018. {
  32019. EXPECT_DECLS;
  32020. #if defined(OPENSSL_EXTRA)
  32021. const char host[] = "www.example.com";
  32022. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  32023. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  32024. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32025. if (pParam != NULL) {
  32026. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  32027. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  32028. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32029. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  32030. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32031. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32032. }
  32033. #endif /* OPENSSL_EXTRA */
  32034. return EXPECT_RESULT();
  32035. }
  32036. static int test_wolfSSL_set1_host(void)
  32037. {
  32038. EXPECT_DECLS;
  32039. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32040. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32041. const char host[] = "www.test_wolfSSL_set1_host.com";
  32042. const char emptyStr[] = "";
  32043. SSL_CTX* ctx = NULL;
  32044. SSL* ssl = NULL;
  32045. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  32046. #ifndef NO_WOLFSSL_SERVER
  32047. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32048. #else
  32049. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32050. #endif
  32051. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32052. SSL_FILETYPE_PEM));
  32053. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32054. ExpectNotNull(ssl = SSL_new(ctx));
  32055. pParam = SSL_get0_param(ssl);
  32056. /* we should get back host string */
  32057. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  32058. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32059. /* we should get back empty string */
  32060. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  32061. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  32062. /* we should get back host string */
  32063. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  32064. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32065. /* we should get back empty string */
  32066. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  32067. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  32068. SSL_free(ssl);
  32069. SSL_CTX_free(ctx);
  32070. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32071. #endif /* OPENSSL_EXTRA */
  32072. return EXPECT_RESULT();
  32073. }
  32074. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  32075. {
  32076. EXPECT_DECLS;
  32077. #if defined(OPENSSL_EXTRA)
  32078. unsigned char buf[16] = {0};
  32079. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  32080. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  32081. /* test 127.0.0.1 */
  32082. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  32083. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  32084. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  32085. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  32086. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32087. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  32088. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  32089. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  32090. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32091. ExpectIntEQ(XSTRNCMP(param->ipasc,
  32092. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  32093. /* test 2001:db8:: */
  32094. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32095. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32096. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32097. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  32098. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32099. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  32100. /* test ::1234:5678 */
  32101. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  32102. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32103. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32104. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  32105. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32106. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  32107. /* test 2001:db8::1234:5678 */
  32108. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32109. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32110. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32111. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  32112. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32113. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  32114. sizeof(param->ipasc)), 0);
  32115. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  32116. /* 2001:db8:1::ab9:c0a8:102 */
  32117. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32118. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  32119. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  32120. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  32121. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32122. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  32123. sizeof(param->ipasc)), 0);
  32124. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32125. #endif /* OPENSSL_EXTRA */
  32126. return EXPECT_RESULT();
  32127. }
  32128. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  32129. {
  32130. EXPECT_DECLS;
  32131. #if defined(OPENSSL_EXTRA)
  32132. X509_STORE* store = NULL;
  32133. X509_STORE_CTX* ctx = NULL;
  32134. X509_STORE_CTX* ctx_no_init = NULL;
  32135. ExpectNotNull((store = X509_STORE_new()));
  32136. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32137. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  32138. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  32139. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  32140. /* should return NULL if ctx has not bee initialized */
  32141. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  32142. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  32143. wolfSSL_X509_STORE_CTX_free(ctx);
  32144. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  32145. X509_STORE_free(store);
  32146. #endif /* OPENSSL_EXTRA */
  32147. return EXPECT_RESULT();
  32148. }
  32149. static int test_wolfSSL_CTX_set_client_CA_list(void)
  32150. {
  32151. EXPECT_DECLS;
  32152. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32153. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  32154. WOLFSSL_CTX* ctx = NULL;
  32155. WOLFSSL* ssl = NULL;
  32156. X509_NAME* name = NULL;
  32157. STACK_OF(X509_NAME)* names = NULL;
  32158. STACK_OF(X509_NAME)* ca_list = NULL;
  32159. int names_len = 0;
  32160. int i;
  32161. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  32162. /* Send two X501 names in cert request */
  32163. names = SSL_load_client_CA_file(cliCertFile);
  32164. ExpectNotNull(names);
  32165. ca_list = SSL_load_client_CA_file(caCertFile);
  32166. ExpectNotNull(ca_list);
  32167. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  32168. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  32169. if (EXPECT_FAIL()) {
  32170. wolfSSL_X509_NAME_free(name);
  32171. name = NULL;
  32172. }
  32173. SSL_CTX_set_client_CA_list(ctx, names);
  32174. /* This should only free the stack structure */
  32175. sk_X509_NAME_free(ca_list);
  32176. ca_list = NULL;
  32177. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  32178. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  32179. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  32180. for (i = 0; i < names_len; i++) {
  32181. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  32182. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  32183. }
  32184. /* Needed to be able to create ssl object */
  32185. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32186. SSL_FILETYPE_PEM));
  32187. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32188. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32189. /* load again as old names are responsibility of ctx to free*/
  32190. names = SSL_load_client_CA_file(cliCertFile);
  32191. ExpectNotNull(names);
  32192. SSL_set_client_CA_list(ssl, names);
  32193. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  32194. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  32195. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  32196. for (i = 0; i < names_len; i++) {
  32197. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  32198. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  32199. }
  32200. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  32201. {
  32202. tcp_ready ready;
  32203. func_args server_args;
  32204. callback_functions server_cb;
  32205. THREAD_TYPE serverThread;
  32206. WOLFSSL* ssl_client = NULL;
  32207. WOLFSSL_CTX* ctx_client = NULL;
  32208. SOCKET_T sockfd = 0;
  32209. /* wolfSSL_get_client_CA_list() with handshake */
  32210. StartTCP();
  32211. InitTcpReady(&ready);
  32212. XMEMSET(&server_args, 0, sizeof(func_args));
  32213. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32214. server_args.signal = &ready;
  32215. server_args.callbacks = &server_cb;
  32216. /* we are responsible for free'ing WOLFSSL_CTX */
  32217. server_cb.ctx = ctx;
  32218. server_cb.isSharedCtx = 1;
  32219. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  32220. cliCertFile, 0));
  32221. start_thread(test_server_nofail, &server_args, &serverThread);
  32222. wait_tcp_ready(&server_args);
  32223. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32224. ExpectNotNull(ctx_client =
  32225. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  32226. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  32227. ctx_client, caCertFile, 0));
  32228. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  32229. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  32230. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  32231. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  32232. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  32233. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  32234. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  32235. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  32236. /* We are expecting two cert names to be sent */
  32237. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  32238. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  32239. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  32240. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  32241. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  32242. }
  32243. wolfSSL_shutdown(ssl_client);
  32244. wolfSSL_free(ssl_client);
  32245. wolfSSL_CTX_free(ctx_client);
  32246. CloseSocket(sockfd);
  32247. join_thread(serverThread);
  32248. FreeTcpReady(&ready);
  32249. }
  32250. #endif
  32251. wolfSSL_free(ssl);
  32252. wolfSSL_CTX_free(ctx);
  32253. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  32254. * !NO_BIO */
  32255. return EXPECT_RESULT();
  32256. }
  32257. static int test_wolfSSL_CTX_add_client_CA(void)
  32258. {
  32259. EXPECT_DECLS;
  32260. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32261. !defined(NO_WOLFSSL_CLIENT)
  32262. WOLFSSL_CTX* ctx = NULL;
  32263. WOLFSSL_X509* x509 = NULL;
  32264. WOLFSSL_X509* x509_a = NULL;
  32265. STACK_OF(X509_NAME)* ca_list = NULL;
  32266. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32267. /* Add client cert */
  32268. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  32269. SSL_FILETYPE_PEM));
  32270. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  32271. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  32272. /* Add another client cert */
  32273. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  32274. SSL_FILETYPE_PEM));
  32275. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  32276. /* test for incorrect parameter */
  32277. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  32278. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  32279. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  32280. X509_free(x509);
  32281. X509_free(x509_a);
  32282. SSL_CTX_free(ctx);
  32283. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  32284. return EXPECT_RESULT();
  32285. }
  32286. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  32287. defined(HAVE_IO_TESTS_DEPENDENCIES)
  32288. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  32289. {
  32290. callback_functions* callbacks = ((func_args*)args)->callbacks;
  32291. WOLFSSL_CTX* ctx = callbacks->ctx;
  32292. WOLFSSL* ssl = NULL;
  32293. SOCKET_T sfd = 0;
  32294. SOCKET_T cfd = 0;
  32295. word16 port;
  32296. char input[1024];
  32297. int idx;
  32298. int ret, err = 0;
  32299. const char* privateName = "ech-private-name.com";
  32300. int privateNameLen = (int)XSTRLEN(privateName);
  32301. ((func_args*)args)->return_code = TEST_FAIL;
  32302. port = ((func_args*)args)->signal->port;
  32303. AssertIntEQ(WOLFSSL_SUCCESS,
  32304. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  32305. AssertIntEQ(WOLFSSL_SUCCESS,
  32306. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  32307. WOLFSSL_FILETYPE_PEM));
  32308. AssertIntEQ(WOLFSSL_SUCCESS,
  32309. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32310. WOLFSSL_FILETYPE_PEM));
  32311. if (callbacks->ctx_ready)
  32312. callbacks->ctx_ready(ctx);
  32313. ssl = wolfSSL_new(ctx);
  32314. /* set the sni for the server */
  32315. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  32316. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  32317. CloseSocket(sfd);
  32318. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  32319. if (callbacks->ssl_ready)
  32320. callbacks->ssl_ready(ssl);
  32321. do {
  32322. err = 0; /* Reset error */
  32323. ret = wolfSSL_accept(ssl);
  32324. if (ret != WOLFSSL_SUCCESS) {
  32325. err = wolfSSL_get_error(ssl, 0);
  32326. }
  32327. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  32328. if (ret != WOLFSSL_SUCCESS) {
  32329. char buff[WOLFSSL_MAX_ERROR_SZ];
  32330. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  32331. }
  32332. else {
  32333. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  32334. input[idx] = 0;
  32335. fprintf(stderr, "Client message: %s\n", input);
  32336. }
  32337. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  32338. privateNameLen));
  32339. ((func_args*)args)->return_code = TEST_SUCCESS;
  32340. }
  32341. if (callbacks->on_result)
  32342. callbacks->on_result(ssl);
  32343. wolfSSL_shutdown(ssl);
  32344. wolfSSL_free(ssl);
  32345. wolfSSL_CTX_free(ctx);
  32346. CloseSocket(cfd);
  32347. #ifdef FP_ECC
  32348. wc_ecc_fp_free();
  32349. #endif
  32350. WOLFSSL_RETURN_FROM_THREAD(0);
  32351. }
  32352. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32353. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32354. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  32355. {
  32356. AssertNotNull(ssl);
  32357. AssertNotNull(line);
  32358. XFILE fp;
  32359. const byte lf = '\n';
  32360. fp = XFOPEN("./MyKeyLog.txt", "a");
  32361. XFWRITE( line, 1, strlen(line),fp);
  32362. XFWRITE( (void*)&lf,1,1,fp);
  32363. XFFLUSH(fp);
  32364. XFCLOSE(fp);
  32365. }
  32366. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  32367. static int test_wolfSSL_CTX_set_keylog_callback(void)
  32368. {
  32369. EXPECT_DECLS;
  32370. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  32371. !defined(NO_WOLFSSL_CLIENT)
  32372. SSL_CTX* ctx = NULL;
  32373. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32374. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  32375. SSL_CTX_free(ctx);
  32376. SSL_CTX_set_keylog_callback(NULL, NULL);
  32377. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  32378. return EXPECT_RESULT();
  32379. }
  32380. static int test_wolfSSL_CTX_get_keylog_callback(void)
  32381. {
  32382. EXPECT_DECLS;
  32383. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  32384. !defined(NO_WOLFSSL_CLIENT)
  32385. SSL_CTX* ctx = NULL;
  32386. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32387. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  32388. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  32389. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  32390. SSL_CTX_set_keylog_callback(ctx, NULL );
  32391. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  32392. SSL_CTX_free(ctx);
  32393. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  32394. return EXPECT_RESULT();
  32395. }
  32396. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32397. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  32398. {
  32399. /* set keylog callback */
  32400. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  32401. return TEST_SUCCESS;
  32402. }
  32403. #endif
  32404. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  32405. {
  32406. EXPECT_DECLS;
  32407. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32408. /* This test is intended for checking whether keylog callback is called
  32409. * in client during TLS handshake between the client and a server.
  32410. */
  32411. test_ssl_cbf server_cbf;
  32412. test_ssl_cbf client_cbf;
  32413. XFILE fp = XBADFILE;
  32414. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  32415. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  32416. server_cbf.method = wolfTLSv1_2_server_method;
  32417. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  32418. /* clean up keylog file */
  32419. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  32420. if (fp != XBADFILE) {
  32421. XFFLUSH(fp);
  32422. XFCLOSE(fp);
  32423. fp = XBADFILE;
  32424. }
  32425. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32426. &server_cbf, NULL), TEST_SUCCESS);
  32427. XSLEEP_MS(100);
  32428. /* check if the keylog file exists */
  32429. char buff[300] = {0};
  32430. int found = 0;
  32431. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  32432. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  32433. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  32434. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  32435. found = 1;
  32436. break;
  32437. }
  32438. }
  32439. if (fp != XBADFILE) {
  32440. XFCLOSE(fp);
  32441. }
  32442. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  32443. ExpectIntEQ(found, 1);
  32444. /* clean up */
  32445. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  32446. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  32447. return EXPECT_RESULT();
  32448. }
  32449. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  32450. defined(HAVE_SECRET_CALLBACK)
  32451. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  32452. {
  32453. /* set keylog callback */
  32454. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  32455. return TEST_SUCCESS;
  32456. }
  32457. #endif
  32458. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  32459. {
  32460. EXPECT_DECLS;
  32461. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  32462. defined(HAVE_SECRET_CALLBACK)
  32463. /* This test is intended for checking whether keylog callback is called
  32464. * in client during TLS handshake between the client and a server.
  32465. */
  32466. test_ssl_cbf server_cbf;
  32467. test_ssl_cbf client_cbf;
  32468. XFILE fp = XBADFILE;
  32469. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  32470. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  32471. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  32472. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  32473. /* clean up keylog file */
  32474. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  32475. if (fp != XBADFILE) {
  32476. XFCLOSE(fp);
  32477. fp = XBADFILE;
  32478. }
  32479. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32480. &server_cbf, NULL), TEST_SUCCESS);
  32481. /* check if the keylog file exists */
  32482. {
  32483. char buff[300] = {0};
  32484. int found[4] = {0};
  32485. int numfnd = 0;
  32486. int i;
  32487. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  32488. while (EXPECT_SUCCESS() &&
  32489. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  32490. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  32491. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  32492. found[0] = 1;
  32493. continue;
  32494. }
  32495. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  32496. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  32497. found[1] = 1;
  32498. continue;
  32499. }
  32500. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  32501. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  32502. found[2] = 1;
  32503. continue;
  32504. }
  32505. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  32506. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  32507. found[3] = 1;
  32508. continue;
  32509. }
  32510. }
  32511. if (fp != XBADFILE)
  32512. XFCLOSE(fp);
  32513. for (i = 0; i < 4; i++) {
  32514. if (found[i] != 0)
  32515. numfnd++;
  32516. }
  32517. ExpectIntEQ(numfnd, 4);
  32518. }
  32519. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  32520. return EXPECT_RESULT();
  32521. }
  32522. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  32523. defined(HAVE_IO_TESTS_DEPENDENCIES)
  32524. static int test_wolfSSL_Tls13_ECH_params(void)
  32525. {
  32526. EXPECT_DECLS;
  32527. #if !defined(NO_WOLFSSL_CLIENT)
  32528. word32 outputLen = 0;
  32529. byte testBuf[72];
  32530. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  32531. WOLFSSL *ssl = wolfSSL_new(ctx);
  32532. ExpectNotNull(ctx);
  32533. ExpectNotNull(ssl);
  32534. /* invalid ctx */
  32535. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  32536. "ech-public-name.com", 0, 0, 0));
  32537. /* invalid public name */
  32538. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  32539. 0, 0));
  32540. /* invalid algorithms */
  32541. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  32542. "ech-public-name.com", 1000, 1000, 1000));
  32543. /* invalid ctx */
  32544. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  32545. &outputLen));
  32546. /* invalid output len */
  32547. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  32548. /* invalid ssl */
  32549. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  32550. (char*)testBuf, sizeof(testBuf)));
  32551. /* invalid configs64 */
  32552. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  32553. sizeof(testBuf)));
  32554. /* invalid size */
  32555. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  32556. (char*)testBuf, 0));
  32557. /* invalid ssl */
  32558. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  32559. sizeof(testBuf)));
  32560. /* invalid configs */
  32561. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  32562. sizeof(testBuf)));
  32563. /* invalid size */
  32564. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  32565. /* invalid ssl */
  32566. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  32567. /* invalid size */
  32568. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  32569. wolfSSL_free(ssl);
  32570. wolfSSL_CTX_free(ctx);
  32571. #endif /* !NO_WOLFSSL_CLIENT */
  32572. return EXPECT_RESULT();
  32573. }
  32574. static int test_wolfSSL_Tls13_ECH(void)
  32575. {
  32576. EXPECT_DECLS;
  32577. tcp_ready ready;
  32578. func_args client_args;
  32579. func_args server_args;
  32580. THREAD_TYPE serverThread;
  32581. callback_functions server_cbf;
  32582. callback_functions client_cbf;
  32583. SOCKET_T sockfd = 0;
  32584. WOLFSSL_CTX* ctx = NULL;
  32585. WOLFSSL* ssl = NULL;
  32586. const char* publicName = "ech-public-name.com";
  32587. const char* privateName = "ech-private-name.com";
  32588. int privateNameLen = 20;
  32589. char reply[1024];
  32590. int replyLen = 0;
  32591. byte rawEchConfig[128];
  32592. word32 rawEchConfigLen = sizeof(rawEchConfig);
  32593. InitTcpReady(&ready);
  32594. ready.port = 22222;
  32595. XMEMSET(&client_args, 0, sizeof(func_args));
  32596. XMEMSET(&server_args, 0, sizeof(func_args));
  32597. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  32598. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  32599. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  32600. /* create the server context here so we can get the ech config */
  32601. ExpectNotNull(server_cbf.ctx =
  32602. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  32603. /* generate ech config */
  32604. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  32605. publicName, 0, 0, 0));
  32606. /* get the config for the client to use */
  32607. ExpectIntEQ(WOLFSSL_SUCCESS,
  32608. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  32609. &rawEchConfigLen));
  32610. server_args.callbacks = &server_cbf;
  32611. server_args.signal = &ready;
  32612. /* start server task */
  32613. start_thread(server_task_ech, &server_args, &serverThread);
  32614. wait_tcp_ready(&server_args);
  32615. /* run as a TLS1.3 client */
  32616. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  32617. ExpectIntEQ(WOLFSSL_SUCCESS,
  32618. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  32619. ExpectIntEQ(WOLFSSL_SUCCESS,
  32620. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32621. ExpectIntEQ(WOLFSSL_SUCCESS,
  32622. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32623. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32624. /* get connected the server task */
  32625. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32626. /* set the ech configs for the client */
  32627. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  32628. rawEchConfigLen));
  32629. /* set the sni for the client */
  32630. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  32631. privateName, privateNameLen));
  32632. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  32633. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32634. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  32635. privateNameLen);
  32636. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  32637. /* add th null terminator for string compare */
  32638. reply[replyLen] = 0;
  32639. /* check that the server replied with the private name */
  32640. ExpectStrEQ(privateName, reply);
  32641. wolfSSL_free(ssl);
  32642. wolfSSL_CTX_free(ctx);
  32643. CloseSocket(sockfd);
  32644. join_thread(serverThread);
  32645. FreeTcpReady(&ready);
  32646. return EXPECT_RESULT();
  32647. }
  32648. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32649. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32650. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32651. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32652. static int post_auth_version_cb(WOLFSSL* ssl)
  32653. {
  32654. EXPECT_DECLS;
  32655. /* do handshake and then test version error */
  32656. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32657. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32658. return EXPECT_RESULT();
  32659. }
  32660. static int post_auth_version_client_cb(WOLFSSL* ssl)
  32661. {
  32662. EXPECT_DECLS;
  32663. /* do handshake and then test version error */
  32664. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32665. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32666. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  32667. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  32668. /* check was added to error queue */
  32669. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  32670. /* check the string matches expected string */
  32671. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  32672. "WRONG_SSL_VERSION");
  32673. #endif
  32674. return EXPECT_RESULT();
  32675. }
  32676. static int post_auth_cb(WOLFSSL* ssl)
  32677. {
  32678. EXPECT_DECLS;
  32679. WOLFSSL_X509* x509 = NULL;
  32680. /* do handshake and then test version error */
  32681. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32682. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  32683. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  32684. wolfSSL_X509_free(x509);
  32685. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  32686. return EXPECT_RESULT();
  32687. }
  32688. static int set_post_auth_cb(WOLFSSL* ssl)
  32689. {
  32690. if (!wolfSSL_is_server(ssl)) {
  32691. EXPECT_DECLS;
  32692. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  32693. return EXPECT_RESULT();
  32694. }
  32695. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  32696. return TEST_SUCCESS;
  32697. }
  32698. #endif
  32699. static int test_wolfSSL_Tls13_postauth(void)
  32700. {
  32701. EXPECT_DECLS;
  32702. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32703. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32704. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32705. test_ssl_cbf server_cbf;
  32706. test_ssl_cbf client_cbf;
  32707. /* test version failure doing post auth with TLS 1.2 connection */
  32708. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32709. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32710. server_cbf.method = wolfTLSv1_2_server_method;
  32711. server_cbf.ssl_ready = set_post_auth_cb;
  32712. server_cbf.on_result = post_auth_version_cb;
  32713. client_cbf.ssl_ready = set_post_auth_cb;
  32714. client_cbf.on_result = post_auth_version_client_cb;
  32715. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32716. &server_cbf, NULL), TEST_SUCCESS);
  32717. /* tests on post auth with TLS 1.3 */
  32718. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32719. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32720. server_cbf.method = wolfTLSv1_3_server_method;
  32721. server_cbf.ssl_ready = set_post_auth_cb;
  32722. client_cbf.ssl_ready = set_post_auth_cb;
  32723. server_cbf.on_result = post_auth_cb;
  32724. client_cbf.on_result = NULL;
  32725. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32726. &server_cbf, NULL), TEST_SUCCESS);
  32727. #endif
  32728. return EXPECT_RESULT();
  32729. }
  32730. static int test_wolfSSL_X509_NID(void)
  32731. {
  32732. EXPECT_DECLS;
  32733. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  32734. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  32735. int sigType;
  32736. int nameSz;
  32737. X509* cert = NULL;
  32738. EVP_PKEY* pubKeyTmp = NULL;
  32739. X509_NAME* name = NULL;
  32740. char commonName[80];
  32741. char countryName[80];
  32742. char localityName[80];
  32743. char stateName[80];
  32744. char orgName[80];
  32745. char orgUnit[80];
  32746. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  32747. /* convert cert from DER to internal WOLFSSL_X509 struct */
  32748. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  32749. sizeof_client_cert_der_2048, HEAP_HINT));
  32750. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  32751. /* extract PUBLIC KEY from cert */
  32752. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  32753. /* extract signatureType */
  32754. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  32755. /* extract subjectName info */
  32756. ExpectNotNull(name = X509_get_subject_name(cert));
  32757. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  32758. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32759. NULL, 0)), 0);
  32760. ExpectIntEQ(nameSz, 15);
  32761. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32762. commonName, sizeof(commonName))), 0);
  32763. ExpectIntEQ(nameSz, 15);
  32764. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  32765. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32766. commonName, 9)), 0);
  32767. ExpectIntEQ(nameSz, 8);
  32768. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  32769. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  32770. countryName, sizeof(countryName))), 0);
  32771. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  32772. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  32773. localityName, sizeof(localityName))), 0);
  32774. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  32775. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32776. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  32777. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  32778. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  32779. orgName, sizeof(orgName))), 0);
  32780. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  32781. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32782. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  32783. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  32784. EVP_PKEY_free(pubKeyTmp);
  32785. X509_free(cert);
  32786. #endif
  32787. return EXPECT_RESULT();
  32788. }
  32789. static int test_wolfSSL_CTX_set_srp_username(void)
  32790. {
  32791. EXPECT_DECLS;
  32792. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  32793. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32794. WOLFSSL_CTX* ctx = NULL;
  32795. WOLFSSL* ssl = NULL;
  32796. const char *username = "TESTUSER";
  32797. const char *password = "TESTPASSWORD";
  32798. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32799. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32800. SSL_SUCCESS);
  32801. wolfSSL_CTX_free(ctx);
  32802. ctx = NULL;
  32803. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32804. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32805. SSL_SUCCESS);
  32806. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32807. SSL_SUCCESS);
  32808. ExpectNotNull(ssl = SSL_new(ctx));
  32809. ExpectNotNull(SSL_get_srp_username(ssl));
  32810. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  32811. wolfSSL_free(ssl);
  32812. wolfSSL_CTX_free(ctx);
  32813. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32814. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32815. return EXPECT_RESULT();
  32816. }
  32817. static int test_wolfSSL_CTX_set_srp_password(void)
  32818. {
  32819. EXPECT_DECLS;
  32820. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  32821. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32822. WOLFSSL_CTX* ctx = NULL;
  32823. const char *username = "TESTUSER";
  32824. const char *password = "TESTPASSWORD";
  32825. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32826. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32827. SSL_SUCCESS);
  32828. wolfSSL_CTX_free(ctx);
  32829. ctx = NULL;
  32830. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32831. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32832. SSL_SUCCESS);
  32833. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32834. SSL_SUCCESS);
  32835. wolfSSL_CTX_free(ctx);
  32836. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32837. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32838. return EXPECT_RESULT();
  32839. }
  32840. static int test_wolfSSL_X509_STORE(void)
  32841. {
  32842. EXPECT_DECLS;
  32843. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32844. X509_STORE *store = NULL;
  32845. #ifdef HAVE_CRL
  32846. X509_STORE_CTX *storeCtx = NULL;
  32847. X509_CRL *crl = NULL;
  32848. X509 *ca = NULL;
  32849. X509 *cert = NULL;
  32850. const char crlPem[] = "./certs/crl/crl.revoked";
  32851. const char srvCert[] = "./certs/server-revoked-cert.pem";
  32852. const char caCert[] = "./certs/ca-cert.pem";
  32853. XFILE fp = XBADFILE;
  32854. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32855. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32856. SSL_FILETYPE_PEM)));
  32857. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32858. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32859. SSL_FILETYPE_PEM)));
  32860. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32861. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32862. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32863. X509_STORE_free(store);
  32864. store = NULL;
  32865. X509_STORE_CTX_free(storeCtx);
  32866. storeCtx = NULL;
  32867. X509_free(cert);
  32868. cert = NULL;
  32869. X509_free(ca);
  32870. ca = NULL;
  32871. /* should fail to verify now after adding in CRL */
  32872. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32873. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32874. SSL_FILETYPE_PEM)));
  32875. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32876. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  32877. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  32878. NULL, NULL));
  32879. if (fp != XBADFILE)
  32880. XFCLOSE(fp);
  32881. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  32882. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  32883. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32884. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32885. SSL_FILETYPE_PEM)));
  32886. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32887. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32888. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  32889. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  32890. X509_CRL_free(crl);
  32891. crl = NULL;
  32892. X509_STORE_free(store);
  32893. store = NULL;
  32894. X509_STORE_CTX_free(storeCtx);
  32895. storeCtx = NULL;
  32896. X509_free(cert);
  32897. cert = NULL;
  32898. X509_free(ca);
  32899. ca = NULL;
  32900. #endif /* HAVE_CRL */
  32901. #ifndef WOLFCRYPT_ONLY
  32902. {
  32903. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32904. SSL_CTX* ctx = NULL;
  32905. SSL* ssl = NULL;
  32906. int i;
  32907. for (i = 0; i < 2; i++) {
  32908. #ifndef NO_WOLFSSL_SERVER
  32909. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32910. #else
  32911. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32912. #endif
  32913. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32914. SSL_CTX_set_cert_store(ctx, store);
  32915. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32916. SSL_CTX_set_cert_store(ctx, store);
  32917. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32918. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32919. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32920. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32921. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32922. ExpectNotNull(ssl = SSL_new(ctx));
  32923. if (i == 0) {
  32924. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  32925. SSL_SUCCESS);
  32926. }
  32927. else {
  32928. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  32929. #ifdef OPENSSL_ALL
  32930. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  32931. #endif
  32932. }
  32933. if (EXPECT_FAIL() || (i == 1)) {
  32934. X509_STORE_free(store);
  32935. store = NULL;
  32936. }
  32937. SSL_free(ssl);
  32938. ssl = NULL;
  32939. SSL_CTX_free(ctx);
  32940. ctx = NULL;
  32941. }
  32942. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32943. }
  32944. #endif
  32945. #endif
  32946. return EXPECT_RESULT();
  32947. }
  32948. static int test_wolfSSL_X509_STORE_load_locations(void)
  32949. {
  32950. EXPECT_DECLS;
  32951. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32952. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32953. SSL_CTX *ctx = NULL;
  32954. X509_STORE *store = NULL;
  32955. const char ca_file[] = "./certs/ca-cert.pem";
  32956. const char client_pem_file[] = "./certs/client-cert.pem";
  32957. const char client_der_file[] = "./certs/client-cert.der";
  32958. const char ecc_file[] = "./certs/ecc-key.pem";
  32959. const char certs_path[] = "./certs/";
  32960. const char bad_path[] = "./bad-path/";
  32961. #ifdef HAVE_CRL
  32962. const char crl_path[] = "./certs/crl/";
  32963. const char crl_file[] = "./certs/crl/crl.pem";
  32964. #endif
  32965. #ifndef NO_WOLFSSL_SERVER
  32966. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32967. #else
  32968. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32969. #endif
  32970. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32971. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  32972. WOLFSSL_SUCCESS);
  32973. /* Test bad arguments */
  32974. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  32975. WOLFSSL_FAILURE);
  32976. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  32977. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  32978. WOLFSSL_FAILURE);
  32979. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  32980. WOLFSSL_FAILURE);
  32981. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  32982. WOLFSSL_FAILURE);
  32983. #ifdef HAVE_CRL
  32984. /* Test with CRL */
  32985. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  32986. WOLFSSL_SUCCESS);
  32987. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  32988. WOLFSSL_SUCCESS);
  32989. #endif
  32990. /* Test with CA */
  32991. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  32992. WOLFSSL_SUCCESS);
  32993. /* Test with client_cert and certs path */
  32994. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  32995. WOLFSSL_SUCCESS);
  32996. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  32997. WOLFSSL_SUCCESS);
  32998. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  32999. /* Clear nodes */
  33000. ERR_clear_error();
  33001. #endif
  33002. SSL_CTX_free(ctx);
  33003. #endif
  33004. return EXPECT_RESULT();
  33005. }
  33006. static int test_X509_STORE_get0_objects(void)
  33007. {
  33008. EXPECT_DECLS;
  33009. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  33010. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  33011. X509_STORE *store = NULL;
  33012. X509_STORE *store_cpy = NULL;
  33013. SSL_CTX *ctx = NULL;
  33014. X509_OBJECT *obj = NULL;
  33015. STACK_OF(X509_OBJECT) *objs = NULL;
  33016. int i;
  33017. /* Setup store */
  33018. #ifndef NO_WOLFSSL_SERVER
  33019. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  33020. #else
  33021. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  33022. #endif
  33023. ExpectNotNull(store_cpy = X509_STORE_new());
  33024. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  33025. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  33026. WOLFSSL_SUCCESS);
  33027. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  33028. WOLFSSL_SUCCESS);
  33029. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  33030. WOLFSSL_SUCCESS);
  33031. #ifdef HAVE_CRL
  33032. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  33033. WOLFSSL_SUCCESS);
  33034. #endif
  33035. /* Store ready */
  33036. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  33037. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  33038. #ifdef HAVE_CRL
  33039. #ifdef WOLFSSL_SIGNER_DER_CERT
  33040. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  33041. #else
  33042. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  33043. #endif
  33044. #else
  33045. #ifdef WOLFSSL_SIGNER_DER_CERT
  33046. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  33047. #else
  33048. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  33049. #endif
  33050. #endif
  33051. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  33052. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  33053. switch (X509_OBJECT_get_type(obj)) {
  33054. case X509_LU_X509:
  33055. {
  33056. WOLFSSL_X509* x509;
  33057. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  33058. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  33059. break;
  33060. }
  33061. case X509_LU_CRL:
  33062. #ifdef HAVE_CRL
  33063. {
  33064. WOLFSSL_CRL* crl = NULL;
  33065. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  33066. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  33067. break;
  33068. }
  33069. #endif
  33070. case X509_LU_NONE:
  33071. default:
  33072. Fail(("X509_OBJECT_get_type should return x509 or crl "
  33073. "(when built with crl support)"),
  33074. ("Unrecognized X509_OBJECT type or none"));
  33075. }
  33076. }
  33077. X509_STORE_free(store_cpy);
  33078. SSL_CTX_free(ctx);
  33079. #endif
  33080. return EXPECT_RESULT();
  33081. }
  33082. static int test_wolfSSL_BN_CTX(void)
  33083. {
  33084. EXPECT_DECLS;
  33085. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33086. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33087. WOLFSSL_BN_CTX* bn_ctx = NULL;
  33088. WOLFSSL_BIGNUM* t = NULL;
  33089. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  33090. /* No implementation. */
  33091. BN_CTX_init(NULL);
  33092. ExpectNotNull(t = BN_CTX_get(NULL));
  33093. BN_free(t);
  33094. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  33095. BN_free(t);
  33096. #ifndef NO_WOLFSSL_STUB
  33097. /* No implementation. */
  33098. BN_CTX_start(NULL);
  33099. BN_CTX_start(bn_ctx);
  33100. #endif
  33101. BN_CTX_free(NULL);
  33102. BN_CTX_free(bn_ctx);
  33103. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33104. return EXPECT_RESULT();
  33105. }
  33106. static int test_wolfSSL_BN(void)
  33107. {
  33108. EXPECT_DECLS;
  33109. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33110. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33111. BIGNUM* a = NULL;
  33112. BIGNUM* b = NULL;
  33113. BIGNUM* c = NULL;
  33114. BIGNUM* d = NULL;
  33115. BIGNUM emptyBN;
  33116. /* Setup */
  33117. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33118. /* internal not set emptyBN. */
  33119. ExpectNotNull(a = BN_new());
  33120. ExpectNotNull(b = BN_new());
  33121. ExpectNotNull(c = BN_dup(b));
  33122. ExpectNotNull(d = BN_new());
  33123. /* Invalid parameter testing. */
  33124. BN_free(NULL);
  33125. ExpectNull(BN_dup(NULL));
  33126. ExpectNull(BN_dup(&emptyBN));
  33127. ExpectNull(BN_copy(NULL, NULL));
  33128. ExpectNull(BN_copy(b, NULL));
  33129. ExpectNull(BN_copy(NULL, c));
  33130. ExpectNull(BN_copy(b, &emptyBN));
  33131. ExpectNull(BN_copy(&emptyBN, c));
  33132. BN_clear(NULL);
  33133. BN_clear(&emptyBN);
  33134. ExpectIntEQ(BN_num_bytes(NULL), 0);
  33135. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  33136. ExpectIntEQ(BN_num_bits(NULL), 0);
  33137. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  33138. ExpectIntEQ(BN_is_negative(NULL), 0);
  33139. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  33140. /* END Invalid Parameters */
  33141. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  33142. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  33143. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  33144. ExpectIntEQ(BN_num_bits(a), 2);
  33145. ExpectIntEQ(BN_num_bytes(a), 1);
  33146. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  33147. defined(WOLFSSL_SP_INT_NEGATIVE))
  33148. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  33149. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  33150. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  33151. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  33152. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  33153. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33154. {
  33155. /* Do additional tests on negative BN conversions. */
  33156. char* ret = NULL;
  33157. ASN1_INTEGER* asn1 = NULL;
  33158. BIGNUM* tmp = NULL;
  33159. /* Sanity check we have a negative BN. */
  33160. ExpectIntEQ(BN_is_negative(c), 1);
  33161. ExpectNotNull(ret = BN_bn2dec(c));
  33162. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  33163. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  33164. ret = NULL;
  33165. /* Convert to ASN1_INTEGER and back to BN. */
  33166. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  33167. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  33168. /* After converting back BN should be negative and correct. */
  33169. ExpectIntEQ(BN_is_negative(tmp), 1);
  33170. ExpectNotNull(ret = BN_bn2dec(tmp));
  33171. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  33172. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  33173. ASN1_INTEGER_free(asn1);
  33174. BN_free(tmp);
  33175. }
  33176. #endif
  33177. ExpectIntEQ(BN_get_word(c), 4);
  33178. #endif
  33179. ExpectIntEQ(BN_set_word(a, 3), 1);
  33180. ExpectIntEQ(BN_set_word(b, 3), 1);
  33181. ExpectIntEQ(BN_set_word(c, 4), 1);
  33182. /* NULL == NULL, NULL < num, num > NULL */
  33183. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  33184. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  33185. ExpectIntLT(BN_cmp(NULL, b), 0);
  33186. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  33187. ExpectIntGT(BN_cmp(a, NULL), 0);
  33188. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  33189. ExpectIntEQ(BN_cmp(a, b), 0);
  33190. ExpectIntLT(BN_cmp(a, c), 0);
  33191. ExpectIntGT(BN_cmp(c, b), 0);
  33192. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  33193. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  33194. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  33195. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  33196. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  33197. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  33198. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  33199. #endif
  33200. BN_clear(a);
  33201. BN_free(a);
  33202. BN_free(b);
  33203. BN_free(c);
  33204. BN_clear_free(d);
  33205. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33206. return EXPECT_RESULT();
  33207. }
  33208. static int test_wolfSSL_BN_init(void)
  33209. {
  33210. EXPECT_DECLS;
  33211. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33212. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33213. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  33214. BIGNUM* ap = NULL;
  33215. BIGNUM bv;
  33216. BIGNUM cv;
  33217. BIGNUM dv;
  33218. ExpectNotNull(ap = BN_new());
  33219. BN_init(NULL);
  33220. XMEMSET(&bv, 0, sizeof(bv));
  33221. ExpectNull(BN_dup(&bv));
  33222. BN_init(&bv);
  33223. BN_init(&cv);
  33224. BN_init(&dv);
  33225. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  33226. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  33227. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  33228. /* a^b mod c = */
  33229. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  33230. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  33231. /* check result 3^2 mod 5 */
  33232. ExpectIntEQ(BN_get_word(&dv), 4);
  33233. /* a*b mod c = */
  33234. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  33235. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  33236. /* check result 3*2 mod 5 */
  33237. ExpectIntEQ(BN_get_word(&dv), 1);
  33238. BN_free(ap);
  33239. #endif
  33240. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33241. return EXPECT_RESULT();
  33242. }
  33243. static int test_wolfSSL_BN_enc_dec(void)
  33244. {
  33245. EXPECT_DECLS;
  33246. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  33247. BIGNUM* a = NULL;
  33248. BIGNUM* b = NULL;
  33249. BIGNUM* c = NULL;
  33250. BIGNUM emptyBN;
  33251. char* str = NULL;
  33252. const char* emptyStr = "";
  33253. const char* numberStr = "12345";
  33254. const char* badStr = "g12345";
  33255. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33256. const char* twoStr = "2";
  33257. #endif
  33258. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  33259. unsigned char outNum[5];
  33260. /* Setup */
  33261. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33262. ExpectNotNull(a = BN_new());
  33263. ExpectNotNull(b = BN_new());
  33264. ExpectIntEQ(BN_set_word(a, 2), 1);
  33265. /* Invalid parameters */
  33266. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  33267. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  33268. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  33269. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  33270. ExpectNull(BN_bn2hex(NULL));
  33271. ExpectNull(BN_bn2hex(&emptyBN));
  33272. ExpectNull(BN_bn2dec(NULL));
  33273. ExpectNull(BN_bn2dec(&emptyBN));
  33274. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  33275. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  33276. ExpectNull(BN_bin2bn(binNum, -1, a));
  33277. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  33278. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  33279. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  33280. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  33281. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  33282. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  33283. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  33284. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  33285. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  33286. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  33287. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  33288. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  33289. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  33290. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  33291. ExpectIntEQ(BN_set_word(a, 2), 1);
  33292. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  33293. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  33294. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  33295. ExpectIntEQ(BN_cmp(a, b), 0);
  33296. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  33297. ExpectIntEQ(BN_cmp(a, b), -1);
  33298. ExpectNotNull(str = BN_bn2hex(a));
  33299. ExpectNotNull(BN_hex2bn(&b, str));
  33300. ExpectIntEQ(BN_cmp(a, b), 0);
  33301. ExpectNotNull(BN_hex2bn(&b, numberStr));
  33302. ExpectIntEQ(BN_cmp(a, b), -1);
  33303. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33304. str = NULL;
  33305. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33306. ExpectNotNull(str = BN_bn2dec(a));
  33307. ExpectStrEQ(str, twoStr);
  33308. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33309. str = NULL;
  33310. #ifndef NO_RSA
  33311. ExpectNotNull(str = BN_bn2dec(a));
  33312. ExpectNotNull(BN_dec2bn(&b, str));
  33313. ExpectIntEQ(BN_cmp(a, b), 0);
  33314. ExpectNotNull(BN_dec2bn(&b, numberStr));
  33315. ExpectIntEQ(BN_cmp(a, b), -1);
  33316. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33317. str = NULL;
  33318. #else
  33319. /* No implementation - fail with good parameters. */
  33320. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  33321. #endif
  33322. #endif
  33323. BN_free(b);
  33324. BN_free(a);
  33325. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33326. return EXPECT_RESULT();
  33327. }
  33328. static int test_wolfSSL_BN_word(void)
  33329. {
  33330. EXPECT_DECLS;
  33331. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  33332. BIGNUM* a = NULL;
  33333. BIGNUM* b = NULL;
  33334. BIGNUM* c = NULL;
  33335. BIGNUM av;
  33336. ExpectNotNull(a = BN_new());
  33337. ExpectNotNull(b = BN_new());
  33338. ExpectNotNull(c = BN_new());
  33339. XMEMSET(&av, 0, sizeof(av));
  33340. /* Invalid parameter. */
  33341. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  33342. ExpectIntEQ(BN_add_word(&av, 3), 0);
  33343. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  33344. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  33345. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  33346. ExpectIntEQ(BN_set_word(&av, 3), 0);
  33347. ExpectIntEQ(BN_get_word(NULL), 0);
  33348. ExpectIntEQ(BN_get_word(&av), 0);
  33349. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  33350. ExpectIntEQ(BN_is_word(&av, 3), 0);
  33351. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  33352. !defined(NO_DSA))
  33353. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  33354. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  33355. #endif
  33356. ExpectIntEQ(BN_one(NULL), 0);
  33357. ExpectIntEQ(BN_one(&av), 0);
  33358. BN_zero(NULL);
  33359. BN_zero(&av);
  33360. ExpectIntEQ(BN_is_one(NULL), 0);
  33361. ExpectIntEQ(BN_is_one(&av), 0);
  33362. ExpectIntEQ(BN_is_zero(NULL), 0);
  33363. ExpectIntEQ(BN_is_zero(&av), 0);
  33364. ExpectIntEQ(BN_set_word(a, 3), 1);
  33365. ExpectIntEQ(BN_set_word(b, 2), 1);
  33366. ExpectIntEQ(BN_set_word(c, 5), 1);
  33367. /* a + 3 = */
  33368. ExpectIntEQ(BN_add_word(a, 3), 1);
  33369. /* check result 3 + 3*/
  33370. ExpectIntEQ(BN_get_word(a), 6);
  33371. ExpectIntEQ(BN_is_word(a, 6), 1);
  33372. ExpectIntEQ(BN_is_word(a, 5), 0);
  33373. /* set a back to 3 */
  33374. ExpectIntEQ(BN_set_word(a, 3), 1);
  33375. /* a - 3 = */
  33376. ExpectIntEQ(BN_sub_word(a, 3), 1);
  33377. /* check result 3 - 3*/
  33378. ExpectIntEQ(BN_get_word(a), 0);
  33379. ExpectIntEQ(BN_one(a), 1);
  33380. ExpectIntEQ(BN_is_word(a, 1), 1);
  33381. ExpectIntEQ(BN_is_word(a, 0), 0);
  33382. ExpectIntEQ(BN_is_one(a), 1);
  33383. ExpectIntEQ(BN_is_zero(a), 0);
  33384. BN_zero(a);
  33385. ExpectIntEQ(BN_is_word(a, 0), 1);
  33386. ExpectIntEQ(BN_is_word(a, 1), 0);
  33387. ExpectIntEQ(BN_is_zero(a), 1);
  33388. ExpectIntEQ(BN_is_one(a), 0);
  33389. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  33390. !defined(NO_DSA))
  33391. ExpectIntEQ(BN_set_word(a, 5), 1);
  33392. ExpectIntEQ(BN_mod_word(a, 3), 2);
  33393. ExpectIntEQ(BN_mod_word(a, 0), -1);
  33394. #endif
  33395. BN_free(c);
  33396. BN_free(b);
  33397. BN_free(a);
  33398. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33399. return EXPECT_RESULT();
  33400. }
  33401. static int test_wolfSSL_BN_bits(void)
  33402. {
  33403. EXPECT_DECLS;
  33404. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33405. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33406. BIGNUM* a = NULL;
  33407. BIGNUM emptyBN;
  33408. /* Setup */
  33409. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33410. ExpectNotNull(a = BN_new());
  33411. /* Invalid parameters. */
  33412. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  33413. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  33414. ExpectIntEQ(BN_set_bit(a, -1), 0);
  33415. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  33416. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  33417. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  33418. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  33419. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  33420. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  33421. ExpectIntEQ(BN_is_odd(NULL), 0);
  33422. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  33423. ExpectIntEQ(BN_set_word(a, 0), 1);
  33424. ExpectIntEQ(BN_is_zero(a), 1);
  33425. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  33426. ExpectIntEQ(BN_is_zero(a), 0);
  33427. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  33428. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  33429. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  33430. ExpectIntEQ(BN_is_zero(a), 1);
  33431. ExpectIntEQ(BN_set_bit(a, 0), 1);
  33432. ExpectIntEQ(BN_is_odd(a), 1);
  33433. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33434. ExpectIntEQ(BN_is_odd(a), 0);
  33435. ExpectIntEQ(BN_set_bit(a, 1), 1);
  33436. ExpectIntEQ(BN_is_odd(a), 0);
  33437. ExpectIntEQ(BN_set_bit(a, 129), 1);
  33438. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  33439. #ifndef NO_WOLFSSL_STUB
  33440. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  33441. #endif
  33442. BN_free(a);
  33443. #endif
  33444. return EXPECT_RESULT();
  33445. }
  33446. static int test_wolfSSL_BN_shift(void)
  33447. {
  33448. EXPECT_DECLS;
  33449. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33450. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33451. BIGNUM* a = NULL;
  33452. BIGNUM* b = NULL;
  33453. BIGNUM emptyBN;
  33454. /* Setup */
  33455. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33456. ExpectNotNull(a = BN_new());
  33457. ExpectNotNull(b = BN_new());
  33458. /* Invalid parameters. */
  33459. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  33460. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  33461. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  33462. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  33463. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  33464. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  33465. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  33466. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  33467. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  33468. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  33469. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  33470. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  33471. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  33472. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  33473. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  33474. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  33475. ExpectIntEQ(BN_set_word(a, 1), 1);
  33476. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  33477. ExpectIntEQ(BN_is_word(b, 2), 1);
  33478. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  33479. ExpectIntEQ(BN_is_word(a, 2), 1);
  33480. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  33481. ExpectIntEQ(BN_is_word(b, 1), 1);
  33482. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  33483. ExpectIntEQ(BN_is_word(a, 1), 1);
  33484. BN_free(b);
  33485. BN_free(a);
  33486. #endif
  33487. return EXPECT_RESULT();
  33488. }
  33489. static int test_wolfSSL_BN_math(void)
  33490. {
  33491. EXPECT_DECLS;
  33492. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33493. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33494. BIGNUM* a = NULL;
  33495. BIGNUM* b = NULL;
  33496. BIGNUM* r = NULL;
  33497. BIGNUM* rem = NULL;
  33498. BIGNUM emptyBN;
  33499. BN_ULONG val1;
  33500. BN_ULONG val2;
  33501. /* Setup */
  33502. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33503. ExpectNotNull(a = BN_new());
  33504. ExpectNotNull(b = BN_new());
  33505. ExpectNotNull(r = BN_new());
  33506. ExpectNotNull(rem = BN_new());
  33507. /* Invalid parameters. */
  33508. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  33509. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  33510. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  33511. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  33512. ExpectIntEQ(BN_add(r, a, NULL), 0);
  33513. ExpectIntEQ(BN_add(r, NULL, b), 0);
  33514. ExpectIntEQ(BN_add(NULL, a, b), 0);
  33515. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  33516. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  33517. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  33518. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  33519. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  33520. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  33521. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  33522. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  33523. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  33524. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  33525. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  33526. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  33527. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  33528. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  33529. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  33530. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  33531. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  33532. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  33533. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  33534. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  33535. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  33536. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  33537. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  33538. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  33539. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  33540. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  33541. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  33542. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  33543. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33544. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  33545. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  33546. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  33547. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  33548. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  33549. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  33550. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  33551. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  33552. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  33553. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  33554. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  33555. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  33556. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  33557. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  33558. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  33559. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33560. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33561. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  33562. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  33563. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  33564. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  33565. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  33566. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  33567. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  33568. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  33569. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  33570. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  33571. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  33572. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  33573. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  33574. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  33575. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33576. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  33577. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  33578. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  33579. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  33580. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  33581. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  33582. /* END Invalid parameters. */
  33583. val1 = 8;
  33584. val2 = 3;
  33585. ExpectIntEQ(BN_set_word(a, val1), 1);
  33586. ExpectIntEQ(BN_set_word(b, val2), 1);
  33587. ExpectIntEQ(BN_add(r, a, b), 1);
  33588. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  33589. ExpectIntEQ(BN_sub(r, a, b), 1);
  33590. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  33591. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  33592. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  33593. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  33594. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  33595. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  33596. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  33597. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  33598. BN_free(rem);
  33599. BN_free(r);
  33600. BN_free(b);
  33601. BN_free(a);
  33602. #endif
  33603. return EXPECT_RESULT();
  33604. }
  33605. static int test_wolfSSL_BN_math_mod(void)
  33606. {
  33607. EXPECT_DECLS;
  33608. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33609. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33610. BIGNUM* a = NULL;
  33611. BIGNUM* b = NULL;
  33612. BIGNUM* m = NULL;
  33613. BIGNUM* r = NULL;
  33614. BIGNUM* t = NULL;
  33615. BIGNUM emptyBN;
  33616. BN_ULONG val1;
  33617. BN_ULONG val2;
  33618. BN_ULONG val3;
  33619. /* Setup */
  33620. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33621. ExpectNotNull(a = BN_new());
  33622. ExpectNotNull(b = BN_new());
  33623. ExpectNotNull(m = BN_new());
  33624. ExpectNotNull(r = BN_new());
  33625. /* Invalid parameters. */
  33626. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  33627. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  33628. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  33629. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  33630. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  33631. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  33632. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  33633. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  33634. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  33635. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33636. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33637. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33638. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33639. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33640. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  33641. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  33642. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  33643. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  33644. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  33645. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  33646. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  33647. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  33648. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  33649. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  33650. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  33651. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  33652. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  33653. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33654. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33655. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33656. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33657. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33658. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  33659. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  33660. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  33661. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  33662. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  33663. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  33664. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  33665. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  33666. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  33667. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  33668. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  33669. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  33670. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  33671. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33672. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33673. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33674. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33675. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33676. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  33677. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  33678. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  33679. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  33680. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  33681. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33682. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33683. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33684. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33685. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  33686. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  33687. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  33688. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  33689. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  33690. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  33691. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  33692. /* END Invalid parameters. */
  33693. val1 = 9;
  33694. val2 = 13;
  33695. val3 = 5;
  33696. ExpectIntEQ(BN_set_word(a, val1), 1);
  33697. ExpectIntEQ(BN_set_word(b, val2), 1);
  33698. ExpectIntEQ(BN_set_word(m, val3), 1);
  33699. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  33700. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  33701. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  33702. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  33703. ExpectIntEQ(BN_set_word(a, 2), 1);
  33704. ExpectIntEQ(BN_set_word(b, 3), 1);
  33705. ExpectIntEQ(BN_set_word(m, 5), 1);
  33706. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  33707. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  33708. ExpectIntEQ(BN_is_word(r, 3), 1);
  33709. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  33710. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  33711. ExpectIntEQ(BN_is_word(r, 3), 1);
  33712. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  33713. ExpectIntEQ(BN_is_word(t, 3), 1);
  33714. BN_free(t);
  33715. /* No inverse case. No inverse when a divides b. */
  33716. ExpectIntEQ(BN_set_word(a, 3), 1);
  33717. ExpectIntEQ(BN_set_word(m, 9), 1);
  33718. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  33719. BN_free(r);
  33720. BN_free(m);
  33721. BN_free(b);
  33722. BN_free(a);
  33723. #endif
  33724. return EXPECT_RESULT();
  33725. }
  33726. static int test_wolfSSL_BN_math_other(void)
  33727. {
  33728. EXPECT_DECLS;
  33729. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33730. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33731. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  33732. BIGNUM* a = NULL;
  33733. BIGNUM* b = NULL;
  33734. BIGNUM* r = NULL;
  33735. BIGNUM emptyBN;
  33736. /* Setup */
  33737. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33738. ExpectNotNull(a = BN_new());
  33739. ExpectNotNull(b = BN_new());
  33740. ExpectNotNull(r = BN_new());
  33741. /* Invalid parameters. */
  33742. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  33743. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  33744. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  33745. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  33746. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  33747. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  33748. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  33749. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33750. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  33751. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  33752. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  33753. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  33754. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  33755. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  33756. /* END Invalid parameters. */
  33757. /* No common factors between 2 and 3. */
  33758. ExpectIntEQ(BN_set_word(a, 2), 1);
  33759. ExpectIntEQ(BN_set_word(b, 3), 1);
  33760. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33761. ExpectIntEQ(BN_is_word(r, 1), 1);
  33762. /* 3 is largest value that divides both 6 and 9. */
  33763. ExpectIntEQ(BN_set_word(a, 6), 1);
  33764. ExpectIntEQ(BN_set_word(b, 9), 1);
  33765. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33766. ExpectIntEQ(BN_is_word(r, 3), 1);
  33767. /* GCD of 0 and 0 is undefined. */
  33768. ExpectIntEQ(BN_set_word(a, 0), 1);
  33769. ExpectIntEQ(BN_set_word(b, 0), 1);
  33770. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  33771. /* Teardown */
  33772. BN_free(r);
  33773. BN_free(b);
  33774. BN_free(a);
  33775. #endif
  33776. #endif
  33777. return EXPECT_RESULT();
  33778. }
  33779. static int test_wolfSSL_BN_rand(void)
  33780. {
  33781. EXPECT_DECLS;
  33782. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  33783. BIGNUM* bn = NULL;
  33784. BIGNUM* range = NULL;
  33785. BIGNUM emptyBN;
  33786. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33787. ExpectNotNull(bn = BN_new());
  33788. ExpectNotNull(range = BN_new());
  33789. /* Invalid parameters. */
  33790. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  33791. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33792. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  33793. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  33794. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33795. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  33796. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  33797. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33798. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  33799. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  33800. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33801. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  33802. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  33803. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  33804. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  33805. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  33806. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  33807. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  33808. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  33809. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33810. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33811. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33812. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33813. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33814. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33815. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33816. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33817. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33818. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33819. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33820. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33821. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33822. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33823. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33824. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33825. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33826. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33827. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33828. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33829. /* 1 bit random value must have no more than one top bit set. */
  33830. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33831. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33832. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33833. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33834. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33835. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33836. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33837. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33838. /* END Invalid parameters. */
  33839. /* 0 bit random: 0. */
  33840. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33841. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33842. ExpectIntEQ(BN_is_zero(bn), 1);
  33843. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  33844. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33845. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33846. ExpectIntEQ(BN_is_zero(bn), 1);
  33847. /* 1 bit random: 0 or 1. */
  33848. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33849. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33850. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33851. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33852. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33853. ExpectIntEQ(BN_get_word(bn), 1);
  33854. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33855. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33856. ExpectIntEQ(BN_get_word(bn), 1);
  33857. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33858. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33859. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33860. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33861. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33862. ExpectIntEQ(BN_get_word(bn), 1);
  33863. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33864. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33865. ExpectIntEQ(BN_get_word(bn), 1);
  33866. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33867. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33868. ExpectIntEQ(BN_num_bits(bn), 8);
  33869. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33870. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33871. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33872. ExpectIntEQ(BN_num_bits(bn), 8);
  33873. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33874. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33875. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33876. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33877. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33878. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33879. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33880. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33881. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33882. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33883. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33884. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33885. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33886. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33887. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33888. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33889. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33890. * case, requesting a 13-bit random number would actually return a 16-bit
  33891. * random number. */
  33892. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  33893. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33894. ExpectIntEQ(BN_num_bits(bn), 13);
  33895. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  33896. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33897. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33898. ExpectIntEQ(BN_set_word(range, 0), 1);
  33899. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33900. ExpectIntEQ(BN_set_word(range, 1), 1);
  33901. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33902. BN_free(bn);
  33903. BN_free(range);
  33904. #endif
  33905. return EXPECT_RESULT();
  33906. }
  33907. static int test_wolfSSL_BN_prime(void)
  33908. {
  33909. EXPECT_DECLS;
  33910. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33911. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33912. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  33913. BIGNUM* a = NULL;
  33914. BIGNUM* add = NULL;
  33915. BIGNUM* rem = NULL;
  33916. BIGNUM emptyBN;
  33917. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33918. ExpectNotNull(a = BN_new());
  33919. ExpectNotNull(add = BN_new());
  33920. ExpectNotNull(rem = BN_new());
  33921. /* Invalid parameters. */
  33922. /* BN_generate_prime_ex()
  33923. * prime - must have valid BIGNUM
  33924. * bits - Greater then 0
  33925. * safe - not supported, must be 0
  33926. * add - not supported, must be NULL
  33927. * rem - not supported, must be NULL
  33928. * cb - anything
  33929. */
  33930. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  33931. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  33932. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  33933. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  33934. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  33935. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  33936. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  33937. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  33938. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  33939. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  33940. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  33941. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  33942. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  33943. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  33944. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  33945. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  33946. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  33947. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  33948. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  33949. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  33950. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  33951. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  33952. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  33953. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  33954. /* END Invalid parameters. */
  33955. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  33956. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  33957. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33958. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  33959. BN_free(rem);
  33960. BN_free(add);
  33961. BN_free(a);
  33962. #endif
  33963. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33964. return EXPECT_RESULT();
  33965. }
  33966. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33967. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33968. #define TEST_ARG 0x1234
  33969. static void msg_cb(int write_p, int version, int content_type,
  33970. const void *buf, size_t len, SSL *ssl, void *arg)
  33971. {
  33972. (void)write_p;
  33973. (void)version;
  33974. (void)content_type;
  33975. (void)buf;
  33976. (void)len;
  33977. (void)ssl;
  33978. AssertTrue(arg == (void*)TEST_ARG);
  33979. }
  33980. #endif
  33981. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33982. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33983. #if defined(SESSION_CERTS)
  33984. #include "wolfssl/internal.h"
  33985. #endif
  33986. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  33987. {
  33988. EXPECT_DECLS;
  33989. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33990. STACK_OF(X509)* sk = NULL;
  33991. X509* x509 = NULL;
  33992. int i, num;
  33993. BIO* bio = NULL;
  33994. #endif
  33995. ExpectNotNull(ctx);
  33996. ExpectNotNull(ssl);
  33997. fprintf(stderr, "\n===== msgcb called ====\n");
  33998. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  33999. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  34000. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  34001. ExpectNotNull(SSL_get0_verified_chain(ssl));
  34002. #endif
  34003. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  34004. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  34005. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  34006. if (sk == NULL) {
  34007. BIO_free(bio);
  34008. return TEST_FAIL;
  34009. }
  34010. num = sk_X509_num(sk);
  34011. ExpectTrue(num > 0);
  34012. for (i = 0; i < num; i++) {
  34013. ExpectNotNull(x509 = sk_X509_value(sk,i));
  34014. if (x509 == NULL)
  34015. break;
  34016. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  34017. X509_print(bio,x509);
  34018. fprintf(stderr, "\n\n");
  34019. }
  34020. BIO_free(bio);
  34021. #endif
  34022. return EXPECT_RESULT();
  34023. }
  34024. #endif
  34025. static int test_wolfSSL_msgCb(void)
  34026. {
  34027. EXPECT_DECLS;
  34028. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  34029. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34030. test_ssl_cbf client_cb;
  34031. test_ssl_cbf server_cb;
  34032. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34033. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34034. #ifndef WOLFSSL_NO_TLS12
  34035. client_cb.method = wolfTLSv1_2_client_method;
  34036. server_cb.method = wolfTLSv1_2_server_method;
  34037. #else
  34038. client_cb.method = wolfTLSv1_3_client_method;
  34039. server_cb.method = wolfTLSv1_3_server_method;
  34040. #endif
  34041. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34042. &server_cb, msgCb), TEST_SUCCESS);
  34043. #endif
  34044. return EXPECT_RESULT();
  34045. }
  34046. static int test_wolfSSL_either_side(void)
  34047. {
  34048. EXPECT_DECLS;
  34049. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  34050. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34051. test_ssl_cbf client_cb;
  34052. test_ssl_cbf server_cb;
  34053. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34054. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34055. /* Use different CTX for client and server */
  34056. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  34057. ExpectNotNull(client_cb.ctx);
  34058. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  34059. ExpectNotNull(server_cb.ctx);
  34060. /* we are responsible for free'ing WOLFSSL_CTX */
  34061. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  34062. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34063. &server_cb, NULL), TEST_SUCCESS);
  34064. wolfSSL_CTX_free(client_cb.ctx);
  34065. wolfSSL_CTX_free(server_cb.ctx);
  34066. #endif
  34067. return EXPECT_RESULT();
  34068. }
  34069. static int test_wolfSSL_DTLS_either_side(void)
  34070. {
  34071. EXPECT_DECLS;
  34072. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  34073. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  34074. test_ssl_cbf client_cb;
  34075. test_ssl_cbf server_cb;
  34076. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34077. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34078. /* Use different CTX for client and server */
  34079. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  34080. ExpectNotNull(client_cb.ctx);
  34081. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  34082. ExpectNotNull(server_cb.ctx);
  34083. /* we are responsible for free'ing WOLFSSL_CTX */
  34084. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  34085. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34086. &server_cb, NULL), TEST_SUCCESS);
  34087. wolfSSL_CTX_free(client_cb.ctx);
  34088. wolfSSL_CTX_free(server_cb.ctx);
  34089. #endif
  34090. return EXPECT_RESULT();
  34091. }
  34092. static int test_generate_cookie(void)
  34093. {
  34094. EXPECT_DECLS;
  34095. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  34096. SSL_CTX* ctx = NULL;
  34097. SSL* ssl = NULL;
  34098. byte buf[FOURK_BUF] = {0};
  34099. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  34100. ExpectNotNull(ssl = SSL_new(ctx));
  34101. /* Test unconnected */
  34102. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  34103. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  34104. wolfSSL_SetCookieCtx(ssl, ctx);
  34105. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  34106. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  34107. SSL_free(ssl);
  34108. SSL_CTX_free(ctx);
  34109. #endif
  34110. return EXPECT_RESULT();
  34111. }
  34112. static int test_wolfSSL_set_options(void)
  34113. {
  34114. EXPECT_DECLS;
  34115. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34116. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34117. WOLFSSL* ssl = NULL;
  34118. WOLFSSL_CTX* ctx = NULL;
  34119. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  34120. char appData[] = "extra msg";
  34121. #endif
  34122. #ifdef OPENSSL_EXTRA
  34123. unsigned char protos[] = {
  34124. 7, 't', 'l', 's', '/', '1', '.', '2',
  34125. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  34126. };
  34127. unsigned int len = sizeof(protos);
  34128. void *arg = (void *)TEST_ARG;
  34129. #endif
  34130. #ifndef NO_WOLFSSL_SERVER
  34131. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34132. #else
  34133. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34134. #endif
  34135. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  34136. WOLFSSL_FILETYPE_PEM));
  34137. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34138. WOLFSSL_FILETYPE_PEM));
  34139. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  34140. == WOLFSSL_OP_NO_TLSv1);
  34141. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  34142. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  34143. WOLFSSL_OP_NO_SSLv2)), 0);
  34144. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  34145. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  34146. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  34147. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  34148. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  34149. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  34150. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  34151. WOLFSSL_OP_NO_COMPRESSION));
  34152. wolfSSL_CTX_free(ctx);
  34153. ctx = NULL;
  34154. #ifndef NO_WOLFSSL_SERVER
  34155. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34156. #else
  34157. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34158. #endif
  34159. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  34160. WOLFSSL_FILETYPE_PEM));
  34161. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34162. WOLFSSL_FILETYPE_PEM));
  34163. #ifdef OPENSSL_EXTRA
  34164. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  34165. #endif
  34166. ExpectNotNull(ssl = wolfSSL_new(ctx));
  34167. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  34168. #ifdef HAVE_EX_DATA
  34169. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  34170. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  34171. if (ssl != NULL) {
  34172. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  34173. appData, sizeof(appData)), 0);
  34174. }
  34175. #else
  34176. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  34177. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  34178. #endif
  34179. #endif
  34180. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  34181. WOLFSSL_OP_NO_TLSv1);
  34182. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  34183. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  34184. WOLFSSL_OP_NO_SSLv2)), 0);
  34185. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  34186. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  34187. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  34188. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  34189. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  34190. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  34191. #ifdef OPENSSL_EXTRA
  34192. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  34193. WOLFSSL_OP_NO_COMPRESSION));
  34194. #endif
  34195. #ifdef OPENSSL_EXTRA
  34196. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  34197. wolfSSL_set_msg_callback_arg(ssl, arg);
  34198. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  34199. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  34200. #else
  34201. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  34202. #endif
  34203. #endif
  34204. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  34205. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  34206. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  34207. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  34208. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  34209. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  34210. #else
  34211. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  34212. #endif
  34213. #endif /* HAVE_ALPN && !NO_BIO */
  34214. #endif
  34215. wolfSSL_free(ssl);
  34216. wolfSSL_CTX_free(ctx);
  34217. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34218. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34219. return EXPECT_RESULT();
  34220. }
  34221. static int test_wolfSSL_sk_SSL_CIPHER(void)
  34222. {
  34223. EXPECT_DECLS;
  34224. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34225. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34226. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34227. SSL* ssl = NULL;
  34228. SSL_CTX* ctx = NULL;
  34229. STACK_OF(SSL_CIPHER) *sk = NULL;
  34230. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  34231. #ifndef NO_WOLFSSL_SERVER
  34232. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34233. #else
  34234. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34235. #endif
  34236. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  34237. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34238. ExpectNotNull(ssl = SSL_new(ctx));
  34239. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  34240. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  34241. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  34242. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  34243. /* error case because connection has not been established yet */
  34244. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  34245. sk_SSL_CIPHER_free(dupSk);
  34246. /* sk is pointer to internal struct that should be free'd in SSL_free */
  34247. SSL_free(ssl);
  34248. SSL_CTX_free(ctx);
  34249. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34250. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34251. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34252. return EXPECT_RESULT();
  34253. }
  34254. static int test_wolfSSL_set1_curves_list(void)
  34255. {
  34256. EXPECT_DECLS;
  34257. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  34258. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34259. SSL* ssl = NULL;
  34260. SSL_CTX* ctx = NULL;
  34261. #ifndef NO_WOLFSSL_SERVER
  34262. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34263. #else
  34264. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34265. #endif
  34266. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  34267. SSL_FILETYPE_PEM));
  34268. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  34269. ExpectNotNull(ssl = SSL_new(ctx));
  34270. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  34271. #ifdef HAVE_ECC
  34272. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  34273. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  34274. #endif
  34275. #ifdef HAVE_CURVE25519
  34276. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  34277. #else
  34278. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  34279. #endif
  34280. #ifdef HAVE_CURVE448
  34281. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  34282. #else
  34283. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  34284. #endif
  34285. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  34286. #ifdef HAVE_ECC
  34287. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  34288. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  34289. #endif
  34290. #ifdef HAVE_CURVE25519
  34291. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  34292. #else
  34293. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  34294. #endif
  34295. #ifdef HAVE_CURVE448
  34296. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  34297. #else
  34298. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  34299. #endif
  34300. SSL_free(ssl);
  34301. SSL_CTX_free(ctx);
  34302. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34303. #endif
  34304. return EXPECT_RESULT();
  34305. }
  34306. static int test_wolfSSL_set1_sigalgs_list(void)
  34307. {
  34308. EXPECT_DECLS;
  34309. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  34310. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34311. SSL* ssl = NULL;
  34312. SSL_CTX* ctx = NULL;
  34313. #ifndef NO_WOLFSSL_SERVER
  34314. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34315. #else
  34316. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34317. #endif
  34318. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34319. SSL_FILETYPE_PEM));
  34320. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34321. ExpectNotNull(ssl = SSL_new(ctx));
  34322. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  34323. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  34324. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  34325. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  34326. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  34327. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  34328. #ifndef NO_RSA
  34329. #ifndef NO_SHA256
  34330. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  34331. WOLFSSL_FAILURE);
  34332. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  34333. WOLFSSL_FAILURE);
  34334. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  34335. WOLFSSL_SUCCESS);
  34336. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  34337. WOLFSSL_SUCCESS);
  34338. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  34339. WOLFSSL_FAILURE);
  34340. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  34341. WOLFSSL_FAILURE);
  34342. #ifdef WC_RSA_PSS
  34343. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  34344. WOLFSSL_SUCCESS);
  34345. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  34346. WOLFSSL_SUCCESS);
  34347. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  34348. WOLFSSL_SUCCESS);
  34349. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  34350. WOLFSSL_SUCCESS);
  34351. #endif
  34352. #ifdef WOLFSSL_SHA512
  34353. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34354. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  34355. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34356. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  34357. #elif defined(WOLFSSL_SHA384)
  34358. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34359. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  34360. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34361. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  34362. #endif
  34363. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  34364. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  34365. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  34366. WOLFSSL_FAILURE);
  34367. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  34368. WOLFSSL_FAILURE);
  34369. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  34370. WOLFSSL_FAILURE);
  34371. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  34372. WOLFSSL_FAILURE);
  34373. #endif
  34374. #endif
  34375. #ifdef HAVE_ECC
  34376. #ifndef NO_SHA256
  34377. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  34378. WOLFSSL_SUCCESS);
  34379. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  34380. WOLFSSL_SUCCESS);
  34381. #ifdef WOLFSSL_SHA512
  34382. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34383. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  34384. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34385. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  34386. #elif defined(WOLFSSL_SHA384)
  34387. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34388. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  34389. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34390. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  34391. #endif
  34392. #endif
  34393. #endif
  34394. #ifdef HAVE_ED25519
  34395. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  34396. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  34397. #endif
  34398. #ifdef HAVE_ED448
  34399. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  34400. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  34401. #endif
  34402. #ifndef NO_DSA
  34403. #ifndef NO_SHA256
  34404. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  34405. WOLFSSL_SUCCESS);
  34406. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  34407. WOLFSSL_SUCCESS);
  34408. #endif
  34409. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  34410. defined(WOLFSSL_ALLOW_TLS_SHA1))
  34411. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  34412. WOLFSSL_SUCCESS);
  34413. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  34414. WOLFSSL_SUCCESS);
  34415. #endif
  34416. #endif
  34417. SSL_free(ssl);
  34418. SSL_CTX_free(ctx);
  34419. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34420. #endif
  34421. return EXPECT_RESULT();
  34422. }
  34423. /* Testing wolfSSL_set_tlsext_status_type function.
  34424. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  34425. */
  34426. static int test_wolfSSL_set_tlsext_status_type(void)
  34427. {
  34428. EXPECT_DECLS;
  34429. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  34430. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  34431. SSL* ssl = NULL;
  34432. SSL_CTX* ctx = NULL;
  34433. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34434. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34435. SSL_FILETYPE_PEM));
  34436. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34437. ExpectNotNull(ssl = SSL_new(ctx));
  34438. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  34439. SSL_SUCCESS);
  34440. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  34441. SSL_free(ssl);
  34442. SSL_CTX_free(ctx);
  34443. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  34444. return EXPECT_RESULT();
  34445. }
  34446. #ifndef NO_BIO
  34447. static int test_wolfSSL_PEM_read_bio(void)
  34448. {
  34449. EXPECT_DECLS;
  34450. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34451. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34452. byte buff[6000];
  34453. XFILE f = XBADFILE;
  34454. int bytes;
  34455. X509* x509 = NULL;
  34456. BIO* bio = NULL;
  34457. BUF_MEM* buf = NULL;
  34458. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  34459. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  34460. if (f != XBADFILE)
  34461. XFCLOSE(f);
  34462. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  34463. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  34464. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  34465. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  34466. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  34467. /* BIO should return the set EOF value */
  34468. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  34469. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  34470. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  34471. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  34472. BIO_free(bio);
  34473. BUF_MEM_free(buf);
  34474. X509_free(x509);
  34475. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  34476. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34477. return EXPECT_RESULT();
  34478. }
  34479. #if defined(OPENSSL_EXTRA)
  34480. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  34481. long argl, long ret)
  34482. {
  34483. (void)bio;
  34484. (void)cmd;
  34485. (void)argp;
  34486. (void)argi;
  34487. (void)argl;
  34488. return ret;
  34489. }
  34490. #endif
  34491. static int test_wolfSSL_BIO(void)
  34492. {
  34493. EXPECT_DECLS;
  34494. #if defined(OPENSSL_EXTRA)
  34495. const unsigned char* p = NULL;
  34496. byte buff[20];
  34497. BIO* bio1 = NULL;
  34498. BIO* bio2 = NULL;
  34499. BIO* bio3 = NULL;
  34500. char* bufPt = NULL;
  34501. int i;
  34502. for (i = 0; i < 20; i++) {
  34503. buff[i] = i;
  34504. }
  34505. /* test BIO_free with NULL */
  34506. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  34507. /* Creating and testing type BIO_s_bio */
  34508. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34509. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  34510. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  34511. /* read/write before set up */
  34512. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  34513. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  34514. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  34515. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  34516. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  34517. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  34518. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  34519. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  34520. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  34521. /* write buffer full */
  34522. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  34523. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  34524. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  34525. /* write the other direction with pair */
  34526. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  34527. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  34528. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  34529. /* try read */
  34530. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  34531. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  34532. /* try read using ctrl function */
  34533. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  34534. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  34535. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  34536. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  34537. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  34538. for (i = 0; i < 20; i++) {
  34539. ExpectIntEQ((int)bufPt[i], i);
  34540. }
  34541. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  34542. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  34543. for (i = 0; i < 8; i++) {
  34544. ExpectIntEQ((int)bufPt[i], i);
  34545. }
  34546. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  34547. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  34548. /* new pair */
  34549. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  34550. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  34551. bio2 = NULL;
  34552. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  34553. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34554. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  34555. /* test wrap around... */
  34556. ExpectIntEQ(BIO_reset(bio1), 0);
  34557. ExpectIntEQ(BIO_reset(bio3), 0);
  34558. /* fill write buffer, read only small amount then write again */
  34559. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34560. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34561. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  34562. for (i = 0; i < 4; i++) {
  34563. ExpectIntEQ(bufPt[i], i);
  34564. }
  34565. /* try writing over read index */
  34566. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  34567. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  34568. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  34569. /* read and write 0 bytes */
  34570. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34571. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  34572. /* should read only to end of write buffer then need to read again */
  34573. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  34574. for (i = 0; i < 16; i++) {
  34575. ExpectIntEQ(bufPt[i], buff[4 + i]);
  34576. }
  34577. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  34578. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  34579. for (i = 0; i < 4; i++) {
  34580. ExpectIntEQ(bufPt[i], 0);
  34581. }
  34582. /* read index should not have advanced with nread0 */
  34583. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  34584. for (i = 0; i < 4; i++) {
  34585. ExpectIntEQ(bufPt[i], 0);
  34586. }
  34587. /* write and fill up buffer checking reset of index state */
  34588. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34589. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34590. /* test reset on data in bio1 write buffer */
  34591. ExpectIntEQ(BIO_reset(bio1), 0);
  34592. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34593. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  34594. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34595. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  34596. ExpectNotNull(p);
  34597. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34598. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  34599. for (i = 0; i < 6; i++) {
  34600. ExpectIntEQ(bufPt[i], i);
  34601. }
  34602. /* test case of writing twice with offset read index */
  34603. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  34604. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  34605. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34606. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34607. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34608. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  34609. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  34610. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34611. BIO_free(bio1);
  34612. bio1 = NULL;
  34613. BIO_free(bio3);
  34614. bio3 = NULL;
  34615. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  34616. {
  34617. BIO* bioA = NULL;
  34618. BIO* bioB = NULL;
  34619. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  34620. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  34621. BIO_free(bioA);
  34622. bioA = NULL;
  34623. BIO_free(bioB);
  34624. bioB = NULL;
  34625. }
  34626. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  34627. /* BIOs with file pointers */
  34628. #if !defined(NO_FILESYSTEM)
  34629. {
  34630. XFILE f1 = XBADFILE;
  34631. XFILE f2 = XBADFILE;
  34632. BIO* f_bio1 = NULL;
  34633. BIO* f_bio2 = NULL;
  34634. unsigned char cert[300];
  34635. char testFile[] = "tests/bio_write_test.txt";
  34636. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  34637. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  34638. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  34639. /* Failure due to wrong BIO type */
  34640. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34641. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  34642. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  34643. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  34644. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  34645. WOLFSSL_SUCCESS);
  34646. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34647. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  34648. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  34649. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  34650. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  34651. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  34652. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  34653. ExpectIntEQ(BIO_reset(f_bio2), 0);
  34654. ExpectIntEQ(BIO_tell(NULL),-1);
  34655. ExpectIntEQ(BIO_tell(f_bio2),0);
  34656. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  34657. ExpectIntEQ(BIO_tell(f_bio2),4);
  34658. BIO_free(f_bio1);
  34659. f_bio1 = NULL;
  34660. BIO_free(f_bio2);
  34661. f_bio2 = NULL;
  34662. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  34663. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34664. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34665. BIO_free(f_bio1);
  34666. f_bio1 = NULL;
  34667. }
  34668. #endif /* !defined(NO_FILESYSTEM) */
  34669. /* BIO info callback */
  34670. {
  34671. const char* testArg = "test";
  34672. BIO* cb_bio = NULL;
  34673. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  34674. BIO_set_callback(cb_bio, bioCallback);
  34675. ExpectNotNull(BIO_get_callback(cb_bio));
  34676. BIO_set_callback(cb_bio, NULL);
  34677. ExpectNull(BIO_get_callback(cb_bio));
  34678. BIO_set_callback_arg(cb_bio, (char*)testArg);
  34679. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  34680. ExpectNull(BIO_get_callback_arg(NULL));
  34681. BIO_free(cb_bio);
  34682. cb_bio = NULL;
  34683. }
  34684. /* BIO_vfree */
  34685. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34686. BIO_vfree(NULL);
  34687. BIO_vfree(bio1);
  34688. #endif
  34689. return EXPECT_RESULT();
  34690. }
  34691. static int test_wolfSSL_BIO_BIO_ring_read(void)
  34692. {
  34693. EXPECT_DECLS;
  34694. #if defined(OPENSSL_ALL)
  34695. BIO* bio1 = NULL;
  34696. BIO* bio2 = NULL;
  34697. byte data[50];
  34698. byte tmp[50];
  34699. XMEMSET(data, 42, sizeof(data));
  34700. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  34701. SSL_SUCCESS);
  34702. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  34703. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  34704. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  34705. ExpectBufEQ(tmp, data, 20);
  34706. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  34707. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  34708. ExpectBufEQ(tmp, data, 40);
  34709. BIO_free(bio1);
  34710. BIO_free(bio2);
  34711. #endif
  34712. return EXPECT_RESULT();
  34713. }
  34714. #endif /* !NO_BIO */
  34715. static int test_wolfSSL_a2i_IPADDRESS(void)
  34716. {
  34717. EXPECT_DECLS;
  34718. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  34719. const unsigned char* data = NULL;
  34720. int dataSz = 0;
  34721. ASN1_OCTET_STRING *st = NULL;
  34722. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  34723. const unsigned char ipv6_exp[] = {
  34724. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  34725. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  34726. };
  34727. const unsigned char ipv6_home[] = {
  34728. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34729. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  34730. };
  34731. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  34732. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  34733. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34734. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  34735. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  34736. ASN1_STRING_free(st);
  34737. st = NULL;
  34738. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  34739. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34740. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34741. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  34742. ASN1_STRING_free(st);
  34743. st = NULL;
  34744. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  34745. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34746. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34747. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  34748. ASN1_STRING_free(st);
  34749. #endif
  34750. return EXPECT_RESULT();
  34751. }
  34752. static int test_wolfSSL_X509_cmp_time(void)
  34753. {
  34754. EXPECT_DECLS;
  34755. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  34756. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  34757. WOLFSSL_ASN1_TIME asn_time;
  34758. time_t t;
  34759. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  34760. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  34761. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  34762. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  34763. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  34764. #endif
  34765. return EXPECT_RESULT();
  34766. }
  34767. static int test_wolfSSL_X509_time_adj(void)
  34768. {
  34769. EXPECT_DECLS;
  34770. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  34771. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  34772. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  34773. !defined(NO_ASN_TIME)
  34774. X509* x509 = NULL;
  34775. time_t t;
  34776. time_t not_before;
  34777. time_t not_after;
  34778. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  34779. client_cert_der_2048, sizeof_client_cert_der_2048,
  34780. WOLFSSL_FILETYPE_ASN1));
  34781. t = 0;
  34782. not_before = wc_Time(0);
  34783. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  34784. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  34785. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  34786. /* Check X509_gmtime_adj, too. */
  34787. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  34788. X509_free(x509);
  34789. #endif
  34790. return EXPECT_RESULT();
  34791. }
  34792. static int test_wolfSSL_X509(void)
  34793. {
  34794. EXPECT_DECLS;
  34795. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34796. !defined(NO_RSA)
  34797. X509* x509 = NULL;
  34798. #ifndef NO_BIO
  34799. BIO* bio = NULL;
  34800. X509_STORE_CTX* ctx = NULL;
  34801. X509_STORE* store = NULL;
  34802. #endif
  34803. char der[] = "certs/ca-cert.der";
  34804. XFILE fp = XBADFILE;
  34805. ExpectNotNull(x509 = X509_new());
  34806. X509_free(x509);
  34807. x509 = NULL;
  34808. #ifndef NO_BIO
  34809. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34810. SSL_FILETYPE_PEM));
  34811. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34812. #ifdef WOLFSSL_CERT_GEN
  34813. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  34814. #endif
  34815. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34816. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  34817. ExpectNotNull(store = X509_STORE_new());
  34818. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  34819. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  34820. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  34821. X509_STORE_CTX_free(ctx);
  34822. X509_STORE_free(store);
  34823. X509_free(x509);
  34824. x509 = NULL;
  34825. BIO_free(bio);
  34826. #endif
  34827. /** d2i_X509_fp test **/
  34828. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34829. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  34830. ExpectNotNull(x509);
  34831. X509_free(x509);
  34832. x509 = NULL;
  34833. if (fp != XBADFILE) {
  34834. XFCLOSE(fp);
  34835. fp = XBADFILE;
  34836. }
  34837. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34838. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  34839. ExpectNotNull(x509);
  34840. X509_free(x509);
  34841. if (fp != XBADFILE)
  34842. XFCLOSE(fp);
  34843. /* X509_up_ref test */
  34844. ExpectIntEQ(X509_up_ref(NULL), 0);
  34845. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  34846. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  34847. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  34848. X509_free(x509); /* refCount = 2 */
  34849. X509_free(x509); /* refCount = 1 */
  34850. X509_free(x509); /* refCount = 0, free */
  34851. #endif
  34852. return EXPECT_RESULT();
  34853. }
  34854. static int test_wolfSSL_X509_get_ext_count(void)
  34855. {
  34856. EXPECT_DECLS;
  34857. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34858. !defined(NO_RSA)
  34859. int ret = 0;
  34860. WOLFSSL_X509* x509 = NULL;
  34861. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  34862. XFILE f = XBADFILE;
  34863. /* NULL parameter check */
  34864. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  34865. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  34866. SSL_FILETYPE_PEM));
  34867. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34868. wolfSSL_X509_free(x509);
  34869. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  34870. SSL_FILETYPE_PEM));
  34871. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34872. wolfSSL_X509_free(x509);
  34873. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  34874. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  34875. if (f != XBADFILE)
  34876. XFCLOSE(f);
  34877. /* wolfSSL_X509_get_ext_count() valid input */
  34878. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  34879. /* wolfSSL_X509_get_ext_count() NULL argument */
  34880. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  34881. wolfSSL_X509_free(x509);
  34882. #endif
  34883. return EXPECT_RESULT();
  34884. }
  34885. static int test_wolfSSL_X509_sign2(void)
  34886. {
  34887. EXPECT_DECLS;
  34888. /* test requires WOLFSSL_AKID_NAME to match expected output */
  34889. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34890. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  34891. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  34892. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  34893. defined(WOLFSSL_IP_ALT_NAME))
  34894. WOLFSSL_X509 *x509 = NULL;
  34895. WOLFSSL_X509 *ca = NULL;
  34896. const unsigned char *der = NULL;
  34897. const unsigned char *pt = NULL;
  34898. WOLFSSL_EVP_PKEY *priv = NULL;
  34899. WOLFSSL_X509_NAME *name = NULL;
  34900. int derSz;
  34901. #ifndef NO_ASN_TIME
  34902. WOLFSSL_ASN1_TIME *notBefore = NULL;
  34903. WOLFSSL_ASN1_TIME *notAfter = NULL;
  34904. const int year = 365*24*60*60;
  34905. const int day = 24*60*60;
  34906. const int hour = 60*60;
  34907. const int mini = 60;
  34908. time_t t;
  34909. #endif
  34910. const unsigned char expected[] = {
  34911. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  34912. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  34913. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  34914. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  34915. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  34916. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  34917. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  34918. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  34919. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  34920. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  34921. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  34922. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  34923. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  34924. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  34925. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34926. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  34927. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  34928. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  34929. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  34930. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  34931. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  34932. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  34933. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  34934. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  34935. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  34936. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  34937. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  34938. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  34939. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  34940. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  34941. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  34942. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  34943. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  34944. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34945. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  34946. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  34947. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  34948. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  34949. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  34950. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  34951. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  34952. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  34953. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  34954. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  34955. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  34956. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  34957. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  34958. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  34959. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  34960. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  34961. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  34962. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  34963. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  34964. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  34965. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  34966. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  34967. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  34968. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  34969. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  34970. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  34971. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  34972. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  34973. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34974. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34975. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  34976. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34977. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34978. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  34979. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  34980. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  34981. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  34982. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  34983. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  34984. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  34985. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  34986. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  34987. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  34988. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  34989. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  34990. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  34991. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  34992. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  34993. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  34994. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  34995. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  34996. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  34997. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  34998. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  34999. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  35000. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  35001. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  35002. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  35003. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  35004. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  35005. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  35006. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  35007. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  35008. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  35009. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  35010. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  35011. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  35012. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  35013. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  35014. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  35015. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  35016. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  35017. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  35018. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  35019. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  35020. };
  35021. pt = ca_key_der_2048;
  35022. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  35023. sizeof_ca_key_der_2048));
  35024. pt = client_cert_der_2048;
  35025. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  35026. sizeof_client_cert_der_2048));
  35027. pt = ca_cert_der_2048;
  35028. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  35029. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  35030. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  35031. #ifndef NO_ASN_TIME
  35032. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  35033. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  35034. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  35035. ExpectIntEQ(notAfter->length, 13);
  35036. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  35037. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  35038. #endif
  35039. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  35040. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  35041. ExpectIntEQ(derSz, sizeof(expected));
  35042. #ifndef NO_ASN_TIME
  35043. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  35044. #endif
  35045. wolfSSL_X509_free(ca);
  35046. wolfSSL_X509_free(x509);
  35047. wolfSSL_EVP_PKEY_free(priv);
  35048. #ifndef NO_ASN_TIME
  35049. wolfSSL_ASN1_TIME_free(notBefore);
  35050. wolfSSL_ASN1_TIME_free(notAfter);
  35051. #endif
  35052. #endif
  35053. return EXPECT_RESULT();
  35054. }
  35055. static int test_wolfSSL_X509_sign(void)
  35056. {
  35057. EXPECT_DECLS;
  35058. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  35059. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  35060. int ret = 0;
  35061. char *cn = NULL;
  35062. word32 cnSz = 0;
  35063. X509_NAME *name = NULL;
  35064. X509 *x509 = NULL;
  35065. X509 *ca = NULL;
  35066. DecodedCert dCert;
  35067. EVP_PKEY *pub = NULL;
  35068. EVP_PKEY *priv = NULL;
  35069. EVP_MD_CTX *mctx = NULL;
  35070. #if defined(USE_CERT_BUFFERS_1024)
  35071. const unsigned char* rsaPriv = client_key_der_1024;
  35072. const unsigned char* rsaPub = client_keypub_der_1024;
  35073. const unsigned char* certIssuer = client_cert_der_1024;
  35074. long clientKeySz = (long)sizeof_client_key_der_1024;
  35075. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  35076. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  35077. #elif defined(USE_CERT_BUFFERS_2048)
  35078. const unsigned char* rsaPriv = client_key_der_2048;
  35079. const unsigned char* rsaPub = client_keypub_der_2048;
  35080. const unsigned char* certIssuer = client_cert_der_2048;
  35081. long clientKeySz = (long)sizeof_client_key_der_2048;
  35082. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  35083. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  35084. #endif
  35085. byte sn[16];
  35086. int snSz = sizeof(sn);
  35087. /* Set X509_NAME fields */
  35088. ExpectNotNull(name = X509_NAME_new());
  35089. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  35090. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  35091. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  35092. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  35093. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  35094. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  35095. /* Get private and public keys */
  35096. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  35097. clientKeySz));
  35098. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  35099. ExpectNotNull(x509 = X509_new());
  35100. /* Set version 3 */
  35101. ExpectIntNE(X509_set_version(x509, 2L), 0);
  35102. /* Set subject name, add pubkey, and sign certificate */
  35103. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  35104. X509_NAME_free(name);
  35105. name = NULL;
  35106. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  35107. #ifdef WOLFSSL_ALT_NAMES
  35108. /* Add some subject alt names */
  35109. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  35110. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  35111. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35112. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  35113. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35114. "sphygmomanometer",
  35115. ASN_DNS_TYPE), SSL_SUCCESS);
  35116. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35117. "supercalifragilisticexpialidocious",
  35118. ASN_DNS_TYPE), SSL_SUCCESS);
  35119. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35120. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  35121. ASN_DNS_TYPE), SSL_SUCCESS);
  35122. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35123. {
  35124. unsigned char ip4_type[] = {127,128,0,255};
  35125. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  35126. 0xff, 0xee, 0x99, 0x88,
  35127. 0x77, 0x66, 0x55, 0x44,
  35128. 0x00, 0x33, 0x22, 0x11};
  35129. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  35130. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  35131. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  35132. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  35133. }
  35134. #endif
  35135. #endif /* WOLFSSL_ALT_NAMES */
  35136. /* test valid sign case */
  35137. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  35138. /* test valid X509_sign_ctx case */
  35139. ExpectNotNull(mctx = EVP_MD_CTX_new());
  35140. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  35141. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  35142. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  35143. ExpectIntEQ(X509_get_ext_count(x509), 1);
  35144. #endif
  35145. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  35146. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  35147. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  35148. #endif
  35149. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  35150. WOLFSSL_SUCCESS);
  35151. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  35152. /* Variation in size depends on ASN.1 encoding when MSB is set.
  35153. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  35154. * with the MSB set. See GenerateInteger in asn.c */
  35155. #ifndef USE_CERT_BUFFERS_1024
  35156. #ifndef WOLFSSL_ALT_NAMES
  35157. /* Valid case - size should be 781-786 with 16 byte serial number */
  35158. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  35159. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35160. /* Valid case - size should be 955-960 with 16 byte serial number */
  35161. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  35162. #else
  35163. /* Valid case - size should be 926-931 with 16 byte serial number */
  35164. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  35165. #endif
  35166. #else
  35167. #ifndef WOLFSSL_ALT_NAMES
  35168. /* Valid case - size should be 537-542 with 16 byte serial number */
  35169. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  35170. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35171. /* Valid case - size should be 695-670 with 16 byte serial number */
  35172. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  35173. #else
  35174. /* Valid case - size should be 666-671 with 16 byte serial number */
  35175. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  35176. #endif
  35177. #endif
  35178. /* check that issuer name is as expected after signature */
  35179. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  35180. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  35181. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  35182. ExpectNotNull(name = X509_get_subject_name(ca));
  35183. cnSz = X509_NAME_get_sz(name);
  35184. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  35185. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  35186. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  35187. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  35188. cn = NULL;
  35189. #ifdef WOLFSSL_MULTI_ATTRIB
  35190. /* test adding multiple OU's to the signer */
  35191. ExpectNotNull(name = X509_get_subject_name(ca));
  35192. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  35193. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  35194. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  35195. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  35196. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  35197. #endif
  35198. ExpectNotNull(name = X509_get_subject_name(ca));
  35199. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  35200. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  35201. ExpectNotNull(name = X509_get_issuer_name(x509));
  35202. cnSz = X509_NAME_get_sz(name);
  35203. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  35204. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  35205. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  35206. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  35207. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  35208. cn = NULL;
  35209. FreeDecodedCert(&dCert);
  35210. /* Test invalid parameters */
  35211. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  35212. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  35213. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  35214. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  35215. EVP_MD_CTX_free(mctx);
  35216. mctx = NULL;
  35217. ExpectNotNull(mctx = EVP_MD_CTX_new());
  35218. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  35219. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  35220. /* test invalid version number */
  35221. #if defined(OPENSSL_ALL)
  35222. ExpectIntNE(X509_set_version(x509, 6L), 0);
  35223. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  35224. /* uses ParseCert which fails on bad version number */
  35225. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  35226. #endif
  35227. EVP_MD_CTX_free(mctx);
  35228. EVP_PKEY_free(priv);
  35229. EVP_PKEY_free(pub);
  35230. X509_free(x509);
  35231. X509_free(ca);
  35232. #endif
  35233. return EXPECT_RESULT();
  35234. }
  35235. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  35236. {
  35237. EXPECT_DECLS;
  35238. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  35239. X509* x509 = NULL;
  35240. const X509_ALGOR* alg;
  35241. ExpectNotNull(x509 = X509_new());
  35242. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  35243. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  35244. X509_free(x509);
  35245. #endif
  35246. return EXPECT_RESULT();
  35247. }
  35248. static int test_wolfSSL_X509_ALGOR_get0(void)
  35249. {
  35250. EXPECT_DECLS;
  35251. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35252. !defined(NO_SHA256) && !defined(NO_RSA)
  35253. X509* x509 = NULL;
  35254. const ASN1_OBJECT* obj = NULL;
  35255. const X509_ALGOR* alg = NULL;
  35256. int pptype = 0;
  35257. const void *ppval = NULL;
  35258. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  35259. SSL_FILETYPE_PEM));
  35260. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  35261. /* Invalid case */
  35262. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  35263. ExpectNull(obj);
  35264. /* Valid case */
  35265. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  35266. ExpectNotNull(obj);
  35267. ExpectNull(ppval);
  35268. ExpectIntNE(pptype, 0);
  35269. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  35270. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  35271. X509_free(x509);
  35272. #endif
  35273. return EXPECT_RESULT();
  35274. }
  35275. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  35276. {
  35277. EXPECT_DECLS;
  35278. #if defined(OPENSSL_EXTRA)
  35279. X509_VERIFY_PARAM *paramTo = NULL;
  35280. X509_VERIFY_PARAM *paramFrom = NULL;
  35281. char testIPv4[] = "127.0.0.1";
  35282. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  35283. char testhostName1[] = "foo.hoge.com";
  35284. char testhostName2[] = "foobar.hoge.com";
  35285. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  35286. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  35287. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  35288. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  35289. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  35290. (int)XSTRLEN(testhostName1)), 1);
  35291. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  35292. (int)XSTRLEN(testhostName1)));
  35293. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  35294. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  35295. ExpectIntEQ(0x01, paramFrom->hostFlags);
  35296. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  35297. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  35298. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  35299. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  35300. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  35301. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35302. /* null pointer */
  35303. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  35304. /* in the case of "from" null, returns success */
  35305. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  35306. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  35307. /* inherit flags test : VPARAM_DEFAULT */
  35308. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35309. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35310. (int)XSTRLEN(testhostName1)));
  35311. ExpectIntEQ(0x01, paramTo->hostFlags);
  35312. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35313. /* inherit flags test : VPARAM OVERWRITE */
  35314. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35315. (int)XSTRLEN(testhostName2)), 1);
  35316. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35317. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  35318. if (paramTo != NULL) {
  35319. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  35320. }
  35321. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35322. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35323. (int)XSTRLEN(testhostName1)));
  35324. ExpectIntEQ(0x01, paramTo->hostFlags);
  35325. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35326. /* inherit flags test : VPARAM_RESET_FLAGS */
  35327. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35328. (int)XSTRLEN(testhostName2)), 1);
  35329. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35330. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  35331. if (paramTo != NULL) {
  35332. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  35333. }
  35334. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35335. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35336. (int)XSTRLEN(testhostName1)));
  35337. ExpectIntEQ(0x01, paramTo->hostFlags);
  35338. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35339. /* inherit flags test : VPARAM_LOCKED */
  35340. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35341. (int)XSTRLEN(testhostName2)), 1);
  35342. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35343. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  35344. if (paramTo != NULL) {
  35345. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  35346. }
  35347. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35348. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  35349. (int)XSTRLEN(testhostName2)));
  35350. ExpectIntEQ(0x00, paramTo->hostFlags);
  35351. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  35352. /* test for incorrect parameters */
  35353. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  35354. 0);
  35355. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  35356. /* inherit flags test : VPARAM_ONCE, not testable yet */
  35357. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  35358. 1);
  35359. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  35360. X509_V_FLAG_CRL_CHECK_ALL);
  35361. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  35362. X509_V_FLAG_CRL_CHECK_ALL), 1);
  35363. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  35364. X509_VERIFY_PARAM_free(paramTo);
  35365. X509_VERIFY_PARAM_free(paramFrom);
  35366. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  35367. #endif
  35368. return EXPECT_RESULT();
  35369. }
  35370. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35371. static int test_wolfSSL_check_domain_verify_count = 0;
  35372. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  35373. WOLFSSL_X509_STORE_CTX* store)
  35374. {
  35375. EXPECT_DECLS;
  35376. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  35377. ExpectIntEQ(preverify, 1);
  35378. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  35379. return EXPECT_SUCCESS();
  35380. }
  35381. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  35382. {
  35383. EXPECT_DECLS;
  35384. X509_VERIFY_PARAM *param = NULL;
  35385. ExpectNotNull(param = SSL_get0_param(ssl));
  35386. /* Domain check should only be done on the leaf cert */
  35387. X509_VERIFY_PARAM_set_hostflags(param,
  35388. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  35389. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  35390. "wolfSSL Server Chain", 0), 1);
  35391. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  35392. test_wolfSSL_check_domain_verify_cb);
  35393. return EXPECT_RESULT();
  35394. }
  35395. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  35396. {
  35397. EXPECT_DECLS;
  35398. /* Use a cert with different domains in chain */
  35399. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  35400. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  35401. return EXPECT_RESULT();
  35402. }
  35403. static int test_wolfSSL_check_domain(void)
  35404. {
  35405. EXPECT_DECLS;
  35406. test_ssl_cbf func_cb_client;
  35407. test_ssl_cbf func_cb_server;
  35408. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35409. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35410. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  35411. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  35412. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35413. &func_cb_server, NULL), TEST_SUCCESS);
  35414. /* Should have been called once for each cert in sent chain */
  35415. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  35416. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  35417. #else
  35418. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  35419. #endif
  35420. return EXPECT_RESULT();
  35421. }
  35422. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  35423. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  35424. {
  35425. EXPECT_DECLS;
  35426. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  35427. X509* x509 = NULL;
  35428. X509_PUBKEY* pubKey;
  35429. ExpectNotNull(x509 = X509_new());
  35430. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  35431. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  35432. X509_free(x509);
  35433. #endif
  35434. return EXPECT_RESULT();
  35435. }
  35436. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  35437. {
  35438. EXPECT_DECLS;
  35439. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35440. !defined(NO_SHA256) && !defined(NO_RSA)
  35441. X509* x509 = NULL;
  35442. ASN1_OBJECT* obj = NULL;
  35443. const ASN1_OBJECT* pa_oid = NULL;
  35444. X509_PUBKEY* pubKey = NULL;
  35445. X509_PUBKEY* pubKey2 = NULL;
  35446. EVP_PKEY* evpKey = NULL;
  35447. const unsigned char *pk = NULL;
  35448. int ppklen;
  35449. int pptype;
  35450. X509_ALGOR *pa = NULL;
  35451. const void *pval;
  35452. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  35453. SSL_FILETYPE_PEM));
  35454. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  35455. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  35456. ExpectNotNull(pk);
  35457. ExpectNotNull(pa);
  35458. ExpectNotNull(pubKey);
  35459. ExpectIntGT(ppklen, 0);
  35460. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  35461. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  35462. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  35463. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  35464. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  35465. ExpectNotNull(pk);
  35466. ExpectNotNull(pa);
  35467. ExpectIntGT(ppklen, 0);
  35468. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35469. ExpectNotNull(pa_oid);
  35470. ExpectNull(pval);
  35471. ExpectIntEQ(pptype, V_ASN1_NULL);
  35472. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  35473. X509_PUBKEY_free(pubKey2);
  35474. X509_free(x509);
  35475. EVP_PKEY_free(evpKey);
  35476. #endif
  35477. return EXPECT_RESULT();
  35478. }
  35479. static int test_wolfSSL_X509_PUBKEY_EC(void)
  35480. {
  35481. EXPECT_DECLS;
  35482. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  35483. X509* x509 = NULL;
  35484. ASN1_OBJECT* obj = NULL;
  35485. ASN1_OBJECT* poid = NULL;
  35486. const ASN1_OBJECT* pa_oid = NULL;
  35487. X509_PUBKEY* pubKey = NULL;
  35488. X509_PUBKEY* pubKey2 = NULL;
  35489. EVP_PKEY* evpKey = NULL;
  35490. const unsigned char *pk = NULL;
  35491. int ppklen;
  35492. int pptype;
  35493. X509_ALGOR *pa = NULL;
  35494. const void *pval;
  35495. char buf[50];
  35496. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  35497. SSL_FILETYPE_PEM));
  35498. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  35499. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  35500. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  35501. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  35502. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  35503. ExpectNotNull(pk);
  35504. ExpectNotNull(pa);
  35505. ExpectIntGT(ppklen, 0);
  35506. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35507. ExpectNotNull(pa_oid);
  35508. ExpectNotNull(pval);
  35509. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  35510. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  35511. poid = (ASN1_OBJECT *)pval;
  35512. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  35513. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  35514. X509_PUBKEY_free(pubKey2);
  35515. X509_free(x509);
  35516. EVP_PKEY_free(evpKey);
  35517. #endif
  35518. return EXPECT_RESULT();
  35519. }
  35520. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  35521. {
  35522. EXPECT_DECLS;
  35523. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  35524. word32 bytes;
  35525. #ifdef USE_CERT_BUFFERS_1024
  35526. byte tmp[ONEK_BUF];
  35527. #elif defined(USE_CERT_BUFFERS_2048)
  35528. byte tmp[TWOK_BUF];
  35529. #else
  35530. byte tmp[TWOK_BUF];
  35531. #endif /* END USE_CERT_BUFFERS_1024 */
  35532. const unsigned char* dsaKeyDer = tmp;
  35533. ASN1_OBJECT* obj = NULL;
  35534. ASN1_STRING* str;
  35535. const ASN1_OBJECT* pa_oid = NULL;
  35536. X509_PUBKEY* pubKey = NULL;
  35537. EVP_PKEY* evpKey = NULL;
  35538. const unsigned char *pk = NULL;
  35539. int ppklen, pptype;
  35540. X509_ALGOR *pa = NULL;
  35541. const void *pval;
  35542. #ifdef USE_CERT_BUFFERS_1024
  35543. XMEMSET(tmp, 0, sizeof(tmp));
  35544. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  35545. bytes = sizeof_dsa_key_der_1024;
  35546. #elif defined(USE_CERT_BUFFERS_2048)
  35547. XMEMSET(tmp, 0, sizeof(tmp));
  35548. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  35549. bytes = sizeof_dsa_key_der_2048;
  35550. #else
  35551. {
  35552. XFILE fp = XBADFILE;
  35553. XMEMSET(tmp, 0, sizeof(tmp));
  35554. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  35555. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  35556. if (fp != XBADFILE)
  35557. XFCLOSE(fp);
  35558. }
  35559. #endif
  35560. /* Initialize pkey with der format dsa key */
  35561. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  35562. ExpectNotNull(pubKey = X509_PUBKEY_new());
  35563. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  35564. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  35565. ExpectNotNull(pk);
  35566. ExpectNotNull(pa);
  35567. ExpectIntGT(ppklen, 0);
  35568. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35569. ExpectNotNull(pa_oid);
  35570. ExpectNotNull(pval);
  35571. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  35572. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  35573. str = (ASN1_STRING *)pval;
  35574. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  35575. #ifdef USE_CERT_BUFFERS_1024
  35576. ExpectIntEQ(ASN1_STRING_length(str), 291);
  35577. #else
  35578. ExpectIntEQ(ASN1_STRING_length(str), 549);
  35579. #endif /* END USE_CERT_BUFFERS_1024 */
  35580. X509_PUBKEY_free(pubKey);
  35581. EVP_PKEY_free(evpKey);
  35582. #endif
  35583. return EXPECT_RESULT();
  35584. }
  35585. static int test_wolfSSL_BUF(void)
  35586. {
  35587. EXPECT_DECLS;
  35588. #if defined(OPENSSL_EXTRA)
  35589. BUF_MEM* buf = NULL;
  35590. ExpectNotNull(buf = BUF_MEM_new());
  35591. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  35592. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  35593. BUF_MEM_free(buf);
  35594. #endif
  35595. return EXPECT_RESULT();
  35596. }
  35597. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35598. static int stub_rand_seed(const void *buf, int num)
  35599. {
  35600. (void)buf;
  35601. (void)num;
  35602. return 123;
  35603. }
  35604. static int stub_rand_bytes(unsigned char *buf, int num)
  35605. {
  35606. (void)buf;
  35607. (void)num;
  35608. return 456;
  35609. }
  35610. static byte* was_stub_rand_cleanup_called(void)
  35611. {
  35612. static byte was_called = 0;
  35613. return &was_called;
  35614. }
  35615. static void stub_rand_cleanup(void)
  35616. {
  35617. byte* was_called = was_stub_rand_cleanup_called();
  35618. *was_called = 1;
  35619. return;
  35620. }
  35621. static byte* was_stub_rand_add_called(void)
  35622. {
  35623. static byte was_called = 0;
  35624. return &was_called;
  35625. }
  35626. static int stub_rand_add(const void *buf, int num, double entropy)
  35627. {
  35628. byte* was_called = was_stub_rand_add_called();
  35629. (void)buf;
  35630. (void)num;
  35631. (void)entropy;
  35632. *was_called = 1;
  35633. return 0;
  35634. }
  35635. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  35636. {
  35637. (void)buf;
  35638. (void)num;
  35639. return 9876;
  35640. }
  35641. static int stub_rand_status(void)
  35642. {
  35643. return 5432;
  35644. }
  35645. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35646. static int test_wolfSSL_RAND_set_rand_method(void)
  35647. {
  35648. EXPECT_DECLS;
  35649. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35650. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  35651. unsigned char* buf = NULL;
  35652. int num = 0;
  35653. double entropy = 0;
  35654. int ret;
  35655. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  35656. byte* was_add_called = was_stub_rand_add_called();
  35657. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  35658. DYNAMIC_TYPE_TMP_BUFFER));
  35659. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  35660. ExpectIntEQ(*was_cleanup_called, 0);
  35661. RAND_cleanup();
  35662. ExpectIntEQ(*was_cleanup_called, 0);
  35663. rand_methods.seed = &stub_rand_seed;
  35664. rand_methods.bytes = &stub_rand_bytes;
  35665. rand_methods.cleanup = &stub_rand_cleanup;
  35666. rand_methods.add = &stub_rand_add;
  35667. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  35668. rand_methods.status = &stub_rand_status;
  35669. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  35670. ExpectIntEQ(RAND_seed(buf, num), 123);
  35671. ExpectIntEQ(RAND_bytes(buf, num), 456);
  35672. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  35673. ExpectIntEQ(RAND_status(), 5432);
  35674. ExpectIntEQ(*was_add_called, 0);
  35675. /* The function pointer for RAND_add returns int, but RAND_add itself
  35676. * returns void. */
  35677. RAND_add(buf, num, entropy);
  35678. ExpectIntEQ(*was_add_called, 1);
  35679. was_add_called = 0;
  35680. ExpectIntEQ(*was_cleanup_called, 0);
  35681. RAND_cleanup();
  35682. ExpectIntEQ(*was_cleanup_called, 1);
  35683. *was_cleanup_called = 0;
  35684. ret = RAND_set_rand_method(NULL);
  35685. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  35686. ExpectIntNE(RAND_status(), 5432);
  35687. ExpectIntEQ(*was_cleanup_called, 0);
  35688. RAND_cleanup();
  35689. ExpectIntEQ(*was_cleanup_called, 0);
  35690. RAND_set_rand_method(NULL);
  35691. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35692. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35693. return EXPECT_RESULT();
  35694. }
  35695. static int test_wolfSSL_RAND_bytes(void)
  35696. {
  35697. EXPECT_DECLS;
  35698. #if defined(OPENSSL_EXTRA)
  35699. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  35700. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  35701. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  35702. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  35703. int max_bufsize;
  35704. byte *my_buf = NULL;
  35705. /* sanity check */
  35706. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  35707. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  35708. max_bufsize = size4;
  35709. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  35710. DYNAMIC_TYPE_TMP_BUFFER));
  35711. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  35712. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  35713. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  35714. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  35715. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  35716. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  35717. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  35718. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35719. #endif
  35720. return EXPECT_RESULT();
  35721. }
  35722. static int test_wolfSSL_RAND(void)
  35723. {
  35724. EXPECT_DECLS;
  35725. #if defined(OPENSSL_EXTRA)
  35726. byte seed[16];
  35727. XMEMSET(seed, 0, sizeof(seed));
  35728. /* No global methods set. */
  35729. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  35730. ExpectIntEQ(RAND_poll(), 1);
  35731. RAND_cleanup();
  35732. ExpectIntEQ(RAND_egd(NULL), -1);
  35733. #ifndef NO_FILESYSTEM
  35734. {
  35735. char fname[100];
  35736. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  35737. ExpectIntEQ(RAND_write_file(NULL), 0);
  35738. }
  35739. #endif
  35740. #endif
  35741. return EXPECT_RESULT();
  35742. }
  35743. static int test_wolfSSL_PKCS8_Compat(void)
  35744. {
  35745. EXPECT_DECLS;
  35746. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  35747. !defined(NO_BIO)
  35748. PKCS8_PRIV_KEY_INFO* pt = NULL;
  35749. BIO* bio = NULL;
  35750. XFILE f = XBADFILE;
  35751. int bytes;
  35752. char pkcs8_buffer[512];
  35753. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35754. EVP_PKEY *pkey = NULL;
  35755. #endif
  35756. /* file from wolfssl/certs/ directory */
  35757. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  35758. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  35759. 0);
  35760. if (f != XBADFILE)
  35761. XFCLOSE(f);
  35762. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35763. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  35764. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35765. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  35766. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  35767. /* gets PKCS8 pointer to pkey */
  35768. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  35769. EVP_PKEY_free(pkey);
  35770. #endif
  35771. BIO_free(bio);
  35772. PKCS8_PRIV_KEY_INFO_free(pt);
  35773. #endif
  35774. return EXPECT_RESULT();
  35775. }
  35776. static int test_wolfSSL_PKCS8_d2i(void)
  35777. {
  35778. EXPECT_DECLS;
  35779. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  35780. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  35781. * requires a 12 byte password in FIPS mode. This test ends up
  35782. * trying to use an 8 byte password. */
  35783. #ifndef NO_FILESYSTEM
  35784. unsigned char pkcs8_buffer[2048];
  35785. const unsigned char* p = NULL;
  35786. int bytes = 0;
  35787. XFILE file = XBADFILE;
  35788. WOLFSSL_EVP_PKEY* pkey = NULL;
  35789. #ifndef NO_BIO
  35790. BIO* bio = NULL;
  35791. #if defined(OPENSSL_ALL) && \
  35792. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  35793. defined(HAVE_ECC)) && \
  35794. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35795. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  35796. #endif
  35797. #endif
  35798. #ifndef NO_RSA
  35799. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  35800. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  35801. #ifndef NO_DES3
  35802. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  35803. #endif
  35804. #endif /* NO_RSA */
  35805. #ifdef HAVE_ECC
  35806. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  35807. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  35808. #ifndef NO_DES3
  35809. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  35810. #endif
  35811. #endif /* HAVE_ECC */
  35812. #endif /* !NO_FILESYSTEM */
  35813. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  35814. #ifndef NO_RSA
  35815. #ifdef USE_CERT_BUFFERS_1024
  35816. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  35817. int rsaSz = sizeof_server_key_der_1024;
  35818. #else
  35819. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  35820. int rsaSz = sizeof_server_key_der_2048;
  35821. #endif
  35822. #endif
  35823. #ifdef HAVE_ECC
  35824. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  35825. int ecSz = sizeof_ecc_key_der_256;
  35826. #endif
  35827. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  35828. #ifndef NO_FILESYSTEM
  35829. (void)pkcs8_buffer;
  35830. (void)p;
  35831. (void)bytes;
  35832. (void)file;
  35833. #ifndef NO_BIO
  35834. (void)bio;
  35835. #endif
  35836. #endif
  35837. #ifdef OPENSSL_ALL
  35838. #ifndef NO_RSA
  35839. /* Try to auto-detect normal RSA private key */
  35840. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  35841. EVP_PKEY_free(pkey);
  35842. pkey = NULL;
  35843. #endif
  35844. #ifdef HAVE_ECC
  35845. /* Try to auto-detect normal EC private key */
  35846. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  35847. EVP_PKEY_free(pkey);
  35848. pkey = NULL;
  35849. #endif
  35850. #endif /* OPENSSL_ALL */
  35851. #ifndef NO_FILESYSTEM
  35852. #ifndef NO_RSA
  35853. /* Get DER encoded RSA PKCS#8 data. */
  35854. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  35855. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35856. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35857. file)), 0);
  35858. if (file != XBADFILE) {
  35859. XFCLOSE(file);
  35860. file = XBADFILE;
  35861. }
  35862. p = pkcs8_buffer;
  35863. #ifdef OPENSSL_ALL
  35864. /* Try to decode - auto-detect key type. */
  35865. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35866. #else
  35867. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  35868. #endif
  35869. /* Get PEM encoded RSA PKCS#8 data. */
  35870. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  35871. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35872. file)), 0);
  35873. if (file != XBADFILE) {
  35874. XFCLOSE(file);
  35875. file = XBADFILE;
  35876. }
  35877. #if defined(OPENSSL_ALL) && \
  35878. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35879. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35880. /* Write PKCS#8 PEM to BIO. */
  35881. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35882. NULL), bytes);
  35883. /* Compare file and written data */
  35884. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35885. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35886. BIO_free(bio);
  35887. bio = NULL;
  35888. #if !defined(NO_DES3) && !defined(NO_SHA)
  35889. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35890. /* Write Encrypted PKCS#8 PEM to BIO. */
  35891. bytes = 1834;
  35892. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  35893. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35894. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35895. (void*)"yassl123"));
  35896. EVP_PKEY_free(evpPkey);
  35897. evpPkey = NULL;
  35898. BIO_free(bio);
  35899. bio = NULL;
  35900. #endif /* !NO_DES3 && !NO_SHA */
  35901. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35902. EVP_PKEY_free(pkey);
  35903. pkey = NULL;
  35904. /* PKCS#8 encrypted RSA key */
  35905. #ifndef NO_DES3
  35906. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  35907. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35908. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35909. file)), 0);
  35910. if (file != XBADFILE) {
  35911. XFCLOSE(file);
  35912. file = XBADFILE;
  35913. }
  35914. #if defined(OPENSSL_ALL) && \
  35915. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35916. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35917. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35918. (void*)"yassl123"));
  35919. EVP_PKEY_free(pkey);
  35920. pkey = NULL;
  35921. BIO_free(bio);
  35922. bio = NULL;
  35923. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35924. #endif /* !NO_DES3 */
  35925. #endif /* NO_RSA */
  35926. #ifdef HAVE_ECC
  35927. /* PKCS#8 encode EC key */
  35928. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  35929. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35930. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35931. file)), 0);
  35932. if (file != XBADFILE) {
  35933. XFCLOSE(file);
  35934. file = XBADFILE;
  35935. }
  35936. p = pkcs8_buffer;
  35937. #ifdef OPENSSL_ALL
  35938. /* Try to decode - auto-detect key type. */
  35939. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35940. #else
  35941. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  35942. #endif
  35943. /* Get PEM encoded RSA PKCS#8 data. */
  35944. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  35945. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35946. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35947. file)), 0);
  35948. if (file != XBADFILE) {
  35949. XFCLOSE(file);
  35950. file = XBADFILE;
  35951. }
  35952. #if defined(OPENSSL_ALL) && \
  35953. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  35954. defined(HAVE_AES_CBC)
  35955. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35956. /* Write PKCS#8 PEM to BIO. */
  35957. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35958. NULL), bytes);
  35959. /* Compare file and written data */
  35960. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35961. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35962. BIO_free(bio);
  35963. bio = NULL;
  35964. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35965. /* Write Encrypted PKCS#8 PEM to BIO. */
  35966. bytes = 379;
  35967. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  35968. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35969. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35970. (void*)"yassl123"));
  35971. EVP_PKEY_free(evpPkey);
  35972. evpPkey = NULL;
  35973. BIO_free(bio);
  35974. bio = NULL;
  35975. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  35976. EVP_PKEY_free(pkey);
  35977. pkey = NULL;
  35978. /* PKCS#8 encrypted EC key */
  35979. #ifndef NO_DES3
  35980. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  35981. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35982. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35983. file)), 0);
  35984. if (file != XBADFILE) {
  35985. XFCLOSE(file);
  35986. file = XBADFILE;
  35987. }
  35988. #if defined(OPENSSL_ALL) && \
  35989. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35990. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35991. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35992. (void*)"yassl123"));
  35993. EVP_PKEY_free(pkey);
  35994. pkey = NULL;
  35995. BIO_free(bio);
  35996. bio = NULL;
  35997. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35998. #endif /* !NO_DES3 */
  35999. #endif /* HAVE_ECC */
  36000. #endif /* !NO_FILESYSTEM */
  36001. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  36002. return EXPECT_RESULT();
  36003. }
  36004. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  36005. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  36006. #define LOGGING_THREADS 5
  36007. #define ERROR_COUNT 10
  36008. /* copied from logging.c since this is not exposed otherwise */
  36009. #ifndef ERROR_QUEUE_MAX
  36010. #ifdef ERROR_QUEUE_PER_THREAD
  36011. #define ERROR_QUEUE_MAX 16
  36012. #else
  36013. /* this breaks from compat of unlimited error queue size */
  36014. #define ERROR_QUEUE_MAX 100
  36015. #endif
  36016. #endif
  36017. static volatile int loggingThreadsReady;
  36018. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  36019. {
  36020. const char* file;
  36021. int line;
  36022. unsigned long err;
  36023. int errorCount = 0;
  36024. int i;
  36025. (void)args;
  36026. while (!loggingThreadsReady);
  36027. for (i = 0; i < ERROR_COUNT; i++)
  36028. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  36029. while ((err = ERR_get_error_line(&file, &line))) {
  36030. AssertIntEQ(err, 990 + errorCount);
  36031. errorCount++;
  36032. }
  36033. AssertIntEQ(errorCount, ERROR_COUNT);
  36034. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  36035. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  36036. errorCount = 0;
  36037. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  36038. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  36039. while ((err = ERR_get_error_line(&file, &line))) {
  36040. AssertIntEQ(err, 990 + errorCount);
  36041. errorCount++;
  36042. }
  36043. /* test that the 3 errors over the max were dropped */
  36044. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  36045. WOLFSSL_RETURN_FROM_THREAD(0);
  36046. }
  36047. #endif
  36048. static int test_error_queue_per_thread(void)
  36049. {
  36050. int res = TEST_SKIPPED;
  36051. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  36052. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  36053. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  36054. int i;
  36055. ERR_clear_error(); /* clear out any error nodes */
  36056. loggingThreadsReady = 0;
  36057. for (i = 0; i < LOGGING_THREADS; i++)
  36058. start_thread(test_logging, NULL, &loggingThreads[i]);
  36059. loggingThreadsReady = 1;
  36060. for (i = 0; i < LOGGING_THREADS; i++)
  36061. join_thread(loggingThreads[i]);
  36062. res = TEST_SUCCESS;
  36063. #endif
  36064. return res;
  36065. }
  36066. static int test_wolfSSL_ERR_put_error(void)
  36067. {
  36068. EXPECT_DECLS;
  36069. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36070. defined(DEBUG_WOLFSSL)
  36071. const char* file;
  36072. int line;
  36073. ERR_clear_error(); /* clear out any error nodes */
  36074. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  36075. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36076. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  36077. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  36078. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  36079. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  36080. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  36081. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  36082. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  36083. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  36084. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  36085. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  36086. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  36087. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  36088. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  36089. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  36090. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  36091. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  36092. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  36093. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  36094. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  36095. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  36096. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  36097. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  36098. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  36099. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  36100. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  36101. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  36102. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  36103. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  36104. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  36105. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  36106. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  36107. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  36108. "this file", 100);
  36109. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  36110. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  36111. ExpectIntEQ(line, 100);
  36112. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  36113. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  36114. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  36115. #endif
  36116. /* try reading past end of error queue */
  36117. file = NULL;
  36118. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36119. ExpectNull(file);
  36120. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  36121. PEMerr(4,4);
  36122. ExpectIntEQ(ERR_get_error(), 4);
  36123. /* Empty and free up all error nodes */
  36124. ERR_clear_error();
  36125. /* Verify all nodes are cleared */
  36126. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  36127. ERR_clear_error();
  36128. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36129. #endif
  36130. return EXPECT_RESULT();
  36131. }
  36132. /*
  36133. * This is a regression test for a bug where the peek/get error functions were
  36134. * drawing from the end of the queue rather than the front.
  36135. */
  36136. static int test_wolfSSL_ERR_get_error_order(void)
  36137. {
  36138. EXPECT_DECLS;
  36139. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  36140. /* Empty the queue. */
  36141. wolfSSL_ERR_clear_error();
  36142. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  36143. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  36144. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  36145. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  36146. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  36147. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  36148. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  36149. return EXPECT_RESULT();
  36150. }
  36151. #ifndef NO_BIO
  36152. static int test_wolfSSL_ERR_print_errors(void)
  36153. {
  36154. EXPECT_DECLS;
  36155. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36156. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  36157. BIO* bio = NULL;
  36158. char buf[1024];
  36159. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36160. ERR_clear_error(); /* clear out any error nodes */
  36161. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  36162. /* Choosing -600 as an unused errno. */
  36163. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  36164. ERR_print_errors(bio);
  36165. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  36166. ExpectIntEQ(XSTRNCMP(
  36167. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  36168. buf, 55), 0);
  36169. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  36170. ExpectIntEQ(XSTRNCMP(
  36171. "error:600:wolfSSL library:unknown error number:asn.c:100",
  36172. buf, 56), 0);
  36173. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  36174. ExpectIntEQ(buf[0], '\0');
  36175. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  36176. BIO_free(bio);
  36177. #endif
  36178. return EXPECT_RESULT();
  36179. }
  36180. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36181. defined(DEBUG_WOLFSSL)
  36182. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  36183. {
  36184. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  36185. return 0;
  36186. }
  36187. #endif
  36188. static int test_wolfSSL_ERR_print_errors_cb(void)
  36189. {
  36190. EXPECT_DECLS;
  36191. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36192. defined(DEBUG_WOLFSSL)
  36193. BIO* bio = NULL;
  36194. char buf[1024];
  36195. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36196. ERR_clear_error(); /* clear out any error nodes */
  36197. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  36198. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  36199. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  36200. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  36201. ExpectIntEQ(XSTRNCMP(
  36202. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  36203. buf, 53), 0);
  36204. ExpectIntEQ(XSTRNCMP(
  36205. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  36206. buf + 53, 55), 0);
  36207. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  36208. BIO_free(bio);
  36209. #endif
  36210. return EXPECT_RESULT();
  36211. }
  36212. /*
  36213. * Testing WOLFSSL_ERROR_MSG
  36214. */
  36215. static int test_WOLFSSL_ERROR_MSG(void)
  36216. {
  36217. int res = TEST_SKIPPED;
  36218. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  36219. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  36220. const char* msg = TEST_STRING;
  36221. WOLFSSL_ERROR_MSG(msg);
  36222. res = TEST_SUCCESS;
  36223. #endif
  36224. return res;
  36225. } /* End test_WOLFSSL_ERROR_MSG */
  36226. /*
  36227. * Testing wc_ERR_remove_state
  36228. */
  36229. static int test_wc_ERR_remove_state(void)
  36230. {
  36231. int res = TEST_SKIPPED;
  36232. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  36233. wc_ERR_remove_state();
  36234. res = TEST_SUCCESS;
  36235. #endif
  36236. return res;
  36237. } /* End test_wc_ERR_remove_state */
  36238. /*
  36239. * Testing wc_ERR_print_errors_fp
  36240. */
  36241. static int test_wc_ERR_print_errors_fp(void)
  36242. {
  36243. EXPECT_DECLS;
  36244. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  36245. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  36246. long sz;
  36247. XFILE fp = XBADFILE;
  36248. WOLFSSL_ERROR(BAD_FUNC_ARG);
  36249. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  36250. XBADFILE);
  36251. wc_ERR_print_errors_fp(fp);
  36252. #if defined(DEBUG_WOLFSSL)
  36253. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  36254. #ifdef NO_ERROR_QUEUE
  36255. ExpectIntEQ(sz = XFTELL(fp), 0);
  36256. #else
  36257. ExpectIntNE(sz = XFTELL(fp), 0);
  36258. #endif
  36259. #endif
  36260. if (fp != XBADFILE)
  36261. XFCLOSE(fp);
  36262. (void)sz;
  36263. #endif
  36264. return EXPECT_RESULT();
  36265. } /* End test_wc_ERR_print_errors_fp */
  36266. #ifdef DEBUG_WOLFSSL
  36267. static void Logging_cb(const int logLevel, const char *const logMessage)
  36268. {
  36269. (void)logLevel;
  36270. (void)logMessage;
  36271. }
  36272. #endif
  36273. /*
  36274. * Testing wolfSSL_GetLoggingCb
  36275. */
  36276. static int test_wolfSSL_GetLoggingCb(void)
  36277. {
  36278. EXPECT_DECLS;
  36279. #ifdef DEBUG_WOLFSSL
  36280. /* Testing without wolfSSL_SetLoggingCb() */
  36281. ExpectNull(wolfSSL_GetLoggingCb());
  36282. /* Testing with wolfSSL_SetLoggingCb() */
  36283. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  36284. ExpectNotNull(wolfSSL_GetLoggingCb());
  36285. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  36286. #endif
  36287. ExpectNull(wolfSSL_GetLoggingCb());
  36288. return EXPECT_RESULT();
  36289. } /* End test_wolfSSL_GetLoggingCb */
  36290. #endif /* !NO_BIO */
  36291. static int test_wolfSSL_MD4(void)
  36292. {
  36293. EXPECT_DECLS;
  36294. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36295. MD4_CTX md4;
  36296. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36297. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36298. "789012345678901234567890";
  36299. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36300. "\xcc\x05\x36";
  36301. int msgSz = (int)XSTRLEN(msg);
  36302. XMEMSET(out, 0, sizeof(out));
  36303. MD4_Init(&md4);
  36304. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36305. MD4_Final(out, &md4);
  36306. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36307. #endif
  36308. return EXPECT_RESULT();
  36309. }
  36310. static int test_wolfSSL_MD5(void)
  36311. {
  36312. EXPECT_DECLS;
  36313. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36314. byte input1[] = "";
  36315. byte input2[] = "message digest";
  36316. byte hash[WC_MD5_DIGEST_SIZE];
  36317. unsigned char output1[] =
  36318. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  36319. unsigned char output2[] =
  36320. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  36321. WOLFSSL_MD5_CTX md5;
  36322. XMEMSET(&md5, 0, sizeof(md5));
  36323. /* Test cases for illegal parameters */
  36324. ExpectIntEQ(MD5_Init(NULL), 0);
  36325. ExpectIntEQ(MD5_Init(&md5), 1);
  36326. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  36327. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  36328. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  36329. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  36330. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  36331. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  36332. /* Init MD5 CTX */
  36333. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36334. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  36335. 1);
  36336. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36337. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36338. /* Init MD5 CTX */
  36339. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36340. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  36341. (int)XSTRLEN((const char*)input2)), 1);
  36342. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36343. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36344. #if !defined(NO_OLD_NAMES) && \
  36345. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  36346. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  36347. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  36348. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  36349. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  36350. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  36351. &hash);
  36352. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36353. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  36354. &hash);
  36355. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36356. {
  36357. byte data[] = "Data to be hashed.";
  36358. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  36359. ExpectNotNull(MD5(data, sizeof(data), NULL));
  36360. ExpectNotNull(MD5(data, sizeof(data), hash));
  36361. ExpectNotNull(MD5(NULL, 0, hash));
  36362. ExpectNull(MD5(NULL, sizeof(data), hash));
  36363. }
  36364. #endif
  36365. #endif
  36366. return EXPECT_RESULT();
  36367. }
  36368. static int test_wolfSSL_MD5_Transform(void)
  36369. {
  36370. EXPECT_DECLS;
  36371. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36372. byte input1[] = "";
  36373. byte input2[] = "abc";
  36374. byte local[WC_MD5_BLOCK_SIZE];
  36375. word32 sLen = 0;
  36376. #ifdef BIG_ENDIAN_ORDER
  36377. unsigned char output1[] =
  36378. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  36379. unsigned char output2[] =
  36380. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  36381. #else
  36382. unsigned char output1[] =
  36383. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  36384. unsigned char output2[] =
  36385. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  36386. #endif
  36387. union {
  36388. wc_Md5 native;
  36389. MD5_CTX compat;
  36390. } md5;
  36391. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  36392. XMEMSET(&local, 0, sizeof(local));
  36393. /* sanity check */
  36394. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  36395. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  36396. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  36397. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  36398. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36399. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  36400. /* Init MD5 CTX */
  36401. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  36402. /* Do Transform*/
  36403. sLen = (word32)XSTRLEN((char*)input1);
  36404. XMEMCPY(local, input1, sLen);
  36405. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36406. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  36407. /* Init MD5 CTX */
  36408. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  36409. sLen = (word32)XSTRLEN((char*)input2);
  36410. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  36411. XMEMCPY(local, input2, sLen);
  36412. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36413. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  36414. #endif
  36415. return EXPECT_RESULT();
  36416. }
  36417. static int test_wolfSSL_SHA(void)
  36418. {
  36419. EXPECT_DECLS;
  36420. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  36421. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  36422. (!defined(HAVE_FIPS) || \
  36423. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36424. {
  36425. const unsigned char in[] = "abc";
  36426. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  36427. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  36428. unsigned char out[WC_SHA_DIGEST_SIZE];
  36429. unsigned char* p = NULL;
  36430. WOLFSSL_SHA_CTX sha;
  36431. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36432. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  36433. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36434. /* SHA interface test */
  36435. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36436. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  36437. ExpectNotNull(SHA(in, 0, out));
  36438. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  36439. ExpectNotNull(SHA(NULL, 0, out));
  36440. ExpectNotNull(SHA(NULL, 0, NULL));
  36441. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  36442. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36443. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  36444. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  36445. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  36446. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  36447. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  36448. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36449. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  36450. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  36451. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  36452. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36453. }
  36454. #endif
  36455. #if !defined(NO_SHA256)
  36456. {
  36457. const unsigned char in[] = "abc";
  36458. unsigned char expected[] =
  36459. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  36460. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  36461. "\x15\xAD";
  36462. unsigned char out[WC_SHA256_DIGEST_SIZE];
  36463. unsigned char* p = NULL;
  36464. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  36465. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36466. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  36467. #else
  36468. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  36469. #endif
  36470. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  36471. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36472. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  36473. #else
  36474. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  36475. #endif
  36476. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  36477. }
  36478. #endif
  36479. #if defined(WOLFSSL_SHA384)
  36480. {
  36481. const unsigned char in[] = "abc";
  36482. unsigned char expected[] =
  36483. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  36484. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  36485. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  36486. "\xc8\x25\xa7";
  36487. unsigned char out[WC_SHA384_DIGEST_SIZE];
  36488. unsigned char* p = NULL;
  36489. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  36490. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36491. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  36492. #else
  36493. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  36494. #endif
  36495. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  36496. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36497. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  36498. #else
  36499. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  36500. #endif
  36501. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  36502. }
  36503. #endif
  36504. #if defined(WOLFSSL_SHA512)
  36505. {
  36506. const unsigned char in[] = "abc";
  36507. unsigned char expected[] =
  36508. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  36509. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  36510. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  36511. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  36512. "\xa5\x4c\xa4\x9f";
  36513. unsigned char out[WC_SHA512_DIGEST_SIZE];
  36514. unsigned char* p = NULL;
  36515. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  36516. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36517. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  36518. #else
  36519. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  36520. #endif
  36521. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  36522. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36523. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  36524. #else
  36525. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  36526. #endif
  36527. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  36528. }
  36529. #endif
  36530. #endif
  36531. return EXPECT_RESULT();
  36532. }
  36533. static int test_wolfSSL_SHA_Transform(void)
  36534. {
  36535. EXPECT_DECLS;
  36536. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  36537. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36538. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  36539. byte input1[] = "";
  36540. byte input2[] = "abc";
  36541. byte local[WC_SHA_BLOCK_SIZE];
  36542. word32 sLen = 0;
  36543. #ifdef BIG_ENDIAN_ORDER
  36544. unsigned char output1[] =
  36545. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  36546. "\x09\xf7\x3a\x93";
  36547. unsigned char output2[] =
  36548. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  36549. "\x7c\x6e\x08\xb8";
  36550. #else
  36551. unsigned char output1[] =
  36552. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  36553. "\x93\x3a\xf7\x09";
  36554. unsigned char output2[] =
  36555. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  36556. "\xb8\x08\x6e\x7c";
  36557. #endif
  36558. union {
  36559. wc_Sha native;
  36560. SHA_CTX compat;
  36561. } sha;
  36562. union {
  36563. wc_Sha native;
  36564. SHA_CTX compat;
  36565. } sha1;
  36566. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  36567. XMEMSET(&local, 0, sizeof(local));
  36568. /* sanity check */
  36569. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  36570. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  36571. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  36572. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  36573. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  36574. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  36575. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  36576. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36577. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  36578. /* Init SHA CTX */
  36579. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  36580. /* Do Transform*/
  36581. sLen = (word32)XSTRLEN((char*)input1);
  36582. XMEMCPY(local, input1, sLen);
  36583. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36584. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  36585. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36586. /* Init SHA CTX */
  36587. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  36588. sLen = (word32)XSTRLEN((char*)input2);
  36589. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36590. XMEMCPY(local, input2, sLen);
  36591. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36592. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  36593. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36594. /* SHA1 */
  36595. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36596. /* Init SHA CTX */
  36597. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  36598. /* Do Transform*/
  36599. sLen = (word32)XSTRLEN((char*)input1);
  36600. XMEMCPY(local, input1, sLen);
  36601. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36602. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  36603. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  36604. /* Init SHA CTX */
  36605. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  36606. sLen = (word32)XSTRLEN((char*)input2);
  36607. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36608. XMEMCPY(local, input2, sLen);
  36609. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36610. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  36611. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  36612. #endif
  36613. #endif
  36614. return EXPECT_RESULT();
  36615. }
  36616. static int test_wolfSSL_SHA224(void)
  36617. {
  36618. EXPECT_DECLS;
  36619. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  36620. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36621. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36622. unsigned char input[] =
  36623. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36624. unsigned char output[] =
  36625. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  36626. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  36627. size_t inLen;
  36628. byte hash[WC_SHA224_DIGEST_SIZE];
  36629. unsigned char* p;
  36630. inLen = XSTRLEN((char*)input);
  36631. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  36632. ExpectNull(SHA224(NULL, inLen, hash));
  36633. ExpectNotNull(SHA224(input, 0, hash));
  36634. ExpectNotNull(SHA224(input, inLen, NULL));
  36635. ExpectNotNull(SHA224(NULL, 0, hash));
  36636. ExpectNotNull(SHA224(NULL, 0, NULL));
  36637. ExpectNotNull(SHA224(input, inLen, hash));
  36638. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  36639. ExpectNotNull(p = SHA224(input, inLen, NULL));
  36640. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  36641. #endif
  36642. return EXPECT_RESULT();
  36643. }
  36644. static int test_wolfSSL_SHA256(void)
  36645. {
  36646. EXPECT_DECLS;
  36647. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  36648. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  36649. unsigned char input[] =
  36650. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36651. unsigned char output[] =
  36652. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  36653. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  36654. "\x06\xC1";
  36655. size_t inLen;
  36656. byte hash[WC_SHA256_DIGEST_SIZE];
  36657. inLen = XSTRLEN((char*)input);
  36658. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  36659. ExpectNotNull(SHA256(input, inLen, hash));
  36660. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  36661. #endif
  36662. return EXPECT_RESULT();
  36663. }
  36664. static int test_wolfSSL_SHA256_Transform(void)
  36665. {
  36666. EXPECT_DECLS;
  36667. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  36668. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36669. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36670. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  36671. !defined(WOLFSSL_KCAPI_HASH)
  36672. byte input1[] = "";
  36673. byte input2[] = "abc";
  36674. byte local[WC_SHA256_BLOCK_SIZE];
  36675. word32 sLen = 0;
  36676. #ifdef BIG_ENDIAN_ORDER
  36677. unsigned char output1[] =
  36678. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  36679. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  36680. unsigned char output2[] =
  36681. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  36682. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  36683. #else
  36684. unsigned char output1[] =
  36685. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  36686. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  36687. unsigned char output2[] =
  36688. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  36689. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  36690. #endif
  36691. union {
  36692. wc_Sha256 native;
  36693. SHA256_CTX compat;
  36694. } sha256;
  36695. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  36696. XMEMSET(&local, 0, sizeof(local));
  36697. /* sanity check */
  36698. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  36699. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  36700. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  36701. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  36702. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36703. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  36704. /* Init SHA256 CTX */
  36705. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  36706. /* Do Transform*/
  36707. sLen = (word32)XSTRLEN((char*)input1);
  36708. XMEMCPY(local, input1, sLen);
  36709. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36710. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  36711. 0);
  36712. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36713. /* Init SHA256 CTX */
  36714. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  36715. sLen = (word32)XSTRLEN((char*)input2);
  36716. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  36717. XMEMCPY(local, input2, sLen);
  36718. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36719. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  36720. 0);
  36721. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36722. #endif
  36723. #endif
  36724. return EXPECT_RESULT();
  36725. }
  36726. static int test_wolfSSL_SHA512_Transform(void)
  36727. {
  36728. EXPECT_DECLS;
  36729. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  36730. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36731. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36732. !defined(WOLFSSL_KCAPI_HASH)
  36733. byte input1[] = "";
  36734. byte input2[] = "abc";
  36735. byte local[WC_SHA512_BLOCK_SIZE];
  36736. word32 sLen = 0;
  36737. #ifdef BIG_ENDIAN_ORDER
  36738. unsigned char output1[] =
  36739. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  36740. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  36741. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  36742. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  36743. unsigned char output2[] =
  36744. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  36745. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  36746. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  36747. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  36748. #else
  36749. unsigned char output1[] =
  36750. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  36751. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  36752. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  36753. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  36754. unsigned char output2[] =
  36755. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  36756. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  36757. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  36758. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  36759. #endif
  36760. union {
  36761. wc_Sha512 native;
  36762. SHA512_CTX compat;
  36763. } sha512;
  36764. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36765. XMEMSET(&local, 0, sizeof(local));
  36766. /* sanity check */
  36767. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  36768. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  36769. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  36770. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  36771. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36772. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36773. /* Init SHA512 CTX */
  36774. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  36775. /* Do Transform*/
  36776. sLen = (word32)XSTRLEN((char*)input1);
  36777. XMEMCPY(local, input1, sLen);
  36778. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36779. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36780. WC_SHA512_DIGEST_SIZE), 0);
  36781. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36782. /* Init SHA512 CTX */
  36783. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  36784. sLen = (word32)XSTRLEN((char*)input2);
  36785. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36786. XMEMCPY(local, input2, sLen);
  36787. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36788. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36789. WC_SHA512_DIGEST_SIZE), 0);
  36790. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36791. (void)input1;
  36792. #endif
  36793. #endif
  36794. return EXPECT_RESULT();
  36795. }
  36796. static int test_wolfSSL_SHA512_224_Transform(void)
  36797. {
  36798. EXPECT_DECLS;
  36799. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  36800. !defined(WOLFSSL_NOSHA512_224)
  36801. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36802. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36803. !defined(WOLFSSL_KCAPI_HASH)
  36804. byte input1[] = "";
  36805. byte input2[] = "abc";
  36806. byte local[WC_SHA512_BLOCK_SIZE];
  36807. word32 sLen = 0;
  36808. unsigned char output1[] =
  36809. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  36810. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  36811. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  36812. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  36813. unsigned char output2[] =
  36814. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  36815. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  36816. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  36817. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  36818. union {
  36819. wc_Sha512 native;
  36820. SHA512_CTX compat;
  36821. } sha512;
  36822. #ifdef BIG_ENDIAN_ORDER
  36823. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  36824. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  36825. #endif
  36826. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36827. XMEMSET(&local, 0, sizeof(local));
  36828. /* sanity check */
  36829. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  36830. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  36831. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  36832. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  36833. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  36834. BAD_FUNC_ARG);
  36835. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36836. /* Init SHA512 CTX */
  36837. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  36838. /* Do Transform*/
  36839. sLen = (word32)XSTRLEN((char*)input1);
  36840. XMEMCPY(local, input1, sLen);
  36841. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  36842. 1);
  36843. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36844. WC_SHA512_DIGEST_SIZE), 0);
  36845. /* frees resources */
  36846. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  36847. /* Init SHA512 CTX */
  36848. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  36849. sLen = (word32)XSTRLEN((char*)input2);
  36850. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36851. XMEMCPY(local, input2, sLen);
  36852. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  36853. 1);
  36854. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36855. WC_SHA512_DIGEST_SIZE), 0);
  36856. /* frees resources */
  36857. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  36858. #endif
  36859. #endif
  36860. return EXPECT_RESULT();
  36861. }
  36862. static int test_wolfSSL_SHA512_256_Transform(void)
  36863. {
  36864. EXPECT_DECLS;
  36865. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  36866. !defined(WOLFSSL_NOSHA512_256)
  36867. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36868. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36869. !defined(WOLFSSL_KCAPI_HASH)
  36870. byte input1[] = "";
  36871. byte input2[] = "abc";
  36872. byte local[WC_SHA512_BLOCK_SIZE];
  36873. word32 sLen = 0;
  36874. unsigned char output1[] =
  36875. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  36876. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  36877. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  36878. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  36879. unsigned char output2[] =
  36880. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  36881. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  36882. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  36883. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  36884. union {
  36885. wc_Sha512 native;
  36886. SHA512_CTX compat;
  36887. } sha512;
  36888. #ifdef BIG_ENDIAN_ORDER
  36889. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  36890. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  36891. #endif
  36892. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36893. XMEMSET(&local, 0, sizeof(local));
  36894. /* sanity check */
  36895. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  36896. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  36897. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  36898. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  36899. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  36900. BAD_FUNC_ARG);
  36901. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36902. /* Init SHA512 CTX */
  36903. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  36904. /* Do Transform*/
  36905. sLen = (word32)XSTRLEN((char*)input1);
  36906. XMEMCPY(local, input1, sLen);
  36907. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  36908. 1);
  36909. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36910. WC_SHA512_DIGEST_SIZE), 0);
  36911. /* frees resources */
  36912. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  36913. /* Init SHA512 CTX */
  36914. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  36915. sLen = (word32)XSTRLEN((char*)input2);
  36916. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36917. XMEMCPY(local, input2, sLen);
  36918. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  36919. 1);
  36920. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36921. WC_SHA512_DIGEST_SIZE), 0);
  36922. /* frees resources */
  36923. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  36924. #endif
  36925. #endif
  36926. return EXPECT_RESULT();
  36927. }
  36928. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36929. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36930. * buffer of 64 bytes.
  36931. *
  36932. * returns the size of the digest buffer on success and a negative value on
  36933. * failure.
  36934. */
  36935. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  36936. int* sz)
  36937. {
  36938. EXPECT_DECLS;
  36939. HMAC_CTX ctx1;
  36940. HMAC_CTX ctx2;
  36941. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36942. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36943. unsigned char long_key[] =
  36944. "0123456789012345678901234567890123456789"
  36945. "0123456789012345678901234567890123456789"
  36946. "0123456789012345678901234567890123456789"
  36947. "0123456789012345678901234567890123456789";
  36948. unsigned char msg[] = "message to hash";
  36949. unsigned int digestSz = 64;
  36950. int keySz = sizeof(key);
  36951. int long_keySz = sizeof(long_key);
  36952. int msgSz = sizeof(msg);
  36953. unsigned char digest2[64];
  36954. unsigned int digestSz2 = 64;
  36955. HMAC_CTX_init(&ctx1);
  36956. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36957. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36958. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36959. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36960. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36961. HMAC_CTX_cleanup(&ctx1);
  36962. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36963. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  36964. HMAC_CTX_cleanup(&ctx2);
  36965. ExpectIntEQ(digestSz, digestSz2);
  36966. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36967. /* test HMAC_Init with NULL key */
  36968. /* init after copy */
  36969. HMAC_CTX_init(&ctx1);
  36970. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36971. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36972. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36973. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36974. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36975. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36976. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36977. HMAC_CTX_cleanup(&ctx1);
  36978. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36979. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36980. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36981. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36982. HMAC_CTX_cleanup(&ctx2);
  36983. ExpectIntEQ(digestSz, digestSz2);
  36984. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36985. /* long key */
  36986. HMAC_CTX_init(&ctx1);
  36987. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  36988. SSL_SUCCESS);
  36989. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36990. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36991. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36992. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36993. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36994. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36995. HMAC_CTX_cleanup(&ctx1);
  36996. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36997. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36998. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36999. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37000. HMAC_CTX_cleanup(&ctx2);
  37001. ExpectIntEQ(digestSz, digestSz2);
  37002. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37003. /* init before copy */
  37004. HMAC_CTX_init(&ctx1);
  37005. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37006. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37007. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37008. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37009. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37010. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37011. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37012. HMAC_CTX_cleanup(&ctx1);
  37013. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37014. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37015. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37016. HMAC_CTX_cleanup(&ctx2);
  37017. ExpectIntEQ(digestSz, digestSz2);
  37018. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37019. *sz = digestSz;
  37020. return EXPECT_RESULT();
  37021. }
  37022. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  37023. static int test_wolfSSL_HMAC_CTX(void)
  37024. {
  37025. EXPECT_DECLS;
  37026. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  37027. unsigned char digest[64];
  37028. int digestSz;
  37029. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  37030. WOLFSSL_HMAC_CTX ctx1;
  37031. WOLFSSL_HMAC_CTX ctx2;
  37032. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  37033. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  37034. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  37035. wolfSSL_HMAC_CTX_free(NULL);
  37036. wolfSSL_HMAC_CTX_free(hmac_ctx);
  37037. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  37038. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  37039. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  37040. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  37041. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  37042. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  37043. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  37044. ((! defined(HAVE_FIPS_VERSION)) || \
  37045. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  37046. /* Copy object that hasn't had a digest set - MD5. */
  37047. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  37048. #else
  37049. /* Copy object that hasn't had a digest set. */
  37050. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  37051. #endif
  37052. HMAC_CTX_cleanup(NULL);
  37053. HMAC_CTX_cleanup(&ctx2);
  37054. ExpectNull(HMAC_CTX_get_md(NULL));
  37055. #ifndef NO_SHA
  37056. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  37057. TEST_SUCCESS);
  37058. ExpectIntEQ(digestSz, 20);
  37059. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  37060. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  37061. #endif /* !NO_SHA */
  37062. #ifdef WOLFSSL_SHA224
  37063. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  37064. TEST_SUCCESS);
  37065. ExpectIntEQ(digestSz, 28);
  37066. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  37067. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  37068. "\x02\x0E", digest, digestSz), 0);
  37069. #endif /* WOLFSSL_SHA224 */
  37070. #ifndef NO_SHA256
  37071. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  37072. TEST_SUCCESS);
  37073. ExpectIntEQ(digestSz, 32);
  37074. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  37075. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  37076. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  37077. #endif /* !NO_SHA256 */
  37078. #ifdef WOLFSSL_SHA384
  37079. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  37080. TEST_SUCCESS);
  37081. ExpectIntEQ(digestSz, 48);
  37082. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  37083. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  37084. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  37085. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  37086. digest, digestSz), 0);
  37087. #endif /* WOLFSSL_SHA384 */
  37088. #ifdef WOLFSSL_SHA512
  37089. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  37090. TEST_SUCCESS);
  37091. ExpectIntEQ(digestSz, 64);
  37092. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  37093. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  37094. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  37095. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  37096. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  37097. digest, digestSz), 0);
  37098. #endif /* WOLFSSL_SHA512 */
  37099. #ifdef WOLFSSL_SHA3
  37100. #ifndef WOLFSSL_NOSHA3_224
  37101. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  37102. TEST_SUCCESS);
  37103. ExpectIntEQ(digestSz, 28);
  37104. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  37105. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  37106. "\x29\x0f", digest, digestSz), 0);
  37107. #endif
  37108. #ifndef WOLFSSL_NOSHA3_256
  37109. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  37110. TEST_SUCCESS);
  37111. ExpectIntEQ(digestSz, 32);
  37112. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  37113. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  37114. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  37115. #endif
  37116. #ifndef WOLFSSL_NOSHA3_384
  37117. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  37118. TEST_SUCCESS);
  37119. ExpectIntEQ(digestSz, 48);
  37120. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  37121. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  37122. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  37123. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  37124. digest, digestSz), 0);
  37125. #endif
  37126. #ifndef WOLFSSL_NOSHA3_512
  37127. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  37128. TEST_SUCCESS);
  37129. ExpectIntEQ(digestSz, 64);
  37130. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  37131. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  37132. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  37133. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  37134. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  37135. digest, digestSz), 0);
  37136. #endif
  37137. #endif
  37138. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  37139. HAVE_FIPS_VERSION <= 2)
  37140. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  37141. TEST_SUCCESS);
  37142. ExpectIntEQ(digestSz, 16);
  37143. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  37144. "\xE4\x98\xDD", digest, digestSz), 0);
  37145. #endif /* !NO_MD5 */
  37146. #endif
  37147. return EXPECT_RESULT();
  37148. }
  37149. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37150. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37151. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37152. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  37153. {
  37154. EXPECT_DECLS;
  37155. static const unsigned char key[] = "simple test key";
  37156. HMAC_CTX* hmac = NULL;
  37157. ENGINE* e = NULL;
  37158. unsigned char hash[WC_MAX_DIGEST_SIZE];
  37159. unsigned int len;
  37160. ExpectNotNull(hmac = HMAC_CTX_new());
  37161. HMAC_CTX_init(hmac);
  37162. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  37163. ((! defined(HAVE_FIPS_VERSION)) || \
  37164. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  37165. /* Get size on object that hasn't had a digest set - MD5. */
  37166. ExpectIntEQ(HMAC_size(hmac), 16);
  37167. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  37168. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  37169. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  37170. #else
  37171. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  37172. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  37173. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  37174. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  37175. #endif
  37176. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  37177. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  37178. /* re-using test key as data to hash */
  37179. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  37180. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  37181. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  37182. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  37183. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  37184. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  37185. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  37186. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  37187. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  37188. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  37189. ExpectIntEQ(len, md_len);
  37190. ExpectIntEQ(HMAC_size(NULL), 0);
  37191. ExpectIntEQ(HMAC_size(hmac), md_len);
  37192. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  37193. HMAC_cleanup(NULL);
  37194. HMAC_cleanup(hmac);
  37195. HMAC_CTX_free(hmac);
  37196. len = 0;
  37197. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  37198. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  37199. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  37200. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  37201. ExpectIntEQ(len, md_len);
  37202. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  37203. /* With data. */
  37204. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  37205. &len));
  37206. /* With NULL data. */
  37207. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  37208. &len));
  37209. /* With zero length data. */
  37210. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  37211. return EXPECT_RESULT();
  37212. }
  37213. #endif
  37214. static int test_wolfSSL_HMAC(void)
  37215. {
  37216. EXPECT_DECLS;
  37217. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37218. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37219. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37220. #ifndef NO_SHA256
  37221. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  37222. TEST_SUCCESS);
  37223. #endif
  37224. #ifdef WOLFSSL_SHA224
  37225. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  37226. TEST_SUCCESS);
  37227. #endif
  37228. #ifdef WOLFSSL_SHA384
  37229. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  37230. TEST_SUCCESS);
  37231. #endif
  37232. #ifdef WOLFSSL_SHA512
  37233. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  37234. TEST_SUCCESS);
  37235. #endif
  37236. #ifdef WOLFSSL_SHA3
  37237. #ifndef WOLFSSL_NOSHA3_224
  37238. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  37239. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  37240. #endif
  37241. #ifndef WOLFSSL_NOSHA3_256
  37242. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  37243. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  37244. #endif
  37245. #ifndef WOLFSSL_NOSHA3_384
  37246. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  37247. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  37248. #endif
  37249. #ifndef WOLFSSL_NOSHA3_512
  37250. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  37251. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  37252. #endif
  37253. #endif
  37254. #ifndef NO_SHA
  37255. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  37256. TEST_SUCCESS);
  37257. #endif
  37258. #endif
  37259. return EXPECT_RESULT();
  37260. }
  37261. static int test_wolfSSL_CMAC(void)
  37262. {
  37263. EXPECT_DECLS;
  37264. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  37265. defined(WOLFSSL_AES_DIRECT)
  37266. int i;
  37267. byte key[AES_256_KEY_SIZE];
  37268. CMAC_CTX* cmacCtx = NULL;
  37269. byte out[AES_BLOCK_SIZE];
  37270. size_t outLen = AES_BLOCK_SIZE;
  37271. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  37272. key[i] = i;
  37273. }
  37274. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37275. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  37276. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37277. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37278. NULL), 1);
  37279. /* re-using test key as data to hash */
  37280. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37281. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  37282. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  37283. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  37284. /* No Update works. */
  37285. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37286. NULL), 1);
  37287. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37288. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37289. NULL), 1);
  37290. /* Test parameters with CMAC_Update. */
  37291. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  37292. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  37293. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  37294. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  37295. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  37296. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  37297. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  37298. /* Test parameters with CMAC_Final. */
  37299. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  37300. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  37301. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  37302. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  37303. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  37304. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  37305. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37306. CMAC_CTX_free(cmacCtx);
  37307. /* Test parameters with CMAC Init. */
  37308. cmacCtx = NULL;
  37309. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37310. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37311. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  37312. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37313. NULL), 0);
  37314. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37315. NULL), 0);
  37316. /* give a key too small for the cipher, verify we get failure */
  37317. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  37318. NULL), 0);
  37319. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  37320. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  37321. /* Only AES-CBC supported. */
  37322. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  37323. NULL), 0);
  37324. #endif
  37325. CMAC_CTX_free(cmacCtx);
  37326. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  37327. cmacCtx = NULL;
  37328. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37329. /* No Init. */
  37330. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  37331. CMAC_CTX_free(cmacCtx);
  37332. /* Test AES-256-CBC */
  37333. cmacCtx = NULL;
  37334. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37335. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  37336. NULL), 1);
  37337. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37338. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37339. CMAC_CTX_free(cmacCtx);
  37340. /* Test AES-192-CBC */
  37341. cmacCtx = NULL;
  37342. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37343. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37344. NULL), 1);
  37345. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37346. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37347. CMAC_CTX_free(cmacCtx);
  37348. cmacCtx = NULL;
  37349. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37350. CMAC_CTX_free(cmacCtx);
  37351. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  37352. return EXPECT_RESULT();
  37353. }
  37354. static int test_wolfSSL_DES(void)
  37355. {
  37356. EXPECT_DECLS;
  37357. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37358. const_DES_cblock myDes;
  37359. DES_cblock iv;
  37360. DES_key_schedule key;
  37361. word32 i = 0;
  37362. DES_LONG dl = 0;
  37363. unsigned char msg[] = "hello wolfssl";
  37364. unsigned char weakKey[][8] = {
  37365. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  37366. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  37367. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  37368. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  37369. };
  37370. unsigned char semiWeakKey[][8] = {
  37371. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  37372. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  37373. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  37374. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  37375. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  37376. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  37377. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  37378. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  37379. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  37380. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  37381. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  37382. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  37383. };
  37384. DES_check_key(1);
  37385. DES_set_key(&myDes, &key);
  37386. /* check, check of odd parity */
  37387. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  37388. myDes[0] = 6; /* set even parity */
  37389. XMEMSET(key, 5, sizeof(DES_key_schedule));
  37390. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  37391. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  37392. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  37393. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  37394. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  37395. /* set odd parity for success case */
  37396. DES_set_odd_parity(&myDes);
  37397. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  37398. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  37399. myDes[3]);
  37400. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  37401. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37402. ExpectIntEQ(key[i], myDes[i]);
  37403. }
  37404. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  37405. /* check weak key */
  37406. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  37407. XMEMSET(key, 5, sizeof(DES_key_schedule));
  37408. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  37409. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  37410. DES_set_key_unchecked(NULL, NULL);
  37411. DES_set_key_unchecked(&myDes, NULL);
  37412. DES_set_key_unchecked(NULL, &key);
  37413. /* compare arrays, should be the same */
  37414. /* now do unchecked copy of a weak key over */
  37415. DES_set_key_unchecked(&myDes, &key);
  37416. /* compare arrays, should be the same */
  37417. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37418. ExpectIntEQ(key[i], myDes[i]);
  37419. }
  37420. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  37421. myDes[7] = 2;
  37422. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  37423. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  37424. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  37425. /* Test all weak keys. */
  37426. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  37427. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  37428. }
  37429. /* Test all semi-weak keys. */
  37430. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  37431. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  37432. }
  37433. /* check DES_key_sched API */
  37434. XMEMSET(key, 1, sizeof(DES_key_schedule));
  37435. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  37436. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  37437. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  37438. /* compare arrays, should be the same */
  37439. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37440. ExpectIntEQ(key[i], myDes[i]);
  37441. }
  37442. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  37443. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  37444. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  37445. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  37446. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  37447. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  37448. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  37449. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  37450. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  37451. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  37452. * DES_cbc_encrypt on the input */
  37453. XMEMSET(iv, 0, sizeof(DES_cblock));
  37454. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  37455. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  37456. ExpectIntEQ(dl, 480052723);
  37457. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  37458. return EXPECT_RESULT();
  37459. }
  37460. static int test_wolfSSL_DES_ncbc(void)
  37461. {
  37462. EXPECT_DECLS;
  37463. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37464. const_DES_cblock myDes;
  37465. DES_cblock iv = {1};
  37466. DES_key_schedule key = {0};
  37467. unsigned char msg[] = "hello wolfssl";
  37468. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  37469. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  37470. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  37471. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  37472. /* partial block test */
  37473. DES_set_key(&key, &myDes);
  37474. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  37475. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  37476. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37477. DES_set_key(&key, &myDes);
  37478. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37479. *((byte*)&iv) = 1;
  37480. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  37481. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  37482. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37483. /* full block test */
  37484. DES_set_key(&key, &myDes);
  37485. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  37486. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37487. *((byte*)&iv) = 1;
  37488. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  37489. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  37490. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37491. DES_set_key(&key, &myDes);
  37492. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37493. *((byte*)&iv) = 1;
  37494. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  37495. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  37496. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37497. #endif
  37498. return EXPECT_RESULT();
  37499. }
  37500. static int test_wolfSSL_DES_ecb_encrypt(void)
  37501. {
  37502. EXPECT_DECLS;
  37503. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  37504. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  37505. WOLFSSL_DES_key_schedule key;
  37506. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  37507. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  37508. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  37509. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  37510. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  37511. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  37512. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  37513. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  37514. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  37515. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  37516. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  37517. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  37518. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  37519. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  37520. /* Encrypt messages */
  37521. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  37522. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  37523. {
  37524. /* Decrypt messages */
  37525. int ret1 = 0;
  37526. int ret2 = 0;
  37527. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  37528. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  37529. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  37530. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  37531. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  37532. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  37533. }
  37534. #endif
  37535. return EXPECT_RESULT();
  37536. }
  37537. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  37538. {
  37539. EXPECT_DECLS;
  37540. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37541. unsigned char input1[8], input2[8];
  37542. unsigned char output1[8], output2[8];
  37543. unsigned char back1[8], back2[8];
  37544. WOLFSSL_DES_cblock iv1, iv2;
  37545. WOLFSSL_DES_key_schedule key1, key2, key3;
  37546. int i;
  37547. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  37548. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  37549. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  37550. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  37551. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  37552. XMEMSET(output1, 0, sizeof(output1));
  37553. XMEMSET(output2, 0, sizeof(output2));
  37554. XMEMSET(back1, 0, sizeof(back1));
  37555. XMEMSET(back2, 0, sizeof(back2));
  37556. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37557. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37558. /* Encrypt messages */
  37559. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  37560. DES_ENCRYPT);
  37561. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  37562. DES_ENCRYPT);
  37563. {
  37564. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37565. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37566. /* Decrypt messages */
  37567. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  37568. &iv1, DES_DECRYPT);
  37569. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  37570. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  37571. &iv2, DES_DECRYPT);
  37572. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  37573. }
  37574. for (i = 0; i < 8; i++) {
  37575. XMEMSET(output1, 0, sizeof(output1));
  37576. XMEMSET(output2, 0, sizeof(output2));
  37577. XMEMSET(back1, 0, sizeof(back1));
  37578. XMEMSET(back2, 0, sizeof(back2));
  37579. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37580. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37581. /* Encrypt partial messages */
  37582. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  37583. &iv1, DES_ENCRYPT);
  37584. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  37585. &iv2, DES_ENCRYPT);
  37586. {
  37587. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37588. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37589. /* Decrypt messages */
  37590. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  37591. &key3, &iv1, DES_DECRYPT);
  37592. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  37593. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  37594. &key3, &iv2, DES_DECRYPT);
  37595. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  37596. }
  37597. }
  37598. #endif
  37599. return EXPECT_RESULT();
  37600. }
  37601. static int test_wolfSSL_AES_encrypt(void)
  37602. {
  37603. EXPECT_DECLS;
  37604. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  37605. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37606. AES_KEY enc;
  37607. AES_KEY dec;
  37608. const byte msg[] = {
  37609. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  37610. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  37611. };
  37612. const byte exp[] = {
  37613. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  37614. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  37615. };
  37616. const byte key[] = {
  37617. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  37618. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37619. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37620. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  37621. };
  37622. byte eout[sizeof(msg)];
  37623. byte dout[sizeof(msg)];
  37624. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  37625. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  37626. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  37627. wolfSSL_AES_encrypt(msg, NULL, NULL);
  37628. wolfSSL_AES_encrypt(NULL, eout, NULL);
  37629. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  37630. wolfSSL_AES_encrypt(msg, eout, NULL);
  37631. wolfSSL_AES_encrypt(msg, NULL, &enc);
  37632. wolfSSL_AES_encrypt(NULL, eout, &enc);
  37633. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  37634. wolfSSL_AES_decrypt(eout, NULL, NULL);
  37635. wolfSSL_AES_decrypt(NULL, dout, NULL);
  37636. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  37637. wolfSSL_AES_decrypt(eout, dout, NULL);
  37638. wolfSSL_AES_decrypt(eout, NULL, &dec);
  37639. wolfSSL_AES_decrypt(NULL, dout, &dec);
  37640. wolfSSL_AES_encrypt(msg, eout, &enc);
  37641. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  37642. wolfSSL_AES_decrypt(eout, dout, &dec);
  37643. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  37644. #endif
  37645. return EXPECT_RESULT();
  37646. }
  37647. static int test_wolfSSL_AES_ecb_encrypt(void)
  37648. {
  37649. EXPECT_DECLS;
  37650. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  37651. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37652. AES_KEY aes;
  37653. const byte msg[] =
  37654. {
  37655. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37656. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37657. };
  37658. const byte verify[] =
  37659. {
  37660. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37661. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37662. };
  37663. const byte key[] =
  37664. {
  37665. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37666. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37667. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37668. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37669. };
  37670. byte out[AES_BLOCK_SIZE];
  37671. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37672. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37673. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37674. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37675. #ifdef HAVE_AES_DECRYPT
  37676. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37677. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37678. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37679. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37680. #endif
  37681. /* test bad arguments */
  37682. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37683. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37684. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37685. #endif
  37686. return EXPECT_RESULT();
  37687. }
  37688. static int test_wolfSSL_AES_cbc_encrypt(void)
  37689. {
  37690. EXPECT_DECLS;
  37691. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  37692. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37693. AES_KEY aes;
  37694. AES_KEY* aesN = NULL;
  37695. size_t len = 0;
  37696. size_t lenB = 0;
  37697. int keySz0 = 0;
  37698. int keySzN = -1;
  37699. byte out[AES_BLOCK_SIZE] = {0};
  37700. byte* outN = NULL;
  37701. /* Test vectors retrieved from:
  37702. * <begin URL>
  37703. * https://csrc.nist.gov/
  37704. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  37705. * documents/aes/KAT_AES.zip
  37706. * </end URL>
  37707. */
  37708. const byte* pt128N = NULL;
  37709. byte* key128N = NULL;
  37710. byte* iv128N = NULL;
  37711. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  37712. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37713. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37714. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  37715. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  37716. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37718. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  37719. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  37720. len = sizeof(pt128);
  37721. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  37722. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  37723. ExpectIntNE(XMEMCMP(b, g, h), i)
  37724. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  37725. /* Stressing wolfSSL_AES_cbc_encrypt() */
  37726. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37727. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  37728. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  37729. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37730. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  37731. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37732. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37733. /* Stressing wolfSSL_AES_set_encrypt_key */
  37734. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37735. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  37736. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  37737. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  37738. /* Stressing wolfSSL_AES_set_decrypt_key */
  37739. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37740. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  37741. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  37742. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  37743. #ifdef WOLFSSL_AES_128
  37744. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  37745. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37746. RESET_IV(iv128tmp, iv128);
  37747. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37748. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  37749. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37750. wc_AesFree((Aes*)&aes);
  37751. #ifdef HAVE_AES_DECRYPT
  37752. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  37753. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37754. RESET_IV(iv128tmp, iv128);
  37755. len = sizeof(ct128);
  37756. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37757. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  37758. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  37759. wc_AesFree((Aes*)&aes);
  37760. #endif
  37761. #endif /* WOLFSSL_AES_128 */
  37762. #ifdef WOLFSSL_AES_192
  37763. {
  37764. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  37765. * Appendix F.2.3 */
  37766. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  37767. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37768. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37769. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  37770. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  37771. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37772. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37773. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  37774. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  37775. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  37776. len = sizeof(pt192);
  37777. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  37778. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37779. RESET_IV(iv192tmp, iv192);
  37780. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37781. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  37782. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  37783. wc_AesFree((Aes*)&aes);
  37784. #ifdef HAVE_AES_DECRYPT
  37785. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  37786. len = sizeof(ct192);
  37787. RESET_IV(iv192tmp, iv192);
  37788. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37789. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37790. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  37791. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  37792. wc_AesFree((Aes*)&aes);
  37793. #endif
  37794. }
  37795. #endif /* WOLFSSL_AES_192 */
  37796. #ifdef WOLFSSL_AES_256
  37797. {
  37798. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  37799. * Appendix F.2.5 */
  37800. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  37801. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37802. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37803. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  37804. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  37805. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37806. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37807. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37808. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37809. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37810. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  37811. len = sizeof(pt256);
  37812. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  37813. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37814. RESET_IV(iv256tmp, iv256);
  37815. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37816. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  37817. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  37818. wc_AesFree((Aes*)&aes);
  37819. #ifdef HAVE_AES_DECRYPT
  37820. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  37821. len = sizeof(ct256);
  37822. RESET_IV(iv256tmp, iv256);
  37823. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37824. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37825. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  37826. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  37827. wc_AesFree((Aes*)&aes);
  37828. #endif
  37829. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  37830. !defined(HAVE_SELFTEST)
  37831. {
  37832. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  37833. byte wrapPlain[sizeof(key256)] = { 0 };
  37834. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  37835. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  37836. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37837. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37838. 15), WOLFSSL_FAILURE);
  37839. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37840. sizeof(key256)), sizeof(wrapCipher));
  37841. wc_AesFree((Aes*)&aes);
  37842. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  37843. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37844. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37845. 23), WOLFSSL_FAILURE);
  37846. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37847. sizeof(wrapCipher)), sizeof(wrapPlain));
  37848. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37849. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  37850. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  37851. wc_AesFree((Aes*)&aes);
  37852. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  37853. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37854. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  37855. sizeof(key256)), sizeof(wrapCipher));
  37856. wc_AesFree((Aes*)&aes);
  37857. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  37858. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37859. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  37860. sizeof(wrapCipher)), sizeof(wrapPlain));
  37861. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37862. wc_AesFree((Aes*)&aes);
  37863. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  37864. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  37865. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  37866. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  37867. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  37868. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  37869. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  37870. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  37871. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  37872. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  37873. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  37874. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  37875. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  37876. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  37877. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  37878. 0);
  37879. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  37880. 0);
  37881. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  37882. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  37883. }
  37884. #endif /* HAVE_AES_KEYWRAP */
  37885. }
  37886. #endif /* WOLFSSL_AES_256 */
  37887. #endif
  37888. return EXPECT_RESULT();
  37889. }
  37890. static int test_wolfSSL_AES_cfb128_encrypt(void)
  37891. {
  37892. EXPECT_DECLS;
  37893. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  37894. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37895. AES_KEY aesEnc;
  37896. AES_KEY aesDec;
  37897. const byte msg[] = {
  37898. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  37899. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  37900. };
  37901. const byte exp[] = {
  37902. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  37903. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  37904. };
  37905. const byte key[] = {
  37906. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  37907. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37908. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37909. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  37910. };
  37911. const byte ivData[] = {
  37912. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37913. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37914. };
  37915. byte out[AES_BLOCK_SIZE];
  37916. byte iv[AES_BLOCK_SIZE];
  37917. word32 i;
  37918. int num;
  37919. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  37920. XMEMCPY(iv, ivData, sizeof(iv));
  37921. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37922. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  37923. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  37924. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37925. #ifdef HAVE_AES_DECRYPT
  37926. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  37927. XMEMCPY(iv, ivData, sizeof(iv));
  37928. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37929. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  37930. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  37931. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37932. #endif
  37933. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  37934. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  37935. XMEMCPY(iv, ivData, sizeof(iv));
  37936. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37937. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  37938. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  37939. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  37940. if (i == 0) {
  37941. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37942. }
  37943. else {
  37944. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37945. }
  37946. #ifdef HAVE_AES_DECRYPT
  37947. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  37948. XMEMCPY(iv, ivData, sizeof(iv));
  37949. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37950. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  37951. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  37952. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  37953. if (i == 0) {
  37954. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37955. }
  37956. else {
  37957. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37958. }
  37959. #endif
  37960. }
  37961. if (EXPECT_SUCCESS()) {
  37962. /* test bad arguments */
  37963. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  37964. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  37965. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  37966. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  37967. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  37968. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  37969. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  37970. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  37971. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  37972. }
  37973. #endif
  37974. return EXPECT_RESULT();
  37975. }
  37976. static int test_wolfSSL_CRYPTO_cts128(void)
  37977. {
  37978. EXPECT_DECLS;
  37979. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  37980. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37981. byte tmp[64]; /* Largest vector size */
  37982. /* Test vectors taken form RFC3962 Appendix B */
  37983. const testVector vects[] = {
  37984. {
  37985. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37986. "\x20",
  37987. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  37988. "\x97",
  37989. 17, 17
  37990. },
  37991. {
  37992. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37993. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  37994. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  37995. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  37996. 31, 31
  37997. },
  37998. {
  37999. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38000. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38001. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38002. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38003. 32, 32
  38004. },
  38005. {
  38006. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38007. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38008. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38009. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38010. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38011. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38012. 47, 47
  38013. },
  38014. {
  38015. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38016. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38017. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38018. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38019. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38020. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38021. 48, 48
  38022. },
  38023. {
  38024. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38025. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38026. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38027. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38028. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38029. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38030. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38031. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38032. 64, 64
  38033. }
  38034. };
  38035. byte keyBytes[AES_128_KEY_SIZE] = {
  38036. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38037. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38038. };
  38039. size_t i;
  38040. AES_KEY encKey;
  38041. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38042. XMEMSET(tmp, 0, sizeof(tmp));
  38043. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38044. AES_KEY decKey;
  38045. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38046. &encKey), 0);
  38047. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38048. &decKey), 0);
  38049. XMEMSET(iv, 0, sizeof(iv));
  38050. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38051. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38052. vects[i].outLen);
  38053. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38054. XMEMSET(iv, 0, sizeof(iv));
  38055. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38056. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38057. vects[i].inLen);
  38058. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38059. }
  38060. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  38061. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  38062. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  38063. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  38064. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  38065. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  38066. (cbc128_f)AES_cbc_encrypt), 0);
  38067. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  38068. (cbc128_f)AES_cbc_encrypt), 0);
  38069. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  38070. (cbc128_f)AES_cbc_encrypt), 0);
  38071. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  38072. (cbc128_f)AES_cbc_encrypt), 0);
  38073. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  38074. (cbc128_f)AES_cbc_encrypt), 0);
  38075. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  38076. /* Length too small. */
  38077. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  38078. (cbc128_f)AES_cbc_encrypt), 0);
  38079. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  38080. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  38081. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  38082. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  38083. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  38084. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  38085. (cbc128_f)AES_cbc_encrypt), 0);
  38086. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  38087. (cbc128_f)AES_cbc_encrypt), 0);
  38088. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  38089. (cbc128_f)AES_cbc_encrypt), 0);
  38090. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  38091. (cbc128_f)AES_cbc_encrypt), 0);
  38092. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  38093. (cbc128_f)AES_cbc_encrypt), 0);
  38094. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  38095. /* Length too small. */
  38096. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  38097. (cbc128_f)AES_cbc_encrypt), 0);
  38098. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38099. return EXPECT_RESULT();
  38100. }
  38101. static int test_wolfSSL_RC4(void)
  38102. {
  38103. EXPECT_DECLS;
  38104. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  38105. WOLFSSL_RC4_KEY rc4Key;
  38106. unsigned char key[] = {
  38107. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38108. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38109. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38110. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38111. };
  38112. unsigned char data[] = {
  38113. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38114. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38115. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38116. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38117. };
  38118. unsigned char enc[sizeof(data)];
  38119. unsigned char dec[sizeof(data)];
  38120. word32 i;
  38121. word32 j;
  38122. wolfSSL_RC4_set_key(NULL, -1, NULL);
  38123. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  38124. wolfSSL_RC4_set_key(NULL, 0, NULL);
  38125. wolfSSL_RC4_set_key(NULL, -1, key);
  38126. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  38127. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  38128. wolfSSL_RC4_set_key(NULL, 0, key);
  38129. wolfSSL_RC4(NULL, 0, NULL, NULL);
  38130. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  38131. wolfSSL_RC4(NULL, 0, data, NULL);
  38132. wolfSSL_RC4(NULL, 0, NULL, enc);
  38133. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  38134. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  38135. wolfSSL_RC4(NULL, 0, data, enc);
  38136. ExpectIntEQ(1, 1);
  38137. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  38138. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  38139. XMEMSET(enc, 0, sizeof(enc));
  38140. XMEMSET(dec, 0, sizeof(dec));
  38141. /* Encrypt */
  38142. wolfSSL_RC4_set_key(&rc4Key, i, key);
  38143. wolfSSL_RC4(&rc4Key, j, data, enc);
  38144. /* Decrypt */
  38145. wolfSSL_RC4_set_key(&rc4Key, i, key);
  38146. wolfSSL_RC4(&rc4Key, j, enc, dec);
  38147. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  38148. }
  38149. }
  38150. #endif
  38151. return EXPECT_RESULT();
  38152. }
  38153. static int test_wolfSSL_OBJ(void)
  38154. {
  38155. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  38156. * mode
  38157. */
  38158. EXPECT_DECLS;
  38159. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  38160. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  38161. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  38162. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  38163. ASN1_OBJECT *obj = NULL;
  38164. ASN1_OBJECT *obj2 = NULL;
  38165. char buf[50];
  38166. XFILE fp = XBADFILE;
  38167. X509 *x509 = NULL;
  38168. X509_NAME *x509Name = NULL;
  38169. X509_NAME_ENTRY *x509NameEntry = NULL;
  38170. ASN1_OBJECT *asn1Name = NULL;
  38171. int numNames = 0;
  38172. BIO *bio = NULL;
  38173. int nid;
  38174. int i, j;
  38175. const char *f[] = {
  38176. #ifndef NO_RSA
  38177. "./certs/ca-cert.der",
  38178. #endif
  38179. #ifdef HAVE_ECC
  38180. "./certs/ca-ecc-cert.der",
  38181. "./certs/ca-ecc384-cert.der",
  38182. #endif
  38183. NULL};
  38184. ASN1_OBJECT *field_name_obj = NULL;
  38185. int lastpos = -1;
  38186. int tmp = -1;
  38187. ASN1_STRING *asn1 = NULL;
  38188. unsigned char *buf_dyn = NULL;
  38189. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  38190. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  38191. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  38192. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  38193. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  38194. ASN1_OBJECT_free(obj);
  38195. obj = NULL;
  38196. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  38197. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  38198. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  38199. #ifdef WOLFSSL_CERT_EXT
  38200. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  38201. #endif
  38202. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  38203. ExpectNotNull(obj2 = OBJ_dup(obj));
  38204. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  38205. ASN1_OBJECT_free(obj);
  38206. obj = NULL;
  38207. ASN1_OBJECT_free(obj2);
  38208. obj2 = NULL;
  38209. for (i = 0; f[i] != NULL; i++)
  38210. {
  38211. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  38212. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  38213. if (fp != XBADFILE) {
  38214. XFCLOSE(fp);
  38215. fp = XBADFILE;
  38216. }
  38217. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  38218. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  38219. /* Get the Common Name by using OBJ_txt2obj */
  38220. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  38221. do
  38222. {
  38223. lastpos = tmp;
  38224. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  38225. } while (tmp > -1);
  38226. ExpectIntNE(lastpos, -1);
  38227. ASN1_OBJECT_free(field_name_obj);
  38228. field_name_obj = NULL;
  38229. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  38230. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  38231. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  38232. /*
  38233. * All Common Names should be www.wolfssl.com
  38234. * This makes testing easier as we can test for the expected value.
  38235. */
  38236. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  38237. OPENSSL_free(buf_dyn);
  38238. buf_dyn = NULL;
  38239. bio = BIO_new(BIO_s_mem());
  38240. ExpectTrue(bio != NULL);
  38241. for (j = 0; j < numNames; j++)
  38242. {
  38243. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  38244. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  38245. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  38246. }
  38247. BIO_free(bio);
  38248. bio = NULL;
  38249. X509_free(x509);
  38250. x509 = NULL;
  38251. }
  38252. #ifdef HAVE_PKCS12
  38253. {
  38254. PKCS12 *p12 = NULL;
  38255. int boolRet;
  38256. EVP_PKEY *pkey = NULL;
  38257. const char *p12_f[] = {
  38258. #if !defined(NO_DES3) && !defined(NO_RSA)
  38259. "./certs/test-servercert.p12",
  38260. #endif
  38261. NULL};
  38262. for (i = 0; p12_f[i] != NULL; i++)
  38263. {
  38264. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  38265. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  38266. if (fp != XBADFILE) {
  38267. XFCLOSE(fp);
  38268. fp = XBADFILE;
  38269. }
  38270. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  38271. &pkey, &x509, NULL)) > 0);
  38272. wc_PKCS12_free(p12);
  38273. p12 = NULL;
  38274. EVP_PKEY_free(pkey);
  38275. x509Name = X509_get_issuer_name(x509);
  38276. ExpectNotNull(x509Name);
  38277. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  38278. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  38279. for (j = 0; j < numNames; j++)
  38280. {
  38281. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  38282. ExpectNotNull(asn1Name =
  38283. X509_NAME_ENTRY_get_object(x509NameEntry));
  38284. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  38285. }
  38286. BIO_free(bio);
  38287. bio = NULL;
  38288. X509_free(x509);
  38289. x509 = NULL;
  38290. }
  38291. }
  38292. #endif /* HAVE_PKCS12 */
  38293. #endif
  38294. return EXPECT_RESULT();
  38295. }
  38296. static int test_wolfSSL_OBJ_cmp(void)
  38297. {
  38298. EXPECT_DECLS;
  38299. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  38300. ASN1_OBJECT *obj = NULL;
  38301. ASN1_OBJECT *obj2 = NULL;
  38302. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  38303. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  38304. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  38305. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  38306. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  38307. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  38308. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  38309. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  38310. ASN1_OBJECT_free(obj);
  38311. ASN1_OBJECT_free(obj2);
  38312. #endif
  38313. return EXPECT_RESULT();
  38314. }
  38315. static int test_wolfSSL_OBJ_txt2nid(void)
  38316. {
  38317. EXPECT_DECLS;
  38318. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  38319. defined(WOLFSSL_APACHE_HTTPD)
  38320. int i;
  38321. static const struct {
  38322. const char* sn;
  38323. const char* ln;
  38324. const char* oid;
  38325. int nid;
  38326. } testVals[] = {
  38327. #ifdef WOLFSSL_APACHE_HTTPD
  38328. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  38329. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  38330. NID_id_on_dnsSRV },
  38331. { "msUPN", "Microsoft User Principal Name",
  38332. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  38333. #endif
  38334. { NULL, NULL, NULL, NID_undef }
  38335. };
  38336. /* Invalid cases */
  38337. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  38338. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  38339. /* Valid cases */
  38340. for (i = 0; testVals[i].sn != NULL; i++) {
  38341. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  38342. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  38343. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  38344. }
  38345. #endif
  38346. return EXPECT_RESULT();
  38347. }
  38348. static int test_wolfSSL_OBJ_txt2obj(void)
  38349. {
  38350. EXPECT_DECLS;
  38351. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  38352. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  38353. int i;
  38354. char buf[50];
  38355. ASN1_OBJECT* obj = NULL;
  38356. static const struct {
  38357. const char* oidStr;
  38358. const char* sn;
  38359. const char* ln;
  38360. } objs_list[] = {
  38361. #if defined(WOLFSSL_APACHE_HTTPD)
  38362. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  38363. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  38364. #endif
  38365. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  38366. { NULL, NULL, NULL }
  38367. };
  38368. static const struct {
  38369. const char* numeric;
  38370. const char* name;
  38371. } objs_named[] = {
  38372. /* In dictionary but not in normal list. */
  38373. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  38374. /* Made up OID. */
  38375. { "1.3.5.7", "1.3.5.7" },
  38376. { NULL, NULL }
  38377. };
  38378. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  38379. ASN1_OBJECT_free(obj);
  38380. obj = NULL;
  38381. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  38382. ASN1_OBJECT_free(obj);
  38383. obj = NULL;
  38384. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  38385. /* Test numerical value of oid (oidStr) */
  38386. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  38387. /* Convert object back to text to confirm oid is correct */
  38388. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38389. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38390. ASN1_OBJECT_free(obj);
  38391. obj = NULL;
  38392. XMEMSET(buf, 0, sizeof(buf));
  38393. /* Test short name (sn) */
  38394. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  38395. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  38396. /* Convert object back to text to confirm oid is correct */
  38397. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38398. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38399. ASN1_OBJECT_free(obj);
  38400. obj = NULL;
  38401. XMEMSET(buf, 0, sizeof(buf));
  38402. /* Test long name (ln) - should fail when no_name = 1 */
  38403. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  38404. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  38405. /* Convert object back to text to confirm oid is correct */
  38406. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38407. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38408. ASN1_OBJECT_free(obj);
  38409. obj = NULL;
  38410. XMEMSET(buf, 0, sizeof(buf));
  38411. }
  38412. for (i = 0; objs_named[i].numeric != NULL; i++) {
  38413. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  38414. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  38415. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  38416. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38417. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  38418. ASN1_OBJECT_free(obj);
  38419. obj = NULL;
  38420. }
  38421. #endif
  38422. return EXPECT_RESULT();
  38423. }
  38424. static int test_wolfSSL_PEM_write_bio_X509(void)
  38425. {
  38426. EXPECT_DECLS;
  38427. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  38428. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  38429. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  38430. !defined(NO_FILESYSTEM)
  38431. /* This test contains the hard coded expected
  38432. * lengths. Update if necessary */
  38433. XFILE fp = XBADFILE;
  38434. WOLFSSL_EVP_PKEY *priv = NULL;
  38435. BIO* input = NULL;
  38436. BIO* output = NULL;
  38437. X509* x509a = NULL;
  38438. X509* x509b = NULL;
  38439. ASN1_TIME* notBeforeA = NULL;
  38440. ASN1_TIME* notAfterA = NULL;
  38441. #ifndef NO_ASN_TIME
  38442. ASN1_TIME* notBeforeB = NULL;
  38443. ASN1_TIME* notAfterB = NULL;
  38444. #endif
  38445. int expectedLen;
  38446. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  38447. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  38448. if (fp != XBADFILE) {
  38449. XFCLOSE(fp);
  38450. fp = XBADFILE;
  38451. }
  38452. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  38453. "rb"));
  38454. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  38455. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  38456. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38457. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  38458. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  38459. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  38460. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38461. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38462. /* compare length against expected */
  38463. expectedLen = 2000;
  38464. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38465. #ifndef NO_ASN_TIME
  38466. /* read exported X509 PEM back into struct, sanity check on export,
  38467. * make sure notBefore/notAfter are the same and certs are identical. */
  38468. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38469. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  38470. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  38471. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  38472. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  38473. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38474. X509_free(x509b);
  38475. x509b = NULL;
  38476. #endif
  38477. /* Reset output buffer */
  38478. BIO_free(output);
  38479. output = NULL;
  38480. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38481. /* Test forcing the AKID to be generated just from KeyIdentifier */
  38482. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  38483. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  38484. x509a->authKeyId = x509a->authKeyIdSrc;
  38485. x509a->authKeyIdSrc = NULL;
  38486. x509a->authKeyIdSrcSz = 0;
  38487. }
  38488. /* Resign to re-generate the der */
  38489. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  38490. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38491. /* Check that we generate a smaller output since the AKID will
  38492. * only contain the KeyIdentifier without any additional
  38493. * information */
  38494. /* Here we copy the validity struct from the original */
  38495. expectedLen = 1688;
  38496. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38497. /* Reset buffers and x509 */
  38498. BIO_free(input);
  38499. input = NULL;
  38500. BIO_free(output);
  38501. output = NULL;
  38502. X509_free(x509a);
  38503. x509a = NULL;
  38504. /* test CA and basicConstSet values are encoded when
  38505. * the cert is a CA */
  38506. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  38507. /* read PEM into X509 struct */
  38508. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38509. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38510. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38511. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38512. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  38513. * values are maintained and certs are identical.*/
  38514. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38515. ExpectIntEQ(x509b->isCa, 1);
  38516. ExpectIntEQ(x509b->basicConstSet, 1);
  38517. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38518. X509_free(x509a);
  38519. x509a = NULL;
  38520. X509_free(x509b);
  38521. x509b = NULL;
  38522. BIO_free(input);
  38523. input = NULL;
  38524. BIO_free(output);
  38525. output = NULL;
  38526. /* test CA and basicConstSet values are encoded when
  38527. * the cert is not CA */
  38528. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  38529. /* read PEM into X509 struct */
  38530. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38531. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38532. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38533. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38534. /* read exported X509 PEM back into struct, ensure isCa and
  38535. * basicConstSet values are maintained and certs are identical */
  38536. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38537. ExpectIntEQ(x509b->isCa, 0);
  38538. ExpectIntEQ(x509b->basicConstSet, 1);
  38539. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38540. wolfSSL_EVP_PKEY_free(priv);
  38541. X509_free(x509a);
  38542. X509_free(x509b);
  38543. BIO_free(input);
  38544. BIO_free(output);
  38545. #endif
  38546. return EXPECT_RESULT();
  38547. }
  38548. static int test_wolfSSL_X509_NAME_ENTRY(void)
  38549. {
  38550. EXPECT_DECLS;
  38551. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38552. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  38553. X509* x509 = NULL;
  38554. #ifndef NO_BIO
  38555. BIO* bio = NULL;
  38556. #endif
  38557. X509_NAME* nm = NULL;
  38558. X509_NAME_ENTRY* entry = NULL;
  38559. unsigned char cn[] = "another name to add";
  38560. #ifdef OPENSSL_ALL
  38561. int i;
  38562. int names_len = 0;
  38563. #endif
  38564. ExpectNotNull(x509 =
  38565. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38566. #ifndef NO_BIO
  38567. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38568. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  38569. #endif
  38570. #ifdef WOLFSSL_CERT_REQ
  38571. {
  38572. X509_REQ* req = NULL;
  38573. #ifndef NO_BIO
  38574. BIO* bReq = NULL;
  38575. #endif
  38576. ExpectNotNull(req =
  38577. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38578. #ifndef NO_BIO
  38579. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  38580. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  38581. BIO_free(bReq);
  38582. #endif
  38583. X509_free(req);
  38584. }
  38585. #endif
  38586. ExpectNotNull(nm = X509_get_subject_name(x509));
  38587. /* Test add entry */
  38588. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  38589. 0x0c, cn, (int)sizeof(cn)));
  38590. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38591. #ifdef WOLFSSL_CERT_EXT
  38592. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  38593. (byte*)"support@wolfssl.com", 19, -1,
  38594. 1), WOLFSSL_SUCCESS);
  38595. #endif
  38596. X509_NAME_ENTRY_free(entry);
  38597. entry = NULL;
  38598. #ifdef WOLFSSL_CERT_REQ
  38599. {
  38600. unsigned char srv_pkcs9p[] = "Server";
  38601. unsigned char fvrtDrnk[] = "tequila";
  38602. unsigned char* der = NULL;
  38603. char* subject = NULL;
  38604. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  38605. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  38606. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  38607. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  38608. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  38609. ExpectNotNull(der);
  38610. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  38611. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  38612. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  38613. #ifdef DEBUG_WOLFSSL
  38614. if (subject != NULL) {
  38615. fprintf(stderr, "\n\t%s\n", subject);
  38616. }
  38617. #endif
  38618. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  38619. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  38620. }
  38621. #endif
  38622. /* Test add entry by text */
  38623. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  38624. 0x0c, cn, (int)sizeof(cn)));
  38625. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  38626. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  38627. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  38628. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  38629. #endif
  38630. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38631. X509_NAME_ENTRY_free(entry);
  38632. entry = NULL;
  38633. /* Test add entry by NID */
  38634. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  38635. cn, -1, -1, 0), SSL_SUCCESS);
  38636. #ifdef OPENSSL_ALL
  38637. /* stack of name entry */
  38638. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  38639. for (i = 0; i < names_len; i++) {
  38640. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  38641. }
  38642. #endif
  38643. #ifndef NO_BIO
  38644. BIO_free(bio);
  38645. #endif
  38646. X509_free(x509); /* free's nm */
  38647. #endif
  38648. return EXPECT_RESULT();
  38649. }
  38650. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  38651. static int test_GENERAL_NAME_set0_othername(void) {
  38652. EXPECT_DECLS;
  38653. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38654. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38655. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38656. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  38657. defined(WOLFSSL_FPKI)
  38658. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  38659. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  38660. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  38661. const char * cert_fname = "./certs/server-cert.der";
  38662. const char * key_fname = "./certs/server-key.der";
  38663. X509* x509 = NULL;
  38664. GENERAL_NAME* gn = NULL;
  38665. GENERAL_NAMES* gns = NULL;
  38666. ASN1_OBJECT* upn_oid = NULL;
  38667. ASN1_UTF8STRING *utf8str = NULL;
  38668. ASN1_TYPE *value = NULL;
  38669. X509_EXTENSION * ext = NULL;
  38670. byte* pt = NULL;
  38671. byte der[4096];
  38672. int derSz = 0;
  38673. EVP_PKEY* priv = NULL;
  38674. XFILE f = XBADFILE;
  38675. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  38676. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  38677. if (f != XBADFILE) {
  38678. XFCLOSE(f);
  38679. f = XBADFILE;
  38680. }
  38681. ExpectNotNull(gn = GENERAL_NAME_new());
  38682. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38683. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  38684. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38685. ExpectNotNull(value = ASN1_TYPE_new());
  38686. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38687. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  38688. wolfSSL_ASN1_STRING_free(utf8str);
  38689. }
  38690. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38691. if (EXPECT_FAIL()) {
  38692. ASN1_TYPE_free(value);
  38693. }
  38694. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38695. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38696. if (EXPECT_FAIL()) {
  38697. GENERAL_NAME_free(gn);
  38698. gn = NULL;
  38699. }
  38700. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38701. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  38702. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  38703. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  38704. if (f != XBADFILE) {
  38705. XFCLOSE(f);
  38706. f = XBADFILE;
  38707. }
  38708. pt = der;
  38709. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38710. (const unsigned char**)&pt, derSz));
  38711. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38712. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38713. gns = NULL;
  38714. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  38715. NULL));
  38716. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  38717. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  38718. ExpectIntEQ(gn->type, 0);
  38719. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38720. ASN1_OBJECT_free(upn_oid);
  38721. X509_EXTENSION_free(ext);
  38722. X509_free(x509);
  38723. EVP_PKEY_free(priv);
  38724. #endif
  38725. return EXPECT_RESULT();
  38726. }
  38727. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  38728. static int test_othername_and_SID_ext(void) {
  38729. EXPECT_DECLS;
  38730. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38731. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38732. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38733. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  38734. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  38735. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  38736. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  38737. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  38738. const char* csr_fname = "./certs/csr.signed.der";
  38739. const char* key_fname = "./certs/server-key.der";
  38740. byte der[4096];
  38741. int derSz = 0;
  38742. X509_REQ* x509 = NULL;
  38743. STACK_OF(X509_EXTENSION) *exts = NULL;
  38744. X509_EXTENSION * san_ext = NULL;
  38745. X509_EXTENSION * ext = NULL;
  38746. GENERAL_NAME* gn = NULL;
  38747. GENERAL_NAMES* gns = NULL;
  38748. ASN1_OBJECT* upn_oid = NULL;
  38749. ASN1_UTF8STRING *utf8str = NULL;
  38750. ASN1_TYPE *value = NULL;
  38751. ASN1_STRING *extval = NULL;
  38752. /* SID extension. SID data format explained here:
  38753. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  38754. */
  38755. uint8_t SidExtension[] = {
  38756. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  38757. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  38758. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  38759. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  38760. uint8_t expectedAltName[] = {
  38761. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  38762. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  38763. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  38764. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  38765. X509_EXTENSION *sid_ext = NULL;
  38766. ASN1_OBJECT* sid_oid = NULL;
  38767. ASN1_OCTET_STRING *sid_data = NULL;
  38768. EVP_PKEY* priv = NULL;
  38769. XFILE f = XBADFILE;
  38770. byte* pt = NULL;
  38771. BIO* bio = NULL;
  38772. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  38773. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  38774. if (f != XBADFILE) {
  38775. XFCLOSE(f);
  38776. f = XBADFILE;
  38777. }
  38778. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  38779. ExpectNotNull(gn = GENERAL_NAME_new());
  38780. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38781. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  38782. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38783. ExpectNotNull(value = ASN1_TYPE_new());
  38784. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38785. if (EXPECT_FAIL()) {
  38786. ASN1_UTF8STRING_free(utf8str);
  38787. }
  38788. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38789. if (EXPECT_FAIL()) {
  38790. ASN1_TYPE_free(value);
  38791. GENERAL_NAME_free(gn);
  38792. gn = NULL;
  38793. }
  38794. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38795. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38796. if (EXPECT_FAIL()) {
  38797. GENERAL_NAME_free(gn);
  38798. }
  38799. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38800. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  38801. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  38802. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  38803. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  38804. sid_data));
  38805. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  38806. /* Ensure an empty stack doesn't raise an error. */
  38807. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38808. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  38809. if (EXPECT_FAIL()) {
  38810. X509_EXTENSION_free(san_ext);
  38811. }
  38812. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  38813. if (EXPECT_FAIL()) {
  38814. X509_EXTENSION_free(sid_ext);
  38815. }
  38816. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38817. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  38818. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  38819. if (f != XBADFILE)
  38820. XFCLOSE(f);
  38821. pt = der;
  38822. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38823. (const unsigned char**)&pt, derSz));
  38824. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  38825. pt = der;
  38826. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  38827. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38828. gns = NULL;
  38829. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38830. exts = NULL;
  38831. ASN1_OBJECT_free(upn_oid);
  38832. ASN1_OBJECT_free(sid_oid);
  38833. ASN1_OCTET_STRING_free(sid_data);
  38834. X509_REQ_free(x509);
  38835. x509 = NULL;
  38836. EVP_PKEY_free(priv);
  38837. /* At this point everything used to generate what is in der is cleaned up.
  38838. * We now read back from der to confirm the extensions were inserted
  38839. * correctly. */
  38840. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  38841. ExpectNotNull(bio);
  38842. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  38843. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  38844. ExpectNotNull(x509);
  38845. BIO_free(bio);
  38846. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  38847. x509));
  38848. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  38849. /* Check the SID extension. */
  38850. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  38851. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  38852. ExpectIntEQ(extval->length, sizeof(SidExtension));
  38853. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  38854. /* Check the AltNames extension. */
  38855. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  38856. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  38857. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  38858. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  38859. 0);
  38860. /* Cleanup */
  38861. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  38862. NULL));
  38863. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  38864. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  38865. ExpectIntEQ(gn->type, 0);
  38866. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38867. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38868. X509_REQ_free(x509);
  38869. #endif
  38870. return EXPECT_RESULT();
  38871. }
  38872. static int test_wolfSSL_X509_set_name(void)
  38873. {
  38874. EXPECT_DECLS;
  38875. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38876. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38877. X509* x509 = NULL;
  38878. X509_NAME* name = NULL;
  38879. ExpectNotNull(name = X509_NAME_new());
  38880. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  38881. (byte*)"wolfssl.com", 11, 0, 1),
  38882. WOLFSSL_SUCCESS);
  38883. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  38884. (byte*)"support@wolfssl.com", 19, -1,
  38885. 1), WOLFSSL_SUCCESS);
  38886. ExpectNotNull(x509 = X509_new());
  38887. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  38888. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  38889. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  38890. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  38891. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  38892. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  38893. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  38894. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  38895. X509_free(x509);
  38896. X509_NAME_free(name);
  38897. #endif /* OPENSSL_ALL && !NO_CERTS */
  38898. return EXPECT_RESULT();
  38899. }
  38900. static int test_wolfSSL_X509_set_notAfter(void)
  38901. {
  38902. EXPECT_DECLS;
  38903. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38904. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38905. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38906. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  38907. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  38908. /* Generalized time will overflow time_t if not long */
  38909. X509* x = NULL;
  38910. BIO* bio = NULL;
  38911. ASN1_TIME *asn_time = NULL;
  38912. ASN1_TIME *time_check = NULL;
  38913. const int year = 365*24*60*60;
  38914. const int day = 24*60*60;
  38915. const int hour = 60*60;
  38916. const int mini = 60;
  38917. int offset_day;
  38918. unsigned char buf[25];
  38919. time_t t;
  38920. /*
  38921. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38922. */
  38923. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  38924. offset_day = 7;
  38925. /*
  38926. * Free these.
  38927. */
  38928. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38929. ExpectNotNull(asn_time);
  38930. ExpectNotNull(x = X509_new());
  38931. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38932. /*
  38933. * Tests
  38934. */
  38935. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  38936. /* time_check is simply (ANS1_TIME*)x->notAfter */
  38937. ExpectNotNull(time_check = X509_get_notAfter(x));
  38938. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38939. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38940. /* Convert to human readable format and compare to intended date */
  38941. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38942. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38943. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  38944. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  38945. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  38946. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  38947. /*
  38948. * Cleanup
  38949. */
  38950. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  38951. X509_free(x);
  38952. BIO_free(bio);
  38953. #endif
  38954. return EXPECT_RESULT();
  38955. }
  38956. static int test_wolfSSL_X509_set_notBefore(void)
  38957. {
  38958. EXPECT_DECLS;
  38959. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38960. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38961. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38962. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  38963. X509* x = NULL;
  38964. BIO* bio = NULL;
  38965. ASN1_TIME *asn_time = NULL;
  38966. ASN1_TIME *time_check = NULL;
  38967. const int year = 365*24*60*60;
  38968. const int day = 24*60*60;
  38969. const int hour = 60*60;
  38970. const int mini = 60;
  38971. int offset_day;
  38972. unsigned char buf[25];
  38973. time_t t;
  38974. /*
  38975. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38976. */
  38977. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  38978. offset_day = 7;
  38979. /*
  38980. * Free these.
  38981. */
  38982. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38983. ExpectNotNull(asn_time);
  38984. ExpectNotNull(x = X509_new());
  38985. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38986. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  38987. /*
  38988. * Main Tests
  38989. */
  38990. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  38991. /* time_check == (ANS1_TIME*)x->notBefore */
  38992. ExpectNotNull(time_check = X509_get_notBefore(x));
  38993. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38994. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38995. /* Convert to human readable format and compare to intended date */
  38996. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38997. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38998. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  38999. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  39000. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  39001. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  39002. /*
  39003. * Cleanup
  39004. */
  39005. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39006. X509_free(x);
  39007. BIO_free(bio);
  39008. #endif
  39009. return EXPECT_RESULT();
  39010. }
  39011. static int test_wolfSSL_X509_set_version(void)
  39012. {
  39013. EXPECT_DECLS;
  39014. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  39015. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  39016. X509* x509 = NULL;
  39017. long v = 2L;
  39018. long maxInt = INT_MAX;
  39019. ExpectNotNull(x509 = X509_new());
  39020. /* These should pass. */
  39021. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  39022. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  39023. /* Fail Case: When v(long) is greater than x509->version(int). */
  39024. v = maxInt+1;
  39025. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  39026. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  39027. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  39028. /* Cleanup */
  39029. X509_free(x509);
  39030. #endif
  39031. return EXPECT_RESULT();
  39032. }
  39033. #ifndef NO_BIO
  39034. static int test_wolfSSL_BIO_gets(void)
  39035. {
  39036. EXPECT_DECLS;
  39037. #if defined(OPENSSL_EXTRA)
  39038. BIO* bio = NULL;
  39039. BIO* bio2 = NULL;
  39040. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  39041. char emp[] = "";
  39042. char bio_buffer[20];
  39043. int bufferSz = 20;
  39044. #ifdef OPENSSL_ALL
  39045. BUF_MEM* emp_bm = NULL;
  39046. BUF_MEM* msg_bm = NULL;
  39047. #endif
  39048. /* try with bad args */
  39049. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  39050. #ifdef OPENSSL_ALL
  39051. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  39052. #endif
  39053. /* try with real msg */
  39054. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  39055. XMEMSET(bio_buffer, 0, bufferSz);
  39056. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  39057. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  39058. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  39059. ExpectFalse(bio2 != BIO_next(bio));
  39060. /* make buffer filled with no terminating characters */
  39061. XMEMSET(bio_buffer, 1, bufferSz);
  39062. /* BIO_gets reads a line of data */
  39063. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39064. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39065. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39066. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39067. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39068. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39069. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39070. #ifdef OPENSSL_ALL
  39071. /* test setting the mem_buf manually */
  39072. BIO_free(bio);
  39073. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  39074. ExpectNotNull(emp_bm = BUF_MEM_new());
  39075. ExpectNotNull(msg_bm = BUF_MEM_new());
  39076. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  39077. if (EXPECT_SUCCESS()) {
  39078. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  39079. msg_bm->data = NULL;
  39080. }
  39081. /* emp size is 1 for terminator */
  39082. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  39083. if (EXPECT_SUCCESS()) {
  39084. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  39085. emp_bm->data = emp;
  39086. msg_bm->data = msg;
  39087. }
  39088. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  39089. /* check reading an empty string */
  39090. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  39091. ExpectStrEQ(emp, bio_buffer);
  39092. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39093. /* BIO_gets reads a line of data */
  39094. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  39095. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39096. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39097. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39098. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39099. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39100. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39101. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39102. if (EXPECT_SUCCESS())
  39103. emp_bm->data = NULL;
  39104. BUF_MEM_free(emp_bm);
  39105. if (EXPECT_SUCCESS())
  39106. msg_bm->data = NULL;
  39107. BUF_MEM_free(msg_bm);
  39108. #endif
  39109. /* check not null terminated string */
  39110. BIO_free(bio);
  39111. bio = NULL;
  39112. msg[0] = 0x33;
  39113. msg[1] = 0x33;
  39114. msg[2] = 0x33;
  39115. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  39116. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  39117. ExpectIntEQ(bio_buffer[0], msg[0]);
  39118. ExpectIntEQ(bio_buffer[1], msg[1]);
  39119. ExpectIntNE(bio_buffer[2], msg[2]);
  39120. BIO_free(bio);
  39121. bio = NULL;
  39122. msg[3] = 0x33;
  39123. bio_buffer[3] = 0x33;
  39124. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  39125. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  39126. ExpectIntEQ(bio_buffer[0], msg[0]);
  39127. ExpectIntEQ(bio_buffer[1], msg[1]);
  39128. ExpectIntEQ(bio_buffer[2], msg[2]);
  39129. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  39130. /* check reading an empty string */
  39131. BIO_free(bio);
  39132. bio = NULL;
  39133. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  39134. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  39135. ExpectStrEQ(emp, bio_buffer);
  39136. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39137. /* check error cases */
  39138. BIO_free(bio);
  39139. bio = NULL;
  39140. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  39141. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39142. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  39143. #if !defined(NO_FILESYSTEM)
  39144. {
  39145. BIO* f_bio = NULL;
  39146. XFILE f = XBADFILE;
  39147. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  39148. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  39149. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  39150. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  39151. if (EXPECT_FAIL() && (f != XBADFILE)) {
  39152. XFCLOSE(f);
  39153. }
  39154. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  39155. BIO_free(f_bio);
  39156. f_bio = NULL;
  39157. }
  39158. #endif /* NO_FILESYSTEM */
  39159. BIO_free(bio);
  39160. bio = NULL;
  39161. BIO_free(bio2);
  39162. bio2 = NULL;
  39163. /* try with type BIO */
  39164. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  39165. sizeof(msg));
  39166. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  39167. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  39168. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  39169. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  39170. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  39171. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  39172. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  39173. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39174. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39175. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39176. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39177. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39178. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39179. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39180. BIO_free(bio);
  39181. bio = NULL;
  39182. BIO_free(bio2);
  39183. bio2 = NULL;
  39184. /* check reading an empty string */
  39185. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  39186. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  39187. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39188. ExpectStrEQ(emp, bio_buffer);
  39189. BIO_free(bio);
  39190. bio = NULL;
  39191. #endif
  39192. return EXPECT_RESULT();
  39193. }
  39194. static int test_wolfSSL_BIO_puts(void)
  39195. {
  39196. EXPECT_DECLS;
  39197. #if defined(OPENSSL_EXTRA)
  39198. BIO* bio = NULL;
  39199. char input[] = "hello\0world\n.....ok\n\0";
  39200. char output[128];
  39201. XMEMSET(output, 0, sizeof(output));
  39202. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39203. ExpectIntEQ(BIO_puts(bio, input), 5);
  39204. ExpectIntEQ(BIO_pending(bio), 5);
  39205. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  39206. ExpectIntEQ(BIO_pending(bio), 19);
  39207. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  39208. ExpectStrEQ(output, "helloworld\n");
  39209. ExpectIntEQ(BIO_pending(bio), 8);
  39210. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  39211. ExpectStrEQ(output, ".....ok\n");
  39212. ExpectIntEQ(BIO_pending(bio), 0);
  39213. ExpectIntEQ(BIO_puts(bio, ""), -1);
  39214. BIO_free(bio);
  39215. #endif
  39216. return EXPECT_RESULT();
  39217. }
  39218. static int test_wolfSSL_BIO_dump(void)
  39219. {
  39220. EXPECT_DECLS;
  39221. #if defined(OPENSSL_EXTRA)
  39222. BIO* bio;
  39223. static const unsigned char data[] = {
  39224. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  39225. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  39226. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  39227. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  39228. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  39229. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  39230. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  39231. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  39232. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  39233. 0xB4
  39234. };
  39235. /* Generated with OpenSSL. */
  39236. static const char expected[] =
  39237. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  39238. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  39239. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  39240. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  39241. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  39242. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  39243. static const char expectedAll[] =
  39244. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  39245. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  39246. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  39247. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  39248. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  39249. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  39250. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  39251. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  39252. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  39253. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  39254. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  39255. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  39256. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  39257. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  39258. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  39259. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  39260. char output[16 * 80];
  39261. int i;
  39262. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39263. /* Example key dumped. */
  39264. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  39265. sizeof(expected) - 1);
  39266. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  39267. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  39268. /* Try every possible value for a character. */
  39269. for (i = 0; i < 256; i++)
  39270. output[i] = i;
  39271. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  39272. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  39273. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  39274. BIO_free(bio);
  39275. #endif
  39276. return EXPECT_RESULT();
  39277. }
  39278. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39279. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  39280. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  39281. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  39282. {
  39283. (void)ssl;
  39284. (void)buf;
  39285. (void)sz;
  39286. (void)ctx;
  39287. return WOLFSSL_CBIO_ERR_WANT_READ;
  39288. }
  39289. #endif
  39290. static int test_wolfSSL_BIO_should_retry(void)
  39291. {
  39292. EXPECT_DECLS;
  39293. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39294. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  39295. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  39296. tcp_ready ready;
  39297. func_args server_args;
  39298. THREAD_TYPE serverThread;
  39299. SOCKET_T sockfd = 0;
  39300. WOLFSSL_CTX* ctx = NULL;
  39301. WOLFSSL* ssl = NULL;
  39302. char msg[64] = "hello wolfssl!";
  39303. char reply[1024];
  39304. int msgSz = (int)XSTRLEN(msg);
  39305. int ret;
  39306. BIO* bio = NULL;
  39307. XMEMSET(&server_args, 0, sizeof(func_args));
  39308. #ifdef WOLFSSL_TIRTOS
  39309. fdOpenSession(Task_self());
  39310. #endif
  39311. StartTCP();
  39312. InitTcpReady(&ready);
  39313. #if defined(USE_WINDOWS_API)
  39314. /* use RNG to get random port if using windows */
  39315. ready.port = GetRandomPort();
  39316. #endif
  39317. server_args.signal = &ready;
  39318. start_thread(test_server_nofail, &server_args, &serverThread);
  39319. wait_tcp_ready(&server_args);
  39320. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39321. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  39322. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  39323. #endif
  39324. ExpectIntEQ(WOLFSSL_SUCCESS,
  39325. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39326. ExpectIntEQ(WOLFSSL_SUCCESS,
  39327. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39328. ExpectIntEQ(WOLFSSL_SUCCESS,
  39329. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39330. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  39331. /* force retry */
  39332. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  39333. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  39334. ExpectNotNull(ssl);
  39335. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39336. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  39337. if (EXPECT_FAIL()) {
  39338. wolfSSL_free(ssl);
  39339. ssl = NULL;
  39340. }
  39341. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  39342. ExpectIntNE(BIO_should_retry(bio), 0);
  39343. ExpectIntEQ(BIO_should_read(bio), 0);
  39344. ExpectIntEQ(BIO_should_write(bio), 0);
  39345. /* now perform successful connection */
  39346. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  39347. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  39348. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  39349. ret = wolfSSL_get_error(ssl, -1);
  39350. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  39351. ExpectIntNE(BIO_should_retry(bio), 0);
  39352. if (ret == WOLFSSL_ERROR_WANT_READ)
  39353. ExpectIntEQ(BIO_should_read(bio), 1);
  39354. else
  39355. ExpectIntEQ(BIO_should_read(bio), 0);
  39356. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  39357. ExpectIntEQ(BIO_should_write(bio), 1);
  39358. else
  39359. ExpectIntEQ(BIO_should_write(bio), 0);
  39360. }
  39361. else {
  39362. ExpectIntEQ(BIO_should_retry(bio), 0);
  39363. ExpectIntEQ(BIO_should_read(bio), 0);
  39364. ExpectIntEQ(BIO_should_write(bio), 0);
  39365. }
  39366. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  39367. XSTRLEN("I hear you fa shizzle!")), 0);
  39368. BIO_free(bio);
  39369. wolfSSL_CTX_free(ctx);
  39370. CloseSocket(sockfd);
  39371. join_thread(serverThread);
  39372. FreeTcpReady(&ready);
  39373. #ifdef WOLFSSL_TIRTOS
  39374. fdOpenSession(Task_self());
  39375. #endif
  39376. #endif
  39377. return EXPECT_RESULT();
  39378. }
  39379. static int test_wolfSSL_BIO_connect(void)
  39380. {
  39381. EXPECT_DECLS;
  39382. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39383. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  39384. tcp_ready ready;
  39385. func_args server_args;
  39386. THREAD_TYPE serverThread;
  39387. BIO *tcpBio = NULL;
  39388. BIO *sslBio = NULL;
  39389. SSL_CTX* ctx = NULL;
  39390. SSL *ssl = NULL;
  39391. SSL *sslPtr;
  39392. char msg[] = "hello wolfssl!";
  39393. char reply[30];
  39394. char buff[10] = {0};
  39395. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39396. ExpectIntEQ(WOLFSSL_SUCCESS,
  39397. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39398. ExpectIntEQ(WOLFSSL_SUCCESS,
  39399. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39400. ExpectIntEQ(WOLFSSL_SUCCESS,
  39401. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39402. /* Setup server */
  39403. XMEMSET(&server_args, 0, sizeof(func_args));
  39404. StartTCP();
  39405. InitTcpReady(&ready);
  39406. #if defined(USE_WINDOWS_API)
  39407. /* use RNG to get random port if using windows */
  39408. ready.port = GetRandomPort();
  39409. #endif
  39410. server_args.signal = &ready;
  39411. start_thread(test_server_nofail, &server_args, &serverThread);
  39412. wait_tcp_ready(&server_args);
  39413. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39414. /* Start the test proper */
  39415. /* Setup the TCP BIO */
  39416. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  39417. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  39418. /* Setup the SSL object */
  39419. ExpectNotNull(ssl = SSL_new(ctx));
  39420. SSL_set_connect_state(ssl);
  39421. /* Setup the SSL BIO */
  39422. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  39423. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  39424. if (EXPECT_FAIL()) {
  39425. wolfSSL_free(ssl);
  39426. }
  39427. /* Verify that BIO_get_ssl works. */
  39428. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  39429. ExpectPtrEq(ssl, sslPtr);
  39430. /* Link BIO's so that sslBio uses tcpBio for IO */
  39431. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  39432. /* Do TCP connect */
  39433. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39434. /* Do TLS handshake */
  39435. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39436. /* Test writing */
  39437. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39438. /* Expect length of default wolfSSL reply */
  39439. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39440. /* Clean it all up */
  39441. BIO_free_all(sslBio);
  39442. /* Server clean up */
  39443. join_thread(serverThread);
  39444. FreeTcpReady(&ready);
  39445. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  39446. * after. */
  39447. XMEMSET(&server_args, 0, sizeof(func_args));
  39448. StartTCP();
  39449. InitTcpReady(&ready);
  39450. #if defined(USE_WINDOWS_API)
  39451. /* use RNG to get random port if using windows */
  39452. ready.port = GetRandomPort();
  39453. #endif
  39454. server_args.signal = &ready;
  39455. start_thread(test_server_nofail, &server_args, &serverThread);
  39456. wait_tcp_ready(&server_args);
  39457. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39458. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  39459. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  39460. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  39461. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39462. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39463. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39464. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39465. /* Attempt to close the TLS connection gracefully. */
  39466. BIO_ssl_shutdown(sslBio);
  39467. BIO_free_all(sslBio);
  39468. join_thread(serverThread);
  39469. FreeTcpReady(&ready);
  39470. SSL_CTX_free(ctx);
  39471. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39472. wc_ecc_fp_free(); /* free per thread cache */
  39473. #endif
  39474. #endif
  39475. return EXPECT_RESULT();
  39476. }
  39477. static int test_wolfSSL_BIO_tls(void)
  39478. {
  39479. EXPECT_DECLS;
  39480. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  39481. SSL_CTX* ctx = NULL;
  39482. SSL *ssl = NULL;
  39483. BIO *readBio = NULL;
  39484. BIO *writeBio = NULL;
  39485. int ret;
  39486. int err = 0;
  39487. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39488. ExpectNotNull(ssl = SSL_new(ctx));
  39489. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  39490. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  39491. /* Qt reads data from write-bio,
  39492. * then writes the read data into plain packet.
  39493. * Qt reads data from plain packet,
  39494. * then writes the read data into read-bio.
  39495. */
  39496. SSL_set_bio(ssl, readBio, writeBio);
  39497. do {
  39498. #ifdef WOLFSSL_ASYNC_CRYPT
  39499. if (err == WC_PENDING_E) {
  39500. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39501. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39502. }
  39503. #endif
  39504. ret = SSL_connect(ssl);
  39505. err = SSL_get_error(ssl, 0);
  39506. } while (err == WC_PENDING_E);
  39507. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  39508. /* in this use case, should return WANT READ
  39509. * so that Qt will read the data from plain packet for next state.
  39510. */
  39511. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  39512. SSL_free(ssl);
  39513. SSL_CTX_free(ctx);
  39514. #endif
  39515. return EXPECT_RESULT();
  39516. }
  39517. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39518. defined(HAVE_HTTP_CLIENT)
  39519. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  39520. {
  39521. BIO* clientBio;
  39522. SSL* sslClient;
  39523. SSL_CTX* ctx;
  39524. char connectAddr[20]; /* IP + port */;
  39525. (void)args;
  39526. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  39527. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  39528. AssertIntEQ(BIO_do_connect(clientBio), 1);
  39529. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39530. AssertNotNull(sslClient = SSL_new(ctx));
  39531. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  39532. SSL_set_bio(sslClient, clientBio, clientBio);
  39533. AssertIntEQ(SSL_connect(sslClient), 1);
  39534. SSL_free(sslClient);
  39535. SSL_CTX_free(ctx);
  39536. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39537. wc_ecc_fp_free(); /* free per thread cache */
  39538. #endif
  39539. WOLFSSL_RETURN_FROM_THREAD(0);
  39540. }
  39541. #endif
  39542. static int test_wolfSSL_BIO_accept(void)
  39543. {
  39544. EXPECT_DECLS;
  39545. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39546. defined(HAVE_HTTP_CLIENT)
  39547. BIO* serverBindBio = NULL;
  39548. BIO* serverAcceptBio = NULL;
  39549. SSL* sslServer = NULL;
  39550. SSL_CTX* ctx = NULL;
  39551. func_args args;
  39552. THREAD_TYPE thread;
  39553. char port[10]; /* 10 bytes should be enough to store the string
  39554. * representation of the port */
  39555. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  39556. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  39557. /* First BIO_do_accept binds the port */
  39558. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39559. XMEMSET(&args, 0, sizeof(func_args));
  39560. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  39561. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39562. /* Let's plug it into SSL to test */
  39563. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39564. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  39565. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39566. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  39567. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39568. ExpectNotNull(sslServer = SSL_new(ctx));
  39569. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  39570. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  39571. ExpectIntEQ(SSL_accept(sslServer), 1);
  39572. join_thread(thread);
  39573. BIO_free(serverBindBio);
  39574. SSL_free(sslServer);
  39575. SSL_CTX_free(ctx);
  39576. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39577. wc_ecc_fp_free(); /* free per thread cache */
  39578. #endif
  39579. #endif
  39580. return EXPECT_RESULT();
  39581. }
  39582. static int test_wolfSSL_BIO_write(void)
  39583. {
  39584. EXPECT_DECLS;
  39585. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  39586. BIO* bio = NULL;
  39587. BIO* bio64 = NULL;
  39588. BIO* bio_mem = NULL;
  39589. BIO* ptr = NULL;
  39590. int sz;
  39591. char msg[] = "conversion test";
  39592. char out[40];
  39593. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  39594. void* bufPtr = NULL;
  39595. BUF_MEM* buf = NULL;
  39596. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39597. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  39598. if (EXPECT_FAIL()) {
  39599. BIO_free(bio64);
  39600. }
  39601. /* now should convert to base64 then write to memory */
  39602. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39603. BIO_flush(bio);
  39604. /* test BIO chain */
  39605. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  39606. ExpectNotNull(buf);
  39607. ExpectIntEQ(buf->length, 25);
  39608. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  39609. ExpectPtrEq(buf->data, bufPtr);
  39610. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  39611. sz = sizeof(out);
  39612. XMEMSET(out, 0, sz);
  39613. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  39614. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  39615. /* write then read should return the same message */
  39616. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39617. sz = sizeof(out);
  39618. XMEMSET(out, 0, sz);
  39619. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  39620. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39621. /* now try encoding with no line ending */
  39622. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  39623. #ifdef HAVE_EX_DATA
  39624. BIO_set_ex_data(bio64, 0, (void*) "data");
  39625. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  39626. #endif
  39627. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39628. BIO_flush(bio);
  39629. sz = sizeof(out);
  39630. XMEMSET(out, 0, sz);
  39631. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  39632. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  39633. BIO_free_all(bio); /* frees bio64 also */
  39634. bio = NULL;
  39635. /* test with more than one bio64 in list */
  39636. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39637. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  39638. if (EXPECT_FAIL()) {
  39639. BIO_free(bio64);
  39640. bio64 = NULL;
  39641. }
  39642. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  39643. ExpectNotNull(BIO_push(bio64, bio_mem));
  39644. if (EXPECT_FAIL()) {
  39645. BIO_free(bio_mem);
  39646. }
  39647. /* now should convert to base64 when stored and then decode with read */
  39648. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  39649. BIO_flush(bio);
  39650. sz = sizeof(out);
  39651. XMEMSET(out, 0, sz);
  39652. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  39653. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  39654. BIO_clear_flags(bio64, ~0);
  39655. BIO_set_retry_read(bio);
  39656. BIO_free_all(bio); /* frees bio64s also */
  39657. bio = NULL;
  39658. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  39659. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39660. BIO_free(bio);
  39661. #endif
  39662. return EXPECT_RESULT();
  39663. }
  39664. static int test_wolfSSL_BIO_printf(void)
  39665. {
  39666. EXPECT_DECLS;
  39667. #if defined(OPENSSL_ALL)
  39668. BIO* bio = NULL;
  39669. int sz = 7;
  39670. char msg[] = "TLS 1.3 for the world";
  39671. char out[60];
  39672. char expected[] = "TLS 1.3 for the world : sz = 7";
  39673. XMEMSET(out, 0, sizeof(out));
  39674. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39675. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  39676. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  39677. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  39678. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  39679. BIO_free(bio);
  39680. #endif
  39681. return EXPECT_RESULT();
  39682. }
  39683. static int test_wolfSSL_BIO_f_md(void)
  39684. {
  39685. EXPECT_DECLS;
  39686. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  39687. BIO* bio = NULL;
  39688. BIO* mem = NULL;
  39689. char msg[] = "message to hash";
  39690. char out[60];
  39691. EVP_MD_CTX* ctx = NULL;
  39692. const unsigned char testKey[] =
  39693. {
  39694. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39695. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39696. 0x0b, 0x0b, 0x0b, 0x0b
  39697. };
  39698. const char testData[] = "Hi There";
  39699. const unsigned char testResult[] =
  39700. {
  39701. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  39702. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  39703. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  39704. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  39705. };
  39706. const unsigned char expectedHash[] =
  39707. {
  39708. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  39709. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  39710. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  39711. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  39712. };
  39713. const unsigned char emptyHash[] =
  39714. {
  39715. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  39716. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  39717. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  39718. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  39719. };
  39720. unsigned char check[sizeof(testResult) + 1];
  39721. size_t checkSz = -1;
  39722. EVP_PKEY* key = NULL;
  39723. XMEMSET(out, 0, sizeof(out));
  39724. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39725. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  39726. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  39727. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  39728. /* should not be able to write/read yet since just digest wrapper and no
  39729. * data is passing through the bio */
  39730. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  39731. ExpectIntEQ(BIO_pending(bio), 0);
  39732. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  39733. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  39734. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39735. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  39736. BIO_reset(bio);
  39737. /* append BIO mem to bio in order to read/write */
  39738. ExpectNotNull(bio = BIO_push(bio, mem));
  39739. XMEMSET(out, 0, sizeof(out));
  39740. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  39741. ExpectIntEQ(BIO_pending(bio), 16);
  39742. /* this just reads the message and does not hash it (gets calls final) */
  39743. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  39744. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39745. /* create a message digest using BIO */
  39746. XMEMSET(out, 0, sizeof(out));
  39747. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  39748. ExpectIntEQ(BIO_pending(mem), 16);
  39749. ExpectIntEQ(BIO_pending(bio), 16);
  39750. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39751. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  39752. BIO_free(bio);
  39753. bio = NULL;
  39754. BIO_free(mem);
  39755. mem = NULL;
  39756. /* test with HMAC */
  39757. XMEMSET(out, 0, sizeof(out));
  39758. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39759. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  39760. BIO_get_md_ctx(bio, &ctx);
  39761. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  39762. (int)sizeof(testKey)));
  39763. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  39764. ExpectNotNull(bio = BIO_push(bio, mem));
  39765. BIO_write(bio, testData, (int)strlen(testData));
  39766. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  39767. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  39768. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  39769. EVP_PKEY_free(key);
  39770. BIO_free(bio);
  39771. BIO_free(mem);
  39772. #endif
  39773. return EXPECT_RESULT();
  39774. }
  39775. static int test_wolfSSL_BIO_up_ref(void)
  39776. {
  39777. EXPECT_DECLS;
  39778. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39779. BIO* bio = NULL;
  39780. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39781. ExpectIntEQ(BIO_up_ref(NULL), 0);
  39782. ExpectIntEQ(BIO_up_ref(bio), 1);
  39783. BIO_free(bio);
  39784. ExpectIntEQ(BIO_up_ref(bio), 1);
  39785. BIO_free(bio);
  39786. BIO_free(bio);
  39787. #endif
  39788. return EXPECT_RESULT();
  39789. }
  39790. static int test_wolfSSL_BIO_reset(void)
  39791. {
  39792. EXPECT_DECLS;
  39793. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39794. BIO* bio = NULL;
  39795. byte buf[16];
  39796. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  39797. (word32)XSTRLEN("secure your data")));
  39798. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  39799. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  39800. XMEMSET(buf, 0, 16);
  39801. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  39802. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  39803. /* You cannot write to MEM BIO with read-only mode. */
  39804. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  39805. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  39806. XMEMSET(buf, 0, 16);
  39807. ExpectIntEQ(BIO_reset(bio), 0);
  39808. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  39809. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  39810. BIO_free(bio);
  39811. #endif
  39812. return EXPECT_RESULT();
  39813. }
  39814. #endif /* !NO_BIO */
  39815. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39816. /* test that the callback arg is correct */
  39817. static int certCbArg = 0;
  39818. static int certCb(WOLFSSL* ssl, void* arg)
  39819. {
  39820. if (ssl == NULL || arg != &certCbArg)
  39821. return 0;
  39822. if (wolfSSL_is_server(ssl)) {
  39823. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  39824. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39825. return 0;
  39826. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  39827. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39828. return 0;
  39829. }
  39830. else {
  39831. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  39832. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39833. return 0;
  39834. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  39835. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39836. return 0;
  39837. }
  39838. return 1;
  39839. }
  39840. static int certSetupCb(WOLFSSL_CTX* ctx)
  39841. {
  39842. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  39843. return TEST_SUCCESS;
  39844. }
  39845. /**
  39846. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  39847. * to stop certificate and key loading
  39848. */
  39849. static int certClearCb(WOLFSSL* ssl)
  39850. {
  39851. /* Clear the loaded certs to force the callbacks to set them up */
  39852. SSL_certs_clear(ssl);
  39853. return TEST_SUCCESS;
  39854. }
  39855. #endif
  39856. static int test_wolfSSL_cert_cb(void)
  39857. {
  39858. EXPECT_DECLS;
  39859. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39860. test_ssl_cbf func_cb_client;
  39861. test_ssl_cbf func_cb_server;
  39862. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  39863. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  39864. func_cb_client.ctx_ready = certSetupCb;
  39865. func_cb_client.ssl_ready = certClearCb;
  39866. func_cb_server.ctx_ready = certSetupCb;
  39867. func_cb_server.ssl_ready = certClearCb;
  39868. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  39869. &func_cb_server, NULL), TEST_SUCCESS);
  39870. #endif
  39871. return EXPECT_RESULT();
  39872. }
  39873. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39874. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  39875. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  39876. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  39877. {
  39878. EXPECT_DECLS;
  39879. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  39880. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  39881. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  39882. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  39883. return EXPECT_RESULT();
  39884. }
  39885. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  39886. {
  39887. const byte* suites = NULL;
  39888. word16 suiteSz = 0;
  39889. const byte* hashSigAlgo = NULL;
  39890. word16 hashSigAlgoSz = 0;
  39891. word16 idx = 0;
  39892. int haveRSA = 0;
  39893. int haveECC = 0;
  39894. (void)arg;
  39895. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  39896. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  39897. return 0;
  39898. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  39899. hashSigAlgoSz == 0)
  39900. return 0;
  39901. for (idx = 0; idx < suiteSz; idx += 2) {
  39902. WOLFSSL_CIPHERSUITE_INFO info =
  39903. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  39904. if (info.rsaAuth)
  39905. haveRSA = 1;
  39906. else if (info.eccAuth)
  39907. haveECC = 1;
  39908. }
  39909. if (hashSigAlgoSz > 0) {
  39910. /* sigalgs extension takes precedence over ciphersuites */
  39911. haveRSA = 0;
  39912. haveECC = 0;
  39913. }
  39914. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  39915. int hashAlgo = 0;
  39916. int sigAlgo = 0;
  39917. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  39918. &hashAlgo, &sigAlgo) != 0)
  39919. return 0;
  39920. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  39921. haveRSA = 1;
  39922. else if (sigAlgo == ECDSAk)
  39923. haveECC = 1;
  39924. }
  39925. if (haveRSA) {
  39926. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  39927. != WOLFSSL_SUCCESS)
  39928. return 0;
  39929. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  39930. != WOLFSSL_SUCCESS)
  39931. return 0;
  39932. }
  39933. else if (haveECC) {
  39934. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  39935. != WOLFSSL_SUCCESS)
  39936. return 0;
  39937. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  39938. != WOLFSSL_SUCCESS)
  39939. return 0;
  39940. }
  39941. return 1;
  39942. }
  39943. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  39944. {
  39945. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  39946. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  39947. return TEST_SUCCESS;
  39948. }
  39949. #endif
  39950. /* Testing dynamic ciphers offered by client */
  39951. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  39952. {
  39953. EXPECT_DECLS;
  39954. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39955. test_ssl_cbf func_cb_client;
  39956. test_ssl_cbf func_cb_server;
  39957. struct {
  39958. method_provider client_meth;
  39959. const char* client_ciphers;
  39960. const char* client_sigalgs;
  39961. const char* client_ca;
  39962. method_provider server_meth;
  39963. } test_params[] = {
  39964. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  39965. #ifdef WOLFSSL_TLS13
  39966. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  39967. {wolfTLSv1_3_client_method,
  39968. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  39969. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  39970. #endif
  39971. #ifdef HAVE_ECC
  39972. {wolfTLSv1_3_client_method,
  39973. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  39974. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  39975. #endif
  39976. #endif
  39977. #ifndef WOLFSSL_NO_TLS12
  39978. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  39979. {wolfTLSv1_2_client_method,
  39980. "DHE-RSA-AES128-GCM-SHA256",
  39981. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  39982. #endif
  39983. #ifdef HAVE_ECC
  39984. {wolfTLSv1_2_client_method,
  39985. "ECDHE-ECDSA-AES128-GCM-SHA256",
  39986. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  39987. #endif
  39988. #endif
  39989. #endif
  39990. };
  39991. size_t i;
  39992. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  39993. if (testCount > 0) {
  39994. for (i = 0; i < testCount; i++) {
  39995. printf("\tTesting %s ciphers with %s sigalgs\n",
  39996. test_params[i].client_ciphers,
  39997. test_params[i].client_sigalgs);
  39998. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  39999. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  40000. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  40001. test_params[i].client_ciphers;
  40002. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  40003. test_params[i].client_sigalgs;
  40004. func_cb_client.method = test_params[i].client_meth;
  40005. func_cb_client.caPemFile = test_params[i].client_ca;
  40006. func_cb_client.ctx_ready =
  40007. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  40008. func_cb_server.ctx_ready =
  40009. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  40010. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  40011. func_cb_server.method = test_params[i].server_meth;
  40012. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  40013. &func_cb_server, NULL), TEST_SUCCESS);
  40014. }
  40015. }
  40016. #endif
  40017. return EXPECT_RESULT();
  40018. }
  40019. static int test_wolfSSL_ciphersuite_auth(void)
  40020. {
  40021. EXPECT_DECLS;
  40022. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  40023. WOLFSSL_CIPHERSUITE_INFO info;
  40024. (void)info;
  40025. #ifndef WOLFSSL_NO_TLS12
  40026. #ifdef HAVE_CHACHA
  40027. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40028. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  40029. ExpectIntEQ(info.rsaAuth, 1);
  40030. ExpectIntEQ(info.eccAuth, 0);
  40031. ExpectIntEQ(info.eccStatic, 0);
  40032. ExpectIntEQ(info.psk, 0);
  40033. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40034. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  40035. ExpectIntEQ(info.rsaAuth, 0);
  40036. ExpectIntEQ(info.eccAuth, 1);
  40037. ExpectIntEQ(info.eccStatic, 0);
  40038. ExpectIntEQ(info.psk, 0);
  40039. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40040. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  40041. ExpectIntEQ(info.rsaAuth, 0);
  40042. ExpectIntEQ(info.eccAuth, 0);
  40043. ExpectIntEQ(info.eccStatic, 0);
  40044. ExpectIntEQ(info.psk, 1);
  40045. #endif
  40046. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  40047. #ifndef NO_RSA
  40048. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40049. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  40050. ExpectIntEQ(info.rsaAuth, 1);
  40051. ExpectIntEQ(info.eccAuth, 0);
  40052. ExpectIntEQ(info.eccStatic, 0);
  40053. ExpectIntEQ(info.psk, 0);
  40054. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40055. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  40056. ExpectIntEQ(info.rsaAuth, 1);
  40057. ExpectIntEQ(info.eccAuth, 0);
  40058. ExpectIntEQ(info.eccStatic, 1);
  40059. ExpectIntEQ(info.psk, 0);
  40060. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40061. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  40062. ExpectIntEQ(info.rsaAuth, 1);
  40063. ExpectIntEQ(info.eccAuth, 0);
  40064. ExpectIntEQ(info.eccStatic, 1);
  40065. ExpectIntEQ(info.psk, 0);
  40066. #endif
  40067. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40068. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  40069. ExpectIntEQ(info.rsaAuth, 0);
  40070. ExpectIntEQ(info.eccAuth, 1);
  40071. ExpectIntEQ(info.eccStatic, 0);
  40072. ExpectIntEQ(info.psk, 0);
  40073. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40074. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  40075. ExpectIntEQ(info.rsaAuth, 0);
  40076. ExpectIntEQ(info.eccAuth, 1);
  40077. ExpectIntEQ(info.eccStatic, 1);
  40078. ExpectIntEQ(info.psk, 0);
  40079. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  40080. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  40081. ExpectIntEQ(info.rsaAuth, 0);
  40082. ExpectIntEQ(info.eccAuth, 0);
  40083. ExpectIntEQ(info.eccStatic, 0);
  40084. ExpectIntEQ(info.psk, 1);
  40085. #endif
  40086. #endif
  40087. #ifdef WOLFSSL_TLS13
  40088. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  40089. TLS_AES_128_GCM_SHA256);
  40090. ExpectIntEQ(info.rsaAuth, 0);
  40091. ExpectIntEQ(info.eccAuth, 0);
  40092. ExpectIntEQ(info.eccStatic, 0);
  40093. ExpectIntEQ(info.psk, 0);
  40094. #endif
  40095. #endif
  40096. return EXPECT_RESULT();
  40097. }
  40098. static int test_wolfSSL_sigalg_info(void)
  40099. {
  40100. EXPECT_DECLS;
  40101. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  40102. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  40103. word16 len = 0;
  40104. word16 idx = 0;
  40105. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  40106. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  40107. for (idx = 0; idx < len; idx += 2) {
  40108. int hashAlgo = 0;
  40109. int sigAlgo = 0;
  40110. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  40111. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  40112. ExpectIntNE(hashAlgo, 0);
  40113. ExpectIntNE(sigAlgo, 0);
  40114. }
  40115. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  40116. 0xFFFFFFFF, &len);
  40117. for (idx = 0; idx < len; idx += 2) {
  40118. int hashAlgo = 0;
  40119. int sigAlgo = 0;
  40120. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  40121. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  40122. ExpectIntNE(hashAlgo, 0);
  40123. }
  40124. #endif
  40125. return EXPECT_RESULT();
  40126. }
  40127. static int test_wolfSSL_SESSION(void)
  40128. {
  40129. EXPECT_DECLS;
  40130. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40131. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40132. !defined(NO_SESSION_CACHE)
  40133. WOLFSSL* ssl = NULL;
  40134. WOLFSSL_CTX* ctx = NULL;
  40135. WOLFSSL_SESSION* sess = NULL;
  40136. WOLFSSL_SESSION* sess_copy = NULL;
  40137. #ifdef OPENSSL_EXTRA
  40138. #ifdef HAVE_EXT_CACHE
  40139. unsigned char* sessDer = NULL;
  40140. unsigned char* ptr = NULL;
  40141. int sz = 0;
  40142. #endif
  40143. const unsigned char context[] = "user app context";
  40144. unsigned int contextSz = (unsigned int)sizeof(context);
  40145. #endif
  40146. int ret = 0, err = 0;
  40147. SOCKET_T sockfd;
  40148. tcp_ready ready;
  40149. func_args server_args;
  40150. THREAD_TYPE serverThread;
  40151. char msg[80];
  40152. const char* sendGET = "GET";
  40153. /* TLS v1.3 requires session tickets */
  40154. /* CHACHA and POLY1305 required for myTicketEncCb */
  40155. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  40156. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  40157. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  40158. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  40159. #else
  40160. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40161. #endif
  40162. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  40163. WOLFSSL_FILETYPE_PEM));
  40164. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  40165. WOLFSSL_FILETYPE_PEM));
  40166. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  40167. WOLFSSL_SUCCESS);
  40168. #ifdef WOLFSSL_ENCRYPTED_KEYS
  40169. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  40170. #endif
  40171. #ifdef HAVE_SESSION_TICKET
  40172. /* Use session tickets, for ticket tests below */
  40173. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  40174. #endif
  40175. XMEMSET(&server_args, 0, sizeof(func_args));
  40176. #ifdef WOLFSSL_TIRTOS
  40177. fdOpenSession(Task_self());
  40178. #endif
  40179. StartTCP();
  40180. InitTcpReady(&ready);
  40181. #if defined(USE_WINDOWS_API)
  40182. /* use RNG to get random port if using windows */
  40183. ready.port = GetRandomPort();
  40184. #endif
  40185. server_args.signal = &ready;
  40186. start_thread(test_server_nofail, &server_args, &serverThread);
  40187. wait_tcp_ready(&server_args);
  40188. /* client connection */
  40189. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40190. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  40191. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40192. #ifdef WOLFSSL_ASYNC_CRYPT
  40193. err = 0; /* Reset error */
  40194. #endif
  40195. do {
  40196. #ifdef WOLFSSL_ASYNC_CRYPT
  40197. if (err == WC_PENDING_E) {
  40198. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40199. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40200. }
  40201. #endif
  40202. ret = wolfSSL_connect(ssl);
  40203. err = wolfSSL_get_error(ssl, 0);
  40204. } while (err == WC_PENDING_E);
  40205. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  40206. #ifdef WOLFSSL_ASYNC_CRYPT
  40207. err = 0; /* Reset error */
  40208. #endif
  40209. do {
  40210. #ifdef WOLFSSL_ASYNC_CRYPT
  40211. if (err == WC_PENDING_E) {
  40212. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40213. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40214. }
  40215. #endif
  40216. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  40217. err = wolfSSL_get_error(ssl, 0);
  40218. } while (err == WC_PENDING_E);
  40219. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  40220. #ifdef WOLFSSL_ASYNC_CRYPT
  40221. err = 0; /* Reset error */
  40222. #endif
  40223. do {
  40224. #ifdef WOLFSSL_ASYNC_CRYPT
  40225. if (err == WC_PENDING_E) {
  40226. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40227. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40228. }
  40229. #endif
  40230. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  40231. err = wolfSSL_get_error(ssl, 0);
  40232. } while (err == WC_PENDING_E);
  40233. ExpectIntEQ(ret, 23);
  40234. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  40235. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  40236. #ifdef HAVE_EXT_CACHE
  40237. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  40238. * HAVE_EXT_CACHE we get new objects each time */
  40239. #endif
  40240. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  40241. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  40242. sess = wolfSSL_get_session(ssl);
  40243. #ifdef OPENSSL_EXTRA
  40244. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  40245. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  40246. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  40247. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  40248. #ifdef HAVE_SESSION_TICKET
  40249. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  40250. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  40251. SESSION_TICKET_HINT_DEFAULT);
  40252. #else
  40253. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  40254. #endif
  40255. #else
  40256. (void)sess;
  40257. #endif /* OPENSSL_EXTRA */
  40258. /* Retain copy of the session for later testing */
  40259. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  40260. wolfSSL_shutdown(ssl);
  40261. wolfSSL_free(ssl); ssl = NULL;
  40262. CloseSocket(sockfd);
  40263. join_thread(serverThread);
  40264. FreeTcpReady(&ready);
  40265. #ifdef WOLFSSL_TIRTOS
  40266. fdOpenSession(Task_self());
  40267. #endif
  40268. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  40269. {
  40270. X509 *x509 = NULL;
  40271. char buf[30];
  40272. int bufSz = 0;
  40273. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  40274. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  40275. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  40276. sizeof(buf))), 0);
  40277. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  40278. if (bufSz == 7) {
  40279. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  40280. }
  40281. if (bufSz == 16) {
  40282. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  40283. }
  40284. }
  40285. #endif
  40286. #ifdef HAVE_EXT_CACHE
  40287. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  40288. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  40289. sess_copy = NULL;
  40290. #endif
  40291. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  40292. /* get session from DER and update the timeout */
  40293. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  40294. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  40295. wolfSSL_SESSION_free(sess); sess = NULL;
  40296. sess = NULL;
  40297. ptr = sessDer;
  40298. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  40299. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  40300. (const unsigned char**)&ptr, sz));
  40301. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  40302. sessDer = NULL;
  40303. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  40304. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  40305. #endif
  40306. /* successful set session test */
  40307. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40308. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  40309. #ifdef HAVE_SESSION_TICKET
  40310. /* Test set/get session ticket */
  40311. {
  40312. const char* ticket = "This is a session ticket";
  40313. char buf[64] = {0};
  40314. word32 bufSz = (word32)sizeof(buf);
  40315. ExpectIntEQ(SSL_SUCCESS,
  40316. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  40317. (word32)XSTRLEN(ticket)));
  40318. ExpectIntEQ(SSL_SUCCESS,
  40319. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  40320. ExpectStrEQ(ticket, buf);
  40321. }
  40322. #endif
  40323. #ifdef OPENSSL_EXTRA
  40324. /* session timeout case */
  40325. /* make the session to be expired */
  40326. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  40327. XSLEEP_MS(1200);
  40328. /* SSL_set_session should reject specified session but return success
  40329. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  40330. */
  40331. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40332. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  40333. #else
  40334. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  40335. #endif
  40336. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  40337. #ifdef WOLFSSL_SESSION_ID_CTX
  40338. /* fail case with miss match session context IDs (use compatibility API) */
  40339. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  40340. SSL_SUCCESS);
  40341. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  40342. wolfSSL_free(ssl); ssl = NULL;
  40343. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  40344. SSL_FAILURE);
  40345. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  40346. SSL_SUCCESS);
  40347. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40348. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  40349. #endif
  40350. #endif /* OPENSSL_EXTRA */
  40351. wolfSSL_free(ssl);
  40352. wolfSSL_SESSION_free(sess);
  40353. wolfSSL_CTX_free(ctx);
  40354. #endif
  40355. return EXPECT_RESULT();
  40356. }
  40357. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40358. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40359. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  40360. !defined(WOLFSSL_NO_TLS12)
  40361. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  40362. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  40363. {
  40364. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  40365. /* returns previous timeout value */
  40366. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  40367. #else
  40368. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  40369. #endif
  40370. }
  40371. /* set the session to timeout in a second */
  40372. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  40373. {
  40374. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  40375. }
  40376. /* store the client side session from the first successful connection */
  40377. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  40378. {
  40379. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  40380. NULL); /* ref count 1 */
  40381. }
  40382. /* wait till session is expired then set it in the WOLFSSL struct for use */
  40383. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  40384. {
  40385. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  40386. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40387. WOLFSSL_SUCCESS);
  40388. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  40389. }
  40390. /* set expired session in the WOLFSSL struct for use */
  40391. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  40392. {
  40393. XSLEEP_MS(1200); /* wait a second for session to expire */
  40394. /* set the expired session, call to set session fails but continuing on
  40395. after failure should be handled here */
  40396. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40397. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40398. WOLFSSL_SUCCESS);
  40399. #else
  40400. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40401. WOLFSSL_SUCCESS);
  40402. #endif
  40403. }
  40404. /* check that the expired session was not reused */
  40405. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  40406. {
  40407. /* since the session has expired it should not have been reused */
  40408. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  40409. }
  40410. #endif
  40411. static int test_wolfSSL_SESSION_expire_downgrade(void)
  40412. {
  40413. EXPECT_DECLS;
  40414. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40415. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40416. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  40417. !defined(WOLFSSL_NO_TLS12)
  40418. WOLFSSL_CTX* ctx = NULL;
  40419. callback_functions server_cbf, client_cbf;
  40420. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  40421. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  40422. /* force server side to use TLS 1.2 */
  40423. server_cbf.ctx = ctx;
  40424. server_cbf.method = wolfTLSv1_2_server_method;
  40425. client_cbf.method = wolfSSLv23_client_method;
  40426. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40427. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  40428. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  40429. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40430. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40431. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40432. /* set the previously created session and wait till expired */
  40433. server_cbf.ctx = ctx;
  40434. client_cbf.method = wolfSSLv23_client_method;
  40435. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40436. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  40437. client_cbf.on_result =
  40438. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  40439. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40440. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40441. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40442. /* set the previously created expired session */
  40443. server_cbf.ctx = ctx;
  40444. client_cbf.method = wolfSSLv23_client_method;
  40445. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40446. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  40447. client_cbf.on_result =
  40448. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  40449. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40450. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40451. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40452. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  40453. wolfSSL_CTX_free(ctx);
  40454. #endif
  40455. return EXPECT_RESULT();
  40456. }
  40457. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  40458. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  40459. static int clientSessRemCountMalloc = 0;
  40460. static int serverSessRemCountMalloc = 0;
  40461. static int clientSessRemCountFree = 0;
  40462. static int serverSessRemCountFree = 0;
  40463. static WOLFSSL_CTX* serverSessCtx = NULL;
  40464. static WOLFSSL_SESSION* serverSess = NULL;
  40465. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40466. !defined(NO_SESSION_CACHE_REF)
  40467. static WOLFSSL_CTX* clientSessCtx = NULL;
  40468. static WOLFSSL_SESSION* clientSess = NULL;
  40469. #endif
  40470. static int serverSessRemIdx = 3;
  40471. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  40472. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  40473. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  40474. {
  40475. int* side;
  40476. (void)ctx;
  40477. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  40478. if (side != NULL) {
  40479. if (*side == WOLFSSL_CLIENT_END)
  40480. clientSessRemCountFree++;
  40481. else
  40482. serverSessRemCountFree++;
  40483. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  40484. }
  40485. }
  40486. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  40487. {
  40488. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  40489. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  40490. !defined(NO_SESSION_CACHE_REF)
  40491. {
  40492. EXPECT_DECLS;
  40493. /* Allow downgrade, set min version, and disable TLS 1.3.
  40494. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  40495. * reference to the session cache. But with WOLFSSL_TLS13 and without
  40496. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  40497. * the session in the cache. In this case we need to downgrade to
  40498. * previous versions to just use the legacy session ID field. */
  40499. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  40500. SSL_SUCCESS);
  40501. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  40502. SSL_SUCCESS);
  40503. return EXPECT_RESULT();
  40504. }
  40505. #else
  40506. return TEST_SUCCESS;
  40507. #endif
  40508. }
  40509. static int SessRemSslSetupCb(WOLFSSL* ssl)
  40510. {
  40511. EXPECT_DECLS;
  40512. int* side;
  40513. if (SSL_is_server(ssl)) {
  40514. side = &sessRemCtx_Server;
  40515. serverSessRemCountMalloc++;
  40516. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  40517. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  40518. SSL_SUCCESS);
  40519. }
  40520. else {
  40521. side = &sessRemCtx_Client;
  40522. clientSessRemCountMalloc++;
  40523. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40524. !defined(NO_SESSION_CACHE_REF)
  40525. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  40526. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  40527. SSL_SUCCESS);
  40528. #endif
  40529. }
  40530. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  40531. serverSessRemIdx, side), SSL_SUCCESS);
  40532. return EXPECT_RESULT();
  40533. }
  40534. #endif
  40535. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  40536. {
  40537. EXPECT_DECLS;
  40538. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  40539. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  40540. /* Check that the remove callback gets called for external data in a
  40541. * session object */
  40542. test_ssl_cbf func_cb;
  40543. XMEMSET(&func_cb, 0, sizeof(func_cb));
  40544. func_cb.ctx_ready = SessRemCtxSetupCb;
  40545. func_cb.on_result = SessRemSslSetupCb;
  40546. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  40547. NULL), TEST_SUCCESS);
  40548. /* Both should have been allocated */
  40549. ExpectIntEQ(clientSessRemCountMalloc, 1);
  40550. ExpectIntEQ(serverSessRemCountMalloc, 1);
  40551. /* This should not be called yet. Session wasn't evicted from cache yet. */
  40552. ExpectIntEQ(clientSessRemCountFree, 0);
  40553. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40554. !defined(NO_SESSION_CACHE_REF)
  40555. /* Force a cache lookup */
  40556. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  40557. /* Force a cache update */
  40558. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  40559. /* This should set the timeout to 0 and call the remove callback from within
  40560. * the session cache. */
  40561. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  40562. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  40563. ExpectIntEQ(clientSessRemCountFree, 1);
  40564. #endif
  40565. /* Server session is in the cache so ex_data isn't free'd with the SSL
  40566. * object */
  40567. ExpectIntEQ(serverSessRemCountFree, 0);
  40568. /* Force a cache lookup */
  40569. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  40570. /* Force a cache update */
  40571. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  40572. /* This should set the timeout to 0 and call the remove callback from within
  40573. * the session cache. */
  40574. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  40575. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  40576. ExpectIntEQ(serverSessRemCountFree, 1);
  40577. /* Need to free the references that we kept */
  40578. SSL_CTX_free(serverSessCtx);
  40579. SSL_SESSION_free(serverSess);
  40580. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40581. !defined(NO_SESSION_CACHE_REF)
  40582. SSL_CTX_free(clientSessCtx);
  40583. SSL_SESSION_free(clientSess);
  40584. #endif
  40585. #endif
  40586. return EXPECT_RESULT();
  40587. }
  40588. static int test_wolfSSL_ticket_keys(void)
  40589. {
  40590. EXPECT_DECLS;
  40591. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  40592. !defined(NO_WOLFSSL_SERVER)
  40593. WOLFSSL_CTX* ctx = NULL;
  40594. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  40595. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  40596. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  40597. WOLFSSL_FAILURE);
  40598. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  40599. WOLFSSL_FAILURE);
  40600. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  40601. WOLFSSL_FAILURE);
  40602. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  40603. WOLFSSL_FAILURE);
  40604. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  40605. WOLFSSL_FAILURE);
  40606. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  40607. WOLFSSL_FAILURE);
  40608. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  40609. WOLFSSL_FAILURE);
  40610. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  40611. WOLFSSL_FAILURE);
  40612. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  40613. WOLFSSL_FAILURE);
  40614. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  40615. WOLFSSL_FAILURE);
  40616. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  40617. WOLFSSL_FAILURE);
  40618. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  40619. WOLFSSL_FAILURE);
  40620. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  40621. WOLFSSL_FAILURE);
  40622. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  40623. WOLFSSL_FAILURE);
  40624. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  40625. WOLFSSL_SUCCESS);
  40626. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  40627. WOLFSSL_SUCCESS);
  40628. wolfSSL_CTX_free(ctx);
  40629. #endif
  40630. return EXPECT_RESULT();
  40631. }
  40632. #ifndef NO_BIO
  40633. static int test_wolfSSL_d2i_PUBKEY(void)
  40634. {
  40635. EXPECT_DECLS;
  40636. #if defined(OPENSSL_EXTRA)
  40637. BIO* bio = NULL;
  40638. EVP_PKEY* pkey = NULL;
  40639. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40640. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  40641. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  40642. /* RSA PUBKEY test */
  40643. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  40644. sizeof_client_keypub_der_2048), 0);
  40645. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40646. EVP_PKEY_free(pkey);
  40647. pkey = NULL;
  40648. #endif
  40649. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  40650. /* ECC PUBKEY test */
  40651. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  40652. sizeof_ecc_clikeypub_der_256), 0);
  40653. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40654. EVP_PKEY_free(pkey);
  40655. pkey = NULL;
  40656. #endif
  40657. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  40658. /* DSA PUBKEY test */
  40659. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  40660. sizeof_dsa_pub_key_der_2048), 0);
  40661. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40662. EVP_PKEY_free(pkey);
  40663. pkey = NULL;
  40664. #endif
  40665. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  40666. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  40667. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  40668. (HAVE_FIPS_VERSION > 2))
  40669. /* DH PUBKEY test */
  40670. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  40671. sizeof_dh_pub_key_der_2048), 0);
  40672. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40673. EVP_PKEY_free(pkey);
  40674. pkey = NULL;
  40675. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  40676. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  40677. BIO_free(bio);
  40678. (void)pkey;
  40679. #endif
  40680. return EXPECT_RESULT();
  40681. }
  40682. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  40683. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  40684. {
  40685. EXPECT_DECLS;
  40686. BIO* bio = NULL;
  40687. EVP_PKEY* pkey = NULL;
  40688. WOLFSSL_CTX* ctx = NULL;
  40689. #if defined(WOLFSSL_KEY_GEN)
  40690. unsigned char buff[4096];
  40691. unsigned char* bufPtr = buff;
  40692. #endif
  40693. /* test creating new EVP_PKEY with bad arg */
  40694. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  40695. /* test loading RSA key using BIO */
  40696. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  40697. {
  40698. XFILE file = XBADFILE;
  40699. const char* fname = "./certs/server-key.der";
  40700. size_t sz = 0;
  40701. byte* buf = NULL;
  40702. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  40703. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  40704. ExpectTrue((sz = XFTELL(file)) != 0);
  40705. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  40706. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  40707. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  40708. if (file != XBADFILE) {
  40709. XFCLOSE(file);
  40710. }
  40711. /* Test using BIO new mem and loading DER private key */
  40712. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  40713. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  40714. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  40715. BIO_free(bio);
  40716. bio = NULL;
  40717. EVP_PKEY_free(pkey);
  40718. pkey = NULL;
  40719. }
  40720. #endif
  40721. /* test loading ECC key using BIO */
  40722. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  40723. {
  40724. XFILE file = XBADFILE;
  40725. const char* fname = "./certs/ecc-key.der";
  40726. size_t sz = 0;
  40727. byte* buf = NULL;
  40728. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  40729. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  40730. ExpectTrue((sz = XFTELL(file)) != 0);
  40731. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  40732. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  40733. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  40734. if (file != XBADFILE)
  40735. XFCLOSE(file);
  40736. /* Test using BIO new mem and loading DER private key */
  40737. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  40738. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  40739. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  40740. BIO_free(bio);
  40741. bio = NULL;
  40742. EVP_PKEY_free(pkey);
  40743. pkey = NULL;
  40744. }
  40745. #endif
  40746. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40747. #ifndef NO_WOLFSSL_SERVER
  40748. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  40749. #else
  40750. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  40751. #endif
  40752. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  40753. {
  40754. RSA* rsa = NULL;
  40755. /* Tests bad parameters */
  40756. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  40757. /* RSA not set yet, expecting to fail*/
  40758. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  40759. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  40760. /* set RSA using bio*/
  40761. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  40762. sizeof_client_key_der_2048), 0);
  40763. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  40764. ExpectNotNull(rsa);
  40765. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  40766. /* i2d RSAprivate key tests */
  40767. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  40768. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  40769. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  40770. sizeof_client_key_der_2048);
  40771. bufPtr -= sizeof_client_key_der_2048;
  40772. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  40773. sizeof_client_key_der_2048), 0);
  40774. bufPtr = NULL;
  40775. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  40776. sizeof_client_key_der_2048);
  40777. ExpectNotNull(bufPtr);
  40778. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  40779. sizeof_client_key_der_2048), 0);
  40780. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  40781. RSA_free(rsa);
  40782. rsa = RSA_new();
  40783. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  40784. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  40785. RSA_free(rsa);
  40786. }
  40787. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  40788. SSL_CTX_free(ctx);
  40789. ctx = NULL;
  40790. BIO_free(bio);
  40791. bio = NULL;
  40792. return EXPECT_RESULT();
  40793. }
  40794. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  40795. #endif /* !NO_BIO */
  40796. static int test_wolfSSL_sk_GENERAL_NAME(void)
  40797. {
  40798. EXPECT_DECLS;
  40799. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40800. !defined(NO_RSA)
  40801. X509* x509 = NULL;
  40802. GENERAL_NAME* gn = NULL;
  40803. unsigned char buf[4096];
  40804. const unsigned char* bufPt = NULL;
  40805. int bytes = 0;
  40806. int i;
  40807. int j;
  40808. XFILE f = XBADFILE;
  40809. STACK_OF(GENERAL_NAME)* sk = NULL;
  40810. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  40811. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40812. if (f != XBADFILE)
  40813. XFCLOSE(f);
  40814. for (j = 0; j < 2; ++j) {
  40815. bufPt = buf;
  40816. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40817. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40818. NID_subject_alt_name, NULL, NULL));
  40819. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  40820. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  40821. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  40822. if (gn != NULL) {
  40823. switch (gn->type) {
  40824. case GEN_DNS:
  40825. fprintf(stderr, "found type GEN_DNS\n");
  40826. break;
  40827. case GEN_EMAIL:
  40828. fprintf(stderr, "found type GEN_EMAIL\n");
  40829. break;
  40830. case GEN_URI:
  40831. fprintf(stderr, "found type GEN_URI\n");
  40832. break;
  40833. }
  40834. }
  40835. }
  40836. X509_free(x509);
  40837. x509 = NULL;
  40838. if (j == 0) {
  40839. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40840. }
  40841. else {
  40842. /*
  40843. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  40844. * it was supposed to. This is a regression test for that bug.
  40845. */
  40846. GENERAL_NAMES_free(sk);
  40847. }
  40848. sk = NULL;
  40849. }
  40850. #endif
  40851. return EXPECT_RESULT();
  40852. }
  40853. static int test_wolfSSL_GENERAL_NAME_print(void)
  40854. {
  40855. EXPECT_DECLS;
  40856. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  40857. X509* x509 = NULL;
  40858. GENERAL_NAME* gn = NULL;
  40859. unsigned char buf[4096];
  40860. const unsigned char* bufPt = NULL;
  40861. int bytes;
  40862. XFILE f = XBADFILE;
  40863. STACK_OF(GENERAL_NAME)* sk = NULL;
  40864. BIO* out = NULL;
  40865. unsigned char outbuf[128];
  40866. X509_EXTENSION* ext = NULL;
  40867. AUTHORITY_INFO_ACCESS* aia = NULL;
  40868. ACCESS_DESCRIPTION* ad = NULL;
  40869. ASN1_IA5STRING *dnsname = NULL;
  40870. const unsigned char v4Addr[] = {192,168,53,1};
  40871. const unsigned char v6Addr[] =
  40872. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  40873. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  40874. const unsigned char email[] =
  40875. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  40876. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  40877. const char* dnsStr = "DNS:example.com";
  40878. const char* uriStr = "URI:http://127.0.0.1:22220";
  40879. const char* v4addStr = "IP Address:192.168.53.1";
  40880. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  40881. const char* emailStr = "email:info@wolfssl.com";
  40882. const char* othrStr = "othername:<unsupported>";
  40883. const char* x400Str = "X400Name:<unsupported>";
  40884. const char* ediStr = "EdiPartyName:<unsupported>";
  40885. /* BIO to output */
  40886. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  40887. /* test for NULL param */
  40888. gn = NULL;
  40889. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  40890. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  40891. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  40892. /* test for GEN_DNS */
  40893. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  40894. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40895. if (f != XBADFILE) {
  40896. XFCLOSE(f);
  40897. f = XBADFILE;
  40898. }
  40899. bufPt = buf;
  40900. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40901. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40902. NID_subject_alt_name, NULL, NULL));
  40903. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  40904. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40905. XMEMSET(outbuf, 0, sizeof(outbuf));
  40906. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40907. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40908. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40909. gn = NULL;
  40910. sk = NULL;
  40911. X509_free(x509);
  40912. x509 = NULL;
  40913. /* Lets test for setting as well. */
  40914. ExpectNotNull(gn = GENERAL_NAME_new());
  40915. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  40916. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  40917. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  40918. dnsname = NULL;
  40919. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40920. XMEMSET(outbuf, 0, sizeof(outbuf));
  40921. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40922. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40923. GENERAL_NAME_free(gn);
  40924. /* test for GEN_URI */
  40925. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  40926. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40927. if (f != XBADFILE) {
  40928. XFCLOSE(f);
  40929. f = XBADFILE;
  40930. }
  40931. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  40932. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  40933. ext));
  40934. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  40935. if (ad != NULL) {
  40936. gn = ad->location;
  40937. }
  40938. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40939. gn = NULL;
  40940. XMEMSET(outbuf,0,sizeof(outbuf));
  40941. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40942. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  40943. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40944. aia = NULL;
  40945. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  40946. ExpectNotNull(aia);
  40947. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  40948. aia = NULL;
  40949. X509_free(x509);
  40950. x509 = NULL;
  40951. /* test for GEN_IPADD */
  40952. /* ip v4 address */
  40953. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40954. if (gn != NULL) {
  40955. gn->type = GEN_IPADD;
  40956. if (gn->d.iPAddress != NULL) {
  40957. gn->d.iPAddress->length = sizeof(v4Addr);
  40958. }
  40959. }
  40960. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  40961. sizeof(v4Addr)), 1);
  40962. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40963. XMEMSET(outbuf,0,sizeof(outbuf));
  40964. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40965. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  40966. GENERAL_NAME_free(gn);
  40967. gn = NULL;
  40968. /* ip v6 address */
  40969. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40970. if (gn != NULL) {
  40971. gn->type = GEN_IPADD;
  40972. if (gn->d.iPAddress != NULL) {
  40973. gn->d.iPAddress->length = sizeof(v6Addr);
  40974. }
  40975. }
  40976. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  40977. sizeof(v6Addr)), 1);
  40978. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40979. XMEMSET(outbuf,0,sizeof(outbuf));
  40980. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40981. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  40982. GENERAL_NAME_free(gn);
  40983. gn = NULL;
  40984. /* test for GEN_EMAIL */
  40985. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40986. if (gn != NULL) {
  40987. gn->type = GEN_EMAIL;
  40988. if (gn->d.rfc822Name != NULL) {
  40989. gn->d.rfc822Name->length = sizeof(email);
  40990. }
  40991. }
  40992. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  40993. 1);
  40994. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40995. XMEMSET(outbuf,0,sizeof(outbuf));
  40996. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40997. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  40998. GENERAL_NAME_free(gn);
  40999. gn = NULL;
  41000. /* test for GEN_OTHERNAME */
  41001. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41002. if (gn != NULL) {
  41003. gn->type = GEN_OTHERNAME;
  41004. }
  41005. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41006. XMEMSET(outbuf,0,sizeof(outbuf));
  41007. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41008. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  41009. GENERAL_NAME_free(gn);
  41010. gn = NULL;
  41011. /* test for GEN_X400 */
  41012. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41013. if (gn != NULL) {
  41014. gn->type = GEN_X400;
  41015. }
  41016. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41017. XMEMSET(outbuf,0,sizeof(outbuf));
  41018. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41019. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  41020. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  41021. if (gn != NULL) {
  41022. gn->type = GEN_IA5;
  41023. }
  41024. GENERAL_NAME_free(gn);
  41025. gn = NULL;
  41026. /* test for GEN_EDIPARTY */
  41027. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41028. if (gn != NULL) {
  41029. gn->type = GEN_EDIPARTY;
  41030. }
  41031. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41032. XMEMSET(outbuf,0,sizeof(outbuf));
  41033. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41034. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  41035. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  41036. if (gn != NULL) {
  41037. gn->type = GEN_IA5;
  41038. }
  41039. GENERAL_NAME_free(gn);
  41040. gn = NULL;
  41041. BIO_free(out);
  41042. #endif /* OPENSSL_ALL */
  41043. return EXPECT_RESULT();
  41044. }
  41045. static int test_wolfSSL_sk_DIST_POINT(void)
  41046. {
  41047. EXPECT_DECLS;
  41048. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41049. !defined(NO_RSA)
  41050. X509* x509 = NULL;
  41051. unsigned char buf[4096];
  41052. const unsigned char* bufPt;
  41053. int bytes = 0;
  41054. int i = 0;
  41055. int j = 0;
  41056. XFILE f = XBADFILE;
  41057. DIST_POINT* dp = NULL;
  41058. DIST_POINT_NAME* dpn = NULL;
  41059. GENERAL_NAME* gn = NULL;
  41060. ASN1_IA5STRING* uri = NULL;
  41061. STACK_OF(DIST_POINT)* dps = NULL;
  41062. STACK_OF(GENERAL_NAME)* gns = NULL;
  41063. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  41064. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  41065. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41066. if (f != XBADFILE)
  41067. XFCLOSE(f);
  41068. bufPt = buf;
  41069. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41070. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  41071. NID_crl_distribution_points, NULL, NULL));
  41072. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  41073. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  41074. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  41075. ExpectNotNull(dpn = dp->distpoint);
  41076. /* this should be type 0, fullname */
  41077. ExpectIntEQ(dpn->type, 0);
  41078. ExpectNotNull(gns = dp->distpoint->name.fullname);
  41079. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  41080. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  41081. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  41082. ExpectIntEQ(gn->type, GEN_URI);
  41083. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  41084. ExpectNotNull(uri->data);
  41085. ExpectIntGT(uri->length, 0);
  41086. }
  41087. }
  41088. X509_free(x509);
  41089. CRL_DIST_POINTS_free(dps);
  41090. #endif
  41091. return EXPECT_RESULT();
  41092. }
  41093. static int test_wolfSSL_verify_mode(void)
  41094. {
  41095. EXPECT_DECLS;
  41096. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  41097. WOLFSSL* ssl = NULL;
  41098. WOLFSSL_CTX* ctx = NULL;
  41099. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41100. ExpectNotNull(ssl = SSL_new(ctx));
  41101. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41102. SSL_free(ssl);
  41103. ssl = NULL;
  41104. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41105. ExpectNotNull(ssl = SSL_new(ctx));
  41106. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41107. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  41108. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  41109. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  41110. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  41111. SSL_free(ssl);
  41112. ssl = NULL;
  41113. wolfSSL_CTX_set_verify(ctx,
  41114. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  41115. ExpectNotNull(ssl = SSL_new(ctx));
  41116. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41117. ExpectIntEQ(SSL_get_verify_mode(ssl),
  41118. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41119. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  41120. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  41121. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41122. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  41123. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  41124. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  41125. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  41126. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41127. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  41128. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  41129. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  41130. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  41131. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  41132. #endif
  41133. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  41134. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41135. SSL_free(ssl);
  41136. SSL_CTX_free(ctx);
  41137. #endif
  41138. return EXPECT_RESULT();
  41139. }
  41140. static int test_wolfSSL_verify_depth(void)
  41141. {
  41142. EXPECT_DECLS;
  41143. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41144. WOLFSSL* ssl = NULL;
  41145. WOLFSSL_CTX* ctx = NULL;
  41146. long depth;
  41147. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41148. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  41149. ExpectNotNull(ssl = SSL_new(ctx));
  41150. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  41151. SSL_free(ssl);
  41152. ssl = NULL;
  41153. SSL_CTX_set_verify_depth(ctx, -1);
  41154. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  41155. SSL_CTX_set_verify_depth(ctx, 2);
  41156. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  41157. ExpectNotNull(ssl = SSL_new(ctx));
  41158. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  41159. SSL_free(ssl);
  41160. SSL_CTX_free(ctx);
  41161. #endif
  41162. return EXPECT_RESULT();
  41163. }
  41164. static int test_wolfSSL_verify_result(void)
  41165. {
  41166. EXPECT_DECLS;
  41167. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  41168. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  41169. WOLFSSL* ssl = NULL;
  41170. WOLFSSL_CTX* ctx = NULL;
  41171. long result = 0xDEADBEEF;
  41172. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  41173. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41174. ExpectNotNull(ssl = SSL_new(ctx));
  41175. wolfSSL_set_verify_result(ssl, result);
  41176. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  41177. SSL_free(ssl);
  41178. SSL_CTX_free(ctx);
  41179. #endif
  41180. return EXPECT_RESULT();
  41181. }
  41182. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41183. static void sslMsgCb(int w, int version, int type, const void* buf,
  41184. size_t sz, SSL* ssl, void* arg)
  41185. {
  41186. int i;
  41187. unsigned char* pt = (unsigned char*)buf;
  41188. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  41189. (w)?"Writing":"Reading", (int)sz, version, type);
  41190. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  41191. fprintf(stderr, "\n");
  41192. (void)ssl;
  41193. (void)arg;
  41194. }
  41195. #endif /* OPENSSL_EXTRA */
  41196. static int test_wolfSSL_msg_callback(void)
  41197. {
  41198. EXPECT_DECLS;
  41199. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41200. WOLFSSL* ssl = NULL;
  41201. WOLFSSL_CTX* ctx = NULL;
  41202. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41203. ExpectNotNull(ssl = SSL_new(ctx));
  41204. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  41205. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  41206. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  41207. SSL_free(ssl);
  41208. SSL_CTX_free(ctx);
  41209. #endif
  41210. return EXPECT_RESULT();
  41211. }
  41212. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  41213. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  41214. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  41215. static void binary_dump(void *ptr, int size)
  41216. {
  41217. #ifdef WOLFSSL_EVP_PRINT
  41218. int i = 0;
  41219. unsigned char *p = (unsigned char *) ptr;
  41220. fprintf(stderr, "{");
  41221. while ((p != NULL) && (i < size)) {
  41222. if ((i % 8) == 0) {
  41223. fprintf(stderr, "\n");
  41224. fprintf(stderr, " ");
  41225. }
  41226. fprintf(stderr, "0x%02x, ", p[i]);
  41227. i++;
  41228. }
  41229. fprintf(stderr, "\n};\n");
  41230. #else
  41231. (void) ptr;
  41232. (void) size;
  41233. #endif
  41234. }
  41235. static int last_val = 0x0f;
  41236. static int check_result(unsigned char *data, int len)
  41237. {
  41238. int i;
  41239. for ( ; len; ) {
  41240. last_val = (last_val + 1) % 16;
  41241. for (i = 0; i < 16; len--, i++, data++)
  41242. if (*data != last_val) {
  41243. return -1;
  41244. }
  41245. }
  41246. return 0;
  41247. }
  41248. static int r_offset;
  41249. static int w_offset;
  41250. static void init_offset(void)
  41251. {
  41252. r_offset = 0;
  41253. w_offset = 0;
  41254. }
  41255. static void get_record(unsigned char *data, unsigned char *buf, int len)
  41256. {
  41257. XMEMCPY(buf, data+r_offset, len);
  41258. r_offset += len;
  41259. }
  41260. static void set_record(unsigned char *data, unsigned char *buf, int len)
  41261. {
  41262. XMEMCPY(data+w_offset, buf, len);
  41263. w_offset += len;
  41264. }
  41265. static void set_plain(unsigned char *plain, int rec)
  41266. {
  41267. int i, j;
  41268. unsigned char *p = plain;
  41269. #define BLOCKSZ 16
  41270. for (i=0; i<(rec/BLOCKSZ); i++) {
  41271. for (j=0; j<BLOCKSZ; j++)
  41272. *p++ = (i % 16);
  41273. }
  41274. }
  41275. #endif
  41276. static int test_wolfSSL_EVP_Cipher_extra(void)
  41277. {
  41278. EXPECT_DECLS;
  41279. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  41280. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  41281. /* aes128-cbc, keylen=16, ivlen=16 */
  41282. byte aes128_cbc_key[] = {
  41283. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  41284. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  41285. };
  41286. byte aes128_cbc_iv[] = {
  41287. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  41288. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  41289. };
  41290. /* teset data size table */
  41291. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  41292. int test_drive2[] = {8, 3, 8, 512, 0};
  41293. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  41294. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  41295. int test_drive_len[100];
  41296. int ret = 0;
  41297. EVP_CIPHER_CTX *evp = NULL;
  41298. int ilen = 0;
  41299. int klen = 0;
  41300. int i, j;
  41301. const EVP_CIPHER *type;
  41302. byte *iv;
  41303. byte *key;
  41304. int ivlen;
  41305. int keylen;
  41306. #define RECORDS 16
  41307. #define BUFFSZ 512
  41308. byte plain [BUFFSZ * RECORDS];
  41309. byte cipher[BUFFSZ * RECORDS];
  41310. byte inb[BUFFSZ];
  41311. byte outb[BUFFSZ+16];
  41312. int outl = 0;
  41313. int inl;
  41314. iv = aes128_cbc_iv;
  41315. ivlen = sizeof(aes128_cbc_iv);
  41316. key = aes128_cbc_key;
  41317. keylen = sizeof(aes128_cbc_key);
  41318. type = EVP_aes_128_cbc();
  41319. set_plain(plain, BUFFSZ * RECORDS);
  41320. SSL_library_init();
  41321. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  41322. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41323. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41324. klen = EVP_CIPHER_CTX_key_length(evp);
  41325. if (klen > 0 && keylen != klen) {
  41326. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41327. }
  41328. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41329. if (ilen > 0 && ivlen != ilen) {
  41330. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41331. }
  41332. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41333. for (j = 0; j<RECORDS; j++)
  41334. {
  41335. inl = BUFFSZ;
  41336. get_record(plain, inb, inl);
  41337. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  41338. set_record(cipher, outb, outl);
  41339. }
  41340. for (i = 0; test_drive[i]; i++) {
  41341. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41342. init_offset();
  41343. test_drive_len[i] = 0;
  41344. for (j = 0; test_drive[i][j]; j++)
  41345. {
  41346. inl = test_drive[i][j];
  41347. test_drive_len[i] += inl;
  41348. get_record(plain, inb, inl);
  41349. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  41350. 0);
  41351. /* output to cipher buffer, so that following Dec test can detect
  41352. if any error */
  41353. set_record(cipher, outb, outl);
  41354. }
  41355. EVP_CipherFinal(evp, outb, &outl);
  41356. if (outl > 0)
  41357. set_record(cipher, outb, outl);
  41358. }
  41359. for (i = 0; test_drive[i]; i++) {
  41360. last_val = 0x0f;
  41361. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  41362. init_offset();
  41363. for (j = 0; test_drive[i][j]; j++) {
  41364. inl = test_drive[i][j];
  41365. get_record(cipher, inb, inl);
  41366. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  41367. 0);
  41368. binary_dump(outb, outl);
  41369. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  41370. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  41371. }
  41372. ret = EVP_CipherFinal(evp, outb, &outl);
  41373. binary_dump(outb, outl);
  41374. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  41375. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  41376. ExpectTrue(ret);
  41377. }
  41378. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  41379. EVP_CIPHER_CTX_free(evp);
  41380. evp = NULL;
  41381. /* Do an extra test to verify correct behavior with empty input. */
  41382. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  41383. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41384. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41385. klen = EVP_CIPHER_CTX_key_length(evp);
  41386. if (klen > 0 && keylen != klen) {
  41387. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41388. }
  41389. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41390. if (ilen > 0 && ivlen != ilen) {
  41391. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41392. }
  41393. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41394. /* outl should be set to 0 after passing NULL, 0 for input args. */
  41395. outl = -1;
  41396. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  41397. ExpectIntEQ(outl, 0);
  41398. EVP_CIPHER_CTX_free(evp);
  41399. #endif /* test_EVP_Cipher */
  41400. return EXPECT_RESULT();
  41401. }
  41402. static int test_wolfSSL_PEM_read_DHparams(void)
  41403. {
  41404. EXPECT_DECLS;
  41405. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  41406. !defined(NO_FILESYSTEM)
  41407. DH* dh = NULL;
  41408. XFILE fp = XBADFILE;
  41409. unsigned char derOut[300];
  41410. unsigned char* derOutBuf = derOut;
  41411. int derOutSz = 0;
  41412. unsigned char derExpected[300];
  41413. int derExpectedSz = 0;
  41414. XMEMSET(derOut, 0, sizeof(derOut));
  41415. XMEMSET(derExpected, 0, sizeof(derExpected));
  41416. /* open DH param file, read into DH struct */
  41417. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  41418. /* bad args */
  41419. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  41420. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  41421. /* good args */
  41422. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41423. if (fp != XBADFILE) {
  41424. XFCLOSE(fp);
  41425. fp = XBADFILE;
  41426. }
  41427. /* read in certs/dh2048.der for comparison against exported params */
  41428. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  41429. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  41430. fp), 0);
  41431. if (fp != XBADFILE) {
  41432. XFCLOSE(fp);
  41433. fp = XBADFILE;
  41434. }
  41435. /* export DH back to DER and compare */
  41436. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  41437. ExpectIntEQ(derOutSz, derExpectedSz);
  41438. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  41439. DH_free(dh);
  41440. dh = NULL;
  41441. /* Test parsing with X9.42 header */
  41442. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  41443. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41444. if (fp != XBADFILE)
  41445. XFCLOSE(fp);
  41446. DH_free(dh);
  41447. dh = NULL;
  41448. #endif
  41449. return EXPECT_RESULT();
  41450. }
  41451. static int test_wolfSSL_X509_get_serialNumber(void)
  41452. {
  41453. EXPECT_DECLS;
  41454. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  41455. ASN1_INTEGER* a = NULL;
  41456. BIGNUM* bn = NULL;
  41457. X509* x509 = NULL;
  41458. char *serialHex = NULL;
  41459. byte serial[3];
  41460. int serialSz;
  41461. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41462. SSL_FILETYPE_PEM));
  41463. ExpectNotNull(a = X509_get_serialNumber(x509));
  41464. /* check on value of ASN1 Integer */
  41465. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  41466. a = NULL;
  41467. /* test setting serial number and then retrieving it */
  41468. ExpectNotNull(a = ASN1_INTEGER_new());
  41469. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  41470. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41471. serialSz = sizeof(serial);
  41472. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41473. WOLFSSL_SUCCESS);
  41474. ExpectIntEQ(serialSz, 1);
  41475. ExpectIntEQ(serial[0], 3);
  41476. ASN1_INTEGER_free(a);
  41477. a = NULL;
  41478. /* test setting serial number with 0's in it */
  41479. serial[0] = 0x01;
  41480. serial[1] = 0x00;
  41481. serial[2] = 0x02;
  41482. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  41483. if (a != NULL) {
  41484. a->data[0] = ASN_INTEGER;
  41485. a->data[1] = sizeof(serial);
  41486. XMEMCPY(&a->data[2], serial, sizeof(serial));
  41487. a->length = sizeof(serial) + 2;
  41488. }
  41489. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41490. XMEMSET(serial, 0, sizeof(serial));
  41491. serialSz = sizeof(serial);
  41492. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41493. WOLFSSL_SUCCESS);
  41494. ExpectIntEQ(serialSz, 3);
  41495. ExpectIntEQ(serial[0], 0x01);
  41496. ExpectIntEQ(serial[1], 0x00);
  41497. ExpectIntEQ(serial[2], 0x02);
  41498. ASN1_INTEGER_free(a);
  41499. a = NULL;
  41500. X509_free(x509); /* free's a */
  41501. ExpectNotNull(serialHex = BN_bn2hex(bn));
  41502. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  41503. ExpectStrEQ(serialHex, "01");
  41504. #else
  41505. ExpectStrEQ(serialHex, "1");
  41506. #endif
  41507. OPENSSL_free(serialHex);
  41508. ExpectIntEQ(BN_get_word(bn), 1);
  41509. BN_free(bn);
  41510. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  41511. ExpectNotNull(a = ASN1_INTEGER_new());
  41512. if (a != NULL) {
  41513. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  41514. DYNAMIC_TYPE_OPENSSL));
  41515. a->isDynamic = 1;
  41516. ASN1_INTEGER_free(a);
  41517. }
  41518. #endif
  41519. return EXPECT_RESULT();
  41520. }
  41521. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  41522. {
  41523. EXPECT_DECLS;
  41524. #if defined(OPENSSL_EXTRA)
  41525. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  41526. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  41527. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  41528. #endif
  41529. return EXPECT_RESULT();
  41530. }
  41531. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  41532. {
  41533. EXPECT_DECLS;
  41534. #if defined(OPENSSL_EXTRA)
  41535. #define MAX_HEXSTR_BUFSZ 9
  41536. #define NUM_CASES 5
  41537. struct Output {
  41538. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  41539. long ret;
  41540. };
  41541. int i;
  41542. int j;
  41543. const char* inputs[NUM_CASES] = {
  41544. "aabcd1357e",
  41545. "01:12:23:34:a5:b6:c7:d8:e9",
  41546. ":01:02",
  41547. "012",
  41548. ":ab:ac:d"
  41549. };
  41550. struct Output expectedOutputs[NUM_CASES] = {
  41551. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  41552. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  41553. {{0x01, 0x02}, 2},
  41554. {{0x00}, 0},
  41555. {{0x00}, 0}
  41556. };
  41557. long len = 0;
  41558. unsigned char* returnedBuf = NULL;
  41559. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  41560. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  41561. if (returnedBuf == NULL) {
  41562. ExpectIntEQ(expectedOutputs[i].ret, 0);
  41563. continue;
  41564. }
  41565. ExpectIntEQ(expectedOutputs[i].ret, len);
  41566. for (j = 0; j < len; ++j) {
  41567. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  41568. }
  41569. OPENSSL_free(returnedBuf);
  41570. returnedBuf = NULL;
  41571. }
  41572. #endif
  41573. return EXPECT_RESULT();
  41574. }
  41575. static int test_wolfSSL_X509_CA_num(void)
  41576. {
  41577. EXPECT_DECLS;
  41578. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  41579. defined(HAVE_ECC) && !defined(NO_RSA)
  41580. WOLFSSL_X509_STORE *store = NULL;
  41581. WOLFSSL_X509 *x509_1 = NULL;
  41582. WOLFSSL_X509 *x509_2 = NULL;
  41583. int ca_num = 0;
  41584. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  41585. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41586. WOLFSSL_FILETYPE_PEM));
  41587. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  41588. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  41589. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  41590. WOLFSSL_FILETYPE_PEM));
  41591. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  41592. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  41593. wolfSSL_X509_free(x509_1);
  41594. wolfSSL_X509_free(x509_2);
  41595. wolfSSL_X509_STORE_free(store);
  41596. #endif
  41597. return EXPECT_RESULT();
  41598. }
  41599. static int test_wolfSSL_X509_check_ca(void)
  41600. {
  41601. EXPECT_DECLS;
  41602. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41603. WOLFSSL_X509 *x509 = NULL;
  41604. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41605. WOLFSSL_FILETYPE_PEM));
  41606. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  41607. wolfSSL_X509_free(x509);
  41608. #endif
  41609. return EXPECT_RESULT();
  41610. }
  41611. static int test_wolfSSL_X509_check_ip_asc(void)
  41612. {
  41613. EXPECT_DECLS;
  41614. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41615. WOLFSSL_X509 *x509 = NULL;
  41616. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  41617. WOLFSSL_FILETYPE_PEM));
  41618. #if 0
  41619. /* TODO: add cert gen for testing positive case */
  41620. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  41621. #endif
  41622. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  41623. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  41624. wolfSSL_X509_free(x509);
  41625. #endif
  41626. return EXPECT_RESULT();
  41627. }
  41628. static int test_wolfSSL_make_cert(void)
  41629. {
  41630. EXPECT_DECLS;
  41631. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  41632. defined(WOLFSSL_CERT_EXT)
  41633. int ret = 0;
  41634. Cert cert;
  41635. CertName name;
  41636. RsaKey key;
  41637. WC_RNG rng;
  41638. byte der[FOURK_BUF];
  41639. word32 idx = 0;
  41640. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  41641. #ifdef OPENSSL_EXTRA
  41642. const unsigned char* pt = NULL;
  41643. int certSz = 0;
  41644. X509* x509 = NULL;
  41645. X509_NAME* x509name = NULL;
  41646. X509_NAME_ENTRY* entry = NULL;
  41647. ASN1_STRING* entryValue = NULL;
  41648. #endif
  41649. XMEMSET(&name, 0, sizeof(CertName));
  41650. /* set up cert name */
  41651. XMEMCPY(name.country, "US", sizeof("US"));
  41652. name.countryEnc = CTC_PRINTABLE;
  41653. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  41654. name.stateEnc = CTC_UTF8;
  41655. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  41656. name.localityEnc = CTC_UTF8;
  41657. XMEMCPY(name.sur, "Test", sizeof("Test"));
  41658. name.surEnc = CTC_UTF8;
  41659. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  41660. name.orgEnc = CTC_UTF8;
  41661. XMEMCPY(name.unit, "Development", sizeof("Development"));
  41662. name.unitEnc = CTC_UTF8;
  41663. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  41664. name.commonNameEnc = CTC_UTF8;
  41665. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  41666. name.serialDevEnc = CTC_PRINTABLE;
  41667. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  41668. name.userIdEnc = CTC_PRINTABLE;
  41669. #ifdef WOLFSSL_MULTI_ATTRIB
  41670. #if CTC_MAX_ATTRIB > 2
  41671. {
  41672. NameAttrib* n;
  41673. n = &name.name[0];
  41674. n->id = ASN_DOMAIN_COMPONENT;
  41675. n->type = CTC_UTF8;
  41676. n->sz = sizeof("com");
  41677. XMEMCPY(n->value, "com", sizeof("com"));
  41678. n = &name.name[1];
  41679. n->id = ASN_DOMAIN_COMPONENT;
  41680. n->type = CTC_UTF8;
  41681. n->sz = sizeof("wolfssl");
  41682. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  41683. }
  41684. #endif
  41685. #endif /* WOLFSSL_MULTI_ATTRIB */
  41686. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  41687. #ifndef HAVE_FIPS
  41688. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  41689. #else
  41690. ExpectIntEQ(wc_InitRng(&rng), 0);
  41691. #endif
  41692. /* load test RSA key */
  41693. idx = 0;
  41694. #if defined(USE_CERT_BUFFERS_1024)
  41695. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  41696. sizeof_server_key_der_1024), 0);
  41697. #elif defined(USE_CERT_BUFFERS_2048)
  41698. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  41699. sizeof_server_key_der_2048), 0);
  41700. #else
  41701. /* error case, no RSA key loaded, happens later */
  41702. (void)idx;
  41703. #endif
  41704. XMEMSET(&cert, 0 , sizeof(Cert));
  41705. ExpectIntEQ(wc_InitCert(&cert), 0);
  41706. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  41707. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  41708. cert.serialSz = (int)sizeof(mySerial);
  41709. cert.isCA = 1;
  41710. #ifndef NO_SHA256
  41711. cert.sigType = CTC_SHA256wRSA;
  41712. #else
  41713. cert.sigType = CTC_SHAwRSA;
  41714. #endif
  41715. /* add SKID from the Public Key */
  41716. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  41717. /* add AKID from the Public Key */
  41718. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  41719. ret = 0;
  41720. do {
  41721. #if defined(WOLFSSL_ASYNC_CRYPT)
  41722. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  41723. #endif
  41724. if (ret >= 0) {
  41725. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  41726. }
  41727. } while (ret == WC_PENDING_E);
  41728. ExpectIntGT(ret, 0);
  41729. #ifdef OPENSSL_EXTRA
  41730. /* der holds a certificate with DC's now check X509 parsing of it */
  41731. certSz = ret;
  41732. pt = der;
  41733. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  41734. ExpectNotNull(x509name = X509_get_subject_name(x509));
  41735. #ifdef WOLFSSL_MULTI_ATTRIB
  41736. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41737. -1)), 5);
  41738. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41739. idx)), 6);
  41740. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41741. idx)), -1);
  41742. #endif /* WOLFSSL_MULTI_ATTRIB */
  41743. /* compare DN at index 0 */
  41744. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  41745. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41746. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  41747. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  41748. #ifndef WOLFSSL_MULTI_ATTRIB
  41749. /* compare Serial Number */
  41750. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  41751. -1)), 7);
  41752. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41753. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41754. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  41755. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  41756. #endif
  41757. #ifdef WOLFSSL_MULTI_ATTRIB
  41758. /* get first and second DC and compare result */
  41759. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41760. -1)), 5);
  41761. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41762. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41763. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  41764. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41765. idx)), 6);
  41766. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41767. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41768. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  41769. #endif /* WOLFSSL_MULTI_ATTRIB */
  41770. /* try invalid index locations for regression test and sanity check */
  41771. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  41772. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  41773. X509_free(x509);
  41774. #endif /* OPENSSL_EXTRA */
  41775. wc_FreeRsaKey(&key);
  41776. wc_FreeRng(&rng);
  41777. #endif
  41778. return EXPECT_RESULT();
  41779. }
  41780. static int test_wolfSSL_X509_get_version(void)
  41781. {
  41782. EXPECT_DECLS;
  41783. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41784. WOLFSSL_X509 *x509 = NULL;
  41785. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41786. WOLFSSL_FILETYPE_PEM));
  41787. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  41788. wolfSSL_X509_free(x509);
  41789. #endif
  41790. return EXPECT_RESULT();
  41791. }
  41792. #if defined(OPENSSL_ALL)
  41793. static int test_wolfSSL_sk_CIPHER_description(void)
  41794. {
  41795. EXPECT_DECLS;
  41796. #if !defined(NO_RSA)
  41797. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41798. int i;
  41799. int numCiphers = 0;
  41800. const SSL_METHOD *method = NULL;
  41801. const SSL_CIPHER *cipher = NULL;
  41802. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41803. SSL_CTX *ctx = NULL;
  41804. SSL *ssl = NULL;
  41805. char buf[256];
  41806. char test_str[9] = "0000000";
  41807. const char badStr[] = "unknown";
  41808. const char certPath[] = "./certs/client-cert.pem";
  41809. XMEMSET(buf, 0, sizeof(buf));
  41810. ExpectNotNull(method = TLSv1_2_client_method());
  41811. ExpectNotNull(ctx = SSL_CTX_new(method));
  41812. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41813. SSL_CTX_set_verify_depth(ctx, 4);
  41814. SSL_CTX_set_options(ctx, flags);
  41815. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41816. WOLFSSL_SUCCESS);
  41817. ExpectNotNull(ssl = SSL_new(ctx));
  41818. /* SSL_get_ciphers returns a stack of all configured ciphers
  41819. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  41820. */
  41821. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41822. /* loop through the amount of supportedCiphers */
  41823. numCiphers = sk_num(supportedCiphers);
  41824. for (i = 0; i < numCiphers; ++i) {
  41825. int j;
  41826. /* sk_value increments "sk->data.cipher->cipherOffset".
  41827. * wolfSSL_sk_CIPHER_description sets the description for
  41828. * the cipher based on the provided offset.
  41829. */
  41830. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  41831. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  41832. }
  41833. /* Search cipher description string for "unknown" descriptor */
  41834. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  41835. int k = 0;
  41836. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  41837. test_str[k] = badStr[k];
  41838. j++;
  41839. k++;
  41840. }
  41841. }
  41842. /* Fail if test_str == badStr == "unknown" */
  41843. ExpectStrNE(test_str,badStr);
  41844. }
  41845. SSL_free(ssl);
  41846. SSL_CTX_free(ctx);
  41847. #endif
  41848. return EXPECT_RESULT();
  41849. }
  41850. static int test_wolfSSL_get_ciphers_compat(void)
  41851. {
  41852. EXPECT_DECLS;
  41853. #if !defined(NO_RSA)
  41854. const SSL_METHOD *method = NULL;
  41855. const char certPath[] = "./certs/client-cert.pem";
  41856. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41857. SSL_CTX *ctx = NULL;
  41858. WOLFSSL *ssl = NULL;
  41859. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41860. ExpectNotNull(method = SSLv23_client_method());
  41861. ExpectNotNull(ctx = SSL_CTX_new(method));
  41862. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41863. SSL_CTX_set_verify_depth(ctx, 4);
  41864. SSL_CTX_set_options(ctx, flags);
  41865. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41866. WOLFSSL_SUCCESS);
  41867. ExpectNotNull(ssl = SSL_new(ctx));
  41868. /* Test Bad NULL input */
  41869. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  41870. /* Test for Good input */
  41871. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41872. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  41873. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  41874. SSL_free(ssl);
  41875. SSL_CTX_free(ctx);
  41876. #endif
  41877. return EXPECT_RESULT();
  41878. }
  41879. static int test_wolfSSL_X509_PUBKEY_get(void)
  41880. {
  41881. EXPECT_DECLS;
  41882. WOLFSSL_X509_PUBKEY pubkey;
  41883. WOLFSSL_X509_PUBKEY* key;
  41884. WOLFSSL_EVP_PKEY evpkey ;
  41885. WOLFSSL_EVP_PKEY* evpPkey;
  41886. WOLFSSL_EVP_PKEY* retEvpPkey;
  41887. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  41888. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  41889. key = &pubkey;
  41890. evpPkey = &evpkey;
  41891. evpPkey->type = WOLFSSL_SUCCESS;
  41892. key->pkey = evpPkey;
  41893. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41894. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  41895. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  41896. key->pkey = NULL;
  41897. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41898. return EXPECT_RESULT();
  41899. }
  41900. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  41901. {
  41902. EXPECT_DECLS;
  41903. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41904. DSA *dsa = NULL;
  41905. DSA *setDsa = NULL;
  41906. EVP_PKEY *pkey = NULL;
  41907. EVP_PKEY *set1Pkey = NULL;
  41908. SHA_CTX sha;
  41909. byte signature[DSA_SIG_SIZE];
  41910. byte hash[WC_SHA_DIGEST_SIZE];
  41911. word32 bytes;
  41912. int answer;
  41913. #ifdef USE_CERT_BUFFERS_1024
  41914. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  41915. int dsaKeySz = sizeof_dsa_key_der_1024;
  41916. byte tmp[ONEK_BUF];
  41917. XMEMSET(tmp, 0, sizeof(tmp));
  41918. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41919. bytes = dsaKeySz;
  41920. #elif defined(USE_CERT_BUFFERS_2048)
  41921. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  41922. int dsaKeySz = sizeof_dsa_key_der_2048;
  41923. byte tmp[TWOK_BUF];
  41924. XMEMSET(tmp, 0, sizeof(tmp));
  41925. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41926. bytes = dsaKeySz;
  41927. #else
  41928. byte tmp[TWOK_BUF];
  41929. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  41930. int dsaKeySz;
  41931. XFILE fp = XBADFILE;
  41932. XMEMSET(tmp, 0, sizeof(tmp));
  41933. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  41934. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  41935. if (fp != XBADFILE)
  41936. XFCLOSE(fp);
  41937. #endif /* END USE_CERT_BUFFERS_1024 */
  41938. /* Create hash to later Sign and Verify */
  41939. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  41940. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  41941. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  41942. /* Initialize pkey with der format dsa key */
  41943. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  41944. (long)dsaKeySz));
  41945. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  41946. /* Should Fail: NULL argument */
  41947. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  41948. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  41949. /* Should Pass: Initialized pkey argument */
  41950. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  41951. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  41952. #ifdef USE_CERT_BUFFERS_1024
  41953. ExpectIntEQ(DSA_bits(dsa), 1024);
  41954. #else
  41955. ExpectIntEQ(DSA_bits(dsa), 2048);
  41956. #endif
  41957. /* Sign */
  41958. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  41959. /* Verify. */
  41960. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  41961. WOLFSSL_SUCCESS);
  41962. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  41963. /* Should Fail: set1Pkey not initialized */
  41964. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41965. /* Initialize set1Pkey */
  41966. set1Pkey = EVP_PKEY_new();
  41967. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  41968. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  41969. WOLFSSL_SUCCESS);
  41970. /* Should Pass: set dsa into set1Pkey */
  41971. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41972. DSA_free(dsa);
  41973. DSA_free(setDsa);
  41974. EVP_PKEY_free(pkey);
  41975. EVP_PKEY_free(set1Pkey);
  41976. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41977. return EXPECT_RESULT();
  41978. } /* END test_EVP_PKEY_set1_get1_DSA */
  41979. static int test_wolfSSL_DSA_generate_parameters(void)
  41980. {
  41981. EXPECT_DECLS;
  41982. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41983. !defined(HAVE_FIPS)
  41984. DSA *dsa = NULL;
  41985. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  41986. NULL));
  41987. DSA_free(dsa);
  41988. #endif
  41989. return EXPECT_RESULT();
  41990. }
  41991. static int test_wolfSSL_DSA_SIG(void)
  41992. {
  41993. EXPECT_DECLS;
  41994. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41995. !defined(HAVE_FIPS)
  41996. DSA *dsa = NULL;
  41997. DSA *dsa2 = NULL;
  41998. DSA_SIG *sig = NULL;
  41999. const BIGNUM *p = NULL;
  42000. const BIGNUM *q = NULL;
  42001. const BIGNUM *g = NULL;
  42002. const BIGNUM *pub = NULL;
  42003. const BIGNUM *priv = NULL;
  42004. BIGNUM *dup_p = NULL;
  42005. BIGNUM *dup_q = NULL;
  42006. BIGNUM *dup_g = NULL;
  42007. BIGNUM *dup_pub = NULL;
  42008. BIGNUM *dup_priv = NULL;
  42009. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  42010. ExpectNotNull(dsa = DSA_new());
  42011. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  42012. NULL), 1);
  42013. ExpectIntEQ(DSA_generate_key(dsa), 1);
  42014. DSA_get0_pqg(dsa, &p, &q, &g);
  42015. DSA_get0_key(dsa, &pub, &priv);
  42016. ExpectNotNull(dup_p = BN_dup(p));
  42017. ExpectNotNull(dup_q = BN_dup(q));
  42018. ExpectNotNull(dup_g = BN_dup(g));
  42019. ExpectNotNull(dup_pub = BN_dup(pub));
  42020. ExpectNotNull(dup_priv = BN_dup(priv));
  42021. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  42022. ExpectNotNull(dsa2 = DSA_new());
  42023. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  42024. if (EXPECT_FAIL()) {
  42025. BN_free(dup_p);
  42026. BN_free(dup_q);
  42027. BN_free(dup_g);
  42028. }
  42029. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  42030. if (EXPECT_FAIL()) {
  42031. BN_free(dup_pub);
  42032. BN_free(dup_priv);
  42033. }
  42034. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  42035. DSA_free(dsa);
  42036. DSA_free(dsa2);
  42037. DSA_SIG_free(sig);
  42038. #endif
  42039. return EXPECT_RESULT();
  42040. }
  42041. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  42042. {
  42043. EXPECT_DECLS;
  42044. #ifdef HAVE_ECC
  42045. WOLFSSL_EC_KEY* ecKey = NULL;
  42046. WOLFSSL_EC_KEY* ecGet1 = NULL;
  42047. EVP_PKEY* pkey = NULL;
  42048. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42049. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42050. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  42051. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42052. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42053. /* Should fail since ecKey is empty */
  42054. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42055. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42056. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42057. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  42058. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  42059. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  42060. wolfSSL_EC_KEY_free(ecKey);
  42061. wolfSSL_EC_KEY_free(ecGet1);
  42062. EVP_PKEY_free(pkey);
  42063. #endif /* HAVE_ECC */
  42064. return EXPECT_RESULT();
  42065. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  42066. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  42067. {
  42068. EXPECT_DECLS;
  42069. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  42070. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42071. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  42072. DH *dh = NULL;
  42073. DH *setDh = NULL;
  42074. EVP_PKEY *pkey = NULL;
  42075. XFILE f = XBADFILE;
  42076. unsigned char buf[4096];
  42077. const unsigned char* pt = buf;
  42078. const char* dh2048 = "./certs/dh2048.der";
  42079. long len = 0;
  42080. int code = -1;
  42081. XMEMSET(buf, 0, sizeof(buf));
  42082. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  42083. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42084. if (f != XBADFILE)
  42085. XFCLOSE(f);
  42086. /* Load dh2048.der into DH with internal format */
  42087. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42088. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  42089. ExpectIntEQ(code, 0);
  42090. code = -1;
  42091. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42092. /* Set DH into PKEY */
  42093. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  42094. /* Get DH from PKEY */
  42095. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  42096. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  42097. ExpectIntEQ(code, 0);
  42098. EVP_PKEY_free(pkey);
  42099. DH_free(setDh);
  42100. setDh = NULL;
  42101. DH_free(dh);
  42102. dh = NULL;
  42103. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  42104. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  42105. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  42106. return EXPECT_RESULT();
  42107. } /* END test_EVP_PKEY_set1_get1_DH */
  42108. static int test_wolfSSL_CTX_ctrl(void)
  42109. {
  42110. EXPECT_DECLS;
  42111. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42112. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42113. char caFile[] = "./certs/client-ca.pem";
  42114. char clientFile[] = "./certs/client-cert.pem";
  42115. SSL_CTX* ctx = NULL;
  42116. X509* x509 = NULL;
  42117. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42118. byte buf[6000];
  42119. char file[] = "./certs/dsaparams.pem";
  42120. XFILE f = XBADFILE;
  42121. int bytes = 0;
  42122. BIO* bio = NULL;
  42123. DSA* dsa = NULL;
  42124. DH* dh = NULL;
  42125. #endif
  42126. #ifdef HAVE_ECC
  42127. WOLFSSL_EC_KEY* ecKey = NULL;
  42128. #endif
  42129. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42130. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  42131. WOLFSSL_FILETYPE_PEM));
  42132. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  42133. if (EXPECT_FAIL()) {
  42134. wolfSSL_X509_free(x509);
  42135. }
  42136. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  42137. WOLFSSL_FILETYPE_PEM));
  42138. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42139. /* Initialize DH */
  42140. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  42141. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  42142. if (f != XBADFILE)
  42143. XFCLOSE(f);
  42144. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  42145. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  42146. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  42147. #endif
  42148. #ifdef HAVE_ECC
  42149. /* Initialize WOLFSSL_EC_KEY */
  42150. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42151. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42152. #endif
  42153. /* additional test of getting EVP_PKEY key size from X509
  42154. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  42155. * allowed with user RSA */
  42156. {
  42157. EVP_PKEY* pkey = NULL;
  42158. #if defined(HAVE_ECC)
  42159. X509* ecX509 = NULL;
  42160. #endif /* HAVE_ECC */
  42161. ExpectNotNull(pkey = X509_get_pubkey(x509));
  42162. /* current RSA key is 2048 bit (256 bytes) */
  42163. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  42164. EVP_PKEY_free(pkey);
  42165. pkey = NULL;
  42166. #if defined(HAVE_ECC)
  42167. #if defined(USE_CERT_BUFFERS_256)
  42168. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  42169. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  42170. SSL_FILETYPE_ASN1));
  42171. #else
  42172. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  42173. cliEccCertFile, SSL_FILETYPE_PEM));
  42174. #endif
  42175. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  42176. /* current ECC key is 256 bit (32 bytes) */
  42177. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  42178. X509_free(ecX509);
  42179. EVP_PKEY_free(pkey);
  42180. #endif /* HAVE_ECC */
  42181. }
  42182. /* Tests should fail with passed in NULL pointer */
  42183. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  42184. SSL_FAILURE);
  42185. #if !defined(NO_DH) && !defined(NO_DSA)
  42186. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  42187. SSL_FAILURE);
  42188. #endif
  42189. #ifdef HAVE_ECC
  42190. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  42191. SSL_FAILURE);
  42192. #endif
  42193. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  42194. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  42195. */
  42196. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  42197. SSL_SUCCESS);
  42198. if (EXPECT_FAIL()) {
  42199. wolfSSL_X509_free(x509);
  42200. }
  42201. /* Test with SSL_CTRL_OPTIONS
  42202. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  42203. */
  42204. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  42205. NULL) == SSL_OP_NO_TLSv1);
  42206. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  42207. /* Test with SSL_CTRL_SET_TMP_DH
  42208. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  42209. */
  42210. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42211. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  42212. SSL_SUCCESS);
  42213. #endif
  42214. /* Test with SSL_CTRL_SET_TMP_ECDH
  42215. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  42216. */
  42217. #ifdef HAVE_ECC
  42218. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  42219. SSL_SUCCESS);
  42220. #endif
  42221. #ifdef WOLFSSL_ENCRYPTED_KEYS
  42222. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  42223. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  42224. #endif
  42225. /* Test for min/max proto */
  42226. #ifndef WOLFSSL_NO_TLS12
  42227. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42228. 0, NULL), SSL_SUCCESS);
  42229. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42230. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42231. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  42232. #endif
  42233. #ifdef WOLFSSL_TLS13
  42234. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42235. 0, NULL), SSL_SUCCESS);
  42236. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42237. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  42238. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  42239. #ifndef WOLFSSL_NO_TLS12
  42240. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42241. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42242. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  42243. #endif
  42244. #endif
  42245. /* Cleanup and Pass */
  42246. #if !defined(NO_DH) && !defined(NO_DSA)
  42247. #ifndef NO_BIO
  42248. BIO_free(bio);
  42249. DSA_free(dsa);
  42250. DH_free(dh);
  42251. dh = NULL;
  42252. #endif
  42253. #endif
  42254. #ifdef HAVE_ECC
  42255. wolfSSL_EC_KEY_free(ecKey);
  42256. #endif
  42257. SSL_CTX_free(ctx);
  42258. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42259. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  42260. return EXPECT_RESULT();
  42261. }
  42262. static int test_wolfSSL_EVP_PKEY_assign(void)
  42263. {
  42264. EXPECT_DECLS;
  42265. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  42266. int type;
  42267. WOLFSSL_EVP_PKEY* pkey = NULL;
  42268. #ifndef NO_RSA
  42269. WOLFSSL_RSA* rsa = NULL;
  42270. #endif
  42271. #ifndef NO_DSA
  42272. WOLFSSL_DSA* dsa = NULL;
  42273. #endif
  42274. #ifdef HAVE_ECC
  42275. WOLFSSL_EC_KEY* ecKey = NULL;
  42276. #endif
  42277. #ifndef NO_RSA
  42278. type = EVP_PKEY_RSA;
  42279. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42280. ExpectNotNull(rsa = wolfSSL_RSA_new());
  42281. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  42282. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42283. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  42284. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  42285. if (EXPECT_FAIL()) {
  42286. wolfSSL_RSA_free(rsa);
  42287. }
  42288. wolfSSL_EVP_PKEY_free(pkey);
  42289. pkey = NULL;
  42290. #endif /* NO_RSA */
  42291. #ifndef NO_DSA
  42292. type = EVP_PKEY_DSA;
  42293. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42294. ExpectNotNull(dsa = wolfSSL_DSA_new());
  42295. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  42296. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42297. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  42298. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  42299. if (EXPECT_FAIL()) {
  42300. wolfSSL_DSA_free(dsa);
  42301. }
  42302. wolfSSL_EVP_PKEY_free(pkey);
  42303. pkey = NULL;
  42304. #endif /* NO_DSA */
  42305. #ifdef HAVE_ECC
  42306. type = EVP_PKEY_EC;
  42307. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42308. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42309. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  42310. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42311. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  42312. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  42313. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42314. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  42315. if (EXPECT_FAIL()) {
  42316. wolfSSL_EC_KEY_free(ecKey);
  42317. }
  42318. wolfSSL_EVP_PKEY_free(pkey);
  42319. pkey = NULL;
  42320. #endif /* HAVE_ECC */
  42321. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  42322. return EXPECT_RESULT();
  42323. }
  42324. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  42325. {
  42326. EXPECT_DECLS;
  42327. #if !defined(NO_DH) && \
  42328. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42329. XFILE f = XBADFILE;
  42330. unsigned char buf[4096];
  42331. const unsigned char* pt = buf;
  42332. const char* params1 = "./certs/dh2048.der";
  42333. long len = 0;
  42334. WOLFSSL_DH* dh = NULL;
  42335. WOLFSSL_EVP_PKEY* pkey = NULL;
  42336. XMEMSET(buf, 0, sizeof(buf));
  42337. /* Load DH parameters DER. */
  42338. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  42339. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42340. if (f != XBADFILE)
  42341. XFCLOSE(f);
  42342. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42343. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  42344. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42345. /* Bad cases */
  42346. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  42347. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  42348. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  42349. /* Good case */
  42350. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  42351. if (EXPECT_FAIL()) {
  42352. wolfSSL_DH_free(dh);
  42353. }
  42354. EVP_PKEY_free(pkey);
  42355. #endif
  42356. return EXPECT_RESULT();
  42357. }
  42358. static int test_wolfSSL_EVP_PKEY_base_id(void)
  42359. {
  42360. EXPECT_DECLS;
  42361. WOLFSSL_EVP_PKEY* pkey = NULL;
  42362. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42363. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  42364. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  42365. EVP_PKEY_free(pkey);
  42366. return EXPECT_RESULT();
  42367. }
  42368. static int test_wolfSSL_EVP_PKEY_id(void)
  42369. {
  42370. EXPECT_DECLS;
  42371. WOLFSSL_EVP_PKEY* pkey = NULL;
  42372. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42373. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  42374. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  42375. EVP_PKEY_free(pkey);
  42376. return EXPECT_RESULT();
  42377. }
  42378. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  42379. {
  42380. EXPECT_DECLS;
  42381. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  42382. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  42383. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  42384. ECC_MIN_KEY_SZ <= 256)
  42385. EVP_PKEY_CTX* ctx = NULL;
  42386. EVP_PKEY* pkey = NULL;
  42387. /* Test error conditions. */
  42388. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  42389. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42390. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  42391. #ifndef NO_RSA
  42392. EVP_PKEY_CTX_free(ctx);
  42393. /* Parameter generation for RSA not supported yet. */
  42394. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  42395. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  42396. #endif
  42397. #ifdef HAVE_ECC
  42398. EVP_PKEY_CTX_free(ctx);
  42399. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42400. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  42401. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  42402. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  42403. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  42404. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  42405. WOLFSSL_SUCCESS);
  42406. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42407. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42408. #endif
  42409. EVP_PKEY_CTX_free(ctx);
  42410. EVP_PKEY_free(pkey);
  42411. #endif
  42412. return EXPECT_RESULT();
  42413. }
  42414. static int test_wolfSSL_EVP_PKEY_keygen(void)
  42415. {
  42416. EXPECT_DECLS;
  42417. WOLFSSL_EVP_PKEY* pkey = NULL;
  42418. EVP_PKEY_CTX* ctx = NULL;
  42419. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42420. WOLFSSL_EVP_PKEY* params = NULL;
  42421. DH* dh = NULL;
  42422. const BIGNUM* pubkey = NULL;
  42423. const BIGNUM* privkey = NULL;
  42424. ASN1_INTEGER* asn1int = NULL;
  42425. unsigned int length = 0;
  42426. byte* derBuffer = NULL;
  42427. #endif
  42428. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42429. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42430. /* Bad cases */
  42431. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  42432. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  42433. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  42434. /* Good case */
  42435. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  42436. EVP_PKEY_CTX_free(ctx);
  42437. ctx = NULL;
  42438. EVP_PKEY_free(pkey);
  42439. pkey = NULL;
  42440. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42441. /* Test DH keygen */
  42442. {
  42443. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  42444. ExpectNotNull(dh = DH_get_2048_256());
  42445. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42446. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  42447. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42448. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42449. DH_free(dh);
  42450. dh = NULL;
  42451. EVP_PKEY_CTX_free(ctx);
  42452. EVP_PKEY_free(params);
  42453. /* try exporting generated key to DER, to verify */
  42454. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  42455. DH_get0_key(dh, &pubkey, &privkey);
  42456. ExpectNotNull(pubkey);
  42457. ExpectNotNull(privkey);
  42458. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  42459. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  42460. ASN1_INTEGER_free(asn1int);
  42461. DH_free(dh);
  42462. dh = NULL;
  42463. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42464. EVP_PKEY_free(pkey);
  42465. }
  42466. #endif
  42467. return EXPECT_RESULT();
  42468. }
  42469. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  42470. {
  42471. EXPECT_DECLS;
  42472. WOLFSSL_EVP_PKEY* pkey = NULL;
  42473. EVP_PKEY_CTX *ctx = NULL;
  42474. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42475. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42476. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42477. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  42478. EVP_PKEY_CTX_free(ctx);
  42479. EVP_PKEY_free(pkey);
  42480. return EXPECT_RESULT();
  42481. }
  42482. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  42483. {
  42484. EXPECT_DECLS;
  42485. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  42486. WOLFSSL_EVP_PKEY* pkey = NULL;
  42487. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42488. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  42489. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  42490. EVP_PKEY_free(pkey);
  42491. #endif
  42492. return EXPECT_RESULT();
  42493. }
  42494. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  42495. {
  42496. EXPECT_DECLS;
  42497. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  42498. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  42499. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  42500. !defined(NO_FILESYSTEM)
  42501. WOLFSSL_EVP_PKEY* params = NULL;
  42502. WOLFSSL_EVP_PKEY* copy = NULL;
  42503. DH* dh = NULL;
  42504. BIGNUM* p1;
  42505. BIGNUM* g1;
  42506. BIGNUM* q1;
  42507. BIGNUM* p2;
  42508. BIGNUM* g2;
  42509. BIGNUM* q2;
  42510. /* create DH with DH_get_2048_256 params */
  42511. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  42512. ExpectNotNull(dh = DH_get_2048_256());
  42513. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42514. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  42515. (const BIGNUM**)&q1,
  42516. (const BIGNUM**)&g1);
  42517. DH_free(dh);
  42518. dh = NULL;
  42519. /* create DH with random generated DH params */
  42520. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  42521. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  42522. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  42523. DH_free(dh);
  42524. dh = NULL;
  42525. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  42526. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  42527. ExpectNotNull(dh->p);
  42528. ExpectNotNull(dh->g);
  42529. ExpectNotNull(dh->q);
  42530. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  42531. (const BIGNUM**)&q2,
  42532. (const BIGNUM**)&g2);
  42533. ExpectIntEQ(BN_cmp(p1, p2), 0);
  42534. ExpectIntEQ(BN_cmp(q1, q2), 0);
  42535. ExpectIntEQ(BN_cmp(g1, g2), 0);
  42536. DH_free(dh);
  42537. dh = NULL;
  42538. EVP_PKEY_free(copy);
  42539. EVP_PKEY_free(params);
  42540. #endif
  42541. return EXPECT_RESULT();
  42542. }
  42543. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  42544. {
  42545. EXPECT_DECLS;
  42546. WOLFSSL_EVP_PKEY* pkey = NULL;
  42547. EVP_PKEY_CTX* ctx = NULL;
  42548. int bits = 2048;
  42549. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42550. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42551. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  42552. WOLFSSL_SUCCESS);
  42553. EVP_PKEY_CTX_free(ctx);
  42554. EVP_PKEY_free(pkey);
  42555. return EXPECT_RESULT();
  42556. }
  42557. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  42558. {
  42559. EXPECT_DECLS;
  42560. /* This is large enough to be used for all key sizes */
  42561. byte key[AES_256_KEY_SIZE] = {0};
  42562. byte iv[AES_BLOCK_SIZE] = {0};
  42563. int i;
  42564. int nids[] = {
  42565. #ifdef HAVE_AES_CBC
  42566. NID_aes_128_cbc,
  42567. #endif
  42568. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42569. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42570. #ifdef HAVE_AESGCM
  42571. NID_aes_128_gcm,
  42572. #endif
  42573. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42574. #ifdef WOLFSSL_AES_COUNTER
  42575. NID_aes_128_ctr,
  42576. #endif
  42577. #ifndef NO_DES3
  42578. NID_des_cbc,
  42579. NID_des_ede3_cbc,
  42580. #endif
  42581. };
  42582. int iv_lengths[] = {
  42583. #ifdef HAVE_AES_CBC
  42584. AES_BLOCK_SIZE,
  42585. #endif
  42586. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42587. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42588. #ifdef HAVE_AESGCM
  42589. GCM_NONCE_MID_SZ,
  42590. #endif
  42591. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42592. #ifdef WOLFSSL_AES_COUNTER
  42593. AES_BLOCK_SIZE,
  42594. #endif
  42595. #ifndef NO_DES3
  42596. DES_BLOCK_SIZE,
  42597. DES_BLOCK_SIZE,
  42598. #endif
  42599. };
  42600. int nidsLen = (sizeof(nids)/sizeof(int));
  42601. for (i = 0; i < nidsLen; i++) {
  42602. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  42603. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  42604. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42605. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42606. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  42607. EVP_CIPHER_CTX_free(ctx);
  42608. }
  42609. return EXPECT_RESULT();
  42610. }
  42611. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  42612. {
  42613. EXPECT_DECLS;
  42614. byte key[AES_256_KEY_SIZE] = {0};
  42615. byte iv[AES_BLOCK_SIZE] = {0};
  42616. int i;
  42617. int nids[] = {
  42618. #ifdef HAVE_AES_CBC
  42619. NID_aes_128_cbc,
  42620. NID_aes_256_cbc,
  42621. #endif
  42622. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42623. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42624. #ifdef HAVE_AESGCM
  42625. NID_aes_128_gcm,
  42626. NID_aes_256_gcm,
  42627. #endif
  42628. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42629. #ifdef WOLFSSL_AES_COUNTER
  42630. NID_aes_128_ctr,
  42631. NID_aes_256_ctr,
  42632. #endif
  42633. #ifndef NO_DES3
  42634. NID_des_cbc,
  42635. NID_des_ede3_cbc,
  42636. #endif
  42637. };
  42638. int key_lengths[] = {
  42639. #ifdef HAVE_AES_CBC
  42640. AES_128_KEY_SIZE,
  42641. AES_256_KEY_SIZE,
  42642. #endif
  42643. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42644. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42645. #ifdef HAVE_AESGCM
  42646. AES_128_KEY_SIZE,
  42647. AES_256_KEY_SIZE,
  42648. #endif
  42649. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42650. #ifdef WOLFSSL_AES_COUNTER
  42651. AES_128_KEY_SIZE,
  42652. AES_256_KEY_SIZE,
  42653. #endif
  42654. #ifndef NO_DES3
  42655. DES_KEY_SIZE,
  42656. DES3_KEY_SIZE,
  42657. #endif
  42658. };
  42659. int nidsLen = (sizeof(nids)/sizeof(int));
  42660. for (i = 0; i < nidsLen; i++) {
  42661. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  42662. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  42663. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42664. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42665. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  42666. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  42667. WOLFSSL_SUCCESS);
  42668. EVP_CIPHER_CTX_free(ctx);
  42669. }
  42670. return EXPECT_RESULT();
  42671. }
  42672. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  42673. {
  42674. EXPECT_DECLS;
  42675. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  42676. int ivLen, keyLen;
  42677. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  42678. #ifdef HAVE_AESGCM
  42679. byte key[AES_128_KEY_SIZE] = {0};
  42680. byte iv[AES_BLOCK_SIZE] = {0};
  42681. const EVP_CIPHER *init = EVP_aes_128_gcm();
  42682. #else
  42683. byte key[DES3_KEY_SIZE] = {0};
  42684. byte iv[DES_BLOCK_SIZE] = {0};
  42685. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  42686. #endif
  42687. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42688. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42689. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  42690. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  42691. /* Bad cases */
  42692. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  42693. WOLFSSL_FAILURE);
  42694. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  42695. WOLFSSL_FAILURE);
  42696. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  42697. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  42698. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  42699. WOLFSSL_FAILURE);
  42700. /* Good case */
  42701. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  42702. EVP_CIPHER_CTX_free(ctx);
  42703. #endif
  42704. return EXPECT_RESULT();
  42705. }
  42706. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  42707. {
  42708. EXPECT_DECLS;
  42709. WOLFSSL_ENGINE* e = NULL;
  42710. int id = 0;
  42711. EVP_PKEY_CTX *ctx = NULL;
  42712. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  42713. EVP_PKEY_CTX_free(ctx);
  42714. return EXPECT_RESULT();
  42715. }
  42716. static int test_wolfSSL_EVP_rc4(void)
  42717. {
  42718. EXPECT_DECLS;
  42719. #if !defined(NO_RC4)
  42720. ExpectNotNull(wolfSSL_EVP_rc4());
  42721. #endif
  42722. return EXPECT_RESULT();
  42723. }
  42724. static int test_wolfSSL_EVP_enc_null(void)
  42725. {
  42726. EXPECT_DECLS;
  42727. ExpectNotNull(wolfSSL_EVP_enc_null());
  42728. return EXPECT_RESULT();
  42729. }
  42730. static int test_wolfSSL_EVP_rc2_cbc(void)
  42731. {
  42732. EXPECT_DECLS;
  42733. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  42734. ExpectNull(wolfSSL_EVP_rc2_cbc());
  42735. #endif
  42736. return EXPECT_RESULT();
  42737. }
  42738. static int test_wolfSSL_EVP_mdc2(void)
  42739. {
  42740. EXPECT_DECLS;
  42741. #if !defined(NO_WOLFSSL_STUB)
  42742. ExpectNull(wolfSSL_EVP_mdc2());
  42743. #endif
  42744. return EXPECT_RESULT();
  42745. }
  42746. static int test_wolfSSL_EVP_md4(void)
  42747. {
  42748. EXPECT_DECLS;
  42749. #if !defined(NO_MD4)
  42750. ExpectNotNull(wolfSSL_EVP_md4());
  42751. #endif
  42752. return EXPECT_RESULT();
  42753. }
  42754. static int test_wolfSSL_EVP_aes_256_gcm(void)
  42755. {
  42756. EXPECT_DECLS;
  42757. #ifdef HAVE_AESGCM
  42758. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  42759. #endif
  42760. return EXPECT_RESULT();
  42761. }
  42762. static int test_wolfSSL_EVP_aes_192_gcm(void)
  42763. {
  42764. EXPECT_DECLS;
  42765. #ifdef HAVE_AESGCM
  42766. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  42767. #endif
  42768. return EXPECT_RESULT();
  42769. }
  42770. static int test_wolfSSL_EVP_aes_256_ccm(void)
  42771. {
  42772. EXPECT_DECLS;
  42773. #ifdef HAVE_AESCCM
  42774. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  42775. #endif
  42776. return EXPECT_RESULT();
  42777. }
  42778. static int test_wolfSSL_EVP_aes_192_ccm(void)
  42779. {
  42780. EXPECT_DECLS;
  42781. #ifdef HAVE_AESCCM
  42782. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  42783. #endif
  42784. return EXPECT_RESULT();
  42785. }
  42786. static int test_wolfSSL_EVP_aes_128_ccm(void)
  42787. {
  42788. EXPECT_DECLS;
  42789. #ifdef HAVE_AESCCM
  42790. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  42791. #endif
  42792. return EXPECT_RESULT();
  42793. }
  42794. static int test_wolfSSL_EVP_ripemd160(void)
  42795. {
  42796. EXPECT_DECLS;
  42797. #if !defined(NO_WOLFSSL_STUB)
  42798. ExpectNull(wolfSSL_EVP_ripemd160());
  42799. #endif
  42800. return EXPECT_RESULT();
  42801. }
  42802. static int test_wolfSSL_EVP_get_digestbynid(void)
  42803. {
  42804. EXPECT_DECLS;
  42805. #ifndef NO_MD5
  42806. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  42807. #endif
  42808. #ifndef NO_SHA
  42809. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  42810. #endif
  42811. #ifndef NO_SHA256
  42812. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  42813. #endif
  42814. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  42815. return EXPECT_RESULT();
  42816. }
  42817. static int test_wolfSSL_EVP_MD_nid(void)
  42818. {
  42819. EXPECT_DECLS;
  42820. #ifndef NO_MD5
  42821. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  42822. #endif
  42823. #ifndef NO_SHA
  42824. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  42825. #endif
  42826. #ifndef NO_SHA256
  42827. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  42828. #endif
  42829. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  42830. return EXPECT_RESULT();
  42831. }
  42832. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  42833. {
  42834. EXPECT_DECLS;
  42835. #if defined(HAVE_ECC)
  42836. WOLFSSL_EVP_PKEY* pkey = NULL;
  42837. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  42838. ExpectNotNull(pkey = EVP_PKEY_new());
  42839. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  42840. EVP_PKEY_free(pkey);
  42841. #endif
  42842. return EXPECT_RESULT();
  42843. }
  42844. static int test_wolfSSL_EVP_X_STATE(void)
  42845. {
  42846. EXPECT_DECLS;
  42847. #if !defined(NO_DES3) && !defined(NO_RC4)
  42848. byte key[DES3_KEY_SIZE] = {0};
  42849. byte iv[DES_IV_SIZE] = {0};
  42850. EVP_CIPHER_CTX *ctx = NULL;
  42851. const EVP_CIPHER *init = NULL;
  42852. /* Bad test cases */
  42853. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42854. ExpectNotNull(init = EVP_des_ede3_cbc());
  42855. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42856. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42857. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  42858. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  42859. EVP_CIPHER_CTX_free(ctx);
  42860. ctx = NULL;
  42861. /* Good test case */
  42862. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42863. ExpectNotNull(init = wolfSSL_EVP_rc4());
  42864. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42865. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42866. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  42867. EVP_CIPHER_CTX_free(ctx);
  42868. #endif
  42869. return EXPECT_RESULT();
  42870. }
  42871. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  42872. {
  42873. EXPECT_DECLS;
  42874. #if !defined(NO_DES3) && !defined(NO_RC4)
  42875. byte key[DES3_KEY_SIZE] = {0};
  42876. byte iv[DES_IV_SIZE] = {0};
  42877. EVP_CIPHER_CTX *ctx = NULL;
  42878. const EVP_CIPHER *init = NULL;
  42879. /* Bad test cases */
  42880. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42881. ExpectNotNull(init = EVP_des_ede3_cbc());
  42882. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42883. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42884. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  42885. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  42886. EVP_CIPHER_CTX_free(ctx);
  42887. ctx = NULL;
  42888. /* Good test case */
  42889. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42890. ExpectNotNull(init = wolfSSL_EVP_rc4());
  42891. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42892. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42893. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  42894. EVP_CIPHER_CTX_free(ctx);
  42895. #endif
  42896. return EXPECT_RESULT();
  42897. }
  42898. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  42899. {
  42900. EXPECT_DECLS;
  42901. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  42902. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  42903. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  42904. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  42905. #ifdef HAVE_AES_CBC
  42906. #ifdef WOLFSSL_AES_128
  42907. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  42908. #endif
  42909. #ifdef WOLFSSL_AES_192
  42910. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  42911. #endif
  42912. #ifdef WOLFSSL_AES_256
  42913. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  42914. #endif
  42915. #endif
  42916. #ifdef HAVE_AESGCM
  42917. #ifdef WOLFSSL_AES_128
  42918. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  42919. #endif
  42920. #ifdef WOLFSSL_AES_192
  42921. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  42922. #endif
  42923. #ifdef WOLFSSL_AES_256
  42924. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  42925. #endif
  42926. #endif
  42927. #ifdef HAVE_AESCCM
  42928. #ifdef WOLFSSL_AES_128
  42929. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  42930. #endif
  42931. #ifdef WOLFSSL_AES_192
  42932. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  42933. #endif
  42934. #ifdef WOLFSSL_AES_256
  42935. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  42936. #endif
  42937. #endif
  42938. #ifdef WOLFSSL_AES_COUNTER
  42939. #ifdef WOLFSSL_AES_128
  42940. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  42941. #endif
  42942. #ifdef WOLFSSL_AES_192
  42943. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  42944. #endif
  42945. #ifdef WOLFSSL_AES_256
  42946. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  42947. #endif
  42948. #endif
  42949. #ifdef HAVE_AES_ECB
  42950. #ifdef WOLFSSL_AES_128
  42951. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  42952. #endif
  42953. #ifdef WOLFSSL_AES_192
  42954. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  42955. #endif
  42956. #ifdef WOLFSSL_AES_256
  42957. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  42958. #endif
  42959. #endif
  42960. #ifdef WOLFSSL_AES_OFB
  42961. #ifdef WOLFSSL_AES_128
  42962. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  42963. #endif
  42964. #ifdef WOLFSSL_AES_192
  42965. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  42966. #endif
  42967. #ifdef WOLFSSL_AES_256
  42968. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  42969. #endif
  42970. #endif
  42971. #ifndef NO_RC4
  42972. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  42973. #endif
  42974. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42975. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  42976. #endif
  42977. #endif
  42978. #ifdef WOLFSSL_SM4_ECB
  42979. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  42980. #endif
  42981. #ifdef WOLFSSL_SM4_CBC
  42982. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  42983. #endif
  42984. #ifdef WOLFSSL_SM4_CTR
  42985. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  42986. #endif
  42987. #ifdef WOLFSSL_SM4_GCM
  42988. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  42989. #endif
  42990. #ifdef WOLFSSL_SM4_CCM
  42991. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  42992. #endif
  42993. return EXPECT_RESULT();
  42994. }
  42995. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  42996. {
  42997. EXPECT_DECLS;
  42998. int nids[] = {
  42999. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43000. #ifdef WOLFSSL_AES_128
  43001. NID_aes_128_cbc,
  43002. #endif
  43003. #ifdef WOLFSSL_AES_192
  43004. NID_aes_192_cbc,
  43005. #endif
  43006. #ifdef WOLFSSL_AES_256
  43007. NID_aes_256_cbc,
  43008. #endif
  43009. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  43010. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43011. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43012. #ifdef HAVE_AESGCM
  43013. #ifdef WOLFSSL_AES_128
  43014. NID_aes_128_gcm,
  43015. #endif
  43016. #ifdef WOLFSSL_AES_192
  43017. NID_aes_192_gcm,
  43018. #endif
  43019. #ifdef WOLFSSL_AES_256
  43020. NID_aes_256_gcm,
  43021. #endif
  43022. #endif /* HAVE_AESGCM */
  43023. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43024. #ifdef WOLFSSL_AES_COUNTER
  43025. #ifdef WOLFSSL_AES_128
  43026. NID_aes_128_ctr,
  43027. #endif
  43028. #ifdef WOLFSSL_AES_192
  43029. NID_aes_192_ctr,
  43030. #endif
  43031. #ifdef WOLFSSL_AES_256
  43032. NID_aes_256_ctr,
  43033. #endif
  43034. #endif
  43035. #ifndef NO_DES3
  43036. NID_des_cbc,
  43037. NID_des_ede3_cbc,
  43038. #endif
  43039. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43040. NID_chacha20_poly1305,
  43041. #endif
  43042. };
  43043. int iv_lengths[] = {
  43044. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43045. #ifdef WOLFSSL_AES_128
  43046. AES_BLOCK_SIZE,
  43047. #endif
  43048. #ifdef WOLFSSL_AES_192
  43049. AES_BLOCK_SIZE,
  43050. #endif
  43051. #ifdef WOLFSSL_AES_256
  43052. AES_BLOCK_SIZE,
  43053. #endif
  43054. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  43055. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43056. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43057. #ifdef HAVE_AESGCM
  43058. #ifdef WOLFSSL_AES_128
  43059. GCM_NONCE_MID_SZ,
  43060. #endif
  43061. #ifdef WOLFSSL_AES_192
  43062. GCM_NONCE_MID_SZ,
  43063. #endif
  43064. #ifdef WOLFSSL_AES_256
  43065. GCM_NONCE_MID_SZ,
  43066. #endif
  43067. #endif /* HAVE_AESGCM */
  43068. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43069. #ifdef WOLFSSL_AES_COUNTER
  43070. #ifdef WOLFSSL_AES_128
  43071. AES_BLOCK_SIZE,
  43072. #endif
  43073. #ifdef WOLFSSL_AES_192
  43074. AES_BLOCK_SIZE,
  43075. #endif
  43076. #ifdef WOLFSSL_AES_256
  43077. AES_BLOCK_SIZE,
  43078. #endif
  43079. #endif
  43080. #ifndef NO_DES3
  43081. DES_BLOCK_SIZE,
  43082. DES_BLOCK_SIZE,
  43083. #endif
  43084. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43085. CHACHA20_POLY1305_AEAD_IV_SIZE,
  43086. #endif
  43087. };
  43088. int i;
  43089. int nidsLen = (sizeof(nids)/sizeof(int));
  43090. for (i = 0; i < nidsLen; i++) {
  43091. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  43092. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  43093. }
  43094. return EXPECT_RESULT();
  43095. }
  43096. static int test_wolfSSL_EVP_SignInit_ex(void)
  43097. {
  43098. EXPECT_DECLS;
  43099. WOLFSSL_EVP_MD_CTX mdCtx;
  43100. WOLFSSL_ENGINE* e = 0;
  43101. const EVP_MD* md = EVP_sha256();
  43102. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43103. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  43104. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43105. return EXPECT_RESULT();
  43106. }
  43107. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  43108. {
  43109. EXPECT_DECLS;
  43110. #if !defined(NO_SHA256)
  43111. WOLFSSL_EVP_MD_CTX mdCtx;
  43112. unsigned int s = 0;
  43113. unsigned char md[WC_SHA256_DIGEST_SIZE];
  43114. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  43115. /* Bad Case */
  43116. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  43117. (HAVE_FIPS_VERSION > 2))
  43118. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43119. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  43120. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43121. #else
  43122. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43123. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  43124. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  43125. #endif
  43126. /* Good Case */
  43127. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43128. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  43129. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  43130. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  43131. #endif
  43132. return EXPECT_RESULT();
  43133. }
  43134. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  43135. {
  43136. EXPECT_DECLS;
  43137. #if defined(OPENSSL_EXTRA)
  43138. EVP_PKEY* pkey = NULL;
  43139. EVP_PKEY_CTX* ctx = NULL;
  43140. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43141. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43142. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  43143. /* void */
  43144. EVP_PKEY_CTX_free(ctx);
  43145. #else
  43146. /* int */
  43147. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  43148. #endif
  43149. EVP_PKEY_free(pkey);
  43150. #endif
  43151. return EXPECT_RESULT();
  43152. }
  43153. static int test_wolfSSL_EVP_PKEY_param_check(void)
  43154. {
  43155. EXPECT_DECLS;
  43156. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  43157. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43158. DH *dh = NULL;
  43159. DH *setDh = NULL;
  43160. EVP_PKEY *pkey = NULL;
  43161. EVP_PKEY_CTX* ctx = NULL;
  43162. FILE* f = NULL;
  43163. unsigned char buf[512];
  43164. const unsigned char* pt = buf;
  43165. const char* dh2048 = "./certs/dh2048.der";
  43166. long len = 0;
  43167. int code = -1;
  43168. XMEMSET(buf, 0, sizeof(buf));
  43169. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  43170. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43171. if (f != XBADFILE)
  43172. XFCLOSE(f);
  43173. /* Load dh2048.der into DH with internal format */
  43174. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  43175. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43176. ExpectIntEQ(code, 0);
  43177. code = -1;
  43178. pkey = wolfSSL_EVP_PKEY_new();
  43179. /* Set DH into PKEY */
  43180. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43181. /* create ctx from pkey */
  43182. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43183. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  43184. /* TODO: more invalid cases */
  43185. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  43186. EVP_PKEY_CTX_free(ctx);
  43187. EVP_PKEY_free(pkey);
  43188. DH_free(setDh);
  43189. setDh = NULL;
  43190. DH_free(dh);
  43191. dh = NULL;
  43192. #endif
  43193. #endif
  43194. return EXPECT_RESULT();
  43195. }
  43196. static int test_wolfSSL_EVP_BytesToKey(void)
  43197. {
  43198. EXPECT_DECLS;
  43199. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43200. byte key[AES_BLOCK_SIZE] = {0};
  43201. byte iv[AES_BLOCK_SIZE] = {0};
  43202. int count = 0;
  43203. const EVP_MD* md = EVP_sha256();
  43204. const EVP_CIPHER *type;
  43205. const unsigned char *salt = (unsigned char *)"salt1234";
  43206. int sz = 5;
  43207. const byte data[] = {
  43208. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43209. 0x72,0x6c,0x64
  43210. };
  43211. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  43212. /* Bad cases */
  43213. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  43214. 0);
  43215. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  43216. 16);
  43217. md = "2";
  43218. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43219. WOLFSSL_FAILURE);
  43220. /* Good case */
  43221. md = EVP_sha256();
  43222. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43223. 16);
  43224. #endif
  43225. return EXPECT_RESULT();
  43226. }
  43227. static int test_evp_cipher_aes_gcm(void)
  43228. {
  43229. EXPECT_DECLS;
  43230. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  43231. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  43232. (HAVE_FIPS_VERSION >= 2)))
  43233. /*
  43234. * This test checks data at various points in the encrypt/decrypt process
  43235. * against known values produced using the same test with OpenSSL. This
  43236. * interop testing is critical for verifying the correctness of our
  43237. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  43238. * a flow supported by OpenSSL that uses the control command
  43239. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  43240. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  43241. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  43242. * wolfSSL OpenSSH clients because there was a bug in this flow that
  43243. * happened to "cancel out" if both sides of the connection had the bug.
  43244. */
  43245. enum {
  43246. NUM_ENCRYPTIONS = 3,
  43247. AAD_SIZE = 4
  43248. };
  43249. byte plainText1[] = {
  43250. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  43251. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  43252. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  43253. };
  43254. byte plainText2[] = {
  43255. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  43256. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  43257. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  43258. };
  43259. byte plainText3[] = {
  43260. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  43261. 0x5b, 0x57, 0x10
  43262. };
  43263. byte* plainTexts[NUM_ENCRYPTIONS] = {
  43264. plainText1,
  43265. plainText2,
  43266. plainText3
  43267. };
  43268. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  43269. sizeof(plainText1),
  43270. sizeof(plainText2),
  43271. sizeof(plainText3)
  43272. };
  43273. byte aad1[AAD_SIZE] = {
  43274. 0x00, 0x00, 0x00, 0x01
  43275. };
  43276. byte aad2[AAD_SIZE] = {
  43277. 0x00, 0x00, 0x00, 0x10
  43278. };
  43279. byte aad3[AAD_SIZE] = {
  43280. 0x00, 0x00, 0x01, 0x00
  43281. };
  43282. byte* aads[NUM_ENCRYPTIONS] = {
  43283. aad1,
  43284. aad2,
  43285. aad3
  43286. };
  43287. const byte iv[GCM_NONCE_MID_SZ] = {
  43288. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  43289. };
  43290. byte currentIv[GCM_NONCE_MID_SZ];
  43291. const byte key[] = {
  43292. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  43293. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  43294. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  43295. };
  43296. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  43297. {
  43298. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43299. 0xEF
  43300. },
  43301. {
  43302. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43303. 0xF0
  43304. },
  43305. {
  43306. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43307. 0xF1
  43308. }
  43309. };
  43310. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  43311. {
  43312. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  43313. 0x3D, 0x32, 0x18, 0x34, 0xA9
  43314. },
  43315. {
  43316. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  43317. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  43318. },
  43319. {
  43320. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  43321. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  43322. }
  43323. };
  43324. const byte expCipherText1[] = {
  43325. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  43326. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  43327. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  43328. };
  43329. const byte expCipherText2[] = {
  43330. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  43331. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  43332. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  43333. };
  43334. const byte expCipherText3[] = {
  43335. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  43336. 0x80, 0x5E, 0x6B,
  43337. };
  43338. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  43339. expCipherText1,
  43340. expCipherText2,
  43341. expCipherText3
  43342. };
  43343. byte* cipherText = NULL;
  43344. byte* calcPlainText = NULL;
  43345. byte tag[AES_BLOCK_SIZE];
  43346. EVP_CIPHER_CTX* encCtx = NULL;
  43347. EVP_CIPHER_CTX* decCtx = NULL;
  43348. int i, j, outl;
  43349. /****************************************************/
  43350. for (i = 0; i < 3; ++i) {
  43351. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  43352. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  43353. /* First iteration, set key before IV. */
  43354. if (i == 0) {
  43355. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  43356. SSL_SUCCESS);
  43357. /*
  43358. * The call to EVP_CipherInit below (with NULL key) should clear the
  43359. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  43360. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  43361. * behavior.
  43362. */
  43363. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43364. (void*)iv), SSL_SUCCESS);
  43365. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  43366. SSL_SUCCESS);
  43367. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43368. currentIv), SSL_FAILURE);
  43369. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  43370. SSL_SUCCESS);
  43371. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  43372. SSL_SUCCESS);
  43373. }
  43374. /* Second iteration, IV before key. */
  43375. else {
  43376. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  43377. SSL_SUCCESS);
  43378. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  43379. SSL_SUCCESS);
  43380. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  43381. SSL_SUCCESS);
  43382. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  43383. SSL_SUCCESS);
  43384. }
  43385. /*
  43386. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  43387. * been issued first.
  43388. */
  43389. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43390. currentIv), SSL_FAILURE);
  43391. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43392. (void*)iv), SSL_SUCCESS);
  43393. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43394. (void*)iv), SSL_SUCCESS);
  43395. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  43396. /*************** Encrypt ***************/
  43397. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43398. currentIv), SSL_SUCCESS);
  43399. /* Check current IV against expected. */
  43400. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43401. /* Add AAD. */
  43402. if (i == 2) {
  43403. /* Test streaming API. */
  43404. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  43405. AAD_SIZE), SSL_SUCCESS);
  43406. }
  43407. else {
  43408. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  43409. AAD_SIZE);
  43410. }
  43411. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43412. DYNAMIC_TYPE_TMP_BUFFER));
  43413. /* Encrypt plaintext. */
  43414. if (i == 2) {
  43415. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  43416. plainTexts[j], plainTextSzs[j]),
  43417. SSL_SUCCESS);
  43418. }
  43419. else {
  43420. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  43421. plainTextSzs[j]), plainTextSzs[j]);
  43422. }
  43423. if (i == 2) {
  43424. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  43425. SSL_SUCCESS);
  43426. }
  43427. else {
  43428. /*
  43429. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  43430. * akin to calling EVP_CipherFinal.
  43431. */
  43432. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  43433. }
  43434. /* Check ciphertext against expected. */
  43435. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  43436. 0);
  43437. /* Get and check tag against expected. */
  43438. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  43439. sizeof(tag), tag), SSL_SUCCESS);
  43440. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  43441. /*************** Decrypt ***************/
  43442. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43443. currentIv), SSL_SUCCESS);
  43444. /* Check current IV against expected. */
  43445. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43446. /* Add AAD. */
  43447. if (i == 2) {
  43448. /* Test streaming API. */
  43449. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  43450. AAD_SIZE), SSL_SUCCESS);
  43451. }
  43452. else {
  43453. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  43454. AAD_SIZE);
  43455. }
  43456. /* Set expected tag. */
  43457. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  43458. sizeof(tag), tag), SSL_SUCCESS);
  43459. /* Decrypt ciphertext. */
  43460. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43461. DYNAMIC_TYPE_TMP_BUFFER));
  43462. if (i == 2) {
  43463. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  43464. cipherText, plainTextSzs[j]),
  43465. SSL_SUCCESS);
  43466. }
  43467. else {
  43468. /* This first EVP_Cipher call will check the tag, too. */
  43469. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  43470. plainTextSzs[j]), plainTextSzs[j]);
  43471. }
  43472. if (i == 2) {
  43473. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  43474. SSL_SUCCESS);
  43475. }
  43476. else {
  43477. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  43478. }
  43479. /* Check plaintext against expected. */
  43480. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  43481. 0);
  43482. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43483. cipherText = NULL;
  43484. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43485. calcPlainText = NULL;
  43486. }
  43487. EVP_CIPHER_CTX_free(encCtx);
  43488. encCtx = NULL;
  43489. EVP_CIPHER_CTX_free(decCtx);
  43490. decCtx = NULL;
  43491. }
  43492. #endif
  43493. return EXPECT_RESULT();
  43494. }
  43495. static int test_wolfSSL_OBJ_ln(void)
  43496. {
  43497. EXPECT_DECLS;
  43498. const int nid_set[] = {
  43499. NID_commonName,
  43500. NID_serialNumber,
  43501. NID_countryName,
  43502. NID_localityName,
  43503. NID_stateOrProvinceName,
  43504. NID_organizationName,
  43505. NID_organizationalUnitName,
  43506. NID_domainComponent,
  43507. NID_businessCategory,
  43508. NID_jurisdictionCountryName,
  43509. NID_jurisdictionStateOrProvinceName,
  43510. NID_emailAddress
  43511. };
  43512. const char* ln_set[] = {
  43513. "commonName",
  43514. "serialNumber",
  43515. "countryName",
  43516. "localityName",
  43517. "stateOrProvinceName",
  43518. "organizationName",
  43519. "organizationalUnitName",
  43520. "domainComponent",
  43521. "businessCategory",
  43522. "jurisdictionCountryName",
  43523. "jurisdictionStateOrProvinceName",
  43524. "emailAddress",
  43525. };
  43526. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  43527. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  43528. #ifdef HAVE_ECC
  43529. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43530. {
  43531. EC_builtin_curve r[27];
  43532. size_t nCurves = sizeof(r) / sizeof(r[0]);
  43533. nCurves = EC_get_builtin_curves(r, nCurves);
  43534. for (i = 0; i < nCurves; i++) {
  43535. /* skip ECC_CURVE_INVALID */
  43536. if (r[i].nid != ECC_CURVE_INVALID) {
  43537. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  43538. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  43539. }
  43540. }
  43541. }
  43542. #endif
  43543. #endif
  43544. for (i = 0; i < maxIdx; i++) {
  43545. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  43546. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  43547. }
  43548. return EXPECT_RESULT();
  43549. }
  43550. static int test_wolfSSL_OBJ_sn(void)
  43551. {
  43552. EXPECT_DECLS;
  43553. int i = 0, maxIdx = 7;
  43554. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  43555. NID_stateOrProvinceName,NID_organizationName,
  43556. NID_organizationalUnitName,NID_emailAddress};
  43557. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  43558. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  43559. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  43560. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  43561. WOLFSSL_EMAIL_ADDR};
  43562. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  43563. for (i = 0; i < maxIdx; i++) {
  43564. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  43565. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  43566. }
  43567. return EXPECT_RESULT();
  43568. }
  43569. #if !defined(NO_BIO)
  43570. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  43571. {
  43572. return lh_strhash(s[3]);
  43573. }
  43574. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  43575. {
  43576. return XSTRCMP(a[3], b[3]);
  43577. }
  43578. #endif
  43579. static int test_wolfSSL_TXT_DB(void)
  43580. {
  43581. EXPECT_DECLS;
  43582. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43583. BIO *bio = NULL;
  43584. TXT_DB *db = NULL;
  43585. const int columns = 6;
  43586. const char *fields[6] = {
  43587. "V",
  43588. "320926161116Z",
  43589. "",
  43590. "12BD",
  43591. "unknown",
  43592. "/CN=rsa doe",
  43593. };
  43594. char** fields_copy = NULL;
  43595. /* Test read */
  43596. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  43597. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  43598. ExpectNotNull(db = TXT_DB_read(bio, columns));
  43599. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  43600. DYNAMIC_TYPE_OPENSSL));
  43601. if (fields_copy != NULL) {
  43602. XMEMCPY(fields_copy, fields, sizeof(fields));
  43603. }
  43604. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  43605. if (EXPECT_FAIL()) {
  43606. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  43607. }
  43608. BIO_free(bio);
  43609. bio = NULL;
  43610. /* Test write */
  43611. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  43612. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  43613. BIO_free(bio);
  43614. /* Test index */
  43615. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  43616. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  43617. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43618. fields[3] = "12DA";
  43619. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43620. fields[3] = "FFFF";
  43621. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43622. fields[3] = "";
  43623. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43624. TXT_DB_free(db);
  43625. #endif
  43626. return EXPECT_RESULT();
  43627. }
  43628. static int test_wolfSSL_NCONF(void)
  43629. {
  43630. EXPECT_DECLS;
  43631. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43632. const char* confFile = "./tests/NCONF_test.cnf";
  43633. CONF* conf = NULL;
  43634. long eline = 0;
  43635. long num = 0;
  43636. ExpectNotNull(conf = NCONF_new(NULL));
  43637. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  43638. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  43639. ExpectIntEQ(num, 1234);
  43640. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  43641. ExpectIntEQ(num, 4321);
  43642. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  43643. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  43644. "./test-dir/file1");
  43645. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  43646. "./test-dir/file1:./test-dir/file2:./section1:file2");
  43647. NCONF_free(conf);
  43648. #endif
  43649. return EXPECT_RESULT();
  43650. }
  43651. #endif /* OPENSSL_ALL */
  43652. static int test_wolfSSL_X509V3_EXT_get(void) {
  43653. EXPECT_DECLS;
  43654. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43655. XFILE f = XBADFILE;
  43656. int numOfExt =0;
  43657. int extNid = 0;
  43658. int i = 0;
  43659. WOLFSSL_X509* x509 = NULL;
  43660. WOLFSSL_X509_EXTENSION* ext = NULL;
  43661. const WOLFSSL_v3_ext_method* method = NULL;
  43662. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43663. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43664. if (f != XBADFILE)
  43665. XFCLOSE(f);
  43666. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  43667. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43668. for (i = 0; i < numOfExt; i++) {
  43669. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43670. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  43671. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43672. ExpectIntEQ(method->ext_nid, extNid);
  43673. }
  43674. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  43675. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  43676. wolfSSL_X509_free(x509);
  43677. #endif
  43678. return EXPECT_RESULT();
  43679. }
  43680. static int test_wolfSSL_X509V3_EXT_nconf(void)
  43681. {
  43682. EXPECT_DECLS;
  43683. #ifdef OPENSSL_ALL
  43684. const char *ext_names[] = {
  43685. "subjectKeyIdentifier",
  43686. "authorityKeyIdentifier",
  43687. "subjectAltName",
  43688. "keyUsage",
  43689. "extendedKeyUsage",
  43690. };
  43691. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  43692. int ext_nids[] = {
  43693. NID_subject_key_identifier,
  43694. NID_authority_key_identifier,
  43695. NID_subject_alt_name,
  43696. NID_key_usage,
  43697. NID_ext_key_usage,
  43698. };
  43699. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  43700. const char *ext_values[] = {
  43701. "hash",
  43702. "hash",
  43703. "DNS:example.com, IP:127.0.0.1",
  43704. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  43705. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  43706. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  43707. "OCSPSigning",
  43708. };
  43709. size_t i;
  43710. X509_EXTENSION* ext = NULL;
  43711. X509* x509 = NULL;
  43712. unsigned int keyUsageFlags;
  43713. unsigned int extKeyUsageFlags;
  43714. ExpectNotNull(x509 = X509_new());
  43715. /* keyUsage / extKeyUsage should match string above */
  43716. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43717. | KU_NON_REPUDIATION
  43718. | KU_KEY_ENCIPHERMENT
  43719. | KU_DATA_ENCIPHERMENT
  43720. | KU_KEY_AGREEMENT
  43721. | KU_KEY_CERT_SIGN
  43722. | KU_CRL_SIGN
  43723. | KU_ENCIPHER_ONLY
  43724. | KU_DECIPHER_ONLY;
  43725. extKeyUsageFlags = XKU_SSL_CLIENT
  43726. | XKU_SSL_SERVER
  43727. | XKU_CODE_SIGN
  43728. | XKU_SMIME
  43729. | XKU_TIMESTAMP
  43730. | XKU_OCSP_SIGN;
  43731. for (i = 0; i < ext_names_count; i++) {
  43732. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  43733. ext_values[i]));
  43734. X509_EXTENSION_free(ext);
  43735. ext = NULL;
  43736. }
  43737. for (i = 0; i < ext_nids_count; i++) {
  43738. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  43739. ext_values[i]));
  43740. X509_EXTENSION_free(ext);
  43741. ext = NULL;
  43742. }
  43743. /* Test adding extension to X509 */
  43744. for (i = 0; i < ext_nids_count; i++) {
  43745. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  43746. ext_values[i]));
  43747. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  43748. if (ext_nids[i] == NID_key_usage) {
  43749. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43750. }
  43751. else if (ext_nids[i] == NID_ext_key_usage) {
  43752. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  43753. }
  43754. X509_EXTENSION_free(ext);
  43755. ext = NULL;
  43756. }
  43757. X509_free(x509);
  43758. #endif
  43759. return EXPECT_RESULT();
  43760. }
  43761. static int test_wolfSSL_X509V3_EXT(void) {
  43762. EXPECT_DECLS;
  43763. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43764. XFILE f = XBADFILE;
  43765. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  43766. char* str = NULL;
  43767. unsigned char* data = NULL;
  43768. const WOLFSSL_v3_ext_method* method = NULL;
  43769. WOLFSSL_X509* x509 = NULL;
  43770. WOLFSSL_X509_EXTENSION* ext = NULL;
  43771. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  43772. WOLFSSL_ASN1_OBJECT *obj = NULL;
  43773. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  43774. WOLFSSL_ASN1_STRING* asn1str = NULL;
  43775. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  43776. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  43777. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  43778. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  43779. WOLFSSL_GENERAL_NAME* gn = NULL;
  43780. /* Check NULL argument */
  43781. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  43782. /* Using OCSP cert with X509V3 extensions */
  43783. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  43784. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43785. if (f != XBADFILE)
  43786. XFCLOSE(f);
  43787. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43788. /* Basic Constraints */
  43789. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43790. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43791. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  43792. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  43793. ExpectIntEQ(bc->ca, 1);
  43794. ExpectNull(bc->pathlen);
  43795. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  43796. bc = NULL;
  43797. i++;
  43798. /* Subject Key Identifier */
  43799. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43800. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43801. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  43802. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43803. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  43804. asn1str));
  43805. X509_EXTENSION_free(ext2);
  43806. ext2 = NULL;
  43807. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43808. ExpectNotNull(method->i2s);
  43809. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  43810. wolfSSL_ASN1_STRING_free(asn1str);
  43811. asn1str = NULL;
  43812. if (str != NULL) {
  43813. actual = strcmp(str,
  43814. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43815. }
  43816. ExpectIntEQ(actual, 0);
  43817. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43818. str = NULL;
  43819. i++;
  43820. /* Authority Key Identifier */
  43821. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43822. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43823. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  43824. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  43825. ext));
  43826. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43827. ExpectNotNull(asn1str = aKeyId->keyid);
  43828. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  43829. asn1str));
  43830. asn1str = NULL;
  43831. if (str != NULL) {
  43832. actual = strcmp(str,
  43833. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43834. }
  43835. ExpectIntEQ(actual, 0);
  43836. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43837. str = NULL;
  43838. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  43839. aKeyId = NULL;
  43840. i++;
  43841. /* Key Usage */
  43842. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43843. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43844. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  43845. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43846. #if defined(WOLFSSL_QT)
  43847. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  43848. #else
  43849. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  43850. #endif
  43851. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  43852. if (data != NULL) {
  43853. #ifdef BIG_ENDIAN_ORDER
  43854. actual = data[1];
  43855. #else
  43856. actual = data[0];
  43857. #endif
  43858. }
  43859. ExpectIntEQ(actual, expected);
  43860. wolfSSL_ASN1_STRING_free(asn1str);
  43861. asn1str = NULL;
  43862. #if 1
  43863. i++;
  43864. /* Authority Info Access */
  43865. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43866. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43867. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  43868. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  43869. ext));
  43870. #if defined(WOLFSSL_QT)
  43871. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43872. #else
  43873. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43874. #endif
  43875. /* URI entry is an ACCESS_DESCRIPTION type */
  43876. #if defined(WOLFSSL_QT)
  43877. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  43878. #else
  43879. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  43880. #endif
  43881. ExpectNotNull(adObj = ad->method);
  43882. /* Make sure nid is OCSP */
  43883. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  43884. /* GENERAL_NAME stores URI as an ASN1_STRING */
  43885. ExpectNotNull(gn = ad->location);
  43886. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  43887. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  43888. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  43889. #if defined(WOLFSSL_QT)
  43890. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  43891. #else
  43892. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  43893. #endif
  43894. if (str != NULL) {
  43895. actual = strcmp(str, "http://127.0.0.1:22220");
  43896. }
  43897. ExpectIntEQ(actual, 0);
  43898. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  43899. aia = NULL;
  43900. #else
  43901. (void) aia; (void) ad; (void) adObj; (void) gn;
  43902. #endif
  43903. wolfSSL_X509_free(x509);
  43904. #endif
  43905. return EXPECT_RESULT();
  43906. }
  43907. static int test_wolfSSL_X509_get_extension_flags(void)
  43908. {
  43909. EXPECT_DECLS;
  43910. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43911. XFILE f = XBADFILE;
  43912. X509* x509 = NULL;
  43913. unsigned int extFlags;
  43914. unsigned int keyUsageFlags;
  43915. unsigned int extKeyUsageFlags;
  43916. /* client-int-cert.pem has the following extension flags. */
  43917. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  43918. /* and the following key usage flags. */
  43919. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43920. | KU_NON_REPUDIATION
  43921. | KU_KEY_ENCIPHERMENT;
  43922. /* and the following extended key usage flags. */
  43923. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  43924. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  43925. XBADFILE);
  43926. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43927. if (f != XBADFILE) {
  43928. XFCLOSE(f);
  43929. f = XBADFILE;
  43930. }
  43931. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  43932. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43933. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  43934. X509_free(x509);
  43935. x509 = NULL;
  43936. /* client-cert-ext.pem has the following extension flags. */
  43937. extFlags = EXFLAG_KUSAGE;
  43938. /* and the following key usage flags. */
  43939. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43940. | KU_KEY_CERT_SIGN
  43941. | KU_CRL_SIGN;
  43942. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  43943. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43944. if (f != XBADFILE)
  43945. XFCLOSE(f);
  43946. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  43947. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43948. X509_free(x509);
  43949. #endif /* OPENSSL_ALL */
  43950. return EXPECT_RESULT();
  43951. }
  43952. static int test_wolfSSL_X509_get_ext(void)
  43953. {
  43954. EXPECT_DECLS;
  43955. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43956. int ret = 0;
  43957. XFILE f = XBADFILE;
  43958. WOLFSSL_X509* x509 = NULL;
  43959. WOLFSSL_X509_EXTENSION* foundExtension;
  43960. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43961. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43962. if (f != XBADFILE)
  43963. XFCLOSE(f);
  43964. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  43965. /* wolfSSL_X509_get_ext() valid input */
  43966. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  43967. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  43968. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  43969. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  43970. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  43971. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  43972. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  43973. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  43974. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  43975. wolfSSL_X509_free(x509);
  43976. #endif
  43977. return EXPECT_RESULT();
  43978. }
  43979. static int test_wolfSSL_X509_get_ext_by_NID(void)
  43980. {
  43981. EXPECT_DECLS;
  43982. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43983. int rc = 0;
  43984. XFILE f = XBADFILE;
  43985. WOLFSSL_X509* x509 = NULL;
  43986. ASN1_OBJECT* obj = NULL;
  43987. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43988. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43989. if (f != XBADFILE)
  43990. XFCLOSE(f);
  43991. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43992. -1), 0);
  43993. /* Start search from last location (should fail) */
  43994. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43995. rc), -1);
  43996. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43997. -2), -1);
  43998. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  43999. -1), -1);
  44000. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  44001. /* NID_ext_key_usage, check also its nid and oid */
  44002. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  44003. -1);
  44004. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  44005. x509, rc)));
  44006. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  44007. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  44008. wolfSSL_X509_free(x509);
  44009. #endif
  44010. return EXPECT_RESULT();
  44011. }
  44012. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  44013. {
  44014. EXPECT_DECLS;
  44015. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44016. int rc = 0;
  44017. XFILE f = XBADFILE;
  44018. WOLFSSL_X509* x509 = NULL;
  44019. WOLFSSL_X509_EXTENSION* ext = NULL;
  44020. WOLFSSL_ASN1_STRING* sanString = NULL;
  44021. byte* sanDer = NULL;
  44022. const byte expectedDer[] = {
  44023. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  44024. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  44025. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44026. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44027. if (f != XBADFILE)
  44028. XFCLOSE(f);
  44029. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  44030. ExpectNotNull(ext = X509_get_ext(x509, rc));
  44031. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  44032. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  44033. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  44034. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  44035. X509_free(x509);
  44036. #endif
  44037. return EXPECT_RESULT();
  44038. }
  44039. static int test_wolfSSL_X509_EXTENSION_new(void)
  44040. {
  44041. EXPECT_DECLS;
  44042. #if defined (OPENSSL_ALL)
  44043. WOLFSSL_X509_EXTENSION* ext = NULL;
  44044. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  44045. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  44046. wolfSSL_X509_EXTENSION_free(ext);
  44047. #endif
  44048. return EXPECT_RESULT();
  44049. }
  44050. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  44051. {
  44052. EXPECT_DECLS;
  44053. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44054. WOLFSSL_X509* x509 = NULL;
  44055. WOLFSSL_X509_EXTENSION* ext = NULL;
  44056. WOLFSSL_ASN1_OBJECT* o = NULL;
  44057. XFILE file = XBADFILE;
  44058. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44059. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44060. if (file != XBADFILE)
  44061. XFCLOSE(file);
  44062. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  44063. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44064. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  44065. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  44066. ExpectIntEQ(o->nid, 128);
  44067. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  44068. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  44069. wolfSSL_X509_free(x509);
  44070. #endif
  44071. return EXPECT_RESULT();
  44072. }
  44073. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  44074. {
  44075. EXPECT_DECLS;
  44076. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44077. WOLFSSL_X509* x509 = NULL;
  44078. WOLFSSL_X509_EXTENSION* ext = NULL;
  44079. WOLFSSL_ASN1_STRING* str = NULL;
  44080. XFILE file = XBADFILE;
  44081. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44082. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44083. if (file != XBADFILE)
  44084. XFCLOSE(file);
  44085. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44086. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  44087. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  44088. wolfSSL_X509_free(x509);
  44089. #endif
  44090. return EXPECT_RESULT();
  44091. }
  44092. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  44093. {
  44094. EXPECT_DECLS;
  44095. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44096. WOLFSSL_X509* x509 = NULL;
  44097. WOLFSSL_X509_EXTENSION* ext = NULL;
  44098. XFILE file = XBADFILE;
  44099. int crit = 0;
  44100. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44101. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44102. if (file != XBADFILE)
  44103. XFCLOSE(file);
  44104. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44105. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  44106. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  44107. wolfSSL_X509_free(x509);
  44108. #endif
  44109. return EXPECT_RESULT();
  44110. }
  44111. static int test_wolfSSL_X509V3_EXT_print(void)
  44112. {
  44113. EXPECT_DECLS;
  44114. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  44115. !defined(NO_RSA)
  44116. {
  44117. XFILE f = XBADFILE;
  44118. WOLFSSL_X509* x509 = NULL;
  44119. X509_EXTENSION * ext = NULL;
  44120. int loc = 0;
  44121. BIO *bio = NULL;
  44122. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  44123. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44124. if (f != XBADFILE)
  44125. fclose(f);
  44126. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  44127. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44128. NID_basic_constraints, -1), -1);
  44129. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44130. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44131. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44132. NID_subject_key_identifier, -1), -1);
  44133. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44134. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44135. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44136. NID_authority_key_identifier, -1), -1);
  44137. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44138. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44139. wolfSSL_BIO_free(bio);
  44140. wolfSSL_X509_free(x509);
  44141. }
  44142. {
  44143. X509 *x509 = NULL;
  44144. BIO *bio = NULL;
  44145. X509_EXTENSION *ext = NULL;
  44146. unsigned int i = 0;
  44147. unsigned int idx = 0;
  44148. /* Some NIDs to test with */
  44149. int nids[] = {
  44150. /* NID_key_usage, currently X509_get_ext returns this as a bit
  44151. * string, which messes up X509V3_EXT_print */
  44152. /* NID_ext_key_usage, */
  44153. NID_subject_alt_name,
  44154. };
  44155. int* n = NULL;
  44156. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  44157. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  44158. WOLFSSL_FILETYPE_PEM));
  44159. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  44160. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  44161. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  44162. * update the index */
  44163. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  44164. ExpectNotNull(ext = X509_get_ext(x509, idx));
  44165. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  44166. ExpectIntGT(fprintf(stderr, "\n"), 0);
  44167. }
  44168. BIO_free(bio);
  44169. X509_free(x509);
  44170. }
  44171. #endif
  44172. return EXPECT_RESULT();
  44173. }
  44174. static int test_wolfSSL_X509_cmp(void)
  44175. {
  44176. EXPECT_DECLS;
  44177. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44178. XFILE file1 = XBADFILE;
  44179. XFILE file2 = XBADFILE;
  44180. WOLFSSL_X509* cert1 = NULL;
  44181. WOLFSSL_X509* cert2 = NULL;
  44182. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44183. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  44184. XBADFILE);
  44185. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  44186. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  44187. if (file1 != XBADFILE)
  44188. fclose(file1);
  44189. if (file2 != XBADFILE)
  44190. fclose(file2);
  44191. /* wolfSSL_X509_cmp() testing matching certs */
  44192. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  44193. /* wolfSSL_X509_cmp() testing mismatched certs */
  44194. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  44195. /* wolfSSL_X509_cmp() testing NULL, valid args */
  44196. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  44197. /* wolfSSL_X509_cmp() testing valid, NULL args */
  44198. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  44199. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  44200. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  44201. wolfSSL_X509_free(cert1);
  44202. wolfSSL_X509_free(cert2);
  44203. #endif
  44204. return EXPECT_RESULT();
  44205. }
  44206. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  44207. {
  44208. EXPECT_DECLS;
  44209. #if defined(OPENSSL_ALL)
  44210. EVP_PKEY* pkey;
  44211. pkey = EVP_PKEY_new();
  44212. ExpectNotNull(pkey);
  44213. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  44214. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44215. EVP_PKEY_free(pkey);
  44216. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44217. EVP_PKEY_free(pkey);
  44218. EVP_PKEY_free(pkey);
  44219. #endif
  44220. return EXPECT_RESULT();
  44221. }
  44222. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  44223. {
  44224. EXPECT_DECLS;
  44225. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44226. EVP_PKEY* pkey = NULL;
  44227. const unsigned char* p;
  44228. unsigned char *der = NULL;
  44229. unsigned char *tmp = NULL;
  44230. int derLen;
  44231. p = client_keypub_der_2048;
  44232. /* Check that key can be successfully decoded. */
  44233. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  44234. sizeof_client_keypub_der_2048));
  44235. /* Check that key can be successfully encoded. */
  44236. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44237. /* Ensure that the encoded version matches the original. */
  44238. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  44239. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44240. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44241. * is advanced. */
  44242. tmp = der;
  44243. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44244. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  44245. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44246. ExpectTrue(der + derLen == tmp);
  44247. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44248. EVP_PKEY_free(pkey);
  44249. #endif
  44250. return EXPECT_RESULT();
  44251. }
  44252. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  44253. {
  44254. EXPECT_DECLS;
  44255. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  44256. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44257. EVP_PKEY* pkey = NULL;
  44258. const unsigned char* p;
  44259. unsigned char *der = NULL;
  44260. unsigned char *tmp = NULL;
  44261. int derLen;
  44262. unsigned char pub_buf[65];
  44263. const int pub_len = 65;
  44264. BN_CTX* ctx;
  44265. EC_GROUP* curve = NULL;
  44266. EC_KEY* ephemeral_key = NULL;
  44267. const EC_POINT* h;
  44268. /* Generate an x963 key pair and get public part into pub_buf */
  44269. ExpectNotNull(ctx = BN_CTX_new());
  44270. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  44271. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  44272. NID_X9_62_prime256v1));
  44273. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  44274. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  44275. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  44276. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  44277. /* Prepare the EVP_PKEY */
  44278. ExpectNotNull(pkey = EVP_PKEY_new());
  44279. p = pub_buf;
  44280. /* Check that key can be successfully decoded. */
  44281. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  44282. pub_len));
  44283. /* Check that key can be successfully encoded. */
  44284. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44285. /* Ensure that the encoded version matches the original. */
  44286. ExpectIntEQ(derLen, pub_len);
  44287. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44288. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44289. * is advanced. */
  44290. tmp = der;
  44291. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44292. ExpectIntEQ(derLen, pub_len);
  44293. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44294. ExpectTrue(der + derLen == tmp);
  44295. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44296. EVP_PKEY_free(pkey);
  44297. EC_KEY_free(ephemeral_key);
  44298. EC_GROUP_free(curve);
  44299. #endif
  44300. return EXPECT_RESULT();
  44301. }
  44302. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  44303. {
  44304. EXPECT_DECLS;
  44305. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  44306. DSA* dsa = NULL;
  44307. byte derIn[] = {
  44308. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  44309. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  44310. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  44311. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  44312. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  44313. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  44314. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  44315. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  44316. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  44317. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  44318. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  44319. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  44320. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  44321. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  44322. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  44323. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  44324. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  44325. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  44326. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  44327. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  44328. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  44329. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  44330. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  44331. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  44332. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  44333. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  44334. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  44335. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  44336. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  44337. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  44338. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  44339. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  44340. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  44341. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  44342. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  44343. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  44344. 0x1e, 0xde, 0xc4
  44345. };
  44346. int derInLen = sizeof(derIn);
  44347. byte* derOut = NULL;
  44348. int derOutLen;
  44349. byte* p = derIn;
  44350. /* Check that params can be successfully decoded. */
  44351. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  44352. /* Check that params can be successfully encoded. */
  44353. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  44354. /* Ensure that the encoded version matches the original. */
  44355. ExpectIntEQ(derInLen, derOutLen);
  44356. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  44357. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44358. DSA_free(dsa);
  44359. #endif
  44360. return EXPECT_RESULT();
  44361. }
  44362. static int test_wolfSSL_i2d_PrivateKey(void)
  44363. {
  44364. EXPECT_DECLS;
  44365. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  44366. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44367. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44368. {
  44369. EVP_PKEY* pkey = NULL;
  44370. const unsigned char* server_key =
  44371. (const unsigned char*)server_key_der_2048;
  44372. unsigned char buf[FOURK_BUF];
  44373. unsigned char* pt = NULL;
  44374. int bufSz = 0;
  44375. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  44376. (long)sizeof_server_key_der_2048));
  44377. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  44378. pt = buf;
  44379. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  44380. ExpectIntNE((pt - buf), 0);
  44381. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  44382. EVP_PKEY_free(pkey);
  44383. }
  44384. #endif
  44385. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44386. {
  44387. EVP_PKEY* pkey = NULL;
  44388. const unsigned char* client_key =
  44389. (const unsigned char*)ecc_clikey_der_256;
  44390. unsigned char buf[FOURK_BUF];
  44391. unsigned char* pt = NULL;
  44392. int bufSz = 0;
  44393. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  44394. (long)sizeof_ecc_clikey_der_256)));
  44395. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  44396. pt = buf;
  44397. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  44398. ExpectIntNE((pt - buf), 0);
  44399. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  44400. EVP_PKEY_free(pkey);
  44401. }
  44402. #endif
  44403. #endif
  44404. return EXPECT_RESULT();
  44405. }
  44406. static int test_wolfSSL_OCSP_id_get0_info(void)
  44407. {
  44408. EXPECT_DECLS;
  44409. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  44410. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44411. X509* cert = NULL;
  44412. X509* issuer = NULL;
  44413. OCSP_CERTID* id = NULL;
  44414. OCSP_CERTID* id2 = NULL;
  44415. ASN1_STRING* name = NULL;
  44416. ASN1_OBJECT* pmd = NULL;
  44417. ASN1_STRING* keyHash = NULL;
  44418. ASN1_INTEGER* serial = NULL;
  44419. ASN1_INTEGER* x509Int = NULL;
  44420. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  44421. SSL_FILETYPE_PEM));
  44422. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  44423. SSL_FILETYPE_PEM));
  44424. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  44425. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  44426. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  44427. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  44428. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  44429. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  44430. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  44431. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  44432. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  44433. ExpectNotNull(serial);
  44434. /* compare serial number to one in cert, should be equal */
  44435. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  44436. ExpectIntEQ(x509Int->length, serial->length);
  44437. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  44438. /* test OCSP_id_cmp */
  44439. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  44440. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  44441. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  44442. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  44443. if (id != NULL) {
  44444. id->issuerHash[0] = ~id->issuerHash[0];
  44445. }
  44446. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  44447. OCSP_CERTID_free(id);
  44448. OCSP_CERTID_free(id2);
  44449. X509_free(cert); /* free's x509Int */
  44450. X509_free(issuer);
  44451. #endif
  44452. return EXPECT_RESULT();
  44453. }
  44454. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  44455. {
  44456. EXPECT_DECLS;
  44457. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44458. WOLFSSL_OCSP_CERTID certId;
  44459. byte* targetBuffer = NULL;
  44460. byte* p;
  44461. /* OCSP CertID bytes taken from PCAP */
  44462. byte rawCertId[] = {
  44463. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44464. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44465. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44466. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44467. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44468. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44469. 0xcf, 0xbc, 0x91
  44470. };
  44471. int ret = 0;
  44472. int i;
  44473. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  44474. certId.rawCertId = rawCertId;
  44475. certId.rawCertIdSize = sizeof(rawCertId);
  44476. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  44477. DYNAMIC_TYPE_TMP_BUFFER));
  44478. p = targetBuffer;
  44479. /* Function returns the size of the encoded data. */
  44480. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  44481. /* If target buffer is not null, function increments targetBuffer to point
  44482. * just past the end of the encoded data. */
  44483. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  44484. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  44485. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  44486. }
  44487. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44488. targetBuffer = NULL;
  44489. /* If target buffer is null, function allocates memory for a buffer and
  44490. * copies the encoded data into it. targetBuffer then points to the start of
  44491. * this newly allocate buffer. */
  44492. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  44493. sizeof(rawCertId));
  44494. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  44495. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  44496. }
  44497. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  44498. #endif
  44499. return EXPECT_RESULT();
  44500. }
  44501. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  44502. {
  44503. EXPECT_DECLS;
  44504. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44505. WOLFSSL_OCSP_CERTID* certId;
  44506. WOLFSSL_OCSP_CERTID* certIdGood;
  44507. WOLFSSL_OCSP_CERTID* certIdBad;
  44508. const unsigned char* rawCertIdPtr;
  44509. const unsigned char rawCertId[] = {
  44510. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44511. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44512. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44513. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44514. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44515. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44516. 0xcf, 0xbc, 0x91
  44517. };
  44518. rawCertIdPtr = &rawCertId[0];
  44519. /* If the cert ID is NULL the function should allocate it and copy the
  44520. * data to it. */
  44521. certId = NULL;
  44522. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  44523. sizeof(rawCertId)));
  44524. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  44525. if (certId != NULL) {
  44526. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  44527. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  44528. }
  44529. /* If the cert ID is not NULL the function will just copy the data to it. */
  44530. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  44531. DYNAMIC_TYPE_TMP_BUFFER));
  44532. ExpectNotNull(certId);
  44533. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  44534. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  44535. rawCertIdPtr = &rawCertId[0];
  44536. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  44537. sizeof(rawCertId)));
  44538. ExpectPtrEq(certIdGood, certId);
  44539. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  44540. if (certId != NULL) {
  44541. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  44542. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44543. certId = NULL;
  44544. }
  44545. /* The below tests should fail when passed bad parameters. NULL should
  44546. * always be returned. */
  44547. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  44548. sizeof(rawCertId)));
  44549. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  44550. sizeof(rawCertId)));
  44551. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  44552. #endif
  44553. return EXPECT_RESULT();
  44554. }
  44555. static int test_wolfSSL_OCSP_id_cmp(void)
  44556. {
  44557. EXPECT_DECLS;
  44558. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44559. OCSP_CERTID id1;
  44560. OCSP_CERTID id2;
  44561. XMEMSET(&id1, 0, sizeof(id1));
  44562. XMEMSET(&id2, 0, sizeof(id2));
  44563. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  44564. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  44565. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  44566. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  44567. #endif
  44568. return EXPECT_RESULT();
  44569. }
  44570. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  44571. {
  44572. EXPECT_DECLS;
  44573. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44574. WOLFSSL_OCSP_SINGLERESP single;
  44575. const WOLFSSL_OCSP_CERTID* certId;
  44576. XMEMSET(&single, 0, sizeof(single));
  44577. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  44578. ExpectPtrEq(&single, certId);
  44579. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  44580. #endif
  44581. return EXPECT_RESULT();
  44582. }
  44583. static int test_wolfSSL_OCSP_single_get0_status(void)
  44584. {
  44585. EXPECT_DECLS;
  44586. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44587. WOLFSSL_OCSP_SINGLERESP single;
  44588. CertStatus certStatus;
  44589. WOLFSSL_ASN1_TIME* thisDate;
  44590. WOLFSSL_ASN1_TIME* nextDate;
  44591. int ret, i;
  44592. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44593. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  44594. /* Fill the date fields with some dummy data. */
  44595. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  44596. certStatus.thisDateParsed.data[i] = i;
  44597. certStatus.nextDateParsed.data[i] = i;
  44598. }
  44599. certStatus.status = CERT_GOOD;
  44600. single.status = &certStatus;
  44601. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  44602. &nextDate);
  44603. ExpectIntEQ(ret, CERT_GOOD);
  44604. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  44605. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  44606. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  44607. CERT_GOOD);
  44608. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  44609. NULL), CERT_GOOD);
  44610. #endif
  44611. return EXPECT_RESULT();
  44612. }
  44613. static int test_wolfSSL_OCSP_resp_count(void)
  44614. {
  44615. EXPECT_DECLS;
  44616. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44617. WOLFSSL_OCSP_BASICRESP basicResp;
  44618. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  44619. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  44620. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  44621. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44622. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44623. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  44624. basicResp.single = &singleRespOne;
  44625. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  44626. singleRespOne.next = &singleRespTwo;
  44627. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  44628. #endif
  44629. return EXPECT_RESULT();
  44630. }
  44631. static int test_wolfSSL_OCSP_resp_get0(void)
  44632. {
  44633. EXPECT_DECLS;
  44634. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44635. WOLFSSL_OCSP_BASICRESP basicResp;
  44636. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  44637. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  44638. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  44639. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44640. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44641. basicResp.single = &singleRespOne;
  44642. singleRespOne.next = &singleRespTwo;
  44643. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  44644. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  44645. #endif
  44646. return EXPECT_RESULT();
  44647. }
  44648. static int test_wolfSSL_EVP_PKEY_derive(void)
  44649. {
  44650. EXPECT_DECLS;
  44651. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  44652. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  44653. EVP_PKEY_CTX *ctx = NULL;
  44654. unsigned char *skey = NULL;
  44655. size_t skeylen;
  44656. EVP_PKEY *pkey = NULL;
  44657. EVP_PKEY *peerkey = NULL;
  44658. const unsigned char* key;
  44659. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  44660. /* DH */
  44661. key = dh_key_der_2048;
  44662. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  44663. sizeof_dh_key_der_2048)));
  44664. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  44665. key = dh_key_der_2048;
  44666. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  44667. sizeof_dh_key_der_2048)));
  44668. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  44669. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44670. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  44671. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  44672. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  44673. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  44674. DYNAMIC_TYPE_OPENSSL));
  44675. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  44676. EVP_PKEY_CTX_free(ctx);
  44677. ctx = NULL;
  44678. EVP_PKEY_free(peerkey);
  44679. peerkey = NULL;
  44680. EVP_PKEY_free(pkey);
  44681. pkey = NULL;
  44682. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  44683. skey = NULL;
  44684. #endif
  44685. #ifdef HAVE_ECC
  44686. /* ECDH */
  44687. key = ecc_clikey_der_256;
  44688. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  44689. sizeof_ecc_clikey_der_256)));
  44690. key = ecc_clikeypub_der_256;
  44691. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  44692. sizeof_ecc_clikeypub_der_256)));
  44693. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44694. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  44695. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  44696. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  44697. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  44698. DYNAMIC_TYPE_OPENSSL));
  44699. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  44700. EVP_PKEY_CTX_free(ctx);
  44701. EVP_PKEY_free(peerkey);
  44702. EVP_PKEY_free(pkey);
  44703. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  44704. #endif /* HAVE_ECC */
  44705. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  44706. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  44707. return EXPECT_RESULT();
  44708. }
  44709. static int test_wolfSSL_EVP_PBE_scrypt(void)
  44710. {
  44711. EXPECT_DECLS;
  44712. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  44713. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  44714. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  44715. int ret;
  44716. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  44717. int pwdlen = sizeof(pwd);
  44718. const byte salt[] = {'N','a','C','l'};
  44719. int saltlen = sizeof(salt);
  44720. byte key[80];
  44721. word64 numOvr32 = (word64)INT32_MAX + 1;
  44722. /* expected derived key for N:16, r:1, p:1 */
  44723. const byte expectedKey[] = {
  44724. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  44725. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  44726. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  44727. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  44728. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  44729. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  44730. 0xE7, 0xE9, 0xC0, 0x9A};
  44731. /* N r p mx key keylen */
  44732. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  44733. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  44734. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  44735. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  44736. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  44737. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  44738. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  44739. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  44740. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  44741. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  44742. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  44743. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  44744. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  44745. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  44746. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  44747. ExpectIntEQ(ret, 1); /* should succeed */
  44748. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  44749. key, 64);
  44750. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  44751. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  44752. key, 64);
  44753. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  44754. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  44755. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  44756. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  44757. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  44758. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  44759. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  44760. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  44761. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  44762. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  44763. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  44764. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  44765. ExpectIntEQ(ret, 1);
  44766. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  44767. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  44768. #endif /* !NO_PWDBASED && !NO_SHA256 */
  44769. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  44770. return EXPECT_RESULT();
  44771. }
  44772. static int test_no_op_functions(void)
  44773. {
  44774. EXPECT_DECLS;
  44775. #if defined(OPENSSL_EXTRA)
  44776. /* this makes sure wolfSSL can compile and run these no-op functions */
  44777. SSL_load_error_strings();
  44778. ENGINE_load_builtin_engines();
  44779. OpenSSL_add_all_ciphers();
  44780. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  44781. #endif
  44782. return EXPECT_RESULT();
  44783. }
  44784. static int test_wolfSSL_CRYPTO_memcmp(void)
  44785. {
  44786. EXPECT_DECLS;
  44787. #ifdef OPENSSL_EXTRA
  44788. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44789. "implementation of TLS/SSL for embedded devices to the cloud.";
  44790. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44791. "implementation of TLS/SSL for embedded devices to the cloud.";
  44792. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44793. "implementation of TLS/SSL for embedded devices to the cloud!";
  44794. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  44795. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  44796. #endif
  44797. return EXPECT_RESULT();
  44798. }
  44799. /*----------------------------------------------------------------------------*
  44800. | wolfCrypt ASN
  44801. *----------------------------------------------------------------------------*/
  44802. static int test_wc_CreateEncryptedPKCS8Key(void)
  44803. {
  44804. EXPECT_DECLS;
  44805. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  44806. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  44807. WC_RNG rng;
  44808. byte* encKey = NULL;
  44809. word32 encKeySz = 0;
  44810. word32 decKeySz = 0;
  44811. const char password[] = "Lorem ipsum dolor sit amet";
  44812. word32 passwordSz = (word32)XSTRLEN(password);
  44813. word32 tradIdx = 0;
  44814. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44815. ExpectIntEQ(wc_InitRng(&rng), 0);
  44816. /* Call with NULL for out buffer to get necessary length. */
  44817. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44818. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  44819. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44820. LENGTH_ONLY_E);
  44821. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  44822. DYNAMIC_TYPE_TMP_BUFFER));
  44823. /* Call with the allocated out buffer. */
  44824. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44825. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  44826. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44827. 0);
  44828. /* Decrypt the encrypted PKCS8 key we just made. */
  44829. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  44830. passwordSz)), 0);
  44831. /* encKey now holds the decrypted key (decrypted in place). */
  44832. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  44833. /* Check that the decrypted key matches the key prior to encryption. */
  44834. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  44835. sizeof_server_key_der_2048), 0);
  44836. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44837. wc_FreeRng(&rng);
  44838. #endif
  44839. return EXPECT_RESULT();
  44840. }
  44841. static int test_wc_GetPkcs8TraditionalOffset(void)
  44842. {
  44843. EXPECT_DECLS;
  44844. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  44845. int length;
  44846. int derSz = 0;
  44847. word32 inOutIdx;
  44848. const char* path = "./certs/server-keyPkcs8.der";
  44849. XFILE file = XBADFILE;
  44850. byte der[2048];
  44851. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  44852. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  44853. if (file != XBADFILE)
  44854. XFCLOSE(file);
  44855. /* valid case */
  44856. inOutIdx = 0;
  44857. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44858. 0);
  44859. /* inOutIdx > sz */
  44860. inOutIdx = 4000;
  44861. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44862. BAD_FUNC_ARG);
  44863. /* null input */
  44864. inOutIdx = 0;
  44865. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  44866. BAD_FUNC_ARG);
  44867. /* invalid input, fill buffer with 1's */
  44868. XMEMSET(der, 1, sizeof(der));
  44869. inOutIdx = 0;
  44870. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44871. ASN_PARSE_E);
  44872. #endif /* NO_ASN */
  44873. return EXPECT_RESULT();
  44874. }
  44875. static int test_wc_SetSubjectRaw(void)
  44876. {
  44877. EXPECT_DECLS;
  44878. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44879. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44880. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44881. WOLFSSL_X509* x509 = NULL;
  44882. int peerCertSz;
  44883. const byte* peerCertBuf = NULL;
  44884. Cert forgedCert;
  44885. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44886. WOLFSSL_FILETYPE_ASN1));
  44887. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44888. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44889. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  44890. wolfSSL_FreeX509(x509);
  44891. #endif
  44892. return EXPECT_RESULT();
  44893. }
  44894. static int test_wc_GetSubjectRaw(void)
  44895. {
  44896. EXPECT_DECLS;
  44897. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44898. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  44899. Cert cert;
  44900. byte *subjectRaw;
  44901. ExpectIntEQ(0, wc_InitCert(&cert));
  44902. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  44903. #endif
  44904. return EXPECT_RESULT();
  44905. }
  44906. static int test_wc_SetIssuerRaw(void)
  44907. {
  44908. EXPECT_DECLS;
  44909. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44910. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44911. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44912. WOLFSSL_X509* x509 = NULL;
  44913. int peerCertSz;
  44914. const byte* peerCertBuf;
  44915. Cert forgedCert;
  44916. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44917. WOLFSSL_FILETYPE_ASN1));
  44918. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44919. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44920. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  44921. wolfSSL_FreeX509(x509);
  44922. #endif
  44923. return EXPECT_RESULT();
  44924. }
  44925. static int test_wc_SetIssueBuffer(void)
  44926. {
  44927. EXPECT_DECLS;
  44928. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44929. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44930. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44931. WOLFSSL_X509* x509 = NULL;
  44932. int peerCertSz;
  44933. const byte* peerCertBuf;
  44934. Cert forgedCert;
  44935. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44936. WOLFSSL_FILETYPE_ASN1));
  44937. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44938. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44939. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  44940. wolfSSL_FreeX509(x509);
  44941. #endif
  44942. return EXPECT_RESULT();
  44943. }
  44944. /*
  44945. * Testing wc_SetSubjectKeyId
  44946. */
  44947. static int test_wc_SetSubjectKeyId(void)
  44948. {
  44949. EXPECT_DECLS;
  44950. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44951. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44952. Cert cert;
  44953. const char* file = "certs/ecc-client-keyPub.pem";
  44954. ExpectIntEQ(0, wc_InitCert(&cert));
  44955. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  44956. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  44957. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  44958. #endif
  44959. return EXPECT_RESULT();
  44960. } /* END test_wc_SetSubjectKeyId */
  44961. /*
  44962. * Testing wc_SetSubject
  44963. */
  44964. static int test_wc_SetSubject(void)
  44965. {
  44966. EXPECT_DECLS;
  44967. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44968. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44969. Cert cert;
  44970. const char* file = "./certs/ca-ecc-cert.pem";
  44971. ExpectIntEQ(0, wc_InitCert(&cert));
  44972. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  44973. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  44974. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  44975. #endif
  44976. return EXPECT_RESULT();
  44977. } /* END test_wc_SetSubject */
  44978. static int test_CheckCertSignature(void)
  44979. {
  44980. EXPECT_DECLS;
  44981. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  44982. WOLFSSL_CERT_MANAGER* cm = NULL;
  44983. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44984. XFILE fp = XBADFILE;
  44985. byte cert[4096];
  44986. int certSz;
  44987. #endif
  44988. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  44989. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44990. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  44991. #ifndef NO_RSA
  44992. #ifdef USE_CERT_BUFFERS_1024
  44993. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  44994. sizeof_server_cert_der_1024, NULL, cm));
  44995. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44996. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  44997. WOLFSSL_FILETYPE_ASN1));
  44998. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  44999. sizeof_server_cert_der_1024, NULL, cm));
  45000. #elif defined(USE_CERT_BUFFERS_2048)
  45001. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  45002. sizeof_server_cert_der_2048, NULL, cm));
  45003. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45004. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  45005. WOLFSSL_FILETYPE_ASN1));
  45006. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  45007. sizeof_server_cert_der_2048, NULL, cm));
  45008. #endif
  45009. #endif
  45010. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45011. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  45012. sizeof_serv_ecc_der_256, NULL, cm));
  45013. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45014. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  45015. WOLFSSL_FILETYPE_ASN1));
  45016. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  45017. NULL, cm));
  45018. #endif
  45019. #if !defined(NO_FILESYSTEM)
  45020. wolfSSL_CertManagerFree(cm);
  45021. cm = NULL;
  45022. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  45023. #ifndef NO_RSA
  45024. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  45025. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  45026. if (fp != XBADFILE) {
  45027. XFCLOSE(fp);
  45028. fp = XBADFILE;
  45029. }
  45030. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  45031. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  45032. "./certs/ca-cert.pem", NULL));
  45033. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  45034. #endif
  45035. #ifdef HAVE_ECC
  45036. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  45037. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  45038. if (fp != XBADFILE) {
  45039. XFCLOSE(fp);
  45040. fp = XBADFILE;
  45041. }
  45042. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  45043. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  45044. "./certs/ca-ecc-cert.pem", NULL));
  45045. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  45046. #endif
  45047. #endif
  45048. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  45049. (void)fp;
  45050. (void)cert;
  45051. (void)certSz;
  45052. #endif
  45053. wolfSSL_CertManagerFree(cm);
  45054. #endif
  45055. return EXPECT_RESULT();
  45056. }
  45057. static int test_wc_ParseCert(void)
  45058. {
  45059. EXPECT_DECLS;
  45060. #if !defined(NO_CERTS) && !defined(NO_RSA)
  45061. DecodedCert decodedCert;
  45062. const byte* rawCert = client_cert_der_2048;
  45063. const int rawCertSize = sizeof_client_cert_der_2048;
  45064. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  45065. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45066. #ifndef IGNORE_NAME_CONSTRAINTS
  45067. /* check that the subjects emailAddress was not put in the alt name list */
  45068. ExpectNotNull(decodedCert.subjectEmail);
  45069. ExpectNull(decodedCert.altEmailNames);
  45070. #endif
  45071. wc_FreeDecodedCert(&decodedCert);
  45072. #endif
  45073. return EXPECT_RESULT();
  45074. }
  45075. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  45076. * the API safely errors out on badly-formed ASN input.
  45077. * NOTE: Test not compatible with released FIPS implementations!
  45078. */
  45079. static int test_wc_ParseCert_Error(void)
  45080. {
  45081. EXPECT_DECLS;
  45082. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  45083. (!defined(HAVE_FIPS) || \
  45084. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  45085. DecodedCert decodedCert;
  45086. int i;
  45087. /* Certificate data */
  45088. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  45089. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  45090. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  45091. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  45092. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  45093. /* Test data */
  45094. const struct testStruct {
  45095. const byte* c;
  45096. const int cSz;
  45097. const int expRet;
  45098. } t[] = {
  45099. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  45100. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  45101. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  45102. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  45103. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  45104. };
  45105. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  45106. for (i = 0; i < tSz; i++) {
  45107. WOLFSSL_MSG_EX("i == %d", i);
  45108. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  45109. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  45110. wc_FreeDecodedCert(&decodedCert);
  45111. }
  45112. #endif
  45113. return EXPECT_RESULT();
  45114. }
  45115. static int test_MakeCertWithPathLen(void)
  45116. {
  45117. EXPECT_DECLS;
  45118. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  45119. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  45120. const byte expectedPathLen = 7;
  45121. Cert cert;
  45122. DecodedCert decodedCert;
  45123. byte der[FOURK_BUF];
  45124. int derSize = 0;
  45125. WC_RNG rng;
  45126. ecc_key key;
  45127. int ret;
  45128. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45129. XMEMSET(&key, 0, sizeof(ecc_key));
  45130. XMEMSET(&cert, 0, sizeof(Cert));
  45131. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  45132. ExpectIntEQ(wc_InitRng(&rng), 0);
  45133. ExpectIntEQ(wc_ecc_init(&key), 0);
  45134. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  45135. ExpectIntEQ(wc_InitCert(&cert), 0);
  45136. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  45137. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  45138. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  45139. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  45140. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  45141. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  45142. CTC_NAME_SIZE);
  45143. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  45144. CTC_NAME_SIZE);
  45145. cert.selfSigned = 1;
  45146. cert.isCA = 1;
  45147. cert.pathLen = expectedPathLen;
  45148. cert.pathLenSet = 1;
  45149. cert.sigType = CTC_SHA256wECDSA;
  45150. #ifdef WOLFSSL_CERT_EXT
  45151. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  45152. #endif
  45153. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  45154. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  45155. FOURK_BUF, NULL, &key, &rng), 0);
  45156. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  45157. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45158. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  45159. wc_FreeDecodedCert(&decodedCert);
  45160. ret = wc_ecc_free(&key);
  45161. ExpectIntEQ(ret, 0);
  45162. ret = wc_FreeRng(&rng);
  45163. ExpectIntEQ(ret, 0);
  45164. #endif
  45165. return EXPECT_RESULT();
  45166. }
  45167. static int test_MakeCertWithCaFalse(void)
  45168. {
  45169. EXPECT_DECLS;
  45170. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  45171. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  45172. const byte expectedIsCa = 0;
  45173. Cert cert;
  45174. DecodedCert decodedCert;
  45175. byte der[FOURK_BUF];
  45176. int derSize = 0;
  45177. WC_RNG rng;
  45178. ecc_key key;
  45179. int ret;
  45180. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45181. XMEMSET(&key, 0, sizeof(ecc_key));
  45182. XMEMSET(&cert, 0, sizeof(Cert));
  45183. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  45184. ExpectIntEQ(wc_InitRng(&rng), 0);
  45185. ExpectIntEQ(wc_ecc_init(&key), 0);
  45186. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  45187. ExpectIntEQ(wc_InitCert(&cert), 0);
  45188. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  45189. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  45190. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  45191. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  45192. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  45193. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  45194. CTC_NAME_SIZE);
  45195. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  45196. CTC_NAME_SIZE);
  45197. cert.selfSigned = 1;
  45198. cert.isCA = expectedIsCa;
  45199. cert.isCaSet = 1;
  45200. cert.sigType = CTC_SHA256wECDSA;
  45201. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  45202. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  45203. FOURK_BUF, NULL, &key, &rng), 0);
  45204. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  45205. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45206. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  45207. wc_FreeDecodedCert(&decodedCert);
  45208. ret = wc_ecc_free(&key);
  45209. ExpectIntEQ(ret, 0);
  45210. ret = wc_FreeRng(&rng);
  45211. ExpectIntEQ(ret, 0);
  45212. #endif
  45213. return EXPECT_RESULT();
  45214. }
  45215. /*----------------------------------------------------------------------------*
  45216. | wolfCrypt ECC
  45217. *----------------------------------------------------------------------------*/
  45218. static int test_wc_ecc_get_curve_size_from_name(void)
  45219. {
  45220. EXPECT_DECLS;
  45221. #ifdef HAVE_ECC
  45222. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45223. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  45224. #endif
  45225. /* invalid case */
  45226. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  45227. /* NULL input */
  45228. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  45229. #endif /* HAVE_ECC */
  45230. return EXPECT_RESULT();
  45231. }
  45232. static int test_wc_ecc_get_curve_id_from_name(void)
  45233. {
  45234. EXPECT_DECLS;
  45235. #ifdef HAVE_ECC
  45236. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45237. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  45238. ECC_SECP256R1);
  45239. #endif
  45240. /* invalid case */
  45241. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  45242. /* NULL input */
  45243. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  45244. #endif /* HAVE_ECC */
  45245. return EXPECT_RESULT();
  45246. }
  45247. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  45248. !defined(HAVE_SELFTEST) && \
  45249. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  45250. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  45251. {
  45252. EXPECT_DECLS;
  45253. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45254. ecc_key* key;
  45255. const ecc_set_type* params = NULL;
  45256. int ret;
  45257. #endif
  45258. WOLFSSL_EC_KEY *ecKey = NULL;
  45259. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45260. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  45261. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  45262. if (EXPECT_SUCCESS()) {
  45263. ret = EC_KEY_generate_key(ecKey);
  45264. } else
  45265. ret = 0;
  45266. if (ret == 1) {
  45267. /* normal test */
  45268. key = (ecc_key*)ecKey->internal;
  45269. if (key != NULL) {
  45270. params = key->dp;
  45271. }
  45272. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  45273. ECC_SECP256R1);
  45274. }
  45275. #endif
  45276. /* invalid case, NULL input*/
  45277. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  45278. wolfSSL_EC_KEY_free(ecKey);
  45279. return EXPECT_RESULT();
  45280. }
  45281. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  45282. static int test_wc_ecc_get_curve_id_from_params(void)
  45283. {
  45284. EXPECT_DECLS;
  45285. #ifdef HAVE_ECC
  45286. const byte prime[] =
  45287. {
  45288. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45289. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45290. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45291. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  45292. };
  45293. const byte primeInvalid[] =
  45294. {
  45295. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45296. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45297. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45298. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  45299. };
  45300. const byte Af[] =
  45301. {
  45302. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45303. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45304. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45305. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  45306. };
  45307. const byte Bf[] =
  45308. {
  45309. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  45310. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  45311. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  45312. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  45313. };
  45314. const byte order[] =
  45315. {
  45316. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  45317. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  45318. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  45319. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  45320. };
  45321. const byte Gx[] =
  45322. {
  45323. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  45324. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  45325. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  45326. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  45327. };
  45328. const byte Gy[] =
  45329. {
  45330. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  45331. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  45332. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  45333. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  45334. };
  45335. int cofactor = 1;
  45336. int fieldSize = 256;
  45337. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45338. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  45339. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  45340. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  45341. ECC_SECP256R1);
  45342. #endif
  45343. /* invalid case, fieldSize = 0 */
  45344. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  45345. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45346. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  45347. /* invalid case, NULL prime */
  45348. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  45349. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45350. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  45351. /* invalid case, invalid prime */
  45352. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  45353. primeInvalid, sizeof(primeInvalid),
  45354. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45355. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  45356. #endif
  45357. return EXPECT_RESULT();
  45358. }
  45359. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  45360. {
  45361. EXPECT_DECLS;
  45362. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  45363. WOLFSSL_RSA* rsa = NULL;
  45364. WOLFSSL_EVP_PKEY* pkey = NULL;
  45365. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45366. const char* in = "What is easy to do is easy not to do.";
  45367. size_t inlen = XSTRLEN(in);
  45368. size_t outEncLen = 0;
  45369. byte* outEnc = NULL;
  45370. byte* outDec = NULL;
  45371. size_t outDecLen = 0;
  45372. size_t rsaKeySz = 2048/8; /* Bytes */
  45373. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45374. byte* inTmp = NULL;
  45375. byte* outEncTmp = NULL;
  45376. byte* outDecTmp = NULL;
  45377. #endif
  45378. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45379. DYNAMIC_TYPE_TMP_BUFFER));
  45380. if (outEnc != NULL) {
  45381. XMEMSET(outEnc, 0, rsaKeySz);
  45382. }
  45383. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45384. DYNAMIC_TYPE_TMP_BUFFER));
  45385. if (outDec != NULL) {
  45386. XMEMSET(outDec, 0, rsaKeySz);
  45387. }
  45388. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45389. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45390. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45391. if (EXPECT_FAIL()) {
  45392. RSA_free(rsa);
  45393. }
  45394. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45395. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45396. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45397. WOLFSSL_SUCCESS);
  45398. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  45399. since ctx uses it.*/
  45400. ExpectIntEQ(pkey->ref.count, 2);
  45401. EVP_PKEY_free(pkey);
  45402. ExpectIntEQ(pkey->ref.count, 1);
  45403. /* Encrypt data */
  45404. /* Check that we can get the required output buffer length by passing in a
  45405. * NULL output buffer. */
  45406. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  45407. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45408. ExpectIntEQ(rsaKeySz, outEncLen);
  45409. /* Now do the actual encryption. */
  45410. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  45411. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45412. /* Decrypt data */
  45413. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45414. /* Check that we can get the required output buffer length by passing in a
  45415. * NULL output buffer. */
  45416. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  45417. WOLFSSL_SUCCESS);
  45418. ExpectIntEQ(rsaKeySz, outDecLen);
  45419. /* Now do the actual decryption. */
  45420. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  45421. WOLFSSL_SUCCESS);
  45422. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  45423. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45424. /* The input length must be the same size as the RSA key.*/
  45425. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45426. DYNAMIC_TYPE_TMP_BUFFER));
  45427. if (inTmp != NULL) {
  45428. XMEMSET(inTmp, 9, rsaKeySz);
  45429. }
  45430. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45431. DYNAMIC_TYPE_TMP_BUFFER));
  45432. if (outEncTmp != NULL) {
  45433. XMEMSET(outEncTmp, 0, rsaKeySz);
  45434. }
  45435. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45436. DYNAMIC_TYPE_TMP_BUFFER));
  45437. if (outDecTmp != NULL) {
  45438. XMEMSET(outDecTmp, 0, rsaKeySz);
  45439. }
  45440. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45441. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45442. WOLFSSL_SUCCESS);
  45443. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  45444. WOLFSSL_SUCCESS);
  45445. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45446. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  45447. outEncLen), WOLFSSL_SUCCESS);
  45448. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  45449. #endif
  45450. EVP_PKEY_CTX_free(ctx);
  45451. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45452. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45453. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45454. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45455. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45456. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45457. #endif
  45458. #endif
  45459. return EXPECT_RESULT();
  45460. }
  45461. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45462. !defined(HAVE_SELFTEST)
  45463. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45464. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45465. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45466. #endif
  45467. #endif
  45468. #endif
  45469. #if defined(OPENSSL_EXTRA)
  45470. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45471. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45472. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45473. #endif
  45474. #endif
  45475. #endif
  45476. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45477. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45478. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45479. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45480. #endif
  45481. #endif
  45482. #endif
  45483. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45484. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  45485. {
  45486. EXPECT_DECLS;
  45487. #if defined(OPENSSL_EXTRA)
  45488. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45489. !defined(HAVE_SELFTEST)
  45490. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45491. WOLFSSL_RSA* rsa = NULL;
  45492. #endif
  45493. #endif
  45494. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45495. WOLFSSL_DSA* dsa = NULL;
  45496. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45497. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45498. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45499. WOLFSSL_EC_KEY* ecKey = NULL;
  45500. #endif
  45501. #endif
  45502. WOLFSSL_EVP_PKEY* pkey = NULL;
  45503. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45504. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  45505. const char* in = "What is easy to do is easy not to do.";
  45506. size_t inlen = XSTRLEN(in);
  45507. byte hash[SHA256_DIGEST_LENGTH] = {0};
  45508. byte zero[SHA256_DIGEST_LENGTH] = {0};
  45509. SHA256_CTX c;
  45510. byte* sig = NULL;
  45511. byte* sigVerify = NULL;
  45512. size_t siglen;
  45513. size_t siglenOnlyLen;
  45514. size_t keySz = 2048/8; /* Bytes */
  45515. ExpectNotNull(sig =
  45516. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45517. ExpectNotNull(sigVerify =
  45518. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45519. siglen = keySz;
  45520. ExpectNotNull(XMEMSET(sig, 0, keySz));
  45521. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  45522. /* Generate hash */
  45523. SHA256_Init(&c);
  45524. SHA256_Update(&c, in, inlen);
  45525. SHA256_Final(hash, &c);
  45526. #ifdef WOLFSSL_SMALL_STACK_CACHE
  45527. /* workaround for small stack cache case */
  45528. wc_Sha256Free((wc_Sha256*)&c);
  45529. #endif
  45530. /* Generate key */
  45531. ExpectNotNull(pkey = EVP_PKEY_new());
  45532. switch (keyType) {
  45533. case EVP_PKEY_RSA:
  45534. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45535. !defined(HAVE_SELFTEST)
  45536. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45537. {
  45538. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45539. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45540. }
  45541. #endif
  45542. #endif
  45543. break;
  45544. case EVP_PKEY_DSA:
  45545. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45546. ExpectNotNull(dsa = DSA_new());
  45547. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  45548. NULL, 0, NULL, NULL, NULL), 1);
  45549. ExpectIntEQ(DSA_generate_key(dsa), 1);
  45550. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  45551. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45552. break;
  45553. case EVP_PKEY_EC:
  45554. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45555. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45556. {
  45557. ExpectNotNull(ecKey = EC_KEY_new());
  45558. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  45559. ExpectIntEQ(
  45560. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45561. if (EXPECT_FAIL()) {
  45562. EC_KEY_free(ecKey);
  45563. }
  45564. }
  45565. #endif
  45566. #endif
  45567. break;
  45568. }
  45569. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45570. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45571. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45572. !defined(HAVE_SELFTEST)
  45573. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45574. if (keyType == EVP_PKEY_RSA)
  45575. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45576. WOLFSSL_SUCCESS);
  45577. #endif
  45578. #endif
  45579. /* Check returning only length */
  45580. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  45581. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  45582. ExpectIntGT(siglenOnlyLen, 0);
  45583. /* Sign data */
  45584. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  45585. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  45586. ExpectIntGE(siglenOnlyLen, siglen);
  45587. /* Verify signature */
  45588. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  45589. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45590. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45591. !defined(HAVE_SELFTEST)
  45592. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45593. if (keyType == EVP_PKEY_RSA)
  45594. ExpectIntEQ(
  45595. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  45596. WOLFSSL_SUCCESS);
  45597. #endif
  45598. #endif
  45599. ExpectIntEQ(EVP_PKEY_verify(
  45600. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  45601. WOLFSSL_SUCCESS);
  45602. ExpectIntEQ(EVP_PKEY_verify(
  45603. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  45604. WOLFSSL_FAILURE);
  45605. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45606. !defined(HAVE_SELFTEST)
  45607. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45608. if (keyType == EVP_PKEY_RSA) {
  45609. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  45610. /* Try RSA sign/verify with no padding. */
  45611. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45612. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45613. WOLFSSL_SUCCESS);
  45614. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  45615. siglen), WOLFSSL_SUCCESS);
  45616. ExpectIntGE(siglenOnlyLen, siglen);
  45617. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45618. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45619. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  45620. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  45621. siglen), WOLFSSL_SUCCESS);
  45622. #endif
  45623. /* Wrong padding schemes. */
  45624. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45625. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  45626. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  45627. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  45628. siglen), WOLFSSL_SUCCESS);
  45629. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45630. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45631. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  45632. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  45633. siglen), WOLFSSL_SUCCESS);
  45634. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45635. WOLFSSL_SUCCESS);
  45636. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45637. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  45638. }
  45639. #endif
  45640. #endif
  45641. /* error cases */
  45642. siglen = keySz; /* Reset because sig size may vary slightly */
  45643. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  45644. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45645. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  45646. WOLFSSL_SUCCESS);
  45647. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  45648. WOLFSSL_SUCCESS);
  45649. EVP_PKEY_free(pkey);
  45650. pkey = NULL;
  45651. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45652. DSA_free(dsa);
  45653. dsa = NULL;
  45654. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45655. EVP_PKEY_CTX_free(ctx_verify);
  45656. ctx_verify = NULL;
  45657. EVP_PKEY_CTX_free(ctx);
  45658. ctx = NULL;
  45659. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45660. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45661. #endif /* OPENSSL_EXTRA */
  45662. return EXPECT_RESULT();
  45663. }
  45664. #endif
  45665. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  45666. {
  45667. EXPECT_DECLS;
  45668. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45669. !defined(HAVE_SELFTEST)
  45670. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45671. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  45672. #endif
  45673. #endif
  45674. return EXPECT_RESULT();
  45675. }
  45676. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  45677. {
  45678. EXPECT_DECLS;
  45679. #if defined(OPENSSL_EXTRA)
  45680. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45681. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  45682. #endif
  45683. #endif
  45684. return EXPECT_RESULT();
  45685. }
  45686. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  45687. {
  45688. EXPECT_DECLS;
  45689. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45690. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45691. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  45692. #endif
  45693. #endif
  45694. return EXPECT_RESULT();
  45695. }
  45696. static int test_EVP_PKEY_rsa(void)
  45697. {
  45698. EXPECT_DECLS;
  45699. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45700. WOLFSSL_RSA* rsa = NULL;
  45701. WOLFSSL_EVP_PKEY* pkey = NULL;
  45702. ExpectNotNull(rsa = wolfSSL_RSA_new());
  45703. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45704. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  45705. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  45706. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45707. if (EXPECT_FAIL()) {
  45708. wolfSSL_RSA_free(rsa);
  45709. }
  45710. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  45711. wolfSSL_EVP_PKEY_free(pkey);
  45712. #endif
  45713. return EXPECT_RESULT();
  45714. }
  45715. static int test_EVP_PKEY_ec(void)
  45716. {
  45717. EXPECT_DECLS;
  45718. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45719. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45720. WOLFSSL_EC_KEY* ecKey = NULL;
  45721. WOLFSSL_EVP_PKEY* pkey = NULL;
  45722. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  45723. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45724. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  45725. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  45726. /* Should fail since ecKey is empty */
  45727. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  45728. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45729. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45730. if (EXPECT_FAIL()) {
  45731. wolfSSL_EC_KEY_free(ecKey);
  45732. }
  45733. wolfSSL_EVP_PKEY_free(pkey);
  45734. #endif
  45735. #endif
  45736. return EXPECT_RESULT();
  45737. }
  45738. static int test_EVP_PKEY_cmp(void)
  45739. {
  45740. EXPECT_DECLS;
  45741. #if defined(OPENSSL_EXTRA)
  45742. EVP_PKEY *a = NULL;
  45743. EVP_PKEY *b = NULL;
  45744. const unsigned char *in;
  45745. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45746. in = client_key_der_2048;
  45747. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45748. &in, (long)sizeof_client_key_der_2048));
  45749. in = client_key_der_2048;
  45750. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45751. &in, (long)sizeof_client_key_der_2048));
  45752. /* Test success case RSA */
  45753. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45754. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  45755. #else
  45756. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45757. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45758. EVP_PKEY_free(b);
  45759. b = NULL;
  45760. EVP_PKEY_free(a);
  45761. a = NULL;
  45762. #endif
  45763. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45764. in = ecc_clikey_der_256;
  45765. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45766. &in, (long)sizeof_ecc_clikey_der_256));
  45767. in = ecc_clikey_der_256;
  45768. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45769. &in, (long)sizeof_ecc_clikey_der_256));
  45770. /* Test success case ECC */
  45771. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45772. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  45773. #else
  45774. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45775. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45776. EVP_PKEY_free(b);
  45777. b = NULL;
  45778. EVP_PKEY_free(a);
  45779. a = NULL;
  45780. #endif
  45781. /* Test failure cases */
  45782. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  45783. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45784. in = client_key_der_2048;
  45785. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45786. &in, (long)sizeof_client_key_der_2048));
  45787. in = ecc_clikey_der_256;
  45788. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45789. &in, (long)sizeof_ecc_clikey_der_256));
  45790. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45791. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  45792. #else
  45793. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  45794. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45795. EVP_PKEY_free(b);
  45796. b = NULL;
  45797. EVP_PKEY_free(a);
  45798. a = NULL;
  45799. #endif
  45800. /* invalid or empty failure cases */
  45801. a = EVP_PKEY_new();
  45802. b = EVP_PKEY_new();
  45803. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45804. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  45805. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  45806. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  45807. #ifdef NO_RSA
  45808. /* Type check will fail since RSA is the default EVP key type */
  45809. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  45810. #else
  45811. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45812. #endif
  45813. #else
  45814. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  45815. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  45816. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  45817. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  45818. #endif
  45819. EVP_PKEY_free(b);
  45820. EVP_PKEY_free(a);
  45821. (void)in;
  45822. #endif
  45823. return EXPECT_RESULT();
  45824. }
  45825. static int test_ERR_load_crypto_strings(void)
  45826. {
  45827. #if defined(OPENSSL_ALL)
  45828. ERR_load_crypto_strings();
  45829. return TEST_SUCCESS;
  45830. #else
  45831. return TEST_SKIPPED;
  45832. #endif
  45833. }
  45834. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45835. static void free_x509(X509* x)
  45836. {
  45837. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  45838. }
  45839. #endif
  45840. static int test_sk_X509(void)
  45841. {
  45842. EXPECT_DECLS;
  45843. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45844. {
  45845. STACK_OF(X509)* s = NULL;
  45846. ExpectNotNull(s = sk_X509_new_null());
  45847. ExpectIntEQ(sk_X509_num(s), 0);
  45848. sk_X509_pop_free(s, NULL);
  45849. ExpectNotNull(s = sk_X509_new_null());
  45850. ExpectIntEQ(sk_X509_num(s), 0);
  45851. sk_X509_pop_free(s, NULL);
  45852. ExpectNotNull(s = sk_X509_new_null());
  45853. sk_X509_push(s, (X509*)1);
  45854. ExpectIntEQ(sk_X509_num(s), 1);
  45855. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  45856. sk_X509_push(s, (X509*)2);
  45857. ExpectIntEQ(sk_X509_num(s), 2);
  45858. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  45859. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  45860. sk_X509_push(s, (X509*)2);
  45861. sk_X509_pop_free(s, free_x509);
  45862. }
  45863. {
  45864. /* Push a list of 10 X509s onto stack, then verify that
  45865. * value(), push(), shift(), and pop() behave as expected. */
  45866. STACK_OF(X509)* s = NULL;
  45867. X509* xList[10];
  45868. int i = 0;
  45869. const int len = (sizeof(xList) / sizeof(xList[0]));
  45870. for (i = 0; i < len; ++i) {
  45871. xList[i] = NULL;
  45872. ExpectNotNull(xList[i] = X509_new());
  45873. }
  45874. /* test push, pop, and free */
  45875. ExpectNotNull(s = sk_X509_new_null());
  45876. for (i = 0; i < len; ++i) {
  45877. sk_X509_push(s, xList[i]);
  45878. ExpectIntEQ(sk_X509_num(s), i + 1);
  45879. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45880. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45881. }
  45882. /* pop returns and removes last pushed on stack, which is index 0
  45883. * in sk_x509_value */
  45884. for (i = 0; i < len; ++i) {
  45885. X509 * x = sk_X509_value(s, 0);
  45886. X509 * y = sk_X509_pop(s);
  45887. X509 * z = xList[len - 1 - i];
  45888. ExpectIntEQ((x == y), 1);
  45889. ExpectIntEQ((x == z), 1);
  45890. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  45891. }
  45892. sk_free(s);
  45893. s = NULL;
  45894. /* test push, shift, and free */
  45895. ExpectNotNull(s = sk_X509_new_null());
  45896. for (i = 0; i < len; ++i) {
  45897. sk_X509_push(s, xList[i]);
  45898. ExpectIntEQ(sk_X509_num(s), i + 1);
  45899. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45900. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45901. }
  45902. /* shift returns and removes first pushed on stack, which is index i
  45903. * in sk_x509_value() */
  45904. for (i = 0; i < len; ++i) {
  45905. X509 * x = sk_X509_value(s, len - 1 - i);
  45906. X509 * y = sk_X509_shift(s);
  45907. X509 * z = xList[i];
  45908. ExpectIntEQ((x == y), 1);
  45909. ExpectIntEQ((x == z), 1);
  45910. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  45911. }
  45912. sk_free(s);
  45913. for (i = 0; i < len; ++i)
  45914. X509_free(xList[i]);
  45915. }
  45916. #endif
  45917. return EXPECT_RESULT();
  45918. }
  45919. static int test_sk_X509_CRL(void)
  45920. {
  45921. EXPECT_DECLS;
  45922. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  45923. X509_CRL* crl = NULL;
  45924. XFILE fp = XBADFILE;
  45925. STACK_OF(X509_CRL)* s = NULL;
  45926. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  45927. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45928. NULL, NULL));
  45929. if (fp != XBADFILE)
  45930. XFCLOSE(fp);
  45931. ExpectNotNull(s = sk_X509_CRL_new());
  45932. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  45933. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  45934. if (EXPECT_FAIL()) {
  45935. X509_CRL_free(crl);
  45936. }
  45937. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  45938. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  45939. sk_X509_CRL_free(s);
  45940. #endif
  45941. return EXPECT_RESULT();
  45942. }
  45943. static int test_X509_get_signature_nid(void)
  45944. {
  45945. EXPECT_DECLS;
  45946. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45947. X509* x509 = NULL;
  45948. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  45949. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45950. SSL_FILETYPE_PEM));
  45951. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  45952. X509_free(x509);
  45953. #endif
  45954. return EXPECT_RESULT();
  45955. }
  45956. static int test_X509_REQ(void)
  45957. {
  45958. EXPECT_DECLS;
  45959. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  45960. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  45961. X509_NAME* name = NULL;
  45962. #ifndef NO_RSA
  45963. X509_NAME* subject = NULL;
  45964. #endif
  45965. #if !defined(NO_RSA) || defined(HAVE_ECC)
  45966. X509_REQ* req = NULL;
  45967. EVP_PKEY* priv = NULL;
  45968. EVP_PKEY* pub = NULL;
  45969. unsigned char* der = NULL;
  45970. int len;
  45971. #endif
  45972. #ifndef NO_RSA
  45973. EVP_MD_CTX *mctx = NULL;
  45974. EVP_PKEY_CTX *pkctx = NULL;
  45975. #ifdef USE_CERT_BUFFERS_1024
  45976. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  45977. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  45978. #elif defined(USE_CERT_BUFFERS_2048)
  45979. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  45980. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  45981. #endif
  45982. #endif
  45983. #ifdef HAVE_ECC
  45984. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  45985. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  45986. #endif
  45987. ExpectNotNull(name = X509_NAME_new());
  45988. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  45989. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  45990. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  45991. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  45992. #ifndef NO_RSA
  45993. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  45994. (long)sizeof_client_key_der_2048));
  45995. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  45996. (long)sizeof_client_keypub_der_2048));
  45997. ExpectNotNull(req = X509_REQ_new());
  45998. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  45999. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  46000. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  46001. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  46002. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  46003. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46004. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  46005. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  46006. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  46007. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46008. len = i2d_X509_REQ(req, &der);
  46009. DEBUG_WRITE_DER(der, len, "req.der");
  46010. #ifdef USE_CERT_BUFFERS_1024
  46011. ExpectIntEQ(len, 381);
  46012. #else
  46013. ExpectIntEQ(len, 643);
  46014. #endif
  46015. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46016. der = NULL;
  46017. mctx = EVP_MD_CTX_new();
  46018. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  46019. WOLFSSL_SUCCESS);
  46020. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  46021. EVP_MD_CTX_free(mctx);
  46022. mctx = NULL;
  46023. X509_REQ_free(NULL);
  46024. X509_REQ_free(req);
  46025. req = NULL;
  46026. /* Test getting the subject from a newly created X509_REQ */
  46027. ExpectNotNull(req = X509_REQ_new());
  46028. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  46029. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  46030. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  46031. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  46032. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  46033. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  46034. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  46035. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  46036. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  46037. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  46038. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  46039. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  46040. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  46041. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46042. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46043. len = i2d_X509_REQ(req, &der);
  46044. DEBUG_WRITE_DER(der, len, "req2.der");
  46045. #ifdef USE_CERT_BUFFERS_1024
  46046. ExpectIntEQ(len, 435);
  46047. #else
  46048. ExpectIntEQ(len, 696);
  46049. #endif
  46050. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46051. der = NULL;
  46052. EVP_PKEY_free(pub);
  46053. pub = NULL;
  46054. EVP_PKEY_free(priv);
  46055. priv = NULL;
  46056. X509_REQ_free(req);
  46057. req = NULL;
  46058. #endif
  46059. #ifdef HAVE_ECC
  46060. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  46061. sizeof_ecc_clikey_der_256));
  46062. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  46063. sizeof_ecc_clikeypub_der_256));
  46064. ExpectNotNull(req = X509_REQ_new());
  46065. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  46066. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46067. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46068. /* Signature is random and may be shorter or longer. */
  46069. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  46070. ExpectIntLE(len, 253);
  46071. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46072. X509_REQ_free(req);
  46073. EVP_PKEY_free(pub);
  46074. EVP_PKEY_free(priv);
  46075. #ifdef FP_ECC
  46076. wc_ecc_fp_free();
  46077. #endif
  46078. #endif /* HAVE_ECC */
  46079. X509_NAME_free(name);
  46080. #endif
  46081. return EXPECT_RESULT();
  46082. }
  46083. static int test_wolfssl_PKCS7(void)
  46084. {
  46085. EXPECT_DECLS;
  46086. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  46087. !defined(NO_RSA)
  46088. PKCS7* pkcs7 = NULL;
  46089. byte data[FOURK_BUF];
  46090. word32 len = sizeof(data);
  46091. const byte* p = data;
  46092. byte content[] = "Test data to encode.";
  46093. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  46094. BIO* bio = NULL;
  46095. byte key[sizeof(client_key_der_2048)];
  46096. word32 keySz = (word32)sizeof(key);
  46097. byte* out = NULL;
  46098. #endif
  46099. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  46100. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  46101. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  46102. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  46103. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46104. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  46105. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  46106. PKCS7_free(pkcs7);
  46107. pkcs7 = NULL;
  46108. /* fail case, without PKCS7_NOVERIFY */
  46109. p = data;
  46110. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46111. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  46112. 0), WOLFSSL_FAILURE);
  46113. PKCS7_free(pkcs7);
  46114. pkcs7 = NULL;
  46115. /* success case, with PKCS7_NOVERIFY */
  46116. p = data;
  46117. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46118. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  46119. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  46120. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  46121. /* test i2d */
  46122. XMEMCPY(key, client_key_der_2048, keySz);
  46123. if (pkcs7 != NULL) {
  46124. pkcs7->privateKey = key;
  46125. pkcs7->privateKeySz = (word32)sizeof(key);
  46126. pkcs7->encryptOID = RSAk;
  46127. #ifdef NO_SHA
  46128. pkcs7->hashOID = SHA256h;
  46129. #else
  46130. pkcs7->hashOID = SHAh;
  46131. #endif
  46132. }
  46133. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46134. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  46135. #ifndef NO_ASN_TIME
  46136. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  46137. #else
  46138. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  46139. #endif
  46140. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46141. BIO_free(bio);
  46142. #endif
  46143. PKCS7_free(NULL);
  46144. PKCS7_free(pkcs7);
  46145. #endif
  46146. return EXPECT_RESULT();
  46147. }
  46148. static int test_wolfSSL_PKCS7_sign(void)
  46149. {
  46150. EXPECT_DECLS;
  46151. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  46152. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46153. PKCS7* p7 = NULL;
  46154. PKCS7* p7Ver = NULL;
  46155. byte* out = NULL;
  46156. byte* tmpPtr = NULL;
  46157. int outLen = 0;
  46158. int flags = 0;
  46159. byte data[] = "Test data to encode.";
  46160. const char* cert = "./certs/server-cert.pem";
  46161. const char* key = "./certs/server-key.pem";
  46162. const char* ca = "./certs/ca-cert.pem";
  46163. WOLFSSL_BIO* certBio = NULL;
  46164. WOLFSSL_BIO* keyBio = NULL;
  46165. WOLFSSL_BIO* caBio = NULL;
  46166. WOLFSSL_BIO* inBio = NULL;
  46167. X509* signCert = NULL;
  46168. EVP_PKEY* signKey = NULL;
  46169. X509* caCert = NULL;
  46170. X509_STORE* store = NULL;
  46171. /* read signer cert/key into BIO */
  46172. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  46173. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  46174. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  46175. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  46176. /* read CA cert into store (for verify) */
  46177. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  46178. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  46179. ExpectNotNull(store = X509_STORE_new());
  46180. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  46181. /* data to be signed into BIO */
  46182. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46183. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46184. /* PKCS7_sign, bad args: signer NULL */
  46185. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  46186. /* PKCS7_sign, bad args: signer key NULL */
  46187. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  46188. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  46189. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  46190. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  46191. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  46192. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  46193. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  46194. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  46195. {
  46196. flags = PKCS7_BINARY;
  46197. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46198. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46199. /* verify with d2i_PKCS7 */
  46200. tmpPtr = out;
  46201. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46202. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46203. PKCS7_free(p7Ver);
  46204. p7Ver = NULL;
  46205. /* verify with wc_PKCS7_VerifySignedData */
  46206. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46207. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  46208. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46209. /* compare the signer found to expected signer */
  46210. ExpectIntNE(p7Ver->verifyCertSz, 0);
  46211. tmpPtr = NULL;
  46212. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  46213. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  46214. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  46215. tmpPtr = NULL;
  46216. wc_PKCS7_Free(p7Ver);
  46217. p7Ver = NULL;
  46218. ExpectNotNull(out);
  46219. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46220. out = NULL;
  46221. PKCS7_free(p7);
  46222. p7 = NULL;
  46223. }
  46224. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  46225. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  46226. {
  46227. /* re-populate input BIO, may have been consumed */
  46228. BIO_free(inBio);
  46229. inBio = NULL;
  46230. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46231. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46232. flags = PKCS7_BINARY | PKCS7_STREAM;
  46233. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46234. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  46235. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46236. /* PKCS7_final, bad args: PKCS7 null */
  46237. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  46238. /* PKCS7_final, bad args: PKCS7 null */
  46239. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  46240. tmpPtr = out;
  46241. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46242. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46243. PKCS7_free(p7Ver);
  46244. p7Ver = NULL;
  46245. ExpectNotNull(out);
  46246. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46247. out = NULL;
  46248. PKCS7_free(p7);
  46249. p7 = NULL;
  46250. }
  46251. /* TEST SUCCESS: Detached, not streaming, not MIME */
  46252. {
  46253. /* re-populate input BIO, may have been consumed */
  46254. BIO_free(inBio);
  46255. inBio = NULL;
  46256. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46257. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46258. flags = PKCS7_BINARY | PKCS7_DETACHED;
  46259. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46260. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46261. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  46262. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46263. if (p7Ver != NULL) {
  46264. p7Ver->content = data;
  46265. p7Ver->contentSz = sizeof(data);
  46266. }
  46267. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46268. wc_PKCS7_Free(p7Ver);
  46269. p7Ver = NULL;
  46270. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  46271. * yet support detached content */
  46272. tmpPtr = out;
  46273. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46274. PKCS7_free(p7Ver);
  46275. p7Ver = NULL;
  46276. ExpectNotNull(out);
  46277. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46278. out = NULL;
  46279. PKCS7_free(p7);
  46280. p7 = NULL;
  46281. }
  46282. /* TEST SUCCESS: Detached, streaming, not MIME */
  46283. {
  46284. /* re-populate input BIO, may have been consumed */
  46285. BIO_free(inBio);
  46286. inBio = NULL;
  46287. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46288. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46289. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  46290. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46291. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  46292. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46293. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  46294. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46295. if (p7Ver != NULL) {
  46296. p7Ver->content = data;
  46297. p7Ver->contentSz = sizeof(data);
  46298. }
  46299. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46300. wc_PKCS7_Free(p7Ver);
  46301. p7Ver = NULL;
  46302. ExpectNotNull(out);
  46303. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46304. PKCS7_free(p7);
  46305. p7 = NULL;
  46306. }
  46307. X509_STORE_free(store);
  46308. X509_free(caCert);
  46309. X509_free(signCert);
  46310. EVP_PKEY_free(signKey);
  46311. BIO_free(inBio);
  46312. BIO_free(keyBio);
  46313. BIO_free(certBio);
  46314. BIO_free(caBio);
  46315. #endif
  46316. return EXPECT_RESULT();
  46317. }
  46318. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  46319. {
  46320. EXPECT_DECLS;
  46321. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  46322. PKCS7_SIGNED* pkcs7 = NULL;
  46323. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  46324. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  46325. PKCS7_SIGNED_free(pkcs7);
  46326. #endif
  46327. return EXPECT_RESULT();
  46328. }
  46329. #ifndef NO_BIO
  46330. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  46331. {
  46332. EXPECT_DECLS;
  46333. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  46334. PKCS7* pkcs7 = NULL;
  46335. BIO* bio = NULL;
  46336. const byte* cert_buf = NULL;
  46337. int ret = 0;
  46338. WC_RNG rng;
  46339. const byte data[] = { /* Hello World */
  46340. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  46341. 0x72,0x6c,0x64
  46342. };
  46343. #ifndef NO_RSA
  46344. #if defined(USE_CERT_BUFFERS_2048)
  46345. byte key[sizeof(client_key_der_2048)];
  46346. byte cert[sizeof(client_cert_der_2048)];
  46347. word32 keySz = (word32)sizeof(key);
  46348. word32 certSz = (word32)sizeof(cert);
  46349. XMEMSET(key, 0, keySz);
  46350. XMEMSET(cert, 0, certSz);
  46351. XMEMCPY(key, client_key_der_2048, keySz);
  46352. XMEMCPY(cert, client_cert_der_2048, certSz);
  46353. #elif defined(USE_CERT_BUFFERS_1024)
  46354. byte key[sizeof_client_key_der_1024];
  46355. byte cert[sizeof(sizeof_client_cert_der_1024)];
  46356. word32 keySz = (word32)sizeof(key);
  46357. word32 certSz = (word32)sizeof(cert);
  46358. XMEMSET(key, 0, keySz);
  46359. XMEMSET(cert, 0, certSz);
  46360. XMEMCPY(key, client_key_der_1024, keySz);
  46361. XMEMCPY(cert, client_cert_der_1024, certSz);
  46362. #else
  46363. unsigned char cert[ONEK_BUF];
  46364. unsigned char key[ONEK_BUF];
  46365. XFILE fp = XBADFILE;
  46366. int certSz;
  46367. int keySz;
  46368. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  46369. XBADFILE);
  46370. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  46371. fp), 0);
  46372. if (fp != XBADFILE) {
  46373. XFCLOSE(fp);
  46374. fp = XBADFILE;
  46375. }
  46376. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  46377. XBADFILE);
  46378. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  46379. 0);
  46380. if (fp != XBADFILE) {
  46381. XFCLOSE(fp);
  46382. fp = XBADFILE;
  46383. }
  46384. #endif
  46385. #elif defined(HAVE_ECC)
  46386. #if defined(USE_CERT_BUFFERS_256)
  46387. unsigned char cert[sizeof(cliecc_cert_der_256)];
  46388. unsigned char key[sizeof(ecc_clikey_der_256)];
  46389. int certSz = (int)sizeof(cert);
  46390. int keySz = (int)sizeof(key);
  46391. XMEMSET(cert, 0, certSz);
  46392. XMEMSET(key, 0, keySz);
  46393. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  46394. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  46395. #else
  46396. unsigned char cert[ONEK_BUF];
  46397. unsigned char key[ONEK_BUF];
  46398. XFILE fp = XBADFILE;
  46399. int certSz, keySz;
  46400. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  46401. XBADFILE);
  46402. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  46403. fp), 0);
  46404. if (fp != XBADFILE) {
  46405. XFCLOSE(fp);
  46406. fp = XBADFILE;
  46407. }
  46408. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  46409. XBADFILE);
  46410. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  46411. 0);
  46412. if (fp != XBADFILE) {
  46413. XFCLOSE(fp);
  46414. fp = XBADFILE;
  46415. }
  46416. #endif
  46417. #else
  46418. #error PKCS7 requires ECC or RSA
  46419. #endif
  46420. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  46421. /* initialize with DER encoded cert */
  46422. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  46423. /* init rng */
  46424. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46425. ExpectIntEQ(wc_InitRng(&rng), 0);
  46426. if (pkcs7 != NULL) {
  46427. pkcs7->rng = &rng;
  46428. pkcs7->content = (byte*)data; /* not used for ex */
  46429. pkcs7->contentSz = (word32)sizeof(data);
  46430. pkcs7->contentOID = SIGNED_DATA;
  46431. pkcs7->privateKey = key;
  46432. pkcs7->privateKeySz = (word32)sizeof(key);
  46433. pkcs7->encryptOID = RSAk;
  46434. #ifdef NO_SHA
  46435. pkcs7->hashOID = SHA256h;
  46436. #else
  46437. pkcs7->hashOID = SHAh;
  46438. #endif
  46439. pkcs7->signedAttribs = NULL;
  46440. pkcs7->signedAttribsSz = 0;
  46441. }
  46442. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46443. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  46444. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  46445. /* Read PKCS#7 PEM from BIO */
  46446. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  46447. ExpectIntGE(ret, 0);
  46448. BIO_free(bio);
  46449. wc_PKCS7_Free(pkcs7);
  46450. wc_FreeRng(&rng);
  46451. #endif
  46452. return EXPECT_RESULT();
  46453. }
  46454. #ifdef HAVE_SMIME
  46455. static int test_wolfSSL_SMIME_read_PKCS7(void)
  46456. {
  46457. EXPECT_DECLS;
  46458. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  46459. !defined(NO_RSA)
  46460. PKCS7* pkcs7 = NULL;
  46461. BIO* bio = NULL;
  46462. BIO* bcont = NULL;
  46463. BIO* out = NULL;
  46464. const byte* outBuf = NULL;
  46465. int outBufLen = 0;
  46466. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  46467. XFILE smimeTestFile = XBADFILE;
  46468. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  46469. XBADFILE);
  46470. /* smime-test.p7s */
  46471. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  46472. ExpectNotNull(bio);
  46473. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46474. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46475. ExpectNotNull(pkcs7);
  46476. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46477. PKCS7_NOVERIFY), SSL_SUCCESS);
  46478. XFCLOSE(smimeTestFile);
  46479. if (bcont) BIO_free(bcont);
  46480. bcont = NULL;
  46481. wolfSSL_PKCS7_free(pkcs7);
  46482. pkcs7 = NULL;
  46483. /* smime-test-multipart.p7s */
  46484. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  46485. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46486. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46487. ExpectNotNull(pkcs7);
  46488. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46489. PKCS7_NOVERIFY), SSL_SUCCESS);
  46490. XFCLOSE(smimeTestFile);
  46491. if (bcont) BIO_free(bcont);
  46492. bcont = NULL;
  46493. wolfSSL_PKCS7_free(pkcs7);
  46494. pkcs7 = NULL;
  46495. /* smime-test-multipart-badsig.p7s */
  46496. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  46497. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46498. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46499. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  46500. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46501. PKCS7_NOVERIFY), SSL_FAILURE);
  46502. XFCLOSE(smimeTestFile);
  46503. if (bcont) BIO_free(bcont);
  46504. bcont = NULL;
  46505. wolfSSL_PKCS7_free(pkcs7);
  46506. pkcs7 = NULL;
  46507. /* smime-test-canon.p7s */
  46508. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  46509. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46510. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46511. ExpectNotNull(pkcs7);
  46512. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46513. PKCS7_NOVERIFY), SSL_SUCCESS);
  46514. XFCLOSE(smimeTestFile);
  46515. if (bcont) BIO_free(bcont);
  46516. bcont = NULL;
  46517. wolfSSL_PKCS7_free(pkcs7);
  46518. pkcs7 = NULL;
  46519. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  46520. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  46521. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46522. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46523. ExpectNotNull(pkcs7);
  46524. out = wolfSSL_BIO_new(BIO_s_mem());
  46525. ExpectNotNull(out);
  46526. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  46527. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  46528. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  46529. /* Content-Type should not show up at beginning of output buffer */
  46530. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  46531. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  46532. BIO_free(out);
  46533. BIO_free(bio);
  46534. if (bcont) BIO_free(bcont);
  46535. wolfSSL_PKCS7_free(pkcs7);
  46536. #endif
  46537. return EXPECT_RESULT();
  46538. }
  46539. static int test_wolfSSL_SMIME_write_PKCS7(void)
  46540. {
  46541. EXPECT_DECLS;
  46542. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  46543. PKCS7* p7 = NULL;
  46544. PKCS7* p7Ver = NULL;
  46545. int flags = 0;
  46546. byte data[] = "Test data to encode.";
  46547. const char* cert = "./certs/server-cert.pem";
  46548. const char* key = "./certs/server-key.pem";
  46549. const char* ca = "./certs/ca-cert.pem";
  46550. WOLFSSL_BIO* certBio = NULL;
  46551. WOLFSSL_BIO* keyBio = NULL;
  46552. WOLFSSL_BIO* caBio = NULL;
  46553. WOLFSSL_BIO* inBio = NULL;
  46554. WOLFSSL_BIO* outBio = NULL;
  46555. WOLFSSL_BIO* content = NULL;
  46556. X509* signCert = NULL;
  46557. EVP_PKEY* signKey = NULL;
  46558. X509* caCert = NULL;
  46559. X509_STORE* store = NULL;
  46560. /* read signer cert/key into BIO */
  46561. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  46562. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  46563. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  46564. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  46565. /* read CA cert into store (for verify) */
  46566. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  46567. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  46568. ExpectNotNull(store = X509_STORE_new());
  46569. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  46570. /* generate and verify SMIME: not detached */
  46571. {
  46572. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46573. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46574. flags = PKCS7_STREAM;
  46575. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46576. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  46577. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46578. /* bad arg: out NULL */
  46579. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  46580. /* bad arg: pkcs7 NULL */
  46581. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  46582. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46583. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46584. BIO_free(content);
  46585. content = NULL;
  46586. BIO_free(inBio);
  46587. inBio = NULL;
  46588. BIO_free(outBio);
  46589. outBio = NULL;
  46590. PKCS7_free(p7Ver);
  46591. p7Ver = NULL;
  46592. PKCS7_free(p7);
  46593. p7 = NULL;
  46594. }
  46595. /* generate and verify SMIME: not detached, add Content-Type */
  46596. {
  46597. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46598. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46599. flags = PKCS7_STREAM | PKCS7_TEXT;
  46600. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46601. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  46602. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46603. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46604. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46605. BIO_free(content);
  46606. content = NULL;
  46607. BIO_free(inBio);
  46608. inBio = NULL;
  46609. BIO_free(outBio);
  46610. outBio = NULL;
  46611. PKCS7_free(p7Ver);
  46612. p7Ver = NULL;
  46613. PKCS7_free(p7);
  46614. p7 = NULL;
  46615. }
  46616. /* generate and verify SMIME: detached */
  46617. {
  46618. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46619. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46620. flags = PKCS7_DETACHED | PKCS7_STREAM;
  46621. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46622. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  46623. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46624. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46625. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  46626. BIO_free(content);
  46627. content = NULL;
  46628. BIO_free(inBio);
  46629. inBio = NULL;
  46630. BIO_free(outBio);
  46631. outBio = NULL;
  46632. PKCS7_free(p7Ver);
  46633. p7Ver = NULL;
  46634. PKCS7_free(p7);
  46635. p7 = NULL;
  46636. }
  46637. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  46638. {
  46639. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46640. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46641. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  46642. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46643. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  46644. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46645. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46646. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  46647. BIO_free(content);
  46648. content = NULL;
  46649. BIO_free(inBio);
  46650. inBio = NULL;
  46651. BIO_free(outBio);
  46652. outBio = NULL;
  46653. PKCS7_free(p7Ver);
  46654. p7Ver = NULL;
  46655. PKCS7_free(p7);
  46656. p7 = NULL;
  46657. }
  46658. X509_STORE_free(store);
  46659. X509_free(caCert);
  46660. X509_free(signCert);
  46661. EVP_PKEY_free(signKey);
  46662. BIO_free(keyBio);
  46663. BIO_free(certBio);
  46664. BIO_free(caBio);
  46665. #endif
  46666. return EXPECT_RESULT();
  46667. }
  46668. #endif /* HAVE_SMIME */
  46669. #endif /* !NO_BIO */
  46670. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  46671. * returns 0) */
  46672. static int test_X509_STORE_No_SSL_CTX(void)
  46673. {
  46674. EXPECT_DECLS;
  46675. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  46676. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  46677. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  46678. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  46679. defined(HAVE_CRL) && !defined(NO_RSA)
  46680. X509_STORE * store = NULL;
  46681. X509_STORE_CTX * storeCtx = NULL;
  46682. X509_CRL * crl = NULL;
  46683. X509 * ca = NULL;
  46684. X509 * cert = NULL;
  46685. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  46686. const char srvCert[] = "./certs/server-cert.pem";
  46687. const char caCert[] = "./certs/ca-cert.pem";
  46688. const char caDir[] = "./certs/crl/hash_pem";
  46689. XFILE fp = XBADFILE;
  46690. X509_LOOKUP * lookup = NULL;
  46691. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  46692. /* Set up store with CA */
  46693. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46694. SSL_FILETYPE_PEM)));
  46695. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46696. /* Add CRL lookup directory to store
  46697. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  46698. * of crl.pem */
  46699. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  46700. X509_LOOKUP_hash_dir())));
  46701. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  46702. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  46703. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46704. SSL_SUCCESS);
  46705. /* Add CRL to store NOT containing the verified certificate, which
  46706. * forces use of the CRL lookup directory */
  46707. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  46708. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46709. NULL, NULL));
  46710. if (fp != XBADFILE)
  46711. XFCLOSE(fp);
  46712. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46713. /* Create verification context outside of an SSL session */
  46714. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46715. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46716. SSL_FILETYPE_PEM)));
  46717. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46718. /* Perform verification, which should NOT indicate CRL missing due to the
  46719. * store CM's X509 store pointer being NULL */
  46720. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46721. X509_CRL_free(crl);
  46722. X509_STORE_free(store);
  46723. X509_STORE_CTX_free(storeCtx);
  46724. X509_free(cert);
  46725. X509_free(ca);
  46726. #endif
  46727. return EXPECT_RESULT();
  46728. }
  46729. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  46730. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  46731. static int test_X509_LOOKUP_add_dir(void)
  46732. {
  46733. EXPECT_DECLS;
  46734. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  46735. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  46736. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  46737. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  46738. defined(HAVE_CRL) && !defined(NO_RSA)
  46739. X509_STORE * store = NULL;
  46740. X509_STORE_CTX * storeCtx = NULL;
  46741. X509_CRL * crl = NULL;
  46742. X509 * ca = NULL;
  46743. X509 * cert = NULL;
  46744. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  46745. const char srvCert[] = "./certs/server-cert.pem";
  46746. const char caCert[] = "./certs/ca-cert.pem";
  46747. const char caDir[] = "./certs/crl/hash_der";
  46748. XFILE fp = XBADFILE;
  46749. X509_LOOKUP * lookup = NULL;
  46750. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  46751. /* Set up store with CA */
  46752. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46753. SSL_FILETYPE_PEM)));
  46754. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46755. /* Add CRL lookup directory to store.
  46756. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  46757. * of crl.der */
  46758. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  46759. X509_LOOKUP_hash_dir())));
  46760. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  46761. SSL_SUCCESS);
  46762. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46763. SSL_SUCCESS);
  46764. /* Add CRL to store NOT containing the verified certificate, which
  46765. * forces use of the CRL lookup directory */
  46766. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  46767. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46768. NULL, NULL));
  46769. if (fp != XBADFILE) {
  46770. XFCLOSE(fp);
  46771. fp = XBADFILE;
  46772. }
  46773. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46774. /* Create verification context outside of an SSL session */
  46775. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46776. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46777. SSL_FILETYPE_PEM)));
  46778. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46779. /* Perform verification, which should NOT return CRL missing */
  46780. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46781. X509_CRL_free(crl);
  46782. crl = NULL;
  46783. X509_STORE_free(store);
  46784. store = NULL;
  46785. X509_STORE_CTX_free(storeCtx);
  46786. storeCtx = NULL;
  46787. X509_free(cert);
  46788. cert = NULL;
  46789. X509_free(ca);
  46790. ca = NULL;
  46791. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  46792. * We should get CRL_MISSING at the end, because the lookup
  46793. * dir has only ASN1 CRLs. */
  46794. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  46795. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46796. SSL_FILETYPE_PEM)));
  46797. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46798. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  46799. X509_LOOKUP_hash_dir())));
  46800. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  46801. SSL_SUCCESS);
  46802. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46803. SSL_SUCCESS);
  46804. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  46805. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46806. NULL, NULL));
  46807. if (fp != XBADFILE) {
  46808. XFCLOSE(fp);
  46809. fp = XBADFILE;
  46810. }
  46811. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46812. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46813. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46814. SSL_FILETYPE_PEM)));
  46815. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46816. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  46817. * in dir containing only ASN1/DER. */
  46818. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  46819. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  46820. X509_CRL_free(crl);
  46821. X509_STORE_free(store);
  46822. X509_STORE_CTX_free(storeCtx);
  46823. X509_free(cert);
  46824. X509_free(ca);
  46825. #endif
  46826. return EXPECT_RESULT();
  46827. }
  46828. /*----------------------------------------------------------------------------*
  46829. | Certificate Failure Checks
  46830. *----------------------------------------------------------------------------*/
  46831. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  46832. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  46833. #if !defined(NO_RSA) || defined(HAVE_ECC)
  46834. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  46835. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  46836. int type)
  46837. {
  46838. int ret;
  46839. WOLFSSL_CERT_MANAGER* cm = NULL;
  46840. switch (type) {
  46841. case TESTING_RSA:
  46842. #ifdef NO_RSA
  46843. fprintf(stderr, "RSA disabled, skipping test\n");
  46844. return ASN_SIG_CONFIRM_E;
  46845. #else
  46846. break;
  46847. #endif
  46848. case TESTING_ECC:
  46849. #ifndef HAVE_ECC
  46850. fprintf(stderr, "ECC disabled, skipping test\n");
  46851. return ASN_SIG_CONFIRM_E;
  46852. #else
  46853. break;
  46854. #endif
  46855. default:
  46856. fprintf(stderr, "Bad function argument\n");
  46857. return BAD_FUNC_ARG;
  46858. }
  46859. cm = wolfSSL_CertManagerNew();
  46860. if (cm == NULL) {
  46861. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  46862. return -1;
  46863. }
  46864. #ifndef NO_FILESYSTEM
  46865. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  46866. if (ret != WOLFSSL_SUCCESS) {
  46867. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  46868. wolfSSL_CertManagerFree(cm);
  46869. return ret;
  46870. }
  46871. #else
  46872. (void)ca;
  46873. #endif
  46874. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  46875. WOLFSSL_FILETYPE_ASN1);
  46876. /* Let ExpectIntEQ handle return code */
  46877. wolfSSL_CertManagerFree(cm);
  46878. return ret;
  46879. }
  46880. #endif
  46881. #if !defined(NO_FILESYSTEM)
  46882. static int test_RsaSigFailure_cm(void)
  46883. {
  46884. EXPECT_DECLS;
  46885. #ifndef NO_RSA
  46886. const char* ca_cert = "./certs/ca-cert.pem";
  46887. const char* server_cert = "./certs/server-cert.der";
  46888. byte* cert_buf = NULL;
  46889. size_t cert_sz = 0;
  46890. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  46891. if (cert_buf != NULL) {
  46892. /* corrupt DER - invert last byte, which is signature */
  46893. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46894. /* test bad cert */
  46895. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46896. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  46897. WOLFSSL_FATAL_ERROR);
  46898. #else
  46899. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  46900. ASN_SIG_CONFIRM_E);
  46901. #endif
  46902. }
  46903. /* load_file() uses malloc. */
  46904. if (cert_buf != NULL) {
  46905. free(cert_buf);
  46906. }
  46907. #endif /* !NO_RSA */
  46908. return EXPECT_RESULT();
  46909. }
  46910. static int test_EccSigFailure_cm(void)
  46911. {
  46912. EXPECT_DECLS;
  46913. #ifdef HAVE_ECC
  46914. /* self-signed ECC cert, so use server cert as CA */
  46915. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  46916. const char* server_cert = "./certs/server-ecc.der";
  46917. byte* cert_buf = NULL;
  46918. size_t cert_sz = 0;
  46919. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  46920. if (cert_buf != NULL) {
  46921. /* corrupt DER - invert last byte, which is signature */
  46922. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46923. /* test bad cert */
  46924. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46925. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  46926. WOLFSSL_FATAL_ERROR);
  46927. #else
  46928. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  46929. ASN_SIG_CONFIRM_E);
  46930. #endif
  46931. }
  46932. /* load_file() uses malloc. */
  46933. if (cert_buf != NULL) {
  46934. free(cert_buf);
  46935. }
  46936. #ifdef FP_ECC
  46937. wc_ecc_fp_free();
  46938. #endif
  46939. #endif /* HAVE_ECC */
  46940. return EXPECT_RESULT();
  46941. }
  46942. #endif /* !NO_FILESYSTEM */
  46943. #endif /* NO_CERTS */
  46944. #ifdef WOLFSSL_TLS13
  46945. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  46946. #ifdef WC_SHA384_DIGEST_SIZE
  46947. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  46948. #else
  46949. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  46950. #endif
  46951. #endif
  46952. #ifdef WOLFSSL_EARLY_DATA
  46953. static const char earlyData[] = "Early Data";
  46954. static char earlyDataBuffer[1];
  46955. #endif
  46956. static int test_tls13_apis(void)
  46957. {
  46958. EXPECT_DECLS;
  46959. int ret;
  46960. #ifndef WOLFSSL_NO_TLS12
  46961. #ifndef NO_WOLFSSL_CLIENT
  46962. WOLFSSL_CTX* clientTls12Ctx = NULL;
  46963. WOLFSSL* clientTls12Ssl = NULL;
  46964. #endif
  46965. #ifndef NO_WOLFSSL_SERVER
  46966. WOLFSSL_CTX* serverTls12Ctx = NULL;
  46967. WOLFSSL* serverTls12Ssl = NULL;
  46968. #endif
  46969. #endif
  46970. #ifndef NO_WOLFSSL_CLIENT
  46971. WOLFSSL_CTX* clientCtx = NULL;
  46972. WOLFSSL* clientSsl = NULL;
  46973. #endif
  46974. #ifndef NO_WOLFSSL_SERVER
  46975. WOLFSSL_CTX* serverCtx = NULL;
  46976. WOLFSSL* serverSsl = NULL;
  46977. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46978. const char* ourCert = svrCertFile;
  46979. const char* ourKey = svrKeyFile;
  46980. #endif
  46981. #endif
  46982. int required;
  46983. #ifdef WOLFSSL_EARLY_DATA
  46984. int outSz;
  46985. #endif
  46986. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  46987. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  46988. #ifdef HAVE_PQC
  46989. WOLFSSL_KYBER_LEVEL1
  46990. #else
  46991. WOLFSSL_ECC_SECP256R1
  46992. #endif
  46993. };
  46994. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  46995. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  46996. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  46997. int numGroups = 2;
  46998. #endif
  46999. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47000. char groupList[] =
  47001. #ifndef NO_ECC_SECP
  47002. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  47003. "P-521:"
  47004. #endif
  47005. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  47006. "P-384:"
  47007. #endif
  47008. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  47009. "P-256"
  47010. #ifdef HAVE_PQC
  47011. ":P256_KYBER_LEVEL1"
  47012. #endif
  47013. #endif
  47014. #endif /* !defined(NO_ECC_SECP) */
  47015. #ifdef HAVE_PQC
  47016. ":KYBER_LEVEL1"
  47017. #endif
  47018. "";
  47019. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  47020. (void)ret;
  47021. #ifndef WOLFSSL_NO_TLS12
  47022. #ifndef NO_WOLFSSL_CLIENT
  47023. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  47024. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  47025. #endif
  47026. #ifndef NO_WOLFSSL_SERVER
  47027. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  47028. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47029. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  47030. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  47031. WOLFSSL_FILETYPE_PEM);
  47032. #endif
  47033. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  47034. #endif
  47035. #endif
  47036. #ifndef NO_WOLFSSL_CLIENT
  47037. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  47038. clientSsl = wolfSSL_new(clientCtx);
  47039. #endif
  47040. #ifndef NO_WOLFSSL_SERVER
  47041. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  47042. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47043. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  47044. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  47045. #endif
  47046. serverSsl = wolfSSL_new(serverCtx);
  47047. ExpectNotNull(serverSsl);
  47048. #endif
  47049. #ifdef WOLFSSL_SEND_HRR_COOKIE
  47050. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  47051. #ifndef NO_WOLFSSL_CLIENT
  47052. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  47053. #endif
  47054. #ifndef NO_WOLFSSL_SERVER
  47055. #ifndef WOLFSSL_NO_TLS12
  47056. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  47057. BAD_FUNC_ARG);
  47058. #endif
  47059. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  47060. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  47061. WOLFSSL_SUCCESS);
  47062. #endif
  47063. #endif
  47064. #ifdef HAVE_SUPPORTED_CURVES
  47065. #ifdef HAVE_ECC
  47066. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  47067. BAD_FUNC_ARG);
  47068. #ifndef NO_WOLFSSL_SERVER
  47069. do {
  47070. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  47071. #ifdef WOLFSSL_ASYNC_CRYPT
  47072. if (ret == WC_PENDING_E)
  47073. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  47074. #endif
  47075. }
  47076. while (ret == WC_PENDING_E);
  47077. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47078. #endif
  47079. #ifndef NO_WOLFSSL_CLIENT
  47080. #ifndef WOLFSSL_NO_TLS12
  47081. do {
  47082. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  47083. #ifdef WOLFSSL_ASYNC_CRYPT
  47084. if (ret == WC_PENDING_E)
  47085. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  47086. #endif
  47087. }
  47088. while (ret == WC_PENDING_E);
  47089. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47090. #endif
  47091. do {
  47092. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  47093. #ifdef WOLFSSL_ASYNC_CRYPT
  47094. if (ret == WC_PENDING_E)
  47095. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  47096. #endif
  47097. }
  47098. while (ret == WC_PENDING_E);
  47099. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47100. #endif
  47101. #elif defined(HAVE_CURVE25519)
  47102. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  47103. #ifndef NO_WOLFSSL_SERVER
  47104. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  47105. WOLFSSL_SUCCESS);
  47106. #endif
  47107. #ifndef NO_WOLFSSL_CLIENT
  47108. #ifndef WOLFSSL_NO_TLS12
  47109. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  47110. WOLFSSL_SUCCESS);
  47111. #endif
  47112. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  47113. WOLFSSL_SUCCESS);
  47114. #endif
  47115. #elif defined(HAVE_CURVE448)
  47116. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  47117. #ifndef NO_WOLFSSL_SERVER
  47118. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  47119. WOLFSSL_SUCCESS);
  47120. #endif
  47121. #ifndef NO_WOLFSSL_CLIENT
  47122. #ifndef WOLFSSL_NO_TLS12
  47123. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  47124. WOLFSSL_SUCCESS);
  47125. #endif
  47126. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  47127. WOLFSSL_SUCCESS);
  47128. #endif
  47129. #else
  47130. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  47131. BAD_FUNC_ARG);
  47132. #ifndef NO_WOLFSSL_CLIENT
  47133. #ifndef WOLFSSL_NO_TLS12
  47134. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  47135. NOT_COMPILED_IN);
  47136. #endif
  47137. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  47138. NOT_COMPILED_IN);
  47139. #endif
  47140. #endif
  47141. #if defined(HAVE_PQC)
  47142. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  47143. #ifndef NO_WOLFSSL_SERVER
  47144. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  47145. WOLFSSL_SUCCESS);
  47146. #endif
  47147. #ifndef NO_WOLFSSL_CLIENT
  47148. #ifndef WOLFSSL_NO_TLS12
  47149. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  47150. BAD_FUNC_ARG);
  47151. #endif
  47152. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  47153. WOLFSSL_SUCCESS);
  47154. #endif
  47155. #endif
  47156. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  47157. #ifndef NO_WOLFSSL_SERVER
  47158. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  47159. #endif
  47160. #ifndef NO_WOLFSSL_CLIENT
  47161. #ifndef WOLFSSL_NO_TLS12
  47162. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  47163. #endif
  47164. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  47165. #endif
  47166. #endif /* HAVE_SUPPORTED_CURVES */
  47167. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  47168. #ifndef NO_WOLFSSL_CLIENT
  47169. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  47170. #endif
  47171. #ifndef NO_WOLFSSL_SERVER
  47172. #ifndef WOLFSSL_NO_TLS12
  47173. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  47174. #endif
  47175. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  47176. #endif
  47177. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  47178. #ifndef NO_WOLFSSL_CLIENT
  47179. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  47180. #endif
  47181. #ifndef NO_WOLFSSL_SERVER
  47182. #ifndef WOLFSSL_NO_TLS12
  47183. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  47184. #endif
  47185. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  47186. #endif
  47187. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  47188. #ifndef NO_WOLFSSL_CLIENT
  47189. #ifndef WOLFSSL_NO_TLS12
  47190. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  47191. #endif
  47192. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  47193. #endif
  47194. #ifndef NO_WOLFSSL_SERVER
  47195. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  47196. #endif
  47197. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  47198. #ifndef NO_WOLFSSL_CLIENT
  47199. #ifndef WOLFSSL_NO_TLS12
  47200. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  47201. #endif
  47202. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  47203. #endif
  47204. #ifndef NO_WOLFSSL_SERVER
  47205. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  47206. #endif
  47207. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  47208. #ifndef NO_WOLFSSL_CLIENT
  47209. #ifndef WOLFSSL_NO_TLS12
  47210. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  47211. #endif
  47212. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  47213. #endif
  47214. #ifndef NO_WOLFSSL_SERVER
  47215. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  47216. #endif
  47217. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  47218. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  47219. #ifndef NO_WOLFSSL_CLIENT
  47220. #ifndef WOLFSSL_NO_TLS12
  47221. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  47222. BAD_FUNC_ARG);
  47223. #endif
  47224. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  47225. #endif
  47226. #ifndef NO_WOLFSSL_SERVER
  47227. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  47228. #endif
  47229. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  47230. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  47231. #ifndef NO_WOLFSSL_SERVER
  47232. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  47233. #endif
  47234. #ifndef NO_WOLFSSL_CLIENT
  47235. #ifndef WOLFSSL_NO_TLS12
  47236. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  47237. BAD_FUNC_ARG);
  47238. #endif
  47239. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  47240. #endif
  47241. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  47242. #ifndef NO_WOLFSSL_SERVER
  47243. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  47244. #endif
  47245. #ifndef NO_WOLFSSL_CLIENT
  47246. #ifndef WOLFSSL_NO_TLS12
  47247. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  47248. BAD_FUNC_ARG);
  47249. #endif
  47250. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  47251. #endif
  47252. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  47253. #ifndef NO_WOLFSSL_CLIENT
  47254. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  47255. #endif
  47256. #ifndef NO_WOLFSSL_SERVER
  47257. #ifndef WOLFSSL_NO_TLS12
  47258. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  47259. BAD_FUNC_ARG);
  47260. #endif
  47261. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  47262. #endif
  47263. #endif
  47264. #ifdef HAVE_ECC
  47265. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  47266. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  47267. #ifndef NO_WOLFSSL_SERVER
  47268. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  47269. #endif
  47270. #ifndef NO_WOLFSSL_CLIENT
  47271. #ifndef WOLFSSL_NO_TLS12
  47272. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  47273. #endif
  47274. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  47275. #endif
  47276. #endif
  47277. #ifdef HAVE_SUPPORTED_CURVES
  47278. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  47279. #ifndef NO_WOLFSSL_CLIENT
  47280. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  47281. #endif
  47282. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  47283. #ifndef NO_WOLFSSL_CLIENT
  47284. #ifndef WOLFSSL_NO_TLS12
  47285. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  47286. BAD_FUNC_ARG);
  47287. #endif
  47288. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  47289. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  47290. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  47291. WOLFSSL_SUCCESS);
  47292. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  47293. BAD_FUNC_ARG);
  47294. #endif
  47295. #ifndef NO_WOLFSSL_SERVER
  47296. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  47297. WOLFSSL_SUCCESS);
  47298. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  47299. BAD_FUNC_ARG);
  47300. #endif
  47301. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  47302. #ifndef NO_WOLFSSL_CLIENT
  47303. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  47304. #endif
  47305. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  47306. #ifndef NO_WOLFSSL_CLIENT
  47307. #ifndef WOLFSSL_NO_TLS12
  47308. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  47309. BAD_FUNC_ARG);
  47310. #endif
  47311. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  47312. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  47313. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  47314. WOLFSSL_SUCCESS);
  47315. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  47316. BAD_FUNC_ARG);
  47317. #endif
  47318. #ifndef NO_WOLFSSL_SERVER
  47319. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  47320. WOLFSSL_SUCCESS);
  47321. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  47322. BAD_FUNC_ARG);
  47323. #endif
  47324. #ifdef OPENSSL_EXTRA
  47325. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  47326. #ifndef NO_WOLFSSL_CLIENT
  47327. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  47328. WOLFSSL_FAILURE);
  47329. #endif
  47330. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  47331. WOLFSSL_FAILURE);
  47332. #ifndef NO_WOLFSSL_CLIENT
  47333. #ifndef WOLFSSL_NO_TLS12
  47334. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  47335. WOLFSSL_FAILURE);
  47336. #endif
  47337. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  47338. WOLFSSL_SUCCESS);
  47339. #endif
  47340. #ifndef NO_WOLFSSL_SERVER
  47341. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  47342. WOLFSSL_SUCCESS);
  47343. #endif
  47344. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  47345. #ifndef NO_WOLFSSL_CLIENT
  47346. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  47347. #endif
  47348. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  47349. #ifndef NO_WOLFSSL_CLIENT
  47350. #ifndef WOLFSSL_NO_TLS12
  47351. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  47352. WOLFSSL_FAILURE);
  47353. #endif
  47354. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  47355. WOLFSSL_SUCCESS);
  47356. #endif
  47357. #ifndef NO_WOLFSSL_SERVER
  47358. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  47359. WOLFSSL_SUCCESS);
  47360. #endif
  47361. #endif /* OPENSSL_EXTRA */
  47362. #endif /* HAVE_SUPPORTED_CURVES */
  47363. #endif /* HAVE_ECC */
  47364. #ifdef WOLFSSL_EARLY_DATA
  47365. #ifndef OPENSSL_EXTRA
  47366. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47367. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47368. #else
  47369. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47370. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47371. #endif
  47372. #ifndef NO_WOLFSSL_CLIENT
  47373. #ifndef OPENSSL_EXTRA
  47374. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47375. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47376. #else
  47377. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47378. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47379. #endif
  47380. #endif
  47381. #ifndef NO_WOLFSSL_SERVER
  47382. #ifndef WOLFSSL_NO_TLS12
  47383. #ifndef OPENSSL_EXTRA
  47384. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47385. BAD_FUNC_ARG);
  47386. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47387. #else
  47388. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47389. BAD_FUNC_ARG);
  47390. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47391. #endif
  47392. #endif
  47393. #ifndef OPENSSL_EXTRA
  47394. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47395. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  47396. WOLFSSL_SUCCESS);
  47397. #else
  47398. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  47399. #endif
  47400. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  47401. #else
  47402. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  47403. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  47404. #endif
  47405. #endif
  47406. #ifndef OPENSSL_EXTRA
  47407. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47408. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  47409. #else
  47410. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47411. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  47412. #endif
  47413. #ifndef NO_WOLFSSL_CLIENT
  47414. #ifndef OPENSSL_EXTRA
  47415. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47416. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  47417. #else
  47418. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  47419. #endif
  47420. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  47421. #else
  47422. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  47423. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  47424. #endif
  47425. #endif
  47426. #ifndef NO_WOLFSSL_SERVER
  47427. #ifndef WOLFSSL_NO_TLS12
  47428. #ifndef OPENSSL_EXTRA
  47429. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  47430. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  47431. #else
  47432. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  47433. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  47434. #endif
  47435. #endif
  47436. #ifndef OPENSSL_EXTRA
  47437. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47438. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  47439. #else
  47440. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  47441. #endif
  47442. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  47443. #else
  47444. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  47445. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  47446. #endif
  47447. #endif
  47448. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  47449. &outSz), BAD_FUNC_ARG);
  47450. #ifndef NO_WOLFSSL_CLIENT
  47451. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  47452. &outSz), BAD_FUNC_ARG);
  47453. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  47454. BAD_FUNC_ARG);
  47455. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  47456. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  47457. #endif
  47458. #ifndef NO_WOLFSSL_SERVER
  47459. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  47460. sizeof(earlyData), &outSz), SIDE_ERROR);
  47461. #endif
  47462. #ifndef NO_WOLFSSL_CLIENT
  47463. #ifndef WOLFSSL_NO_TLS12
  47464. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  47465. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  47466. #endif
  47467. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  47468. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  47469. #endif
  47470. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  47471. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  47472. #ifndef NO_WOLFSSL_SERVER
  47473. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  47474. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  47475. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  47476. &outSz), BAD_FUNC_ARG);
  47477. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  47478. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  47479. #endif
  47480. #ifndef NO_WOLFSSL_CLIENT
  47481. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  47482. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  47483. #endif
  47484. #ifndef NO_WOLFSSL_SERVER
  47485. #ifndef WOLFSSL_NO_TLS12
  47486. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  47487. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  47488. #endif
  47489. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  47490. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  47491. #endif
  47492. #endif
  47493. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  47494. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  47495. #endif
  47496. #ifndef NO_WOLFSSL_SERVER
  47497. wolfSSL_free(serverSsl);
  47498. wolfSSL_CTX_free(serverCtx);
  47499. #endif
  47500. #ifndef NO_WOLFSSL_CLIENT
  47501. wolfSSL_free(clientSsl);
  47502. wolfSSL_CTX_free(clientCtx);
  47503. #endif
  47504. #ifndef WOLFSSL_NO_TLS12
  47505. #ifndef NO_WOLFSSL_SERVER
  47506. wolfSSL_free(serverTls12Ssl);
  47507. wolfSSL_CTX_free(serverTls12Ctx);
  47508. #endif
  47509. #ifndef NO_WOLFSSL_CLIENT
  47510. wolfSSL_free(clientTls12Ssl);
  47511. wolfSSL_CTX_free(clientTls12Ctx);
  47512. #endif
  47513. #endif
  47514. return EXPECT_RESULT();
  47515. }
  47516. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  47517. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  47518. defined(BUILD_TLS_AES_256_GCM_SHA384)
  47519. /* Called when writing. */
  47520. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  47521. {
  47522. (void)ssl;
  47523. (void)buf;
  47524. (void)sz;
  47525. (void)ctx;
  47526. /* Force error return from wolfSSL_accept_TLSv13(). */
  47527. return WANT_WRITE;
  47528. }
  47529. /* Called when reading. */
  47530. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  47531. {
  47532. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  47533. int len = (int)msg->length;
  47534. (void)ssl;
  47535. (void)sz;
  47536. /* Pass back as much of message as will fit in buffer. */
  47537. if (len > sz)
  47538. len = sz;
  47539. XMEMCPY(buf, msg->buffer, len);
  47540. /* Move over returned data. */
  47541. msg->buffer += len;
  47542. msg->length -= len;
  47543. /* Amount actually copied. */
  47544. return len;
  47545. }
  47546. #endif
  47547. static int test_tls13_cipher_suites(void)
  47548. {
  47549. EXPECT_DECLS;
  47550. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  47551. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  47552. defined(BUILD_TLS_AES_256_GCM_SHA384)
  47553. WOLFSSL_CTX* ctx = NULL;
  47554. WOLFSSL *ssl = NULL;
  47555. int i;
  47556. byte clientHello[] = {
  47557. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  47558. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  47559. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  47560. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  47561. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  47562. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  47563. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  47564. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  47565. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47566. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  47567. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  47568. 0x13, 0x01,
  47569. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  47570. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  47571. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  47572. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  47573. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  47574. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  47575. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  47576. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  47577. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  47578. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  47579. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  47580. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  47581. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  47582. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  47583. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  47584. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  47585. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  47586. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  47587. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  47588. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  47589. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  47590. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  47591. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  47592. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  47593. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  47594. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  47595. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  47596. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  47597. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  47598. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  47599. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  47600. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  47601. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  47602. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  47603. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  47604. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47605. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47606. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  47607. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  47608. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  47609. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  47610. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  47611. };
  47612. WOLFSSL_BUFFER_INFO msg;
  47613. /* Offset into ClientHello message data of first cipher suite. */
  47614. const int csOff = 78;
  47615. /* Server cipher list. */
  47616. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  47617. /* Suite list with duplicates. */
  47618. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  47619. "TLS13-AES128-GCM-SHA256:"
  47620. "TLS13-AES256-GCM-SHA384:"
  47621. "TLS13-AES256-GCM-SHA384:"
  47622. "TLS13-AES128-GCM-SHA256";
  47623. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  47624. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  47625. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  47626. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  47627. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  47628. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  47629. #endif
  47630. /* Set up wolfSSL context. */
  47631. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47632. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  47633. WOLFSSL_FILETYPE_PEM));
  47634. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  47635. WOLFSSL_FILETYPE_PEM));
  47636. /* Read from 'msg'. */
  47637. wolfSSL_SetIORecv(ctx, CsRecv);
  47638. /* No where to send to - dummy sender. */
  47639. wolfSSL_SetIOSend(ctx, CsSend);
  47640. /* Test cipher suite list with many copies of a cipher suite. */
  47641. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47642. msg.buffer = clientHello;
  47643. msg.length = (unsigned int)sizeof(clientHello);
  47644. wolfSSL_SetIOReadCtx(ssl, &msg);
  47645. /* Force server to have as many occurrences of same cipher suite as
  47646. * possible. */
  47647. if (ssl != NULL) {
  47648. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  47649. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  47650. for (i = 0; i < suites->suiteSz; i += 2) {
  47651. suites->suites[i + 0] = TLS13_BYTE;
  47652. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  47653. }
  47654. }
  47655. /* Test multiple occurrences of same cipher suite. */
  47656. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  47657. wolfSSL_free(ssl);
  47658. ssl = NULL;
  47659. /* Set client order opposite to server order:
  47660. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  47661. clientHello[csOff + 0] = TLS13_BYTE;
  47662. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  47663. clientHello[csOff + 2] = TLS13_BYTE;
  47664. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  47665. /* Test server order negotiation. */
  47666. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47667. msg.buffer = clientHello;
  47668. msg.length = (unsigned int)sizeof(clientHello);
  47669. wolfSSL_SetIOReadCtx(ssl, &msg);
  47670. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  47671. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  47672. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  47673. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  47674. /* Check refined order - server order. */
  47675. ExpectIntEQ(ssl->suites->suiteSz, 4);
  47676. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  47677. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  47678. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  47679. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  47680. wolfSSL_free(ssl);
  47681. ssl = NULL;
  47682. /* Test client order negotiation. */
  47683. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47684. msg.buffer = clientHello;
  47685. msg.length = (unsigned int)sizeof(clientHello);
  47686. wolfSSL_SetIOReadCtx(ssl, &msg);
  47687. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  47688. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  47689. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  47690. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  47691. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  47692. /* Check refined order - client order. */
  47693. ExpectIntEQ(ssl->suites->suiteSz, 4);
  47694. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  47695. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  47696. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  47697. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  47698. wolfSSL_free(ssl);
  47699. ssl = NULL;
  47700. /* Check duplicate detection is working. */
  47701. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  47702. ExpectIntEQ(ctx->suites->suiteSz, 4);
  47703. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  47704. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  47705. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  47706. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  47707. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  47708. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  47709. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  47710. ExpectIntEQ(ctx->suites->suiteSz, 4);
  47711. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  47712. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  47713. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  47714. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  47715. #endif
  47716. wolfSSL_CTX_free(ctx);
  47717. #endif
  47718. return EXPECT_RESULT();
  47719. }
  47720. #endif
  47721. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  47722. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  47723. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47724. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  47725. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  47726. const unsigned char* priv, unsigned int privSz,
  47727. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  47728. unsigned char* out, unsigned int* outlen,
  47729. void* ctx)
  47730. {
  47731. int result;
  47732. /* Test fail when context associated with WOLFSSL is NULL */
  47733. if (ctx == NULL) {
  47734. return -1;
  47735. }
  47736. (void)ssl;
  47737. /* return 0 on success */
  47738. PRIVATE_KEY_UNLOCK();
  47739. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  47740. PRIVATE_KEY_LOCK();
  47741. return result;
  47742. }
  47743. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  47744. EXPECT_DECLS;
  47745. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  47746. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  47747. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  47748. WOLFSSL_SUCCESS);
  47749. #endif
  47750. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  47751. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  47752. WOLFSSL_SUCCESS);
  47753. #endif
  47754. return EXPECT_RESULT();
  47755. }
  47756. static int test_dh_ssl_setup(WOLFSSL* ssl)
  47757. {
  47758. EXPECT_DECLS;
  47759. static int dh_test_ctx = 1;
  47760. int ret;
  47761. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  47762. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  47763. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  47764. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  47765. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47766. }
  47767. return EXPECT_RESULT();
  47768. }
  47769. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  47770. {
  47771. EXPECT_DECLS;
  47772. int ret;
  47773. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  47774. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  47775. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  47776. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  47777. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47778. }
  47779. return EXPECT_RESULT();
  47780. }
  47781. #endif
  47782. static int test_DhCallbacks(void)
  47783. {
  47784. EXPECT_DECLS;
  47785. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  47786. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47787. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  47788. WOLFSSL_CTX *ctx = NULL;
  47789. WOLFSSL *ssl = NULL;
  47790. int test;
  47791. test_ssl_cbf func_cb_client;
  47792. test_ssl_cbf func_cb_server;
  47793. /* Test that DH callback APIs work. */
  47794. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  47795. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  47796. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  47797. /* load client ca cert */
  47798. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  47799. WOLFSSL_SUCCESS);
  47800. /* test with NULL arguments */
  47801. wolfSSL_SetDhAgreeCtx(NULL, &test);
  47802. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  47803. /* test success case */
  47804. test = 1;
  47805. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47806. wolfSSL_SetDhAgreeCtx(ssl, &test);
  47807. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  47808. wolfSSL_free(ssl);
  47809. wolfSSL_CTX_free(ctx);
  47810. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  47811. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  47812. /* set callbacks to use DH functions */
  47813. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47814. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  47815. func_cb_client.method = wolfTLSv1_2_client_method;
  47816. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47817. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  47818. func_cb_server.method = wolfTLSv1_2_server_method;
  47819. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  47820. &func_cb_server, NULL), TEST_SUCCESS);
  47821. /* Test fail */
  47822. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  47823. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  47824. /* set callbacks to use DH functions */
  47825. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47826. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  47827. func_cb_client.method = wolfTLSv1_2_client_method;
  47828. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47829. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  47830. func_cb_server.method = wolfTLSv1_2_server_method;
  47831. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  47832. &func_cb_server, NULL), TEST_FAIL);
  47833. #endif
  47834. return EXPECT_RESULT();
  47835. }
  47836. #endif /* HAVE_PK_CALLBACKS */
  47837. #ifdef HAVE_HASHDRBG
  47838. #ifdef TEST_RESEED_INTERVAL
  47839. static int test_wc_RNG_GenerateBlock_Reseed(void)
  47840. {
  47841. EXPECT_DECLS;
  47842. int i;
  47843. WC_RNG rng;
  47844. byte key[32];
  47845. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47846. ExpectIntEQ(wc_InitRng(&rng), 0);
  47847. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  47848. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  47849. }
  47850. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47851. return EXPECT_RESULT();
  47852. }
  47853. #endif /* TEST_RESEED_INTERVAL */
  47854. static int test_wc_RNG_GenerateBlock(void)
  47855. {
  47856. EXPECT_DECLS;
  47857. int i;
  47858. WC_RNG rng;
  47859. byte key[32];
  47860. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47861. ExpectIntEQ(wc_InitRng(&rng), 0);
  47862. for (i = 0; i < 10; i++) {
  47863. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  47864. }
  47865. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47866. return EXPECT_RESULT();
  47867. }
  47868. #endif /* HAVE_HASHDRBG */
  47869. /*
  47870. * Testing get_rand_digit
  47871. */
  47872. static int test_get_rand_digit(void)
  47873. {
  47874. EXPECT_DECLS;
  47875. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  47876. WC_RNG rng;
  47877. mp_digit d;
  47878. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47879. ExpectIntEQ(wc_InitRng(&rng), 0);
  47880. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  47881. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  47882. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  47883. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  47884. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47885. #endif
  47886. return EXPECT_RESULT();
  47887. } /* End test_get_rand_digit*/
  47888. /*
  47889. * Testing get_digit_count
  47890. */
  47891. static int test_get_digit_count(void)
  47892. {
  47893. EXPECT_DECLS;
  47894. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  47895. mp_int a;
  47896. XMEMSET(&a, 0, sizeof(mp_int));
  47897. ExpectIntEQ(mp_init(&a), 0);
  47898. ExpectIntEQ(get_digit_count(NULL), 0);
  47899. ExpectIntEQ(get_digit_count(&a), 0);
  47900. mp_clear(&a);
  47901. #endif
  47902. return EXPECT_RESULT();
  47903. } /* End test_get_digit_count*/
  47904. /*
  47905. * Testing mp_cond_copy
  47906. */
  47907. static int test_mp_cond_copy(void)
  47908. {
  47909. EXPECT_DECLS;
  47910. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  47911. defined(WOLFSSL_PUBLIC_MP)
  47912. mp_int a;
  47913. mp_int b;
  47914. int copy = 0;
  47915. XMEMSET(&a, 0, sizeof(mp_int));
  47916. XMEMSET(&b, 0, sizeof(mp_int));
  47917. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47918. ExpectIntEQ(mp_init(&b), MP_OKAY);
  47919. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  47920. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  47921. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  47922. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  47923. mp_clear(&a);
  47924. mp_clear(&b);
  47925. #endif
  47926. return EXPECT_RESULT();
  47927. } /* End test_mp_cond_copy*/
  47928. /*
  47929. * Testing mp_rand
  47930. */
  47931. static int test_mp_rand(void)
  47932. {
  47933. EXPECT_DECLS;
  47934. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  47935. mp_int a;
  47936. WC_RNG rng;
  47937. int digits = 1;
  47938. XMEMSET(&a, 0, sizeof(mp_int));
  47939. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47940. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47941. ExpectIntEQ(wc_InitRng(&rng), 0);
  47942. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  47943. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  47944. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  47945. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  47946. mp_clear(&a);
  47947. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47948. #endif
  47949. return EXPECT_RESULT();
  47950. } /* End test_mp_rand*/
  47951. /*
  47952. * Testing get_digit
  47953. */
  47954. static int test_get_digit(void)
  47955. {
  47956. EXPECT_DECLS;
  47957. #if defined(WOLFSSL_PUBLIC_MP)
  47958. mp_int a;
  47959. int n = 0;
  47960. XMEMSET(&a, 0, sizeof(mp_int));
  47961. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47962. ExpectIntEQ(get_digit(NULL, n), 0);
  47963. ExpectIntEQ(get_digit(&a, n), 0);
  47964. mp_clear(&a);
  47965. #endif
  47966. return EXPECT_RESULT();
  47967. } /* End test_get_digit*/
  47968. /*
  47969. * Testing wc_export_int
  47970. */
  47971. static int test_wc_export_int(void)
  47972. {
  47973. EXPECT_DECLS;
  47974. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  47975. defined(WOLFSSL_PUBLIC_MP)
  47976. mp_int mp;
  47977. byte buf[32];
  47978. word32 keySz = (word32)sizeof(buf);
  47979. word32 len = (word32)sizeof(buf);
  47980. XMEMSET(&mp, 0, sizeof(mp_int));
  47981. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  47982. ExpectIntEQ(mp_set(&mp, 1234), 0);
  47983. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  47984. BAD_FUNC_ARG);
  47985. len = sizeof(buf)-1;
  47986. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  47987. BUFFER_E);
  47988. len = sizeof(buf);
  47989. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  47990. len = 4; /* test input too small */
  47991. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  47992. len = sizeof(buf);
  47993. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  47994. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  47995. ExpectIntEQ(len, 5);
  47996. mp_clear(&mp);
  47997. #endif
  47998. return EXPECT_RESULT();
  47999. } /* End test_wc_export_int*/
  48000. static int test_wc_InitRngNonce(void)
  48001. {
  48002. EXPECT_DECLS;
  48003. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  48004. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  48005. HAVE_FIPS_VERSION >= 2))
  48006. WC_RNG rng;
  48007. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  48008. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  48009. word32 nonceSz = sizeof(nonce);
  48010. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  48011. ExpectIntEQ(wc_FreeRng(&rng), 0);
  48012. #endif
  48013. return EXPECT_RESULT();
  48014. } /* End test_wc_InitRngNonce*/
  48015. /*
  48016. * Testing wc_InitRngNonce_ex
  48017. */
  48018. static int test_wc_InitRngNonce_ex(void)
  48019. {
  48020. EXPECT_DECLS;
  48021. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  48022. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  48023. HAVE_FIPS_VERSION >= 2))
  48024. WC_RNG rng;
  48025. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  48026. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  48027. word32 nonceSz = sizeof(nonce);
  48028. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  48029. 0);
  48030. ExpectIntEQ(wc_FreeRng(&rng), 0);
  48031. #endif
  48032. return EXPECT_RESULT();
  48033. } /* End test_wc_InitRngNonce_ex */
  48034. static int test_wolfSSL_X509_CRL(void)
  48035. {
  48036. EXPECT_DECLS;
  48037. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  48038. X509_CRL *crl = NULL;
  48039. char pem[][100] = {
  48040. "./certs/crl/crl.pem",
  48041. "./certs/crl/crl2.pem",
  48042. "./certs/crl/caEccCrl.pem",
  48043. "./certs/crl/eccCliCRL.pem",
  48044. "./certs/crl/eccSrvCRL.pem",
  48045. ""
  48046. };
  48047. #ifndef NO_BIO
  48048. BIO *bio = NULL;
  48049. #endif
  48050. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  48051. char der[][100] = {
  48052. "./certs/crl/crl.der",
  48053. "./certs/crl/crl2.der",
  48054. ""};
  48055. #endif
  48056. XFILE fp = XBADFILE;
  48057. int i;
  48058. for (i = 0; pem[i][0] != '\0'; i++)
  48059. {
  48060. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  48061. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48062. NULL, NULL));
  48063. ExpectNotNull(crl);
  48064. X509_CRL_free(crl);
  48065. if (fp != XBADFILE) {
  48066. XFCLOSE(fp);
  48067. fp = XBADFILE;
  48068. }
  48069. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  48070. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  48071. NULL));
  48072. if (EXPECT_FAIL()) {
  48073. crl = NULL;
  48074. }
  48075. ExpectNotNull(crl);
  48076. X509_CRL_free(crl);
  48077. crl = NULL;
  48078. if (fp != XBADFILE) {
  48079. XFCLOSE(fp);
  48080. fp = XBADFILE;
  48081. }
  48082. }
  48083. #ifndef NO_BIO
  48084. for (i = 0; pem[i][0] != '\0'; i++)
  48085. {
  48086. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  48087. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  48088. X509_CRL_free(crl);
  48089. crl = NULL;
  48090. BIO_free(bio);
  48091. bio = NULL;
  48092. }
  48093. #endif
  48094. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  48095. for (i = 0; der[i][0] != '\0'; i++) {
  48096. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  48097. ExpectTrue((fp != XBADFILE));
  48098. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  48099. ExpectNotNull(crl);
  48100. X509_CRL_free(crl);
  48101. if (fp != XBADFILE) {
  48102. XFCLOSE(fp);
  48103. fp = XBADFILE;
  48104. }
  48105. fp = XFOPEN(der[i], "rb");
  48106. ExpectTrue((fp != XBADFILE));
  48107. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  48108. if (EXPECT_FAIL()) {
  48109. crl = NULL;
  48110. }
  48111. ExpectNotNull(crl);
  48112. X509_CRL_free(crl);
  48113. crl = NULL;
  48114. if (fp != XBADFILE) {
  48115. XFCLOSE(fp);
  48116. fp = XBADFILE;
  48117. }
  48118. }
  48119. #endif
  48120. #endif
  48121. return EXPECT_RESULT();
  48122. }
  48123. static int test_wolfSSL_X509_load_crl_file(void)
  48124. {
  48125. EXPECT_DECLS;
  48126. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48127. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  48128. int i;
  48129. char pem[][100] = {
  48130. "./certs/crl/crl.pem",
  48131. "./certs/crl/crl2.pem",
  48132. "./certs/crl/caEccCrl.pem",
  48133. "./certs/crl/eccCliCRL.pem",
  48134. "./certs/crl/eccSrvCRL.pem",
  48135. #ifdef WC_RSA_PSS
  48136. "./certs/crl/crl_rsapss.pem",
  48137. #endif
  48138. ""
  48139. };
  48140. char der[][100] = {
  48141. "./certs/crl/crl.der",
  48142. "./certs/crl/crl2.der",
  48143. ""
  48144. };
  48145. WOLFSSL_X509_STORE* store = NULL;
  48146. WOLFSSL_X509_LOOKUP* lookup = NULL;
  48147. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  48148. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  48149. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  48150. X509_FILETYPE_PEM), 1);
  48151. #ifdef WC_RSA_PSS
  48152. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  48153. X509_FILETYPE_PEM), 1);
  48154. #endif
  48155. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  48156. X509_FILETYPE_PEM), 1);
  48157. if (store) {
  48158. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  48159. WOLFSSL_FILETYPE_PEM), 1);
  48160. /* since store hasn't yet known the revoked cert*/
  48161. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48162. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  48163. }
  48164. for (i = 0; pem[i][0] != '\0'; i++) {
  48165. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  48166. 1);
  48167. }
  48168. if (store) {
  48169. /* since store knows crl list */
  48170. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48171. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  48172. CRL_CERT_REVOKED);
  48173. #ifdef WC_RSA_PSS
  48174. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48175. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  48176. CRL_CERT_REVOKED);
  48177. #endif
  48178. }
  48179. /* once feeing store */
  48180. X509_STORE_free(store);
  48181. store = NULL;
  48182. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  48183. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  48184. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  48185. X509_FILETYPE_PEM), 1);
  48186. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  48187. X509_FILETYPE_PEM), 1);
  48188. if (store) {
  48189. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  48190. WOLFSSL_FILETYPE_PEM), 1);
  48191. /* since store hasn't yet known the revoked cert*/
  48192. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48193. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  48194. }
  48195. for (i = 0; der[i][0] != '\0'; i++) {
  48196. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  48197. 1);
  48198. }
  48199. if (store) {
  48200. /* since store knows crl list */
  48201. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48202. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  48203. CRL_CERT_REVOKED);
  48204. }
  48205. /* test for incorrect parameter */
  48206. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  48207. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  48208. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  48209. X509_STORE_free(store);
  48210. store = NULL;
  48211. #endif
  48212. return EXPECT_RESULT();
  48213. }
  48214. static int test_wolfSSL_i2d_X509(void)
  48215. {
  48216. EXPECT_DECLS;
  48217. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  48218. const unsigned char* cert_buf = server_cert_der_2048;
  48219. unsigned char* out = NULL;
  48220. unsigned char* tmp = NULL;
  48221. X509* cert = NULL;
  48222. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  48223. /* Pointer should be advanced */
  48224. ExpectPtrGT(cert_buf, server_cert_der_2048);
  48225. ExpectIntGT(i2d_X509(cert, &out), 0);
  48226. ExpectNotNull(out);
  48227. tmp = out;
  48228. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  48229. ExpectPtrGT(tmp, out);
  48230. if (out != NULL)
  48231. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  48232. X509_free(cert);
  48233. #endif
  48234. return EXPECT_RESULT();
  48235. }
  48236. static int test_wolfSSL_d2i_X509_REQ(void)
  48237. {
  48238. EXPECT_DECLS;
  48239. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  48240. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  48241. !defined(WOLFSSL_SP_MATH)
  48242. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  48243. * were generated by libest
  48244. * ./certs/csr.attr.der contains sample attributes
  48245. * ./certs/csr.ext.der contains sample extensions */
  48246. const char* csrFile = "./certs/csr.signed.der";
  48247. const char* csrPopFile = "./certs/csr.attr.der";
  48248. const char* csrExtFile = "./certs/csr.ext.der";
  48249. /* ./certs/csr.dsa.pem is generated using
  48250. * openssl req -newkey dsa:certs/dsaparams.pem \
  48251. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  48252. * -outform PEM
  48253. * with the passphrase "wolfSSL"
  48254. */
  48255. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48256. const char* csrDsaFile = "./certs/csr.dsa.pem";
  48257. XFILE f = XBADFILE;
  48258. #endif
  48259. BIO* bio = NULL;
  48260. X509* req = NULL;
  48261. EVP_PKEY *pub_key = NULL;
  48262. {
  48263. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  48264. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48265. /*
  48266. * Extract the public key from the CSR
  48267. */
  48268. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48269. /*
  48270. * Verify the signature in the CSR
  48271. */
  48272. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48273. X509_free(req);
  48274. req = NULL;
  48275. BIO_free(bio);
  48276. bio = NULL;
  48277. EVP_PKEY_free(pub_key);
  48278. pub_key = NULL;
  48279. }
  48280. {
  48281. #ifdef OPENSSL_ALL
  48282. X509_ATTRIBUTE* attr = NULL;
  48283. ASN1_TYPE *at = NULL;
  48284. #endif
  48285. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  48286. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48287. /*
  48288. * Extract the public key from the CSR
  48289. */
  48290. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48291. /*
  48292. * Verify the signature in the CSR
  48293. */
  48294. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48295. #ifdef OPENSSL_ALL
  48296. /*
  48297. * Obtain the challenge password from the CSR
  48298. */
  48299. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  48300. -1), 1);
  48301. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  48302. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48303. ExpectNotNull(at->value.asn1_string);
  48304. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  48305. "2xIE+qqp/rhyTXP+");
  48306. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  48307. #endif
  48308. X509_free(req);
  48309. req = NULL;
  48310. BIO_free(bio);
  48311. bio = NULL;
  48312. EVP_PKEY_free(pub_key);
  48313. pub_key = NULL;
  48314. }
  48315. {
  48316. #ifdef OPENSSL_ALL
  48317. X509_ATTRIBUTE* attr = NULL;
  48318. ASN1_TYPE *at = NULL;
  48319. STACK_OF(X509_EXTENSION) *exts = NULL;
  48320. #endif
  48321. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  48322. /* This CSR contains an Extension Request attribute so
  48323. * we test extension parsing in a CSR attribute here. */
  48324. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48325. /*
  48326. * Extract the public key from the CSR
  48327. */
  48328. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48329. /*
  48330. * Verify the signature in the CSR
  48331. */
  48332. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48333. #ifdef OPENSSL_ALL
  48334. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  48335. req));
  48336. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  48337. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  48338. /*
  48339. * Obtain the challenge password from the CSR
  48340. */
  48341. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  48342. -1), 0);
  48343. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  48344. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48345. ExpectNotNull(at->value.asn1_string);
  48346. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  48347. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  48348. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  48349. #endif
  48350. X509_free(req);
  48351. req = NULL;
  48352. BIO_free(bio);
  48353. bio = NULL;
  48354. EVP_PKEY_free(pub_key);
  48355. pub_key = NULL;
  48356. }
  48357. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48358. {
  48359. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  48360. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  48361. /*
  48362. * Extract the public key from the CSR
  48363. */
  48364. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48365. /*
  48366. * Verify the signature in the CSR
  48367. */
  48368. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48369. X509_free(req);
  48370. req = NULL;
  48371. BIO_free(bio);
  48372. /* Run the same test, but with a file pointer instead of a BIO.
  48373. * (PEM_read_X509_REQ)*/
  48374. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  48375. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  48376. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48377. X509_free(req);
  48378. EVP_PKEY_free(pub_key);
  48379. }
  48380. #endif /* !NO_DSA && !HAVE_SELFTEST */
  48381. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  48382. return EXPECT_RESULT();
  48383. }
  48384. static int test_wolfSSL_PEM_read_X509(void)
  48385. {
  48386. EXPECT_DECLS;
  48387. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48388. !defined(NO_RSA)
  48389. X509 *x509 = NULL;
  48390. XFILE fp = XBADFILE;
  48391. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  48392. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  48393. X509_free(x509);
  48394. if (fp != XBADFILE)
  48395. XFCLOSE(fp);
  48396. #endif
  48397. return EXPECT_RESULT();
  48398. }
  48399. static int test_wolfSSL_PEM_read(void)
  48400. {
  48401. EXPECT_DECLS;
  48402. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  48403. const char* filename = "./certs/server-keyEnc.pem";
  48404. XFILE fp = XBADFILE;
  48405. char* name = NULL;
  48406. char* header = NULL;
  48407. byte* data = NULL;
  48408. long len;
  48409. EVP_CIPHER_INFO cipher;
  48410. WOLFSSL_BIO* bio = NULL;
  48411. byte* fileData = NULL;
  48412. size_t fileDataSz = 0;
  48413. byte* out;
  48414. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  48415. /* Fail cases. */
  48416. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  48417. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  48418. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  48419. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  48420. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48421. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  48422. ExpectIntGT(XSTRLEN(header), 0);
  48423. ExpectIntGT(len, 0);
  48424. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  48425. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  48426. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  48427. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  48428. DYNAMIC_TYPE_TMP_BUFFER));
  48429. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  48430. if (fp != XBADFILE) {
  48431. XFCLOSE(fp);
  48432. fp = XBADFILE;
  48433. }
  48434. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48435. /* Fail cases. */
  48436. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  48437. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  48438. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  48439. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  48440. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  48441. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  48442. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  48443. /* Fail cases. */
  48444. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  48445. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  48446. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  48447. #ifndef NO_DES3
  48448. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  48449. #endif
  48450. /* Fail cases. */
  48451. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  48452. (void*)"yassl123"), WOLFSSL_FAILURE);
  48453. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  48454. (void*)"yassl123"), WOLFSSL_FAILURE);
  48455. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  48456. (void*)"yassl123"), WOLFSSL_FAILURE);
  48457. #if !defined(NO_DES3) && !defined(NO_MD5)
  48458. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  48459. (void*)"yassl123"), WOLFSSL_SUCCESS);
  48460. #endif
  48461. BIO_free(bio);
  48462. bio = NULL;
  48463. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48464. fileData = NULL;
  48465. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48466. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48467. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48468. name = NULL;
  48469. header = NULL;
  48470. data = NULL;
  48471. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  48472. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48473. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  48474. ExpectIntEQ(XSTRLEN(header), 0);
  48475. ExpectIntGT(len, 0);
  48476. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  48477. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  48478. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  48479. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  48480. DYNAMIC_TYPE_TMP_BUFFER));
  48481. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  48482. if (fp != XBADFILE)
  48483. XFCLOSE(fp);
  48484. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48485. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  48486. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  48487. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  48488. BIO_free(bio);
  48489. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48490. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48491. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48492. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48493. #endif
  48494. return EXPECT_RESULT();
  48495. }
  48496. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  48497. {
  48498. EXPECT_DECLS;
  48499. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48500. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48501. const byte iv[12] = { 0 };
  48502. const byte key[16] = { 0 };
  48503. const byte cleartext[16] = { 0 };
  48504. const byte aad[] = {
  48505. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  48506. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  48507. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  48508. 0x4f
  48509. };
  48510. byte out1Part[16];
  48511. byte outTag1Part[16];
  48512. byte out2Part[16];
  48513. byte outTag2Part[16];
  48514. byte decryptBuf[16];
  48515. int len = 0;
  48516. int tlen;
  48517. EVP_CIPHER_CTX* ctx = NULL;
  48518. /* ENCRYPT */
  48519. /* Send AAD and data in 1 part */
  48520. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  48521. tlen = 0;
  48522. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48523. 1);
  48524. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48525. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  48526. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  48527. sizeof(cleartext)), 1);
  48528. tlen += len;
  48529. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  48530. tlen += len;
  48531. ExpectIntEQ(tlen, sizeof(cleartext));
  48532. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  48533. outTag1Part), 1);
  48534. EVP_CIPHER_CTX_free(ctx);
  48535. ctx = NULL;
  48536. /* DECRYPT */
  48537. /* Send AAD and data in 1 part */
  48538. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  48539. tlen = 0;
  48540. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48541. 1);
  48542. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48543. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  48544. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  48545. sizeof(cleartext)), 1);
  48546. tlen += len;
  48547. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  48548. outTag1Part), 1);
  48549. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  48550. tlen += len;
  48551. ExpectIntEQ(tlen, sizeof(cleartext));
  48552. EVP_CIPHER_CTX_free(ctx);
  48553. ctx = NULL;
  48554. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  48555. /* ENCRYPT */
  48556. /* Send AAD and data in 2 parts */
  48557. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  48558. tlen = 0;
  48559. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48560. 1);
  48561. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48562. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  48563. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  48564. 1);
  48565. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  48566. tlen += len;
  48567. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  48568. sizeof(cleartext) - 1), 1);
  48569. tlen += len;
  48570. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  48571. tlen += len;
  48572. ExpectIntEQ(tlen, sizeof(cleartext));
  48573. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  48574. outTag2Part), 1);
  48575. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  48576. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  48577. EVP_CIPHER_CTX_free(ctx);
  48578. ctx = NULL;
  48579. /* DECRYPT */
  48580. /* Send AAD and data in 2 parts */
  48581. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  48582. tlen = 0;
  48583. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48584. 1);
  48585. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48586. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  48587. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  48588. 1);
  48589. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  48590. tlen += len;
  48591. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  48592. sizeof(cleartext) - 1), 1);
  48593. tlen += len;
  48594. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  48595. outTag1Part), 1);
  48596. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  48597. tlen += len;
  48598. ExpectIntEQ(tlen, sizeof(cleartext));
  48599. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  48600. /* Test AAD reuse */
  48601. EVP_CIPHER_CTX_free(ctx);
  48602. #endif
  48603. return EXPECT_RESULT();
  48604. }
  48605. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  48606. {
  48607. EXPECT_DECLS;
  48608. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48609. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48610. /* Zero length plain text */
  48611. byte key[] = {
  48612. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48614. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48615. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48616. }; /* align */
  48617. byte iv[] = {
  48618. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48619. }; /* align */
  48620. byte plaintxt[1];
  48621. int ivSz = 12;
  48622. int plaintxtSz = 0;
  48623. unsigned char tag[16];
  48624. unsigned char tag_kat[] = {
  48625. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  48626. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  48627. };
  48628. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48629. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48630. int ciphertxtSz = 0;
  48631. int decryptedtxtSz = 0;
  48632. int len = 0;
  48633. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48634. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48635. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  48636. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48637. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48638. plaintxtSz));
  48639. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48640. ciphertxtSz += len;
  48641. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  48642. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48643. ExpectIntEQ(0, ciphertxtSz);
  48644. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  48645. EVP_CIPHER_CTX_init(de);
  48646. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  48647. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48648. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48649. decryptedtxtSz = len;
  48650. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  48651. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48652. decryptedtxtSz += len;
  48653. ExpectIntEQ(0, decryptedtxtSz);
  48654. EVP_CIPHER_CTX_free(en);
  48655. EVP_CIPHER_CTX_free(de);
  48656. #endif
  48657. return EXPECT_RESULT();
  48658. }
  48659. static int test_wolfssl_EVP_aes_gcm(void)
  48660. {
  48661. EXPECT_DECLS;
  48662. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48663. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48664. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48665. byte *key = (byte*)"01234567890123456789012345678901";
  48666. /* A 128 bit IV */
  48667. byte *iv = (byte*)"0123456789012345";
  48668. int ivSz = AES_BLOCK_SIZE;
  48669. /* Message to be encrypted */
  48670. byte *plaintxt = (byte*)"for things to change you have to change";
  48671. /* Additional non-confidential data */
  48672. byte *aad = (byte*)"Don't spend major time on minor things.";
  48673. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48674. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48675. int aadSz = (int)XSTRLEN((char*)aad);
  48676. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48677. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48678. int ciphertxtSz = 0;
  48679. int decryptedtxtSz = 0;
  48680. int len = 0;
  48681. int i = 0;
  48682. EVP_CIPHER_CTX en[2];
  48683. EVP_CIPHER_CTX de[2];
  48684. for (i = 0; i < 2; i++) {
  48685. EVP_CIPHER_CTX_init(&en[i]);
  48686. if (i == 0) {
  48687. /* Default uses 96-bits IV length */
  48688. #ifdef WOLFSSL_AES_128
  48689. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  48690. key, iv));
  48691. #elif defined(WOLFSSL_AES_192)
  48692. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  48693. key, iv));
  48694. #elif defined(WOLFSSL_AES_256)
  48695. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  48696. key, iv));
  48697. #endif
  48698. }
  48699. else {
  48700. #ifdef WOLFSSL_AES_128
  48701. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  48702. NULL, NULL));
  48703. #elif defined(WOLFSSL_AES_192)
  48704. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  48705. NULL, NULL));
  48706. #elif defined(WOLFSSL_AES_256)
  48707. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  48708. NULL, NULL));
  48709. #endif
  48710. /* non-default must to set the IV length first */
  48711. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  48712. ivSz, NULL));
  48713. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48714. }
  48715. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48716. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48717. plaintxtSz));
  48718. ciphertxtSz = len;
  48719. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48720. ciphertxtSz += len;
  48721. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  48722. AES_BLOCK_SIZE, tag));
  48723. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48724. EVP_CIPHER_CTX_init(&de[i]);
  48725. if (i == 0) {
  48726. /* Default uses 96-bits IV length */
  48727. #ifdef WOLFSSL_AES_128
  48728. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48729. key, iv));
  48730. #elif defined(WOLFSSL_AES_192)
  48731. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48732. key, iv));
  48733. #elif defined(WOLFSSL_AES_256)
  48734. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48735. key, iv));
  48736. #endif
  48737. }
  48738. else {
  48739. #ifdef WOLFSSL_AES_128
  48740. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48741. NULL, NULL));
  48742. #elif defined(WOLFSSL_AES_192)
  48743. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48744. NULL, NULL));
  48745. #elif defined(WOLFSSL_AES_256)
  48746. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48747. NULL, NULL));
  48748. #endif
  48749. /* non-default must to set the IV length first */
  48750. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  48751. ivSz, NULL));
  48752. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48753. }
  48754. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48755. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48756. ciphertxtSz));
  48757. decryptedtxtSz = len;
  48758. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48759. AES_BLOCK_SIZE, tag));
  48760. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48761. decryptedtxtSz += len;
  48762. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48763. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48764. /* modify tag*/
  48765. if (i == 0) {
  48766. /* Default uses 96-bits IV length */
  48767. #ifdef WOLFSSL_AES_128
  48768. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48769. key, iv));
  48770. #elif defined(WOLFSSL_AES_192)
  48771. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48772. key, iv));
  48773. #elif defined(WOLFSSL_AES_256)
  48774. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48775. key, iv));
  48776. #endif
  48777. }
  48778. else {
  48779. #ifdef WOLFSSL_AES_128
  48780. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48781. NULL, NULL));
  48782. #elif defined(WOLFSSL_AES_192)
  48783. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48784. NULL, NULL));
  48785. #elif defined(WOLFSSL_AES_256)
  48786. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48787. NULL, NULL));
  48788. #endif
  48789. /* non-default must to set the IV length first */
  48790. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  48791. ivSz, NULL));
  48792. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48793. }
  48794. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48795. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48796. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48797. AES_BLOCK_SIZE, tag));
  48798. /* fail due to wrong tag */
  48799. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48800. ciphertxtSz));
  48801. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48802. ExpectIntEQ(0, len);
  48803. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48804. }
  48805. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48806. return EXPECT_RESULT();
  48807. }
  48808. static int test_wolfssl_EVP_aria_gcm(void)
  48809. {
  48810. int res = TEST_SKIPPED;
  48811. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  48812. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48813. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48814. byte *key = (byte*)"01234567890123456789012345678901";
  48815. /* A 128 bit IV */
  48816. byte *iv = (byte*)"0123456789012345";
  48817. int ivSz = ARIA_BLOCK_SIZE;
  48818. /* Message to be encrypted */
  48819. const int plaintxtSz = 40;
  48820. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  48821. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  48822. /* Additional non-confidential data */
  48823. byte *aad = (byte*)"Don't spend major time on minor things.";
  48824. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  48825. int aadSz = (int)XSTRLEN((char*)aad);
  48826. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  48827. byte decryptedtxt[plaintxtSz];
  48828. int ciphertxtSz = 0;
  48829. int decryptedtxtSz = 0;
  48830. int len = 0;
  48831. int i = 0;
  48832. #define TEST_ARIA_GCM_COUNT 6
  48833. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  48834. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  48835. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  48836. EVP_CIPHER_CTX_init(&en[i]);
  48837. switch (i) {
  48838. case 0:
  48839. /* Default uses 96-bits IV length */
  48840. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  48841. break;
  48842. case 1:
  48843. /* Default uses 96-bits IV length */
  48844. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  48845. break;
  48846. case 2:
  48847. /* Default uses 96-bits IV length */
  48848. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  48849. break;
  48850. case 3:
  48851. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  48852. /* non-default must to set the IV length first */
  48853. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48854. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48855. break;
  48856. case 4:
  48857. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  48858. /* non-default must to set the IV length first */
  48859. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48860. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48861. break;
  48862. case 5:
  48863. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  48864. /* non-default must to set the IV length first */
  48865. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48866. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48867. break;
  48868. }
  48869. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  48870. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48871. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  48872. ciphertxtSz = len;
  48873. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48874. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  48875. ciphertxtSz += len;
  48876. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  48877. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48878. EVP_CIPHER_CTX_init(&de[i]);
  48879. switch (i) {
  48880. case 0:
  48881. /* Default uses 96-bits IV length */
  48882. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  48883. break;
  48884. case 1:
  48885. /* Default uses 96-bits IV length */
  48886. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  48887. break;
  48888. case 2:
  48889. /* Default uses 96-bits IV length */
  48890. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  48891. break;
  48892. case 3:
  48893. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  48894. /* non-default must to set the IV length first */
  48895. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48896. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48897. break;
  48898. case 4:
  48899. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  48900. /* non-default must to set the IV length first */
  48901. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48902. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48903. break;
  48904. case 5:
  48905. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  48906. /* non-default must to set the IV length first */
  48907. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48908. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48909. break;
  48910. }
  48911. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  48912. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48913. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48914. decryptedtxtSz = len;
  48915. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  48916. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48917. decryptedtxtSz += len;
  48918. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  48919. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48920. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  48921. /* modify tag*/
  48922. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48923. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48924. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  48925. /* fail due to wrong tag */
  48926. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48927. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48928. AssertIntEQ(0, len);
  48929. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48930. }
  48931. res = TEST_RES_CHECK(1);
  48932. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48933. return res;
  48934. }
  48935. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  48936. {
  48937. EXPECT_DECLS;
  48938. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48939. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48940. /* Zero length plain text */
  48941. byte key[] = {
  48942. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48943. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48944. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48945. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48946. }; /* align */
  48947. byte iv[] = {
  48948. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48949. }; /* align */
  48950. byte plaintxt[1];
  48951. int ivSz = 12;
  48952. int plaintxtSz = 0;
  48953. unsigned char tag[16];
  48954. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48955. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48956. int ciphertxtSz = 0;
  48957. int decryptedtxtSz = 0;
  48958. int len = 0;
  48959. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48960. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48961. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  48962. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48963. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48964. plaintxtSz));
  48965. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48966. ciphertxtSz += len;
  48967. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48968. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48969. ExpectIntEQ(0, ciphertxtSz);
  48970. EVP_CIPHER_CTX_init(de);
  48971. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  48972. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48973. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48974. decryptedtxtSz = len;
  48975. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48976. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48977. decryptedtxtSz += len;
  48978. ExpectIntEQ(0, decryptedtxtSz);
  48979. EVP_CIPHER_CTX_free(en);
  48980. EVP_CIPHER_CTX_free(de);
  48981. #endif
  48982. return EXPECT_RESULT();
  48983. }
  48984. static int test_wolfssl_EVP_aes_ccm(void)
  48985. {
  48986. EXPECT_DECLS;
  48987. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48988. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48989. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48990. byte *key = (byte*)"01234567890123456789012345678901";
  48991. /* A 128 bit IV */
  48992. byte *iv = (byte*)"0123456789012";
  48993. int ivSz = (int)XSTRLEN((char*)iv);
  48994. /* Message to be encrypted */
  48995. byte *plaintxt = (byte*)"for things to change you have to change";
  48996. /* Additional non-confidential data */
  48997. byte *aad = (byte*)"Don't spend major time on minor things.";
  48998. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48999. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49000. int aadSz = (int)XSTRLEN((char*)aad);
  49001. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49002. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49003. int ciphertxtSz = 0;
  49004. int decryptedtxtSz = 0;
  49005. int len = 0;
  49006. int i = 0;
  49007. int ret;
  49008. EVP_CIPHER_CTX en[2];
  49009. EVP_CIPHER_CTX de[2];
  49010. for (i = 0; i < 2; i++) {
  49011. EVP_CIPHER_CTX_init(&en[i]);
  49012. if (i == 0) {
  49013. /* Default uses 96-bits IV length */
  49014. #ifdef WOLFSSL_AES_128
  49015. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  49016. key, iv));
  49017. #elif defined(WOLFSSL_AES_192)
  49018. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  49019. key, iv));
  49020. #elif defined(WOLFSSL_AES_256)
  49021. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  49022. key, iv));
  49023. #endif
  49024. }
  49025. else {
  49026. #ifdef WOLFSSL_AES_128
  49027. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  49028. NULL, NULL));
  49029. #elif defined(WOLFSSL_AES_192)
  49030. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  49031. NULL, NULL));
  49032. #elif defined(WOLFSSL_AES_256)
  49033. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  49034. NULL, NULL));
  49035. #endif
  49036. /* non-default must to set the IV length first */
  49037. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  49038. ivSz, NULL));
  49039. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49040. }
  49041. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49042. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49043. plaintxtSz));
  49044. ciphertxtSz = len;
  49045. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49046. ciphertxtSz += len;
  49047. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  49048. AES_BLOCK_SIZE, tag));
  49049. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  49050. ExpectIntEQ(ret, 1);
  49051. EVP_CIPHER_CTX_init(&de[i]);
  49052. if (i == 0) {
  49053. /* Default uses 96-bits IV length */
  49054. #ifdef WOLFSSL_AES_128
  49055. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  49056. key, iv));
  49057. #elif defined(WOLFSSL_AES_192)
  49058. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  49059. key, iv));
  49060. #elif defined(WOLFSSL_AES_256)
  49061. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  49062. key, iv));
  49063. #endif
  49064. }
  49065. else {
  49066. #ifdef WOLFSSL_AES_128
  49067. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  49068. NULL, NULL));
  49069. #elif defined(WOLFSSL_AES_192)
  49070. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  49071. NULL, NULL));
  49072. #elif defined(WOLFSSL_AES_256)
  49073. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  49074. NULL, NULL));
  49075. #endif
  49076. /* non-default must to set the IV length first */
  49077. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  49078. ivSz, NULL));
  49079. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49080. }
  49081. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49082. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49083. ciphertxtSz));
  49084. decryptedtxtSz = len;
  49085. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49086. AES_BLOCK_SIZE, tag));
  49087. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49088. decryptedtxtSz += len;
  49089. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49090. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49091. /* modify tag*/
  49092. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49093. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49094. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49095. AES_BLOCK_SIZE, tag));
  49096. /* fail due to wrong tag */
  49097. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49098. ciphertxtSz));
  49099. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49100. ExpectIntEQ(0, len);
  49101. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  49102. ExpectIntEQ(ret, 1);
  49103. }
  49104. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  49105. return EXPECT_RESULT();
  49106. }
  49107. static int test_wolfssl_EVP_chacha20_poly1305(void)
  49108. {
  49109. EXPECT_DECLS;
  49110. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  49111. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  49112. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  49113. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49114. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  49115. byte cipherText[sizeof(plainText)];
  49116. byte decryptedText[sizeof(plainText)];
  49117. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  49118. EVP_CIPHER_CTX* ctx = NULL;
  49119. int outSz;
  49120. /* Encrypt. */
  49121. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49122. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  49123. NULL), WOLFSSL_SUCCESS);
  49124. /* Invalid IV length. */
  49125. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49126. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  49127. /* Valid IV length. */
  49128. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49129. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  49130. /* Invalid tag length. */
  49131. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49132. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  49133. /* Valid tag length. */
  49134. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49135. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  49136. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49137. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  49138. WOLFSSL_SUCCESS);
  49139. ExpectIntEQ(outSz, sizeof(aad));
  49140. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49141. sizeof(plainText)), WOLFSSL_SUCCESS);
  49142. ExpectIntEQ(outSz, sizeof(plainText));
  49143. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49144. ExpectIntEQ(outSz, 0);
  49145. /* Invalid tag length. */
  49146. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  49147. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  49148. /* Valid tag length. */
  49149. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  49150. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  49151. EVP_CIPHER_CTX_free(ctx);
  49152. ctx = NULL;
  49153. /* Decrypt. */
  49154. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49155. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  49156. NULL), WOLFSSL_SUCCESS);
  49157. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49158. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  49159. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49160. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  49161. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49162. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  49163. WOLFSSL_SUCCESS);
  49164. ExpectIntEQ(outSz, sizeof(aad));
  49165. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49166. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49167. ExpectIntEQ(outSz, sizeof(cipherText));
  49168. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49169. WOLFSSL_SUCCESS);
  49170. ExpectIntEQ(outSz, 0);
  49171. EVP_CIPHER_CTX_free(ctx);
  49172. ctx = NULL;
  49173. /* Test partial Inits. CipherInit() allow setting of key and iv
  49174. * in separate calls. */
  49175. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49176. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  49177. key, NULL, 1), WOLFSSL_SUCCESS);
  49178. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49179. WOLFSSL_SUCCESS);
  49180. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  49181. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  49182. ExpectIntEQ(outSz, sizeof(aad));
  49183. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49184. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49185. ExpectIntEQ(outSz, sizeof(cipherText));
  49186. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49187. WOLFSSL_SUCCESS);
  49188. ExpectIntEQ(outSz, 0);
  49189. EVP_CIPHER_CTX_free(ctx);
  49190. #endif
  49191. return EXPECT_RESULT();
  49192. }
  49193. static int test_wolfssl_EVP_chacha20(void)
  49194. {
  49195. EXPECT_DECLS;
  49196. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  49197. byte key[CHACHA_MAX_KEY_SZ];
  49198. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  49199. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49200. byte cipherText[sizeof(plainText)];
  49201. byte decryptedText[sizeof(plainText)];
  49202. EVP_CIPHER_CTX* ctx = NULL;
  49203. int outSz;
  49204. XMEMSET(key, 0, sizeof(key));
  49205. XMEMSET(iv, 0, sizeof(iv));
  49206. /* Encrypt. */
  49207. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49208. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  49209. NULL), WOLFSSL_SUCCESS);
  49210. /* Any tag length must fail - not an AEAD cipher. */
  49211. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49212. 16, NULL), WOLFSSL_FAILURE);
  49213. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49214. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49215. sizeof(plainText)), WOLFSSL_SUCCESS);
  49216. ExpectIntEQ(outSz, sizeof(plainText));
  49217. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49218. ExpectIntEQ(outSz, 0);
  49219. EVP_CIPHER_CTX_free(ctx);
  49220. ctx = NULL;
  49221. /* Decrypt. */
  49222. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49223. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  49224. NULL), WOLFSSL_SUCCESS);
  49225. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49226. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49227. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49228. ExpectIntEQ(outSz, sizeof(cipherText));
  49229. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49230. WOLFSSL_SUCCESS);
  49231. ExpectIntEQ(outSz, 0);
  49232. EVP_CIPHER_CTX_free(ctx);
  49233. ctx = NULL;
  49234. /* Test partial Inits. CipherInit() allow setting of key and iv
  49235. * in separate calls. */
  49236. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49237. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  49238. key, NULL, 1), WOLFSSL_SUCCESS);
  49239. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49240. WOLFSSL_SUCCESS);
  49241. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49242. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49243. ExpectIntEQ(outSz, sizeof(cipherText));
  49244. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49245. WOLFSSL_SUCCESS);
  49246. ExpectIntEQ(outSz, 0);
  49247. EVP_CIPHER_CTX_free(ctx);
  49248. #endif
  49249. return EXPECT_RESULT();
  49250. }
  49251. static int test_wolfssl_EVP_sm4_ecb(void)
  49252. {
  49253. int res = TEST_SKIPPED;
  49254. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  49255. EXPECT_DECLS;
  49256. byte key[SM4_KEY_SIZE];
  49257. byte plainText[SM4_BLOCK_SIZE] = {
  49258. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  49259. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  49260. };
  49261. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49262. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49263. EVP_CIPHER_CTX* ctx;
  49264. int outSz;
  49265. XMEMSET(key, 0, sizeof(key));
  49266. /* Encrypt. */
  49267. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49268. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  49269. WOLFSSL_SUCCESS);
  49270. /* Any tag length must fail - not an AEAD cipher. */
  49271. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49272. WOLFSSL_FAILURE);
  49273. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  49274. WOLFSSL_SUCCESS);
  49275. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49276. sizeof(plainText)), WOLFSSL_SUCCESS);
  49277. ExpectIntEQ(outSz, sizeof(plainText));
  49278. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  49279. WOLFSSL_SUCCESS);
  49280. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  49281. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49282. EVP_CIPHER_CTX_free(ctx);
  49283. /* Decrypt. */
  49284. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49285. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  49286. WOLFSSL_SUCCESS);
  49287. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  49288. WOLFSSL_SUCCESS);
  49289. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49290. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49291. ExpectIntEQ(outSz, sizeof(plainText));
  49292. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49293. WOLFSSL_SUCCESS);
  49294. ExpectIntEQ(outSz, 0);
  49295. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49296. EVP_CIPHER_CTX_free(ctx);
  49297. res = EXPECT_RESULT();
  49298. #endif
  49299. return res;
  49300. }
  49301. static int test_wolfssl_EVP_sm4_cbc(void)
  49302. {
  49303. int res = TEST_SKIPPED;
  49304. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  49305. EXPECT_DECLS;
  49306. byte key[SM4_KEY_SIZE];
  49307. byte iv[SM4_BLOCK_SIZE];
  49308. byte plainText[SM4_BLOCK_SIZE] = {
  49309. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  49310. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  49311. };
  49312. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49313. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49314. EVP_CIPHER_CTX* ctx;
  49315. int outSz;
  49316. XMEMSET(key, 0, sizeof(key));
  49317. XMEMSET(iv, 0, sizeof(iv));
  49318. /* Encrypt. */
  49319. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49320. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  49321. WOLFSSL_SUCCESS);
  49322. /* Any tag length must fail - not an AEAD cipher. */
  49323. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49324. WOLFSSL_FAILURE);
  49325. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49326. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49327. sizeof(plainText)), WOLFSSL_SUCCESS);
  49328. ExpectIntEQ(outSz, sizeof(plainText));
  49329. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  49330. WOLFSSL_SUCCESS);
  49331. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  49332. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49333. EVP_CIPHER_CTX_free(ctx);
  49334. /* Decrypt. */
  49335. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49336. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  49337. WOLFSSL_SUCCESS);
  49338. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49339. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49340. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49341. ExpectIntEQ(outSz, sizeof(plainText));
  49342. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49343. WOLFSSL_SUCCESS);
  49344. ExpectIntEQ(outSz, 0);
  49345. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49346. EVP_CIPHER_CTX_free(ctx);
  49347. /* Test partial Inits. CipherInit() allow setting of key and iv
  49348. * in separate calls. */
  49349. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49350. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  49351. WOLFSSL_SUCCESS);
  49352. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  49353. WOLFSSL_SUCCESS);
  49354. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49355. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49356. ExpectIntEQ(outSz, sizeof(plainText));
  49357. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49358. WOLFSSL_SUCCESS);
  49359. ExpectIntEQ(outSz, 0);
  49360. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49361. EVP_CIPHER_CTX_free(ctx);
  49362. res = EXPECT_RESULT();
  49363. #endif
  49364. return res;
  49365. }
  49366. static int test_wolfssl_EVP_sm4_ctr(void)
  49367. {
  49368. int res = TEST_SKIPPED;
  49369. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  49370. EXPECT_DECLS;
  49371. byte key[SM4_KEY_SIZE];
  49372. byte iv[SM4_BLOCK_SIZE];
  49373. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49374. byte cipherText[sizeof(plainText)];
  49375. byte decryptedText[sizeof(plainText)];
  49376. EVP_CIPHER_CTX* ctx;
  49377. int outSz;
  49378. XMEMSET(key, 0, sizeof(key));
  49379. XMEMSET(iv, 0, sizeof(iv));
  49380. /* Encrypt. */
  49381. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49382. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  49383. WOLFSSL_SUCCESS);
  49384. /* Any tag length must fail - not an AEAD cipher. */
  49385. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49386. WOLFSSL_FAILURE);
  49387. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49388. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49389. sizeof(plainText)), WOLFSSL_SUCCESS);
  49390. ExpectIntEQ(outSz, sizeof(plainText));
  49391. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49392. ExpectIntEQ(outSz, 0);
  49393. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49394. EVP_CIPHER_CTX_free(ctx);
  49395. /* Decrypt. */
  49396. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49397. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  49398. WOLFSSL_SUCCESS);
  49399. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49400. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49401. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49402. ExpectIntEQ(outSz, sizeof(cipherText));
  49403. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49404. WOLFSSL_SUCCESS);
  49405. ExpectIntEQ(outSz, 0);
  49406. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49407. EVP_CIPHER_CTX_free(ctx);
  49408. /* Test partial Inits. CipherInit() allow setting of key and iv
  49409. * in separate calls. */
  49410. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49411. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  49412. WOLFSSL_SUCCESS);
  49413. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49414. WOLFSSL_SUCCESS);
  49415. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49416. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49417. ExpectIntEQ(outSz, sizeof(cipherText));
  49418. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49419. WOLFSSL_SUCCESS);
  49420. ExpectIntEQ(outSz, 0);
  49421. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49422. EVP_CIPHER_CTX_free(ctx);
  49423. res = EXPECT_RESULT();
  49424. #endif
  49425. return res;
  49426. }
  49427. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  49428. {
  49429. int res = TEST_SKIPPED;
  49430. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  49431. /* Zero length plain text */
  49432. EXPECT_DECLS;
  49433. byte key[] = {
  49434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49435. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49436. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49437. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49438. }; /* align */
  49439. byte iv[] = {
  49440. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49441. }; /* align */
  49442. byte plaintxt[1];
  49443. int ivSz = 12;
  49444. int plaintxtSz = 0;
  49445. unsigned char tag[16];
  49446. unsigned char tag_kat[16] = {
  49447. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  49448. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  49449. };
  49450. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  49451. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  49452. int ciphertxtSz = 0;
  49453. int decryptedtxtSz = 0;
  49454. int len = 0;
  49455. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49456. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49457. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  49458. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49459. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49460. plaintxtSz));
  49461. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49462. ciphertxtSz += len;
  49463. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  49464. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49465. ExpectIntEQ(0, ciphertxtSz);
  49466. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  49467. EVP_CIPHER_CTX_init(de);
  49468. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  49469. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49470. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49471. decryptedtxtSz = len;
  49472. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  49473. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49474. decryptedtxtSz += len;
  49475. ExpectIntEQ(0, decryptedtxtSz);
  49476. EVP_CIPHER_CTX_free(en);
  49477. EVP_CIPHER_CTX_free(de);
  49478. res = EXPECT_RESULT();
  49479. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  49480. return res;
  49481. }
  49482. static int test_wolfssl_EVP_sm4_gcm(void)
  49483. {
  49484. int res = TEST_SKIPPED;
  49485. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  49486. EXPECT_DECLS;
  49487. byte *key = (byte*)"0123456789012345";
  49488. /* A 128 bit IV */
  49489. byte *iv = (byte*)"0123456789012345";
  49490. int ivSz = SM4_BLOCK_SIZE;
  49491. /* Message to be encrypted */
  49492. byte *plaintxt = (byte*)"for things to change you have to change";
  49493. /* Additional non-confidential data */
  49494. byte *aad = (byte*)"Don't spend major time on minor things.";
  49495. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  49496. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49497. int aadSz = (int)XSTRLEN((char*)aad);
  49498. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  49499. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  49500. int ciphertxtSz = 0;
  49501. int decryptedtxtSz = 0;
  49502. int len = 0;
  49503. int i = 0;
  49504. EVP_CIPHER_CTX en[2];
  49505. EVP_CIPHER_CTX de[2];
  49506. for (i = 0; i < 2; i++) {
  49507. EVP_CIPHER_CTX_init(&en[i]);
  49508. if (i == 0) {
  49509. /* Default uses 96-bits IV length */
  49510. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  49511. iv));
  49512. }
  49513. else {
  49514. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  49515. NULL));
  49516. /* non-default must to set the IV length first */
  49517. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  49518. ivSz, NULL));
  49519. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49520. }
  49521. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49522. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49523. plaintxtSz));
  49524. ciphertxtSz = len;
  49525. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49526. ciphertxtSz += len;
  49527. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  49528. SM4_BLOCK_SIZE, tag));
  49529. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49530. EVP_CIPHER_CTX_init(&de[i]);
  49531. if (i == 0) {
  49532. /* Default uses 96-bits IV length */
  49533. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  49534. iv));
  49535. }
  49536. else {
  49537. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  49538. NULL));
  49539. /* non-default must to set the IV length first */
  49540. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  49541. ivSz, NULL));
  49542. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49543. }
  49544. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49545. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49546. ciphertxtSz));
  49547. decryptedtxtSz = len;
  49548. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  49549. SM4_BLOCK_SIZE, tag));
  49550. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49551. decryptedtxtSz += len;
  49552. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49553. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49554. /* modify tag*/
  49555. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  49556. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49557. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  49558. SM4_BLOCK_SIZE, tag));
  49559. /* fail due to wrong tag */
  49560. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49561. ciphertxtSz));
  49562. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49563. ExpectIntEQ(0, len);
  49564. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49565. }
  49566. res = EXPECT_RESULT();
  49567. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  49568. return res;
  49569. }
  49570. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  49571. {
  49572. int res = TEST_SKIPPED;
  49573. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  49574. /* Zero length plain text */
  49575. EXPECT_DECLS;
  49576. byte key[] = {
  49577. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49578. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49579. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49580. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49581. }; /* align */
  49582. byte iv[] = {
  49583. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49584. }; /* align */
  49585. byte plaintxt[1];
  49586. int ivSz = 12;
  49587. int plaintxtSz = 0;
  49588. unsigned char tag[16];
  49589. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  49590. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  49591. int ciphertxtSz = 0;
  49592. int decryptedtxtSz = 0;
  49593. int len = 0;
  49594. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49595. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49596. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  49597. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49598. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49599. plaintxtSz));
  49600. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49601. ciphertxtSz += len;
  49602. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  49603. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49604. ExpectIntEQ(0, ciphertxtSz);
  49605. EVP_CIPHER_CTX_init(de);
  49606. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  49607. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49608. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49609. decryptedtxtSz = len;
  49610. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  49611. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49612. decryptedtxtSz += len;
  49613. ExpectIntEQ(0, decryptedtxtSz);
  49614. EVP_CIPHER_CTX_free(en);
  49615. EVP_CIPHER_CTX_free(de);
  49616. res = EXPECT_RESULT();
  49617. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  49618. return res;
  49619. }
  49620. static int test_wolfssl_EVP_sm4_ccm(void)
  49621. {
  49622. int res = TEST_SKIPPED;
  49623. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  49624. EXPECT_DECLS;
  49625. byte *key = (byte*)"0123456789012345";
  49626. byte *iv = (byte*)"0123456789012";
  49627. int ivSz = (int)XSTRLEN((char*)iv);
  49628. /* Message to be encrypted */
  49629. byte *plaintxt = (byte*)"for things to change you have to change";
  49630. /* Additional non-confidential data */
  49631. byte *aad = (byte*)"Don't spend major time on minor things.";
  49632. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  49633. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49634. int aadSz = (int)XSTRLEN((char*)aad);
  49635. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  49636. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  49637. int ciphertxtSz = 0;
  49638. int decryptedtxtSz = 0;
  49639. int len = 0;
  49640. int i = 0;
  49641. EVP_CIPHER_CTX en[2];
  49642. EVP_CIPHER_CTX de[2];
  49643. for (i = 0; i < 2; i++) {
  49644. EVP_CIPHER_CTX_init(&en[i]);
  49645. if (i == 0) {
  49646. /* Default uses 96-bits IV length */
  49647. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  49648. iv));
  49649. }
  49650. else {
  49651. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  49652. NULL));
  49653. /* non-default must to set the IV length first */
  49654. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  49655. ivSz, NULL));
  49656. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49657. }
  49658. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49659. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49660. plaintxtSz));
  49661. ciphertxtSz = len;
  49662. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49663. ciphertxtSz += len;
  49664. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  49665. SM4_BLOCK_SIZE, tag));
  49666. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49667. EVP_CIPHER_CTX_init(&de[i]);
  49668. if (i == 0) {
  49669. /* Default uses 96-bits IV length */
  49670. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  49671. iv));
  49672. }
  49673. else {
  49674. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  49675. NULL));
  49676. /* non-default must to set the IV length first */
  49677. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  49678. ivSz, NULL));
  49679. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49680. }
  49681. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49682. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49683. ciphertxtSz));
  49684. decryptedtxtSz = len;
  49685. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49686. SM4_BLOCK_SIZE, tag));
  49687. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49688. decryptedtxtSz += len;
  49689. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49690. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49691. /* modify tag*/
  49692. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  49693. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49694. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49695. SM4_BLOCK_SIZE, tag));
  49696. /* fail due to wrong tag */
  49697. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49698. ciphertxtSz));
  49699. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49700. ExpectIntEQ(0, len);
  49701. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49702. }
  49703. res = EXPECT_RESULT();
  49704. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  49705. return res;
  49706. }
  49707. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  49708. {
  49709. EXPECT_DECLS;
  49710. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  49711. EVP_PKEY_CTX* ctx = NULL;
  49712. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  49713. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  49714. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  49715. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  49716. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  49717. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  49718. byte outKey[34];
  49719. size_t outKeySz = sizeof(outKey);
  49720. /* These expected outputs were gathered by running the same test below using
  49721. * OpenSSL. */
  49722. const byte extractAndExpand[] = {
  49723. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  49724. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  49725. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  49726. };
  49727. const byte extractOnly[] = {
  49728. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  49729. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  49730. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  49731. };
  49732. const byte expandOnly[] = {
  49733. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  49734. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  49735. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  49736. };
  49737. const byte extractAndExpandAddInfo[] = {
  49738. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  49739. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  49740. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  49741. };
  49742. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  49743. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  49744. /* NULL ctx. */
  49745. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  49746. /* NULL md. */
  49747. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  49748. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  49749. /* NULL ctx. */
  49750. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  49751. WOLFSSL_FAILURE);
  49752. /* NULL salt is ok. */
  49753. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  49754. WOLFSSL_SUCCESS);
  49755. /* Salt length <= 0. */
  49756. /* Length 0 salt is ok. */
  49757. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  49758. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  49759. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  49760. WOLFSSL_SUCCESS);
  49761. /* NULL ctx. */
  49762. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  49763. WOLFSSL_FAILURE);
  49764. /* NULL key. */
  49765. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  49766. WOLFSSL_FAILURE);
  49767. /* Key length <= 0 */
  49768. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  49769. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  49770. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  49771. WOLFSSL_SUCCESS);
  49772. /* NULL ctx. */
  49773. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  49774. WOLFSSL_FAILURE);
  49775. /* NULL info is ok. */
  49776. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  49777. WOLFSSL_SUCCESS);
  49778. /* Info length <= 0 */
  49779. /* Length 0 info is ok. */
  49780. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  49781. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  49782. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  49783. WOLFSSL_SUCCESS);
  49784. /* NULL ctx. */
  49785. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  49786. WOLFSSL_FAILURE);
  49787. /* Extract and expand (default). */
  49788. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49789. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  49790. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  49791. /* Extract only. */
  49792. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  49793. WOLFSSL_SUCCESS);
  49794. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49795. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  49796. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  49797. outKeySz = sizeof(outKey);
  49798. /* Expand only. */
  49799. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  49800. WOLFSSL_SUCCESS);
  49801. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49802. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  49803. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  49804. outKeySz = sizeof(outKey);
  49805. /* Extract and expand with appended additional info. */
  49806. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  49807. WOLFSSL_SUCCESS);
  49808. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  49809. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  49810. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49811. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  49812. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  49813. EVP_PKEY_CTX_free(ctx);
  49814. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  49815. return EXPECT_RESULT();
  49816. }
  49817. #ifndef NO_BIO
  49818. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  49819. {
  49820. EXPECT_DECLS;
  49821. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49822. BIO* bio = NULL;
  49823. X509_INFO* info = NULL;
  49824. STACK_OF(X509_INFO)* sk = NULL;
  49825. char* subject = NULL;
  49826. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  49827. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  49828. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  49829. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  49830. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  49831. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  49832. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  49833. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  49834. /* using dereference to maintain testing for Apache port*/
  49835. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  49836. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  49837. 0, 0));
  49838. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  49839. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49840. X509_INFO_free(info);
  49841. info = NULL;
  49842. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  49843. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  49844. 0, 0));
  49845. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  49846. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49847. X509_INFO_free(info);
  49848. ExpectNull(info = sk_X509_INFO_pop(sk));
  49849. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  49850. BIO_free(bio);
  49851. #endif
  49852. return EXPECT_RESULT();
  49853. }
  49854. #endif /* !NO_BIO */
  49855. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  49856. {
  49857. EXPECT_DECLS;
  49858. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49859. X509 *x509 = NULL;
  49860. X509_NAME* name = NULL;
  49861. int idx = 0;
  49862. X509_NAME_ENTRY *ne = NULL;
  49863. ASN1_OBJECT *object = NULL;
  49864. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  49865. WOLFSSL_FILETYPE_PEM));
  49866. ExpectNotNull(name = X509_get_subject_name(x509));
  49867. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  49868. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  49869. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  49870. X509_free(x509);
  49871. #endif
  49872. return EXPECT_RESULT();
  49873. }
  49874. static int test_wolfSSL_X509_STORE_get1_certs(void)
  49875. {
  49876. EXPECT_DECLS;
  49877. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  49878. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49879. X509_STORE_CTX *storeCtx = NULL;
  49880. X509_STORE *store = NULL;
  49881. X509 *caX509 = NULL;
  49882. X509 *svrX509 = NULL;
  49883. X509_NAME *subject = NULL;
  49884. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  49885. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  49886. SSL_FILETYPE_PEM));
  49887. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  49888. SSL_FILETYPE_PEM)));
  49889. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  49890. ExpectNotNull(store = X509_STORE_new());
  49891. ExpectNotNull(subject = X509_get_subject_name(caX509));
  49892. /* Errors */
  49893. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  49894. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  49895. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  49896. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  49897. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  49898. SSL_SUCCESS);
  49899. /* Should find the cert */
  49900. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49901. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  49902. sk_X509_pop_free(certs, NULL);
  49903. certs = NULL;
  49904. /* Should not find the cert */
  49905. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  49906. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49907. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  49908. sk_X509_pop_free(certs, NULL);
  49909. certs = NULL;
  49910. X509_STORE_free(store);
  49911. X509_STORE_CTX_free(storeCtx);
  49912. X509_free(svrX509);
  49913. X509_free(caX509);
  49914. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  49915. return EXPECT_RESULT();
  49916. }
  49917. static int test_wolfSSL_dup_CA_list(void)
  49918. {
  49919. int res = TEST_SKIPPED;
  49920. #if defined(OPENSSL_ALL)
  49921. EXPECT_DECLS;
  49922. STACK_OF(X509_NAME) *originalStack = NULL;
  49923. STACK_OF(X509_NAME) *copyStack = NULL;
  49924. int originalCount = 0;
  49925. int copyCount = 0;
  49926. X509_NAME *name = NULL;
  49927. int i;
  49928. originalStack = sk_X509_NAME_new_null();
  49929. ExpectNotNull(originalStack);
  49930. for (i = 0; i < 3; i++) {
  49931. name = X509_NAME_new();
  49932. ExpectNotNull(name);
  49933. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  49934. }
  49935. copyStack = SSL_dup_CA_list(originalStack);
  49936. ExpectNotNull(copyStack);
  49937. originalCount = sk_X509_NAME_num(originalStack);
  49938. copyCount = sk_X509_NAME_num(copyStack);
  49939. AssertIntEQ(originalCount, copyCount);
  49940. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  49941. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  49942. originalStack = NULL;
  49943. copyStack = NULL;
  49944. res = EXPECT_RESULT();
  49945. #endif /* OPENSSL_ALL */
  49946. return res;
  49947. }
  49948. static int test_ForceZero(void)
  49949. {
  49950. EXPECT_DECLS;
  49951. unsigned char data[32];
  49952. unsigned int i, j, len;
  49953. /* Test case with 0 length */
  49954. ForceZero(data, 0);
  49955. /* Test ForceZero */
  49956. for (i = 0; i < sizeof(data); i++) {
  49957. for (len = 1; len < sizeof(data) - i; len++) {
  49958. for (j = 0; j < sizeof(data); j++)
  49959. data[j] = j + 1;
  49960. ForceZero(data + i, len);
  49961. for (j = 0; j < sizeof(data); j++) {
  49962. if (j < i || j >= i + len) {
  49963. ExpectIntNE(data[j], 0x00);
  49964. }
  49965. else {
  49966. ExpectIntEQ(data[j], 0x00);
  49967. }
  49968. }
  49969. }
  49970. }
  49971. return EXPECT_RESULT();
  49972. }
  49973. #ifndef NO_BIO
  49974. static int test_wolfSSL_X509_print(void)
  49975. {
  49976. EXPECT_DECLS;
  49977. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49978. !defined(NO_RSA) && defined(XSNPRINTF)
  49979. X509 *x509 = NULL;
  49980. BIO *bio = NULL;
  49981. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49982. const X509_ALGOR *cert_sig_alg = NULL;
  49983. #endif
  49984. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  49985. WOLFSSL_FILETYPE_PEM));
  49986. /* print to memory */
  49987. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49988. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49989. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  49990. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  49991. /* Will print IP address subject alt name. */
  49992. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  49993. #elif defined(NO_ASN_TIME)
  49994. /* Will print IP address subject alt name but not Validity. */
  49995. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  49996. #else
  49997. /* Will print IP address subject alt name. */
  49998. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  49999. #endif
  50000. #elif defined(NO_ASN_TIME)
  50001. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  50002. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  50003. #else
  50004. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  50005. #endif
  50006. BIO_free(bio);
  50007. bio = NULL;
  50008. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50009. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  50010. /* Print signature */
  50011. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  50012. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  50013. #endif
  50014. /* print to stderr */
  50015. #if !defined(NO_WOLFSSL_DIR)
  50016. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  50017. #endif
  50018. /* print again */
  50019. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  50020. X509_free(x509);
  50021. BIO_free(bio);
  50022. #endif
  50023. return EXPECT_RESULT();
  50024. }
  50025. static int test_wolfSSL_X509_CRL_print(void)
  50026. {
  50027. EXPECT_DECLS;
  50028. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  50029. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  50030. X509_CRL* crl = NULL;
  50031. BIO *bio = NULL;
  50032. XFILE fp = XBADFILE;
  50033. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  50034. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50035. NULL, NULL));
  50036. if (fp != XBADFILE)
  50037. XFCLOSE(fp);
  50038. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  50039. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  50040. X509_CRL_free(crl);
  50041. BIO_free(bio);
  50042. #endif
  50043. return EXPECT_RESULT();
  50044. }
  50045. static int test_wolfSSL_BIO_get_len(void)
  50046. {
  50047. EXPECT_DECLS;
  50048. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  50049. BIO *bio = NULL;
  50050. const char txt[] = "Some example text to push to the BIO.";
  50051. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  50052. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50053. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  50054. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  50055. BIO_free(bio);
  50056. bio = NULL;
  50057. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50058. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  50059. BIO_free(bio);
  50060. #endif
  50061. return EXPECT_RESULT();
  50062. }
  50063. #endif /* !NO_BIO */
  50064. static int test_wolfSSL_RSA(void)
  50065. {
  50066. EXPECT_DECLS;
  50067. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  50068. RSA* rsa = NULL;
  50069. const BIGNUM *n = NULL;
  50070. const BIGNUM *e = NULL;
  50071. const BIGNUM *d = NULL;
  50072. const BIGNUM *p = NULL;
  50073. const BIGNUM *q = NULL;
  50074. const BIGNUM *dmp1 = NULL;
  50075. const BIGNUM *dmq1 = NULL;
  50076. const BIGNUM *iqmp = NULL;
  50077. ExpectNotNull(rsa = RSA_new());
  50078. ExpectIntEQ(RSA_size(NULL), 0);
  50079. ExpectIntEQ(RSA_size(rsa), 0);
  50080. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  50081. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  50082. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  50083. #ifdef WOLFSSL_RSA_KEY_CHECK
  50084. ExpectIntEQ(RSA_check_key(rsa), 0);
  50085. #endif
  50086. RSA_free(rsa);
  50087. rsa = NULL;
  50088. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50089. ExpectIntEQ(RSA_size(rsa), 256);
  50090. #ifdef WOLFSSL_RSA_KEY_CHECK
  50091. ExpectIntEQ(RSA_check_key(NULL), 0);
  50092. ExpectIntEQ(RSA_check_key(rsa), 1);
  50093. #endif
  50094. /* sanity check */
  50095. ExpectIntEQ(RSA_bits(NULL), 0);
  50096. /* key */
  50097. ExpectIntEQ(RSA_bits(rsa), 2048);
  50098. RSA_get0_key(rsa, &n, &e, &d);
  50099. ExpectPtrEq(rsa->n, n);
  50100. ExpectPtrEq(rsa->e, e);
  50101. ExpectPtrEq(rsa->d, d);
  50102. n = NULL;
  50103. e = NULL;
  50104. d = NULL;
  50105. ExpectNotNull(n = BN_new());
  50106. ExpectNotNull(e = BN_new());
  50107. ExpectNotNull(d = BN_new());
  50108. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  50109. if (EXPECT_FAIL()) {
  50110. BN_free((BIGNUM*)n);
  50111. BN_free((BIGNUM*)e);
  50112. BN_free((BIGNUM*)d);
  50113. }
  50114. ExpectPtrEq(rsa->n, n);
  50115. ExpectPtrEq(rsa->e, e);
  50116. ExpectPtrEq(rsa->d, d);
  50117. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  50118. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  50119. /* crt_params */
  50120. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  50121. ExpectPtrEq(rsa->dmp1, dmp1);
  50122. ExpectPtrEq(rsa->dmq1, dmq1);
  50123. ExpectPtrEq(rsa->iqmp, iqmp);
  50124. dmp1 = NULL;
  50125. dmq1 = NULL;
  50126. iqmp = NULL;
  50127. ExpectNotNull(dmp1 = BN_new());
  50128. ExpectNotNull(dmq1 = BN_new());
  50129. ExpectNotNull(iqmp = BN_new());
  50130. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  50131. (BIGNUM*)iqmp), 1);
  50132. if (EXPECT_FAIL()) {
  50133. BN_free((BIGNUM*)dmp1);
  50134. BN_free((BIGNUM*)dmq1);
  50135. BN_free((BIGNUM*)iqmp);
  50136. }
  50137. ExpectPtrEq(rsa->dmp1, dmp1);
  50138. ExpectPtrEq(rsa->dmq1, dmq1);
  50139. ExpectPtrEq(rsa->iqmp, iqmp);
  50140. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  50141. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  50142. (BIGNUM*)iqmp), 0);
  50143. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  50144. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  50145. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  50146. ExpectNull(dmp1);
  50147. ExpectNull(dmq1);
  50148. ExpectNull(iqmp);
  50149. /* factors */
  50150. RSA_get0_factors(rsa, NULL, NULL);
  50151. RSA_get0_factors(rsa, &p, &q);
  50152. ExpectPtrEq(rsa->p, p);
  50153. ExpectPtrEq(rsa->q, q);
  50154. p = NULL;
  50155. q = NULL;
  50156. ExpectNotNull(p = BN_new());
  50157. ExpectNotNull(q = BN_new());
  50158. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  50159. if (EXPECT_FAIL()) {
  50160. BN_free((BIGNUM*)p);
  50161. BN_free((BIGNUM*)q);
  50162. }
  50163. ExpectPtrEq(rsa->p, p);
  50164. ExpectPtrEq(rsa->q, q);
  50165. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  50166. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  50167. RSA_get0_factors(NULL, NULL, NULL);
  50168. RSA_get0_factors(NULL, &p, &q);
  50169. ExpectNull(p);
  50170. ExpectNull(q);
  50171. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  50172. ExpectIntEQ(RSA_bits(rsa), 21);
  50173. RSA_free(rsa);
  50174. rsa = NULL;
  50175. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  50176. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  50177. ExpectIntEQ(RSA_size(rsa), 384);
  50178. ExpectIntEQ(RSA_bits(rsa), 3072);
  50179. RSA_free(rsa);
  50180. rsa = NULL;
  50181. #endif
  50182. /* remove for now with odd key size until adjusting rsa key size check with
  50183. wc_MakeRsaKey()
  50184. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  50185. RSA_free(rsa);
  50186. rsa = NULL;
  50187. */
  50188. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  50189. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  50190. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  50191. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  50192. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  50193. {
  50194. byte buff[FOURK_BUF];
  50195. byte der[FOURK_BUF];
  50196. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  50197. XFILE f = XBADFILE;
  50198. int bytes = 0;
  50199. /* test loading encrypted RSA private pem w/o password */
  50200. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  50201. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  50202. if (f != XBADFILE)
  50203. XFCLOSE(f);
  50204. XMEMSET(der, 0, sizeof(der));
  50205. /* test that error value is returned with no password */
  50206. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  50207. 0);
  50208. }
  50209. #endif
  50210. #endif
  50211. return EXPECT_RESULT();
  50212. }
  50213. static int test_wolfSSL_RSA_DER(void)
  50214. {
  50215. EXPECT_DECLS;
  50216. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  50217. RSA *rsa = NULL;
  50218. int i;
  50219. const unsigned char *buff = NULL;
  50220. unsigned char *newBuff = NULL;
  50221. struct tbl_s
  50222. {
  50223. const unsigned char *der;
  50224. int sz;
  50225. } tbl[] = {
  50226. #ifdef USE_CERT_BUFFERS_1024
  50227. {client_key_der_1024, sizeof_client_key_der_1024},
  50228. {server_key_der_1024, sizeof_server_key_der_1024},
  50229. #endif
  50230. #ifdef USE_CERT_BUFFERS_2048
  50231. {client_key_der_2048, sizeof_client_key_der_2048},
  50232. {server_key_der_2048, sizeof_server_key_der_2048},
  50233. #endif
  50234. {NULL, 0}
  50235. };
  50236. /* Public Key DER */
  50237. struct tbl_s pub[] = {
  50238. #ifdef USE_CERT_BUFFERS_1024
  50239. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  50240. #endif
  50241. #ifdef USE_CERT_BUFFERS_2048
  50242. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  50243. #endif
  50244. {NULL, 0}
  50245. };
  50246. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  50247. buff = pub[0].der;
  50248. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  50249. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  50250. buff = tbl[0].der;
  50251. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  50252. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  50253. rsa = RSA_new();
  50254. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  50255. RSA_free(rsa);
  50256. rsa = NULL;
  50257. for (i = 0; tbl[i].der != NULL; i++)
  50258. {
  50259. /* Passing in pointer results in pointer moving. */
  50260. buff = tbl[i].der;
  50261. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  50262. ExpectNotNull(rsa);
  50263. RSA_free(rsa);
  50264. rsa = NULL;
  50265. }
  50266. for (i = 0; tbl[i].der != NULL; i++)
  50267. {
  50268. /* Passing in pointer results in pointer moving. */
  50269. buff = tbl[i].der;
  50270. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  50271. ExpectNotNull(rsa);
  50272. RSA_free(rsa);
  50273. rsa = NULL;
  50274. }
  50275. for (i = 0; pub[i].der != NULL; i++)
  50276. {
  50277. buff = pub[i].der;
  50278. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  50279. ExpectNotNull(rsa);
  50280. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  50281. newBuff = NULL;
  50282. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  50283. ExpectNotNull(newBuff);
  50284. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  50285. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50286. RSA_free(rsa);
  50287. rsa = NULL;
  50288. }
  50289. #endif
  50290. return EXPECT_RESULT();
  50291. }
  50292. static int test_wolfSSL_RSA_print(void)
  50293. {
  50294. EXPECT_DECLS;
  50295. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50296. !defined(NO_STDIO_FILESYSTEM) && \
  50297. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  50298. !defined(NO_BIO) && defined(XFPRINTF)
  50299. BIO *bio = NULL;
  50300. WOLFSSL_RSA* rsa = NULL;
  50301. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50302. ExpectNotNull(rsa = RSA_new());
  50303. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  50304. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  50305. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  50306. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  50307. /* Some very large number of indent spaces. */
  50308. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  50309. /* RSA is empty. */
  50310. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  50311. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  50312. RSA_free(rsa);
  50313. rsa = NULL;
  50314. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50315. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  50316. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  50317. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  50318. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  50319. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  50320. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  50321. BIO_free(bio);
  50322. RSA_free(rsa);
  50323. #endif
  50324. return EXPECT_RESULT();
  50325. }
  50326. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  50327. {
  50328. EXPECT_DECLS;
  50329. #ifndef NO_RSA
  50330. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50331. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  50332. RSA *rsa = NULL;
  50333. const unsigned char *derBuf = client_key_der_2048;
  50334. unsigned char em[256] = {0}; /* len = 2048/8 */
  50335. /* Random data simulating a hash */
  50336. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  50337. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  50338. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  50339. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  50340. };
  50341. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  50342. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  50343. RSA_PSS_SALTLEN_DIGEST), 0);
  50344. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  50345. RSA_PSS_SALTLEN_DIGEST), 0);
  50346. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  50347. RSA_PSS_SALTLEN_DIGEST), 0);
  50348. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  50349. RSA_PSS_SALTLEN_DIGEST), 0);
  50350. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  50351. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  50352. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50353. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  50354. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50355. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  50356. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50357. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  50358. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50359. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50360. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50361. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  50362. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50363. RSA_PSS_SALTLEN_DIGEST), 1);
  50364. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50365. RSA_PSS_SALTLEN_DIGEST), 1);
  50366. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50367. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50368. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50369. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50370. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50371. RSA_PSS_SALTLEN_MAX), 1);
  50372. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50373. RSA_PSS_SALTLEN_MAX), 1);
  50374. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  50375. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  50376. RSA_free(rsa);
  50377. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  50378. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50379. #endif
  50380. return EXPECT_RESULT();
  50381. }
  50382. static int test_wolfSSL_RSA_sign_sha3(void)
  50383. {
  50384. EXPECT_DECLS;
  50385. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  50386. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50387. RSA* rsa = NULL;
  50388. const unsigned char *derBuf = client_key_der_2048;
  50389. unsigned char sigRet[256] = {0};
  50390. unsigned int sigLen = sizeof(sigRet);
  50391. /* Random data simulating a hash */
  50392. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  50393. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  50394. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  50395. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  50396. };
  50397. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  50398. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  50399. rsa), 1);
  50400. RSA_free(rsa);
  50401. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50402. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  50403. return EXPECT_RESULT();
  50404. }
  50405. static int test_wolfSSL_RSA_get0_key(void)
  50406. {
  50407. EXPECT_DECLS;
  50408. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50409. RSA *rsa = NULL;
  50410. const BIGNUM* n = NULL;
  50411. const BIGNUM* e = NULL;
  50412. const BIGNUM* d = NULL;
  50413. const unsigned char* der;
  50414. int derSz;
  50415. #ifdef USE_CERT_BUFFERS_1024
  50416. der = client_key_der_1024;
  50417. derSz = sizeof_client_key_der_1024;
  50418. #elif defined(USE_CERT_BUFFERS_2048)
  50419. der = client_key_der_2048;
  50420. derSz = sizeof_client_key_der_2048;
  50421. #else
  50422. der = NULL;
  50423. derSz = 0;
  50424. #endif
  50425. if (der != NULL) {
  50426. RSA_get0_key(NULL, NULL, NULL, NULL);
  50427. RSA_get0_key(rsa, NULL, NULL, NULL);
  50428. RSA_get0_key(NULL, &n, &e, &d);
  50429. ExpectNull(n);
  50430. ExpectNull(e);
  50431. ExpectNull(d);
  50432. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  50433. ExpectNotNull(rsa);
  50434. RSA_get0_key(rsa, NULL, NULL, NULL);
  50435. RSA_get0_key(rsa, &n, NULL, NULL);
  50436. ExpectNotNull(n);
  50437. RSA_get0_key(rsa, NULL, &e, NULL);
  50438. ExpectNotNull(e);
  50439. RSA_get0_key(rsa, NULL, NULL, &d);
  50440. ExpectNotNull(d);
  50441. RSA_get0_key(rsa, &n, &e, &d);
  50442. ExpectNotNull(n);
  50443. ExpectNotNull(e);
  50444. ExpectNotNull(d);
  50445. RSA_free(rsa);
  50446. }
  50447. #endif
  50448. return EXPECT_RESULT();
  50449. }
  50450. static int test_wolfSSL_RSA_meth(void)
  50451. {
  50452. EXPECT_DECLS;
  50453. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50454. RSA *rsa = NULL;
  50455. RSA_METHOD *rsa_meth = NULL;
  50456. #ifdef WOLFSSL_KEY_GEN
  50457. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50458. RSA_free(rsa);
  50459. rsa = NULL;
  50460. #else
  50461. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50462. #endif
  50463. ExpectNotNull(RSA_get_default_method());
  50464. wolfSSL_RSA_meth_free(NULL);
  50465. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  50466. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  50467. RSA_METHOD_FLAG_NO_CHECK));
  50468. #ifndef NO_WOLFSSL_STUB
  50469. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  50470. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  50471. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  50472. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  50473. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  50474. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  50475. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  50476. #endif
  50477. ExpectIntEQ(RSA_flags(NULL), 0);
  50478. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  50479. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  50480. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  50481. ExpectNotNull(rsa = RSA_new());
  50482. /* No method set. */
  50483. ExpectIntEQ(RSA_flags(rsa), 0);
  50484. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50485. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50486. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50487. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  50488. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  50489. if (EXPECT_FAIL()) {
  50490. wolfSSL_RSA_meth_free(rsa_meth);
  50491. }
  50492. ExpectNull(RSA_get_method(NULL));
  50493. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  50494. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  50495. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50496. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50497. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  50498. RSA_METHOD_FLAG_NO_CHECK);
  50499. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50500. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50501. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  50502. /* rsa_meth is freed here */
  50503. RSA_free(rsa);
  50504. #endif
  50505. return EXPECT_RESULT();
  50506. }
  50507. static int test_wolfSSL_RSA_verify(void)
  50508. {
  50509. EXPECT_DECLS;
  50510. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  50511. #ifndef NO_BIO
  50512. XFILE fp = XBADFILE;
  50513. RSA *pKey = NULL;
  50514. RSA *pubKey = NULL;
  50515. X509 *cert = NULL;
  50516. const char *text = "Hello wolfSSL !";
  50517. unsigned char hash[SHA256_DIGEST_LENGTH];
  50518. unsigned char signature[2048/8];
  50519. unsigned int signatureLength;
  50520. byte *buf = NULL;
  50521. BIO *bio = NULL;
  50522. SHA256_CTX c;
  50523. EVP_PKEY *evpPkey = NULL;
  50524. EVP_PKEY *evpPubkey = NULL;
  50525. size_t sz;
  50526. /* generate hash */
  50527. SHA256_Init(&c);
  50528. SHA256_Update(&c, text, strlen(text));
  50529. SHA256_Final(hash, &c);
  50530. #ifdef WOLFSSL_SMALL_STACK_CACHE
  50531. /* workaround for small stack cache case */
  50532. wc_Sha256Free((wc_Sha256*)&c);
  50533. #endif
  50534. /* read privete key file */
  50535. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  50536. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  50537. ExpectTrue((sz = XFTELL(fp)) > 0);
  50538. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  50539. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  50540. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  50541. if (fp != XBADFILE) {
  50542. XFCLOSE(fp);
  50543. fp = XBADFILE;
  50544. }
  50545. /* read private key and sign hash data */
  50546. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  50547. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  50548. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  50549. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  50550. signature, &signatureLength, pKey), SSL_SUCCESS);
  50551. /* read public key and verify signed data */
  50552. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  50553. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  50554. if (fp != XBADFILE)
  50555. XFCLOSE(fp);
  50556. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  50557. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  50558. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  50559. signatureLength, pubKey), SSL_SUCCESS);
  50560. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  50561. signatureLength, NULL), SSL_FAILURE);
  50562. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  50563. signatureLength, pubKey), SSL_FAILURE);
  50564. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  50565. signatureLength, pubKey), SSL_FAILURE);
  50566. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  50567. signatureLength, NULL), SSL_FAILURE);
  50568. RSA_free(pKey);
  50569. EVP_PKEY_free(evpPkey);
  50570. RSA_free(pubKey);
  50571. EVP_PKEY_free(evpPubkey);
  50572. X509_free(cert);
  50573. BIO_free(bio);
  50574. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  50575. #endif
  50576. #endif
  50577. return EXPECT_RESULT();
  50578. }
  50579. static int test_wolfSSL_RSA_sign(void)
  50580. {
  50581. EXPECT_DECLS;
  50582. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50583. RSA *rsa;
  50584. unsigned char hash[SHA256_DIGEST_LENGTH];
  50585. #ifdef USE_CERT_BUFFERS_1024
  50586. const unsigned char* privDer = client_key_der_1024;
  50587. size_t privDerSz = sizeof_client_key_der_1024;
  50588. const unsigned char* pubDer = client_keypub_der_1024;
  50589. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50590. unsigned char signature[1024/8];
  50591. #else
  50592. const unsigned char* privDer = client_key_der_2048;
  50593. size_t privDerSz = sizeof_client_key_der_2048;
  50594. const unsigned char* pubDer = client_keypub_der_2048;
  50595. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50596. unsigned char signature[2048/8];
  50597. #endif
  50598. unsigned int signatureLen;
  50599. const unsigned char* der;
  50600. XMEMSET(hash, 0, sizeof(hash));
  50601. der = privDer;
  50602. rsa = NULL;
  50603. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50604. /* Invalid parameters. */
  50605. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  50606. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  50607. &signatureLen, rsa), 0);
  50608. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  50609. &signatureLen, rsa), 0);
  50610. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  50611. &signatureLen, rsa), 0);
  50612. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50613. NULL, rsa), 0);
  50614. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50615. &signatureLen, NULL), 0);
  50616. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50617. &signatureLen, rsa), 1);
  50618. RSA_free(rsa);
  50619. der = pubDer;
  50620. rsa = NULL;
  50621. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50622. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  50623. signatureLen, rsa), 1);
  50624. RSA_free(rsa);
  50625. #endif
  50626. return EXPECT_RESULT();
  50627. }
  50628. static int test_wolfSSL_RSA_sign_ex(void)
  50629. {
  50630. EXPECT_DECLS;
  50631. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50632. RSA *rsa = NULL;
  50633. unsigned char hash[SHA256_DIGEST_LENGTH];
  50634. #ifdef USE_CERT_BUFFERS_1024
  50635. const unsigned char* privDer = client_key_der_1024;
  50636. size_t privDerSz = sizeof_client_key_der_1024;
  50637. const unsigned char* pubDer = client_keypub_der_1024;
  50638. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50639. unsigned char signature[1024/8];
  50640. #else
  50641. const unsigned char* privDer = client_key_der_2048;
  50642. size_t privDerSz = sizeof_client_key_der_2048;
  50643. const unsigned char* pubDer = client_keypub_der_2048;
  50644. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50645. unsigned char signature[2048/8];
  50646. #endif
  50647. unsigned int signatureLen;
  50648. const unsigned char* der;
  50649. unsigned char encodedHash[51];
  50650. unsigned int encodedHashLen;
  50651. const unsigned char expEncHash[] = {
  50652. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  50653. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  50654. 0x00, 0x04, 0x20,
  50655. /* Hash data */
  50656. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50657. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50658. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50659. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50660. };
  50661. XMEMSET(hash, 0, sizeof(hash));
  50662. ExpectNotNull(rsa = wolfSSL_RSA_new());
  50663. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50664. &signatureLen, rsa, 1), 0);
  50665. wolfSSL_RSA_free(rsa);
  50666. der = privDer;
  50667. rsa = NULL;
  50668. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50669. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  50670. -1), 0);
  50671. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  50672. signature, &signatureLen, rsa, 1), 0);
  50673. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  50674. &signatureLen, rsa, 1), 0);
  50675. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  50676. &signatureLen, rsa, 1), 0);
  50677. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50678. NULL, rsa, 1), 0);
  50679. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50680. &signatureLen, NULL, 1), 0);
  50681. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50682. &signatureLen, rsa, -1), 0);
  50683. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  50684. &signatureLen, rsa, 0), 0);
  50685. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  50686. &signatureLen, rsa, 0), 0);
  50687. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50688. NULL, rsa, 0), 0);
  50689. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50690. &signatureLen, rsa, 1), 1);
  50691. /* Test returning encoded hash. */
  50692. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  50693. &encodedHashLen, rsa, 0), 1);
  50694. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  50695. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  50696. RSA_free(rsa);
  50697. der = pubDer;
  50698. rsa = NULL;
  50699. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50700. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  50701. signatureLen, rsa), 1);
  50702. RSA_free(rsa);
  50703. #endif
  50704. return EXPECT_RESULT();
  50705. }
  50706. static int test_wolfSSL_RSA_public_decrypt(void)
  50707. {
  50708. EXPECT_DECLS;
  50709. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50710. RSA *rsa;
  50711. unsigned char msg[SHA256_DIGEST_LENGTH];
  50712. #ifdef USE_CERT_BUFFERS_1024
  50713. const unsigned char* pubDer = client_keypub_der_1024;
  50714. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50715. unsigned char decMsg[1024/8];
  50716. const unsigned char encMsg[] = {
  50717. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  50718. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  50719. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  50720. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  50721. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  50722. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  50723. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  50724. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  50725. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  50726. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  50727. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  50728. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  50729. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  50730. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  50731. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  50732. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  50733. };
  50734. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50735. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50736. defined(WC_RSA_NO_PADDING)
  50737. const unsigned char encMsgNoPad[] = {
  50738. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  50739. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  50740. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  50741. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  50742. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  50743. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  50744. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  50745. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  50746. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  50747. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  50748. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  50749. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  50750. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  50751. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  50752. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  50753. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  50754. };
  50755. #endif
  50756. #else
  50757. const unsigned char* pubDer = client_keypub_der_2048;
  50758. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50759. unsigned char decMsg[2048/8];
  50760. const unsigned char encMsg[] = {
  50761. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  50762. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  50763. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  50764. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  50765. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  50766. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  50767. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  50768. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  50769. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  50770. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  50771. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  50772. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  50773. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  50774. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  50775. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  50776. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  50777. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50778. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50779. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50780. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50781. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50782. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50783. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50784. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50785. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50786. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50787. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50788. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50789. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50790. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50791. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50792. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50793. };
  50794. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50795. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50796. defined(WC_RSA_NO_PADDING)
  50797. const unsigned char encMsgNoPad[] = {
  50798. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50799. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50800. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50801. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50802. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50803. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50804. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50805. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50806. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50807. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50808. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50809. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50810. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50811. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50812. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50813. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50814. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50815. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50816. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50817. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50818. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50819. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50820. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50821. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50822. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50823. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50824. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50825. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50826. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50827. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50828. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50829. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50830. };
  50831. #endif
  50832. #endif
  50833. const unsigned char* der;
  50834. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50835. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50836. defined(WC_RSA_NO_PADDING)
  50837. int i;
  50838. #endif
  50839. XMEMSET(msg, 0, sizeof(msg));
  50840. der = pubDer;
  50841. rsa = NULL;
  50842. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50843. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  50844. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  50845. RSA_PKCS1_PADDING), -1);
  50846. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  50847. RSA_PKCS1_PADDING), -1);
  50848. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50849. RSA_PKCS1_PADDING), -1);
  50850. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  50851. RSA_PKCS1_PADDING), -1);
  50852. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50853. RSA_PKCS1_PSS_PADDING), -1);
  50854. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50855. RSA_PKCS1_PADDING), 32);
  50856. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  50857. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50858. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50859. defined(WC_RSA_NO_PADDING)
  50860. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  50861. rsa, RSA_NO_PADDING), sizeof(decMsg));
  50862. /* Zeros before actual data. */
  50863. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  50864. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50865. }
  50866. /* Check actual data. */
  50867. XMEMSET(msg, 0x01, sizeof(msg));
  50868. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50869. #endif
  50870. RSA_free(rsa);
  50871. #endif
  50872. return EXPECT_RESULT();
  50873. }
  50874. static int test_wolfSSL_RSA_private_encrypt(void)
  50875. {
  50876. EXPECT_DECLS;
  50877. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50878. RSA *rsa;
  50879. unsigned char msg[SHA256_DIGEST_LENGTH];
  50880. #ifdef USE_CERT_BUFFERS_1024
  50881. const unsigned char* privDer = client_key_der_1024;
  50882. size_t privDerSz = sizeof_client_key_der_1024;
  50883. unsigned char encMsg[1024/8];
  50884. const unsigned char expEncMsg[] = {
  50885. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  50886. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  50887. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  50888. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  50889. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  50890. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  50891. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  50892. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  50893. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  50894. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  50895. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  50896. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  50897. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  50898. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  50899. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  50900. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  50901. };
  50902. #ifdef WC_RSA_NO_PADDING
  50903. const unsigned char expEncMsgNoPad[] = {
  50904. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  50905. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  50906. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  50907. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  50908. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  50909. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  50910. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  50911. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  50912. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  50913. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  50914. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  50915. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  50916. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  50917. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  50918. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  50919. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  50920. };
  50921. #endif
  50922. #else
  50923. const unsigned char* privDer = client_key_der_2048;
  50924. size_t privDerSz = sizeof_client_key_der_2048;
  50925. unsigned char encMsg[2048/8];
  50926. const unsigned char expEncMsg[] = {
  50927. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  50928. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  50929. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  50930. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  50931. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  50932. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  50933. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  50934. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  50935. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  50936. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  50937. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  50938. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  50939. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  50940. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  50941. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  50942. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  50943. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50944. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50945. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50946. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50947. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50948. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50949. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50950. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50951. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50952. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50953. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50954. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50955. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50956. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50957. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50958. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50959. };
  50960. #ifdef WC_RSA_NO_PADDING
  50961. const unsigned char expEncMsgNoPad[] = {
  50962. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50963. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50964. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50965. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50966. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50967. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50968. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50969. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50970. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50971. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50972. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50973. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50974. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50975. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50976. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50977. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50978. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50979. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50980. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50981. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50982. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50983. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50984. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50985. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50986. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50987. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50988. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50989. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50990. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50991. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50992. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50993. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50994. };
  50995. #endif
  50996. #endif
  50997. const unsigned char* der;
  50998. XMEMSET(msg, 0x00, sizeof(msg));
  50999. der = privDer;
  51000. rsa = NULL;
  51001. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51002. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  51003. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  51004. -1);
  51005. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  51006. RSA_PKCS1_PADDING), -1);
  51007. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  51008. RSA_PKCS1_PADDING), -1);
  51009. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  51010. RSA_PKCS1_PADDING), -1);
  51011. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51012. RSA_PKCS1_PSS_PADDING), -1);
  51013. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51014. RSA_PKCS1_PADDING), sizeof(encMsg));
  51015. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  51016. #ifdef WC_RSA_NO_PADDING
  51017. /* Non-zero message. */
  51018. XMEMSET(msg, 0x01, sizeof(msg));
  51019. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51020. RSA_NO_PADDING), sizeof(encMsg));
  51021. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  51022. #endif
  51023. RSA_free(rsa);
  51024. #endif
  51025. return EXPECT_RESULT();
  51026. }
  51027. static int test_wolfSSL_RSA_public_encrypt(void)
  51028. {
  51029. EXPECT_DECLS;
  51030. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51031. RSA* rsa = NULL;
  51032. const unsigned char msg[2048/8] = { 0 };
  51033. unsigned char encMsg[2048/8];
  51034. ExpectNotNull(rsa = RSA_new());
  51035. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  51036. RSA_PKCS1_PADDING), -1);
  51037. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  51038. RSA_PKCS1_PADDING), -1);
  51039. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  51040. RSA_PKCS1_PADDING), -1);
  51041. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  51042. RSA_PKCS1_PADDING), -1);
  51043. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  51044. RSA_PKCS1_PSS_PADDING), -1);
  51045. /* Empty RSA key. */
  51046. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  51047. RSA_PKCS1_PADDING), -1);
  51048. RSA_free(rsa);
  51049. #endif
  51050. return EXPECT_RESULT();
  51051. }
  51052. static int test_wolfSSL_RSA_private_decrypt(void)
  51053. {
  51054. EXPECT_DECLS;
  51055. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51056. RSA* rsa = NULL;
  51057. unsigned char msg[2048/8];
  51058. const unsigned char encMsg[2048/8] = { 0 };
  51059. ExpectNotNull(rsa = RSA_new());
  51060. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  51061. RSA_PKCS1_PADDING), -1);
  51062. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  51063. RSA_PKCS1_PADDING), -1);
  51064. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  51065. RSA_PKCS1_PADDING), -1);
  51066. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  51067. RSA_PKCS1_PADDING), -1);
  51068. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  51069. RSA_PKCS1_PSS_PADDING), -1);
  51070. /* Empty RSA key. */
  51071. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  51072. RSA_PKCS1_PADDING), -1);
  51073. RSA_free(rsa);
  51074. #endif
  51075. return EXPECT_RESULT();
  51076. }
  51077. static int test_wolfSSL_RSA_GenAdd(void)
  51078. {
  51079. EXPECT_DECLS;
  51080. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51081. RSA *rsa;
  51082. #ifdef USE_CERT_BUFFERS_1024
  51083. const unsigned char* privDer = client_key_der_1024;
  51084. size_t privDerSz = sizeof_client_key_der_1024;
  51085. const unsigned char* pubDer = client_keypub_der_1024;
  51086. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51087. #else
  51088. const unsigned char* privDer = client_key_der_2048;
  51089. size_t privDerSz = sizeof_client_key_der_2048;
  51090. const unsigned char* pubDer = client_keypub_der_2048;
  51091. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51092. #endif
  51093. const unsigned char* der;
  51094. der = privDer;
  51095. rsa = NULL;
  51096. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51097. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  51098. #ifndef RSA_LOW_MEM
  51099. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  51100. #else
  51101. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  51102. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  51103. #endif
  51104. RSA_free(rsa);
  51105. der = pubDer;
  51106. rsa = NULL;
  51107. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51108. /* Need private values. */
  51109. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  51110. RSA_free(rsa);
  51111. #endif
  51112. return EXPECT_RESULT();
  51113. }
  51114. static int test_wolfSSL_RSA_blinding_on(void)
  51115. {
  51116. EXPECT_DECLS;
  51117. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  51118. RSA *rsa;
  51119. WOLFSSL_BN_CTX *bnCtx = NULL;
  51120. #ifdef USE_CERT_BUFFERS_1024
  51121. const unsigned char* privDer = client_key_der_1024;
  51122. size_t privDerSz = sizeof_client_key_der_1024;
  51123. #else
  51124. const unsigned char* privDer = client_key_der_2048;
  51125. size_t privDerSz = sizeof_client_key_der_2048;
  51126. #endif
  51127. const unsigned char* der;
  51128. der = privDer;
  51129. rsa = NULL;
  51130. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51131. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  51132. /* Does nothing so all parameters are valid. */
  51133. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  51134. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  51135. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  51136. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  51137. wolfSSL_BN_CTX_free(bnCtx);
  51138. RSA_free(rsa);
  51139. #endif
  51140. return EXPECT_RESULT();
  51141. }
  51142. static int test_wolfSSL_RSA_ex_data(void)
  51143. {
  51144. EXPECT_DECLS;
  51145. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51146. RSA* rsa = NULL;
  51147. unsigned char data[1];
  51148. ExpectNotNull(rsa = RSA_new());
  51149. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  51150. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  51151. #ifdef MAX_EX_DATA
  51152. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  51153. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  51154. #endif
  51155. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  51156. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  51157. #ifdef HAVE_EX_DATA
  51158. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  51159. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  51160. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  51161. #else
  51162. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  51163. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  51164. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  51165. #endif
  51166. RSA_free(rsa);
  51167. #endif /* !NO_RSA && OPENSSL_EXTRA */
  51168. return EXPECT_RESULT();
  51169. }
  51170. static int test_wolfSSL_RSA_LoadDer(void)
  51171. {
  51172. EXPECT_DECLS;
  51173. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  51174. defined(OPENSSL_EXTRA_X509_SMALL))
  51175. RSA *rsa = NULL;
  51176. #ifdef USE_CERT_BUFFERS_1024
  51177. const unsigned char* privDer = client_key_der_1024;
  51178. size_t privDerSz = sizeof_client_key_der_1024;
  51179. #else
  51180. const unsigned char* privDer = client_key_der_2048;
  51181. size_t privDerSz = sizeof_client_key_der_2048;
  51182. #endif
  51183. ExpectNotNull(rsa = RSA_new());
  51184. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  51185. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  51186. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  51187. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  51188. RSA_free(rsa);
  51189. #endif /* !NO_RSA && OPENSSL_EXTRA */
  51190. return EXPECT_RESULT();
  51191. }
  51192. /* Local API. */
  51193. static int test_wolfSSL_RSA_To_Der(void)
  51194. {
  51195. EXPECT_DECLS;
  51196. #ifdef WOLFSSL_TEST_STATIC_BUILD
  51197. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51198. RSA* rsa;
  51199. #ifdef USE_CERT_BUFFERS_1024
  51200. const unsigned char* privDer = client_key_der_1024;
  51201. size_t privDerSz = sizeof_client_key_der_1024;
  51202. const unsigned char* pubDer = client_keypub_der_1024;
  51203. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51204. unsigned char out[sizeof(client_key_der_1024)];
  51205. #else
  51206. const unsigned char* privDer = client_key_der_2048;
  51207. size_t privDerSz = sizeof_client_key_der_2048;
  51208. const unsigned char* pubDer = client_keypub_der_2048;
  51209. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51210. unsigned char out[sizeof(client_key_der_2048)];
  51211. #endif
  51212. const unsigned char* der;
  51213. unsigned char* outDer = NULL;
  51214. der = privDer;
  51215. rsa = NULL;
  51216. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51217. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51218. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  51219. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  51220. outDer = out;
  51221. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  51222. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  51223. outDer = NULL;
  51224. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  51225. ExpectNotNull(outDer);
  51226. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  51227. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  51228. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  51229. outDer = out;
  51230. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  51231. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  51232. RSA_free(rsa);
  51233. ExpectNotNull(rsa = RSA_new());
  51234. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51235. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  51236. RSA_free(rsa);
  51237. der = pubDer;
  51238. rsa = NULL;
  51239. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51240. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51241. RSA_free(rsa);
  51242. #endif
  51243. #endif
  51244. return EXPECT_RESULT();
  51245. }
  51246. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  51247. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  51248. {
  51249. EXPECT_DECLS;
  51250. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  51251. XFILE file = XBADFILE;
  51252. const char* fname = "./certs/server-keyPub.pem";
  51253. RSA *rsa = NULL;
  51254. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  51255. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  51256. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  51257. ExpectIntEQ(RSA_size(rsa), 256);
  51258. RSA_free(rsa);
  51259. if (file != XBADFILE)
  51260. XFCLOSE(file);
  51261. #endif
  51262. return EXPECT_RESULT();
  51263. }
  51264. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  51265. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  51266. {
  51267. EXPECT_DECLS;
  51268. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51269. defined(WOLFSSL_KEY_GEN)
  51270. RSA* rsa = NULL;
  51271. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  51272. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  51273. /* Valid but stub so returns 0. */
  51274. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  51275. #endif
  51276. return EXPECT_RESULT();
  51277. }
  51278. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  51279. {
  51280. EXPECT_DECLS;
  51281. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  51282. (defined(WOLFSSL_PEM_TO_DER) || \
  51283. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  51284. RSA* rsa = NULL;
  51285. #ifdef USE_CERT_BUFFERS_1024
  51286. const unsigned char* privDer = client_key_der_1024;
  51287. size_t privDerSz = sizeof_client_key_der_1024;
  51288. #else
  51289. const unsigned char* privDer = client_key_der_2048;
  51290. size_t privDerSz = sizeof_client_key_der_2048;
  51291. #endif
  51292. const unsigned char* der;
  51293. #ifndef NO_AES
  51294. unsigned char passwd[] = "password";
  51295. #endif
  51296. ExpectNotNull(rsa = RSA_new());
  51297. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  51298. NULL, NULL), 0);
  51299. RSA_free(rsa);
  51300. der = privDer;
  51301. rsa = NULL;
  51302. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51303. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  51304. NULL, NULL), 0);
  51305. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  51306. NULL, NULL), 0);
  51307. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  51308. NULL, NULL), 1);
  51309. #ifndef NO_AES
  51310. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  51311. NULL, 0, NULL, NULL), 1);
  51312. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  51313. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  51314. #endif
  51315. RSA_free(rsa);
  51316. #endif
  51317. return EXPECT_RESULT();
  51318. }
  51319. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  51320. {
  51321. EXPECT_DECLS;
  51322. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  51323. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  51324. RSA* rsa = NULL;
  51325. #ifdef USE_CERT_BUFFERS_1024
  51326. const unsigned char* privDer = client_key_der_1024;
  51327. size_t privDerSz = sizeof_client_key_der_1024;
  51328. #else
  51329. const unsigned char* privDer = client_key_der_2048;
  51330. size_t privDerSz = sizeof_client_key_der_2048;
  51331. #endif
  51332. const unsigned char* der;
  51333. #ifndef NO_AES
  51334. unsigned char passwd[] = "password";
  51335. #endif
  51336. unsigned char* pem = NULL;
  51337. int plen;
  51338. ExpectNotNull(rsa = RSA_new());
  51339. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51340. &plen), 0);
  51341. RSA_free(rsa);
  51342. der = privDer;
  51343. rsa = NULL;
  51344. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51345. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  51346. &plen), 0);
  51347. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  51348. &plen), 0);
  51349. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51350. NULL), 0);
  51351. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51352. &plen), 1);
  51353. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51354. pem = NULL;
  51355. #ifndef NO_AES
  51356. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  51357. NULL, 0, &pem, &plen), 1);
  51358. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51359. pem = NULL;
  51360. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  51361. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  51362. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51363. #endif
  51364. RSA_free(rsa);
  51365. #endif
  51366. return EXPECT_RESULT();
  51367. }
  51368. static int test_wolfSSL_DH(void)
  51369. {
  51370. EXPECT_DECLS;
  51371. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51372. DH *dh = NULL;
  51373. BIGNUM* p;
  51374. BIGNUM* q;
  51375. BIGNUM* g;
  51376. BIGNUM* pub = NULL;
  51377. BIGNUM* priv = NULL;
  51378. #if defined(OPENSSL_ALL)
  51379. #if !defined(HAVE_FIPS) || \
  51380. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  51381. FILE* f = NULL;
  51382. unsigned char buf[268];
  51383. const unsigned char* pt = buf;
  51384. long len = 0;
  51385. dh = NULL;
  51386. XMEMSET(buf, 0, sizeof(buf));
  51387. /* Test 2048 bit parameters */
  51388. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  51389. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51390. if (f != XBADFILE)
  51391. XFCLOSE(f);
  51392. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  51393. ExpectNotNull(dh->p);
  51394. ExpectNotNull(dh->g);
  51395. ExpectTrue(pt == buf);
  51396. ExpectIntEQ(DH_generate_key(dh), 1);
  51397. ExpectIntEQ(DH_generate_key(dh), 1);
  51398. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  51399. ExpectNotNull(pub = BN_new());
  51400. ExpectIntEQ(BN_set_word(pub, 1), 1);
  51401. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  51402. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  51403. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  51404. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  51405. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  51406. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  51407. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  51408. BN_free(pub);
  51409. pub = NULL;
  51410. DH_get0_pqg(dh, (const BIGNUM**)&p,
  51411. (const BIGNUM**)&q,
  51412. (const BIGNUM**)&g);
  51413. ExpectPtrEq(p, dh->p);
  51414. ExpectPtrEq(q, dh->q);
  51415. ExpectPtrEq(g, dh->g);
  51416. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  51417. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  51418. ExpectPtrEq(pub, dh->pub_key);
  51419. ExpectPtrEq(priv, dh->priv_key);
  51420. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  51421. ExpectPtrEq(pub, dh->pub_key);
  51422. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  51423. ExpectPtrEq(priv, dh->priv_key);
  51424. pub = NULL;
  51425. priv = NULL;
  51426. ExpectNotNull(pub = BN_new());
  51427. ExpectNotNull(priv = BN_new());
  51428. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  51429. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  51430. if (EXPECT_FAIL()) {
  51431. BN_free(pub);
  51432. BN_free(priv);
  51433. }
  51434. pub = NULL;
  51435. priv = NULL;
  51436. ExpectNotNull(pub = BN_new());
  51437. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  51438. if (EXPECT_FAIL()) {
  51439. BN_free(pub);
  51440. }
  51441. ExpectNotNull(priv = BN_new());
  51442. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  51443. if (EXPECT_FAIL()) {
  51444. BN_free(priv);
  51445. }
  51446. ExpectPtrEq(pub, dh->pub_key);
  51447. ExpectPtrEq(priv, dh->priv_key);
  51448. pub = NULL;
  51449. priv = NULL;
  51450. DH_free(dh);
  51451. dh = NULL;
  51452. ExpectNotNull(dh = DH_new());
  51453. p = NULL;
  51454. ExpectNotNull(p = BN_new());
  51455. ExpectIntEQ(BN_set_word(p, 1), 1);
  51456. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  51457. ExpectNotNull(pub = BN_new());
  51458. ExpectNotNull(priv = BN_new());
  51459. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  51460. if (EXPECT_FAIL()) {
  51461. BN_free(pub);
  51462. BN_free(priv);
  51463. }
  51464. pub = NULL;
  51465. priv = NULL;
  51466. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  51467. BN_free(p);
  51468. p = NULL;
  51469. DH_free(dh);
  51470. dh = NULL;
  51471. #ifdef WOLFSSL_KEY_GEN
  51472. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  51473. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  51474. DH_free(dh);
  51475. dh = NULL;
  51476. #endif
  51477. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  51478. #endif /* OPENSSL_ALL */
  51479. (void)dh;
  51480. (void)p;
  51481. (void)q;
  51482. (void)g;
  51483. (void)pub;
  51484. (void)priv;
  51485. ExpectNotNull(dh = wolfSSL_DH_new());
  51486. /* invalid parameters test */
  51487. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  51488. (const BIGNUM**)&q,
  51489. (const BIGNUM**)&g);
  51490. DH_get0_pqg(dh, NULL,
  51491. (const BIGNUM**)&q,
  51492. (const BIGNUM**)&g);
  51493. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  51494. DH_get0_pqg(dh, NULL, NULL, NULL);
  51495. DH_get0_pqg(dh, (const BIGNUM**)&p,
  51496. (const BIGNUM**)&q,
  51497. (const BIGNUM**)&g);
  51498. ExpectPtrEq(p, NULL);
  51499. ExpectPtrEq(q, NULL);
  51500. ExpectPtrEq(g, NULL);
  51501. DH_free(dh);
  51502. dh = NULL;
  51503. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  51504. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  51505. #if defined(OPENSSL_ALL) || \
  51506. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  51507. dh = wolfSSL_DH_new();
  51508. ExpectNotNull(dh);
  51509. p = wolfSSL_BN_new();
  51510. ExpectNotNull(p);
  51511. ExpectIntEQ(BN_set_word(p, 11), 1);
  51512. g = wolfSSL_BN_new();
  51513. ExpectNotNull(g);
  51514. ExpectIntEQ(BN_set_word(g, 2), 1);
  51515. q = wolfSSL_BN_new();
  51516. ExpectNotNull(q);
  51517. ExpectIntEQ(BN_set_word(q, 5), 1);
  51518. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  51519. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  51520. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  51521. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  51522. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  51523. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  51524. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  51525. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  51526. /* Don't need q. */
  51527. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51528. if (EXPECT_FAIL()) {
  51529. BN_free(p);
  51530. BN_free(g);
  51531. }
  51532. p = NULL;
  51533. g = NULL;
  51534. /* Setting again will free the p and g. */
  51535. wolfSSL_BN_free(q);
  51536. q = NULL;
  51537. DH_free(dh);
  51538. dh = NULL;
  51539. dh = wolfSSL_DH_new();
  51540. ExpectNotNull(dh);
  51541. p = wolfSSL_BN_new();
  51542. ExpectNotNull(p);
  51543. ExpectIntEQ(BN_set_word(p, 11), 1);
  51544. g = wolfSSL_BN_new();
  51545. ExpectNotNull(g);
  51546. ExpectIntEQ(BN_set_word(g, 2), 1);
  51547. q = wolfSSL_BN_new();
  51548. ExpectNotNull(q);
  51549. ExpectIntEQ(BN_set_word(q, 5), 1);
  51550. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  51551. /* p, q and g are now owned by dh - don't free. */
  51552. if (EXPECT_FAIL()) {
  51553. BN_free(p);
  51554. BN_free(q);
  51555. BN_free(g);
  51556. }
  51557. p = NULL;
  51558. q = NULL;
  51559. g = NULL;
  51560. p = wolfSSL_BN_new();
  51561. ExpectNotNull(p);
  51562. ExpectIntEQ(BN_set_word(p, 11), 1);
  51563. g = wolfSSL_BN_new();
  51564. ExpectNotNull(g);
  51565. ExpectIntEQ(BN_set_word(g, 2), 1);
  51566. q = wolfSSL_BN_new();
  51567. ExpectNotNull(q);
  51568. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  51569. if (EXPECT_FAIL()) {
  51570. BN_free(p);
  51571. }
  51572. p = NULL;
  51573. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  51574. if (EXPECT_FAIL()) {
  51575. BN_free(q);
  51576. }
  51577. q = NULL;
  51578. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  51579. if (EXPECT_FAIL()) {
  51580. BN_free(g);
  51581. }
  51582. g = NULL;
  51583. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  51584. /* p, q and g are now owned by dh - don't free. */
  51585. DH_free(dh);
  51586. dh = NULL;
  51587. ExpectIntEQ(DH_generate_key(NULL), 0);
  51588. ExpectNotNull(dh = DH_new());
  51589. ExpectIntEQ(DH_generate_key(dh), 0);
  51590. p = wolfSSL_BN_new();
  51591. ExpectNotNull(p);
  51592. ExpectIntEQ(BN_set_word(p, 0), 1);
  51593. g = wolfSSL_BN_new();
  51594. ExpectNotNull(g);
  51595. ExpectIntEQ(BN_set_word(g, 2), 1);
  51596. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51597. if (EXPECT_FAIL()) {
  51598. BN_free(p);
  51599. BN_free(g);
  51600. }
  51601. p = NULL;
  51602. g = NULL;
  51603. ExpectIntEQ(DH_generate_key(dh), 0);
  51604. DH_free(dh);
  51605. dh = NULL;
  51606. #endif
  51607. #endif
  51608. /* Test DH_up_ref() */
  51609. dh = wolfSSL_DH_new();
  51610. ExpectNotNull(dh);
  51611. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  51612. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  51613. DH_free(dh); /* decrease ref count */
  51614. DH_free(dh); /* free WOLFSSL_DH */
  51615. dh = NULL;
  51616. q = NULL;
  51617. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  51618. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  51619. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  51620. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  51621. #ifdef HAVE_FFDHE_2048
  51622. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  51623. DH_free(dh);
  51624. dh = NULL;
  51625. q = NULL;
  51626. #endif
  51627. #ifdef HAVE_FFDHE_3072
  51628. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  51629. DH_free(dh);
  51630. dh = NULL;
  51631. q = NULL;
  51632. #endif
  51633. #ifdef HAVE_FFDHE_4096
  51634. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  51635. DH_free(dh);
  51636. dh = NULL;
  51637. q = NULL;
  51638. #endif
  51639. #else
  51640. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  51641. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  51642. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  51643. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  51644. #endif /* OPENSSL_EXTRA && !NO_DH */
  51645. return EXPECT_RESULT();
  51646. }
  51647. static int test_wolfSSL_DH_dup(void)
  51648. {
  51649. EXPECT_DECLS;
  51650. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  51651. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  51652. defined(OPENSSL_EXTRA)
  51653. DH *dh = NULL;
  51654. DH *dhDup = NULL;
  51655. ExpectNotNull(dh = wolfSSL_DH_new());
  51656. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  51657. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  51658. #if defined(OPENSSL_ALL) || \
  51659. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  51660. {
  51661. WOLFSSL_BIGNUM* p = NULL;
  51662. WOLFSSL_BIGNUM* g = NULL;
  51663. ExpectNotNull(p = wolfSSL_BN_new());
  51664. ExpectNotNull(g = wolfSSL_BN_new());
  51665. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  51666. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  51667. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51668. if (EXPECT_FAIL()) {
  51669. wolfSSL_BN_free(p);
  51670. wolfSSL_BN_free(g);
  51671. }
  51672. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  51673. wolfSSL_DH_free(dhDup);
  51674. }
  51675. #endif
  51676. wolfSSL_DH_free(dh);
  51677. #endif
  51678. #endif
  51679. return EXPECT_RESULT();
  51680. }
  51681. static int test_wolfSSL_DH_check(void)
  51682. {
  51683. EXPECT_DECLS;
  51684. #ifdef OPENSSL_ALL
  51685. #ifndef NO_DH
  51686. #ifndef NO_BIO
  51687. #ifndef NO_DSA
  51688. byte buf[6000];
  51689. char file[] = "./certs/dsaparams.pem";
  51690. XFILE f = XBADFILE;
  51691. int bytes;
  51692. BIO* bio = NULL;
  51693. DSA* dsa = NULL;
  51694. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  51695. static const byte dh2048[] = {
  51696. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  51697. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  51698. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  51699. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  51700. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  51701. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  51702. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  51703. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  51704. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  51705. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  51706. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  51707. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  51708. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  51709. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  51710. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  51711. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  51712. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  51713. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  51714. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  51715. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  51716. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  51717. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  51718. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  51719. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  51720. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  51721. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  51722. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  51723. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  51724. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  51725. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  51726. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  51727. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  51728. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  51729. 0x93, 0x02, 0x01, 0x02
  51730. };
  51731. const byte* params;
  51732. #endif
  51733. DH* dh = NULL;
  51734. WOLFSSL_BIGNUM* p = NULL;
  51735. WOLFSSL_BIGNUM* g = NULL;
  51736. WOLFSSL_BIGNUM* pTmp = NULL;
  51737. WOLFSSL_BIGNUM* gTmp = NULL;
  51738. int codes = -1;
  51739. #ifndef NO_DSA
  51740. /* Initialize DH */
  51741. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  51742. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  51743. if (f != XBADFILE)
  51744. XFCLOSE(f);
  51745. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  51746. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  51747. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  51748. ExpectNotNull(dh);
  51749. BIO_free(bio);
  51750. DSA_free(dsa);
  51751. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  51752. params = dh2048;
  51753. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  51754. (long)sizeof(dh2048)));
  51755. #else
  51756. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  51757. #endif
  51758. /* Test assumed to be valid dh.
  51759. * Should return WOLFSSL_SUCCESS
  51760. * codes should be 0
  51761. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  51762. */
  51763. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51764. ExpectIntEQ(codes, 0);
  51765. /* Test NULL dh: expected BAD_FUNC_ARG */
  51766. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  51767. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  51768. if (dh != NULL) {
  51769. pTmp = dh->p;
  51770. dh->p = NULL;
  51771. }
  51772. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51773. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51774. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  51775. /* set dh->p back to normal so it won't fail on next tests */
  51776. if (dh != NULL) {
  51777. dh->p = pTmp;
  51778. pTmp = NULL;
  51779. }
  51780. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  51781. if (dh != NULL) {
  51782. gTmp = dh->g;
  51783. dh->g = NULL;
  51784. }
  51785. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51786. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51787. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  51788. if (dh != NULL) {
  51789. dh->g = gTmp;
  51790. gTmp = NULL;
  51791. }
  51792. /* Cleanup */
  51793. DH_free(dh);
  51794. dh = NULL;
  51795. dh = DH_new();
  51796. ExpectNotNull(dh);
  51797. /* Check empty DH. */
  51798. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51799. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51800. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  51801. /* Check non-prime valued p. */
  51802. ExpectNotNull(p = BN_new());
  51803. ExpectIntEQ(BN_set_word(p, 4), 1);
  51804. ExpectNotNull(g = BN_new());
  51805. ExpectIntEQ(BN_set_word(g, 2), 1);
  51806. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  51807. if (EXPECT_FAIL()) {
  51808. wolfSSL_BN_free(p);
  51809. wolfSSL_BN_free(g);
  51810. }
  51811. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51812. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51813. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  51814. DH_free(dh);
  51815. dh = NULL;
  51816. #endif
  51817. #endif /* !NO_DH && !NO_DSA */
  51818. #endif
  51819. return EXPECT_RESULT();
  51820. }
  51821. static int test_wolfSSL_DH_prime(void)
  51822. {
  51823. EXPECT_DECLS;
  51824. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51825. WOLFSSL_BIGNUM* bn = NULL;
  51826. #if WOLFSSL_MAX_BN_BITS >= 768
  51827. WOLFSSL_BIGNUM* bn2 = NULL;
  51828. #endif
  51829. bn = wolfSSL_DH_768_prime(NULL);
  51830. #if WOLFSSL_MAX_BN_BITS >= 768
  51831. ExpectNotNull(bn);
  51832. bn2 = wolfSSL_DH_768_prime(bn);
  51833. ExpectNotNull(bn2);
  51834. ExpectTrue(bn == bn2);
  51835. wolfSSL_BN_free(bn);
  51836. bn = NULL;
  51837. #else
  51838. ExpectNull(bn);
  51839. #endif
  51840. bn = wolfSSL_DH_1024_prime(NULL);
  51841. #if WOLFSSL_MAX_BN_BITS >= 1024
  51842. ExpectNotNull(bn);
  51843. wolfSSL_BN_free(bn);
  51844. bn = NULL;
  51845. #else
  51846. ExpectNull(bn);
  51847. #endif
  51848. bn = wolfSSL_DH_2048_prime(NULL);
  51849. #if WOLFSSL_MAX_BN_BITS >= 2048
  51850. ExpectNotNull(bn);
  51851. wolfSSL_BN_free(bn);
  51852. bn = NULL;
  51853. #else
  51854. ExpectNull(bn);
  51855. #endif
  51856. bn = wolfSSL_DH_3072_prime(NULL);
  51857. #if WOLFSSL_MAX_BN_BITS >= 3072
  51858. ExpectNotNull(bn);
  51859. wolfSSL_BN_free(bn);
  51860. bn = NULL;
  51861. #else
  51862. ExpectNull(bn);
  51863. #endif
  51864. bn = wolfSSL_DH_4096_prime(NULL);
  51865. #if WOLFSSL_MAX_BN_BITS >= 4096
  51866. ExpectNotNull(bn);
  51867. wolfSSL_BN_free(bn);
  51868. bn = NULL;
  51869. #else
  51870. ExpectNull(bn);
  51871. #endif
  51872. bn = wolfSSL_DH_6144_prime(NULL);
  51873. #if WOLFSSL_MAX_BN_BITS >= 6144
  51874. ExpectNotNull(bn);
  51875. wolfSSL_BN_free(bn);
  51876. bn = NULL;
  51877. #else
  51878. ExpectNull(bn);
  51879. #endif
  51880. bn = wolfSSL_DH_8192_prime(NULL);
  51881. #if WOLFSSL_MAX_BN_BITS >= 8192
  51882. ExpectNotNull(bn);
  51883. wolfSSL_BN_free(bn);
  51884. bn = NULL;
  51885. #else
  51886. ExpectNull(bn);
  51887. #endif
  51888. #endif
  51889. return EXPECT_RESULT();
  51890. }
  51891. static int test_wolfSSL_DH_1536_prime(void)
  51892. {
  51893. EXPECT_DECLS;
  51894. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51895. BIGNUM* bn = NULL;
  51896. unsigned char bits[200];
  51897. int sz = 192; /* known binary size */
  51898. const byte expected[] = {
  51899. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51900. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  51901. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  51902. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  51903. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  51904. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  51905. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  51906. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  51907. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  51908. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  51909. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  51910. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  51911. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  51912. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  51913. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  51914. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  51915. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  51916. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  51917. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  51918. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  51919. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  51920. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  51921. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  51922. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51923. };
  51924. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  51925. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  51926. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  51927. BN_free(bn);
  51928. #endif
  51929. return EXPECT_RESULT();
  51930. }
  51931. static int test_wolfSSL_DH_get_2048_256(void)
  51932. {
  51933. EXPECT_DECLS;
  51934. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51935. WOLFSSL_DH* dh = NULL;
  51936. const WOLFSSL_BIGNUM* pBn;
  51937. const WOLFSSL_BIGNUM* gBn;
  51938. const WOLFSSL_BIGNUM* qBn;
  51939. const byte pExpected[] = {
  51940. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  51941. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  51942. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  51943. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  51944. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  51945. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  51946. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  51947. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  51948. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  51949. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  51950. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  51951. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  51952. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  51953. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  51954. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  51955. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  51956. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  51957. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  51958. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  51959. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  51960. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  51961. 0x1E, 0x1A, 0x15, 0x97
  51962. };
  51963. const byte gExpected[] = {
  51964. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  51965. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  51966. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  51967. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  51968. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  51969. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  51970. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  51971. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  51972. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  51973. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  51974. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  51975. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  51976. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  51977. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  51978. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  51979. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  51980. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  51981. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  51982. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  51983. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  51984. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  51985. 0x6C, 0xC4, 0x16, 0x59
  51986. };
  51987. const byte qExpected[] = {
  51988. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  51989. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  51990. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  51991. };
  51992. int pSz = 0;
  51993. int qSz = 0;
  51994. int gSz = 0;
  51995. byte* pReturned = NULL;
  51996. byte* qReturned = NULL;
  51997. byte* gReturned = NULL;
  51998. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  51999. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  52000. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  52001. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  52002. DYNAMIC_TYPE_TMP_BUFFER));
  52003. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  52004. ExpectIntEQ(pSz, sizeof(pExpected));
  52005. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  52006. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  52007. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  52008. DYNAMIC_TYPE_TMP_BUFFER));
  52009. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  52010. ExpectIntEQ(qSz, sizeof(qExpected));
  52011. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  52012. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  52013. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  52014. DYNAMIC_TYPE_TMP_BUFFER));
  52015. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  52016. ExpectIntEQ(gSz, sizeof(gExpected));
  52017. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  52018. wolfSSL_DH_free(dh);
  52019. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52020. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52021. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52022. #endif
  52023. return EXPECT_RESULT();
  52024. }
  52025. static int test_wolfSSL_PEM_write_DHparams(void)
  52026. {
  52027. EXPECT_DECLS;
  52028. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  52029. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  52030. DH* dh = NULL;
  52031. BIO* bio = NULL;
  52032. XFILE fp = XBADFILE;
  52033. byte pem[2048];
  52034. int pemSz = 0;
  52035. const char expected[] =
  52036. "-----BEGIN DH PARAMETERS-----\n"
  52037. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  52038. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  52039. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  52040. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  52041. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  52042. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  52043. "-----END DH PARAMETERS-----\n";
  52044. const char badPem[] =
  52045. "-----BEGIN DH PARAMETERS-----\n"
  52046. "-----END DH PARAMETERS-----\n";
  52047. const char emptySeqPem[] =
  52048. "-----BEGIN DH PARAMETERS-----\n"
  52049. "MAA=\n"
  52050. "-----END DH PARAMETERS-----\n";
  52051. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  52052. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  52053. if (fp != XBADFILE) {
  52054. XFCLOSE(fp);
  52055. fp = XBADFILE;
  52056. }
  52057. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  52058. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52059. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52060. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  52061. (int)sizeof(badPem));
  52062. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52063. BIO_free(bio);
  52064. bio = NULL;
  52065. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52066. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52067. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  52068. (int)sizeof(emptySeqPem));
  52069. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52070. BIO_free(bio);
  52071. bio = NULL;
  52072. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52073. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  52074. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52075. BIO_free(bio);
  52076. bio = NULL;
  52077. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  52078. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  52079. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  52080. DH_free(dh);
  52081. dh = NULL;
  52082. dh = wolfSSL_DH_new();
  52083. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  52084. if (fp != XBADFILE) {
  52085. XFCLOSE(fp);
  52086. fp = XBADFILE;
  52087. }
  52088. wolfSSL_DH_free(dh);
  52089. dh = NULL;
  52090. /* check results */
  52091. XMEMSET(pem, 0, sizeof(pem));
  52092. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  52093. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  52094. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  52095. if (fp != XBADFILE)
  52096. XFCLOSE(fp);
  52097. #endif
  52098. return EXPECT_RESULT();
  52099. }
  52100. static int test_wolfSSL_d2i_DHparams(void)
  52101. {
  52102. EXPECT_DECLS;
  52103. #ifdef OPENSSL_ALL
  52104. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  52105. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52106. XFILE f = XBADFILE;
  52107. unsigned char buf[4096];
  52108. const unsigned char* pt = buf;
  52109. #ifdef HAVE_FFDHE_2048
  52110. const char* params1 = "./certs/dh2048.der";
  52111. #endif
  52112. #ifdef HAVE_FFDHE_3072
  52113. const char* params2 = "./certs/dh3072.der";
  52114. #endif
  52115. long len = 0;
  52116. WOLFSSL_DH* dh = NULL;
  52117. XMEMSET(buf, 0, sizeof(buf));
  52118. /* Test 2048 bit parameters */
  52119. #ifdef HAVE_FFDHE_2048
  52120. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  52121. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52122. if (f != XBADFILE) {
  52123. XFCLOSE(f);
  52124. f = XBADFILE;
  52125. }
  52126. /* Valid case */
  52127. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52128. ExpectNotNull(dh->p);
  52129. ExpectNotNull(dh->g);
  52130. ExpectTrue(pt == buf);
  52131. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  52132. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  52133. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  52134. /* Invalid cases */
  52135. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  52136. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  52137. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  52138. DH_free(dh);
  52139. dh = NULL;
  52140. *buf = 0;
  52141. pt = buf;
  52142. #endif /* HAVE_FFDHE_2048 */
  52143. /* Test 3072 bit parameters */
  52144. #ifdef HAVE_FFDHE_3072
  52145. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  52146. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52147. if (f != XBADFILE) {
  52148. XFCLOSE(f);
  52149. f = XBADFILE;
  52150. }
  52151. /* Valid case */
  52152. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  52153. ExpectNotNull(dh->p);
  52154. ExpectNotNull(dh->g);
  52155. ExpectTrue(pt != buf);
  52156. ExpectIntEQ(DH_generate_key(dh), 1);
  52157. /* Invalid cases */
  52158. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  52159. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  52160. DH_free(dh);
  52161. dh = NULL;
  52162. #endif /* HAVE_FFDHE_3072 */
  52163. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52164. #endif /* !NO_DH */
  52165. #endif
  52166. return EXPECT_RESULT();
  52167. }
  52168. static int test_wolfSSL_DH_LoadDer(void)
  52169. {
  52170. EXPECT_DECLS;
  52171. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  52172. defined(OPENSSL_EXTRA)
  52173. static const byte dh2048[] = {
  52174. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  52175. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  52176. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  52177. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  52178. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  52179. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  52180. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  52181. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  52182. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  52183. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  52184. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  52185. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  52186. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  52187. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  52188. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  52189. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  52190. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  52191. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  52192. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  52193. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  52194. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  52195. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  52196. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  52197. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  52198. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  52199. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  52200. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  52201. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  52202. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  52203. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  52204. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  52205. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  52206. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  52207. 0x93, 0x02, 0x01, 0x02
  52208. };
  52209. WOLFSSL_DH* dh = NULL;
  52210. ExpectNotNull(dh = wolfSSL_DH_new());
  52211. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  52212. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  52213. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  52214. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  52215. wolfSSL_DH_free(dh);
  52216. #endif
  52217. return EXPECT_RESULT();
  52218. }
  52219. static int test_wolfSSL_i2d_DHparams(void)
  52220. {
  52221. EXPECT_DECLS;
  52222. #ifdef OPENSSL_ALL
  52223. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  52224. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52225. XFILE f = XBADFILE;
  52226. unsigned char buf[4096];
  52227. const unsigned char* pt;
  52228. unsigned char* pt2;
  52229. #ifdef HAVE_FFDHE_2048
  52230. const char* params1 = "./certs/dh2048.der";
  52231. #endif
  52232. #ifdef HAVE_FFDHE_3072
  52233. const char* params2 = "./certs/dh3072.der";
  52234. #endif
  52235. long len = 0;
  52236. WOLFSSL_DH* dh = NULL;
  52237. /* Test 2048 bit parameters */
  52238. #ifdef HAVE_FFDHE_2048
  52239. pt = buf;
  52240. pt2 = buf;
  52241. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  52242. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52243. if (f != XBADFILE) {
  52244. XFCLOSE(f);
  52245. f = XBADFILE;
  52246. }
  52247. /* Valid case */
  52248. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52249. ExpectTrue(pt == buf);
  52250. ExpectIntEQ(DH_generate_key(dh), 1);
  52251. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  52252. /* Invalid case */
  52253. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  52254. /* Return length only */
  52255. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  52256. DH_free(dh);
  52257. dh = NULL;
  52258. *buf = 0;
  52259. #endif
  52260. /* Test 3072 bit parameters */
  52261. #ifdef HAVE_FFDHE_3072
  52262. pt = buf;
  52263. pt2 = buf;
  52264. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  52265. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52266. if (f != XBADFILE) {
  52267. XFCLOSE(f);
  52268. f = XBADFILE;
  52269. }
  52270. /* Valid case */
  52271. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52272. ExpectTrue(pt == buf);
  52273. ExpectIntEQ(DH_generate_key(dh), 1);
  52274. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  52275. /* Invalid case */
  52276. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  52277. /* Return length only */
  52278. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  52279. DH_free(dh);
  52280. dh = NULL;
  52281. #endif
  52282. dh = DH_new();
  52283. ExpectNotNull(dh);
  52284. pt2 = buf;
  52285. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  52286. DH_free(dh);
  52287. dh = NULL;
  52288. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52289. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  52290. #endif
  52291. return EXPECT_RESULT();
  52292. }
  52293. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  52294. /*----------------------------------------------------------------------------*
  52295. | EC
  52296. *----------------------------------------------------------------------------*/
  52297. static int test_wolfSSL_EC_GROUP(void)
  52298. {
  52299. EXPECT_DECLS;
  52300. #ifdef OPENSSL_EXTRA
  52301. EC_GROUP *group = NULL;
  52302. EC_GROUP *group2 = NULL;
  52303. EC_GROUP *group3 = NULL;
  52304. #ifndef HAVE_ECC_BRAINPOOL
  52305. EC_GROUP *group4 = NULL;
  52306. #endif
  52307. WOLFSSL_BIGNUM* order = NULL;
  52308. int group_bits;
  52309. int i;
  52310. static const int knownEccNids[] = {
  52311. NID_X9_62_prime192v1,
  52312. NID_X9_62_prime192v2,
  52313. NID_X9_62_prime192v3,
  52314. NID_X9_62_prime239v1,
  52315. NID_X9_62_prime239v2,
  52316. NID_X9_62_prime239v3,
  52317. NID_X9_62_prime256v1,
  52318. NID_secp112r1,
  52319. NID_secp112r2,
  52320. NID_secp128r1,
  52321. NID_secp128r2,
  52322. NID_secp160r1,
  52323. NID_secp160r2,
  52324. NID_secp224r1,
  52325. NID_secp384r1,
  52326. NID_secp521r1,
  52327. NID_secp160k1,
  52328. NID_secp192k1,
  52329. NID_secp224k1,
  52330. NID_secp256k1,
  52331. NID_brainpoolP160r1,
  52332. NID_brainpoolP192r1,
  52333. NID_brainpoolP224r1,
  52334. NID_brainpoolP256r1,
  52335. NID_brainpoolP320r1,
  52336. NID_brainpoolP384r1,
  52337. NID_brainpoolP512r1,
  52338. };
  52339. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  52340. static const int knownEccEnums[] = {
  52341. ECC_SECP192R1,
  52342. ECC_PRIME192V2,
  52343. ECC_PRIME192V3,
  52344. ECC_PRIME239V1,
  52345. ECC_PRIME239V2,
  52346. ECC_PRIME239V3,
  52347. ECC_SECP256R1,
  52348. ECC_SECP112R1,
  52349. ECC_SECP112R2,
  52350. ECC_SECP128R1,
  52351. ECC_SECP128R2,
  52352. ECC_SECP160R1,
  52353. ECC_SECP160R2,
  52354. ECC_SECP224R1,
  52355. ECC_SECP384R1,
  52356. ECC_SECP521R1,
  52357. ECC_SECP160K1,
  52358. ECC_SECP192K1,
  52359. ECC_SECP224K1,
  52360. ECC_SECP256K1,
  52361. ECC_BRAINPOOLP160R1,
  52362. ECC_BRAINPOOLP192R1,
  52363. ECC_BRAINPOOLP224R1,
  52364. ECC_BRAINPOOLP256R1,
  52365. ECC_BRAINPOOLP320R1,
  52366. ECC_BRAINPOOLP384R1,
  52367. ECC_BRAINPOOLP512R1,
  52368. };
  52369. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  52370. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52371. ExpectNotNull(group2 = EC_GROUP_dup(group));
  52372. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  52373. #ifndef HAVE_ECC_BRAINPOOL
  52374. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  52375. NID_brainpoolP256r1));
  52376. #endif
  52377. ExpectNull(EC_GROUP_dup(NULL));
  52378. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  52379. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  52380. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  52381. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  52382. #ifndef HAVE_ECC_BRAINPOOL
  52383. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  52384. #endif
  52385. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  52386. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  52387. ExpectNotNull(order = BN_new());
  52388. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  52389. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  52390. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  52391. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  52392. wolfSSL_BN_free(order);
  52393. ExpectNotNull(EC_GROUP_method_of(group));
  52394. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  52395. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  52396. NID_X9_62_prime_field);
  52397. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  52398. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  52399. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  52400. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  52401. #ifndef NO_WOLFSSL_STUB
  52402. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  52403. #endif
  52404. #ifndef HAVE_ECC_BRAINPOOL
  52405. EC_GROUP_free(group4);
  52406. #endif
  52407. EC_GROUP_free(group3);
  52408. EC_GROUP_free(group2);
  52409. EC_GROUP_free(group);
  52410. for (i = 0; i < knowEccNidsLen; i++) {
  52411. group = NULL;
  52412. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  52413. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  52414. EC_GROUP_free(group);
  52415. }
  52416. for (i = 0; i < knowEccEnumsLen; i++) {
  52417. group = NULL;
  52418. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  52419. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  52420. EC_GROUP_free(group);
  52421. }
  52422. #endif
  52423. return EXPECT_RESULT();
  52424. }
  52425. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  52426. {
  52427. EXPECT_DECLS;
  52428. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52429. EC_GROUP *group = NULL;
  52430. BIO* bio = NULL;
  52431. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  52432. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  52433. EC_GROUP *ret = NULL;
  52434. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  52435. "BgUrgQQAIg==\n"
  52436. "-----END EC PARAMETERS-----";
  52437. #endif
  52438. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  52439. "MAA=\n"
  52440. "-----END EC PARAMETERS-----";
  52441. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  52442. "BgA=\n"
  52443. "-----END EC PARAMETERS-----";
  52444. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  52445. "BgE=\n"
  52446. "-----END EC PARAMETERS-----";
  52447. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  52448. "BgE*\n"
  52449. "-----END EC PARAMETERS-----";
  52450. /* Test that first parameter, bio, being NULL fails. */
  52451. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  52452. /* Test that reading named parameters works. */
  52453. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  52454. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  52455. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52456. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  52457. BIO_free(bio);
  52458. bio = NULL;
  52459. EC_GROUP_free(group);
  52460. group = NULL;
  52461. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  52462. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  52463. /* Test that reusing group works. */
  52464. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  52465. sizeof(ec_nc_p384)));
  52466. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  52467. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  52468. BIO_free(bio);
  52469. bio = NULL;
  52470. EC_GROUP_free(group);
  52471. group = NULL;
  52472. /* Test that returning through group works. */
  52473. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  52474. sizeof(ec_nc_p384)));
  52475. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  52476. ExpectIntEQ(group == ret, 1);
  52477. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  52478. BIO_free(bio);
  52479. bio = NULL;
  52480. EC_GROUP_free(group);
  52481. group = NULL;
  52482. #endif
  52483. /* Test 0x30, 0x00 (not and object id) fails. */
  52484. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  52485. sizeof(ec_nc_bad_1)));
  52486. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52487. BIO_free(bio);
  52488. bio = NULL;
  52489. /* Test 0x06, 0x00 (empty object id) fails. */
  52490. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  52491. sizeof(ec_nc_bad_2)));
  52492. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52493. BIO_free(bio);
  52494. bio = NULL;
  52495. /* Test 0x06, 0x01 (badly formed object id) fails. */
  52496. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  52497. sizeof(ec_nc_bad_3)));
  52498. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52499. BIO_free(bio);
  52500. bio = NULL;
  52501. /* Test invalid PEM encoding - invalid character. */
  52502. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  52503. sizeof(ec_nc_bad_4)));
  52504. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52505. BIO_free(bio);
  52506. #endif
  52507. return EXPECT_RESULT();
  52508. }
  52509. static int test_wolfSSL_EC_POINT(void)
  52510. {
  52511. EXPECT_DECLS;
  52512. #if !defined(WOLFSSL_SP_MATH) && \
  52513. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  52514. #ifdef OPENSSL_EXTRA
  52515. BN_CTX* ctx = NULL;
  52516. EC_GROUP* group = NULL;
  52517. #ifndef HAVE_ECC_BRAINPOOL
  52518. EC_GROUP* group2 = NULL;
  52519. #endif
  52520. EC_POINT* Gxy = NULL;
  52521. EC_POINT* new_point = NULL;
  52522. EC_POINT* set_point = NULL;
  52523. EC_POINT* infinity = NULL;
  52524. BIGNUM* k = NULL;
  52525. BIGNUM* Gx = NULL;
  52526. BIGNUM* Gy = NULL;
  52527. BIGNUM* Gz = NULL;
  52528. BIGNUM* X = NULL;
  52529. BIGNUM* Y = NULL;
  52530. BIGNUM* set_point_bn = NULL;
  52531. char* hexStr = NULL;
  52532. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  52533. "17AF381913FF7A96314EA47055EA0FD0";
  52534. /* NISTP256R1 Gx/Gy */
  52535. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  52536. "77037D812DEB33A0F4A13945D898C296";
  52537. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  52538. "2BCE33576B315ECECBB6406837BF51F5";
  52539. #ifndef HAVE_SELFTEST
  52540. EC_POINT *tmp = NULL;
  52541. size_t bin_len;
  52542. unsigned int blen = 0;
  52543. unsigned char* buf = NULL;
  52544. unsigned char bufInf[1] = { 0x00 };
  52545. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  52546. "77037D812DEB33A0F4A13945D898C296"
  52547. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  52548. "2BCE33576B315ECECBB6406837BF51F5";
  52549. const unsigned char binUncompG[] = {
  52550. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52551. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52552. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52553. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52554. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52555. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52556. };
  52557. const unsigned char binUncompGBad[] = {
  52558. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52559. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52560. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52561. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52562. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52563. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52564. };
  52565. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  52566. "77037D812DEB33A0F4A13945D898C296";
  52567. #ifdef HAVE_COMP_KEY
  52568. const unsigned char binCompG[] = {
  52569. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52570. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52571. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52572. };
  52573. #endif
  52574. #endif
  52575. ExpectNotNull(ctx = BN_CTX_new());
  52576. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52577. #ifndef HAVE_ECC_BRAINPOOL
  52578. /* Used to make groups curve_idx == -1. */
  52579. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  52580. #endif
  52581. ExpectNull(EC_POINT_new(NULL));
  52582. ExpectNotNull(Gxy = EC_POINT_new(group));
  52583. ExpectNotNull(new_point = EC_POINT_new(group));
  52584. ExpectNotNull(set_point = EC_POINT_new(group));
  52585. ExpectNotNull(X = BN_new());
  52586. ExpectNotNull(Y = BN_new());
  52587. ExpectNotNull(set_point_bn = BN_new());
  52588. ExpectNotNull(infinity = EC_POINT_new(group));
  52589. /* load test values */
  52590. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  52591. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  52592. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  52593. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  52594. /* populate coordinates for input point */
  52595. if (Gxy != NULL) {
  52596. Gxy->X = Gx;
  52597. Gxy->Y = Gy;
  52598. Gxy->Z = Gz;
  52599. }
  52600. /* Test handling of NULL point. */
  52601. EC_POINT_clear_free(NULL);
  52602. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52603. NULL, NULL, ctx), 0);
  52604. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  52605. NULL, NULL, ctx), 0);
  52606. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  52607. NULL, NULL, ctx), 0);
  52608. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52609. X, NULL, ctx), 0);
  52610. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52611. NULL, Y, ctx), 0);
  52612. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  52613. X, Y, ctx), 0);
  52614. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  52615. X, Y, ctx), 0);
  52616. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  52617. NULL, Y, ctx), 0);
  52618. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  52619. X, NULL, ctx), 0);
  52620. /* Getting point at infinity returns an error. */
  52621. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  52622. X, Y, ctx), 0);
  52623. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  52624. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  52625. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52626. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  52627. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  52628. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  52629. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  52630. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  52631. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  52632. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  52633. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  52634. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  52635. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  52636. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  52637. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  52638. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  52639. /* perform point multiplication */
  52640. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  52641. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52642. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  52643. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  52644. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  52645. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52646. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  52647. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  52648. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  52649. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52650. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  52651. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  52652. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  52653. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  52654. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  52655. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  52656. /* Set point to something. */
  52657. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  52658. #else
  52659. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  52660. ctx), 1);
  52661. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52662. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  52663. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  52664. #endif
  52665. /* check if point X coordinate is zero */
  52666. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52667. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  52668. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  52669. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  52670. #endif
  52671. /* extract the coordinates from point */
  52672. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  52673. ctx), WOLFSSL_SUCCESS);
  52674. /* check if point X coordinate is zero */
  52675. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  52676. /* set the same X and Y points in another object */
  52677. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  52678. ctx), WOLFSSL_SUCCESS);
  52679. /* compare points as they should be the same */
  52680. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  52681. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  52682. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  52683. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  52684. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  52685. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  52686. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  52687. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  52688. /* Test copying */
  52689. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  52690. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  52691. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  52692. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  52693. /* Test inverting */
  52694. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  52695. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  52696. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  52697. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  52698. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  52699. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  52700. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52701. {
  52702. EC_POINT* orig_point = NULL;
  52703. ExpectNotNull(orig_point = EC_POINT_new(group));
  52704. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  52705. 1);
  52706. /* new_point should be set_point inverted so adding it will revert
  52707. * the point back to set_point */
  52708. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  52709. NULL), 1);
  52710. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  52711. EC_POINT_free(orig_point);
  52712. }
  52713. #endif
  52714. /* Test getting affine converts from projective. */
  52715. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  52716. /* Force non-affine coordinates */
  52717. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  52718. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  52719. if (new_point != NULL) {
  52720. new_point->inSet = 0;
  52721. }
  52722. /* extract the coordinates from point */
  52723. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  52724. ctx), WOLFSSL_SUCCESS);
  52725. /* check if point ordinates have changed. */
  52726. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  52727. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  52728. /* Test check for infinity */
  52729. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  52730. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  52731. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  52732. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  52733. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  52734. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  52735. #else
  52736. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  52737. #endif
  52738. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  52739. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  52740. /* check bn2hex */
  52741. hexStr = BN_bn2hex(k);
  52742. ExpectStrEQ(hexStr, kTest);
  52743. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  52744. defined(XFPRINTF)
  52745. BN_print_fp(stderr, k);
  52746. fprintf(stderr, "\n");
  52747. #endif
  52748. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52749. hexStr = BN_bn2hex(Gx);
  52750. ExpectStrEQ(hexStr, kGx);
  52751. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  52752. defined(XFPRINTF)
  52753. BN_print_fp(stderr, Gx);
  52754. fprintf(stderr, "\n");
  52755. #endif
  52756. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52757. hexStr = BN_bn2hex(Gy);
  52758. ExpectStrEQ(hexStr, kGy);
  52759. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  52760. defined(XFPRINTF)
  52761. BN_print_fp(stderr, Gy);
  52762. fprintf(stderr, "\n");
  52763. #endif
  52764. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52765. #ifndef HAVE_SELFTEST
  52766. /* Test point to hex */
  52767. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52768. ctx));
  52769. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52770. ctx));
  52771. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52772. ctx));
  52773. #ifndef HAVE_ECC_BRAINPOOL
  52774. /* Group not supported in wolfCrypt. */
  52775. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52776. ctx));
  52777. #endif
  52778. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  52779. ExpectNotNull(hexStr);
  52780. ExpectStrEQ(hexStr, uncompG);
  52781. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52782. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  52783. ExpectNotNull(hexStr);
  52784. ExpectStrEQ(hexStr, compG);
  52785. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52786. /* Test point to oct */
  52787. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52788. NULL, 0, ctx), 0);
  52789. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52790. NULL, 0, ctx), 0);
  52791. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52792. NULL, 0, ctx), 0);
  52793. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52794. NULL, 0, ctx);
  52795. ExpectIntEQ(bin_len, sizeof(binUncompG));
  52796. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  52797. DYNAMIC_TYPE_ECC));
  52798. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52799. buf, bin_len, ctx), bin_len);
  52800. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  52801. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52802. /* Infinity (x=0, y=0) encodes as '0x00'. */
  52803. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52804. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  52805. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52806. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  52807. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52808. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  52809. ExpectIntEQ(bufInf[0], 0);
  52810. wolfSSL_EC_POINT_dump(NULL, NULL);
  52811. /* Test point i2d */
  52812. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  52813. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  52814. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  52815. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  52816. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  52817. ExpectIntEQ(blen, sizeof(binUncompG));
  52818. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  52819. blen -= 1;
  52820. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  52821. blen += 1;
  52822. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  52823. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  52824. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52825. #ifdef HAVE_COMP_KEY
  52826. /* Test point to oct compressed */
  52827. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  52828. 0, ctx);
  52829. ExpectIntEQ(bin_len, sizeof(binCompG));
  52830. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  52831. DYNAMIC_TYPE_ECC));
  52832. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  52833. bin_len, ctx), bin_len);
  52834. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  52835. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52836. #endif
  52837. /* Test point BN */
  52838. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  52839. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52840. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  52841. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52842. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  52843. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52844. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  52845. /* Test oct to point */
  52846. ExpectNotNull(tmp = EC_POINT_new(group));
  52847. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  52848. ctx), 0);
  52849. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  52850. ctx), 0);
  52851. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  52852. ctx), 0);
  52853. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  52854. sizeof(binUncompGBad), ctx), 0);
  52855. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  52856. ctx), 1);
  52857. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52858. EC_POINT_free(tmp);
  52859. tmp = NULL;
  52860. /* Test setting BN ordinates. */
  52861. ExpectNotNull(tmp = EC_POINT_new(group));
  52862. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52863. NULL, ctx), 0);
  52864. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  52865. NULL, ctx), 0);
  52866. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  52867. NULL, ctx), 0);
  52868. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  52869. NULL, ctx), 0);
  52870. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52871. Gy, ctx), 0);
  52872. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  52873. ctx), 0);
  52874. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  52875. ctx), 0);
  52876. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  52877. Gy, ctx), 0);
  52878. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  52879. NULL, ctx), 0);
  52880. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  52881. ctx), 1);
  52882. EC_POINT_free(tmp);
  52883. tmp = NULL;
  52884. /* Test point d2i */
  52885. ExpectNotNull(tmp = EC_POINT_new(group));
  52886. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  52887. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  52888. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  52889. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  52890. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  52891. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  52892. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  52893. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  52894. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  52895. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52896. EC_POINT_free(tmp);
  52897. tmp = NULL;
  52898. #ifdef HAVE_COMP_KEY
  52899. /* Test oct compressed to point */
  52900. ExpectNotNull(tmp = EC_POINT_new(group));
  52901. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  52902. 1);
  52903. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52904. EC_POINT_free(tmp);
  52905. tmp = NULL;
  52906. /* Test point d2i - compressed */
  52907. ExpectNotNull(tmp = EC_POINT_new(group));
  52908. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  52909. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52910. EC_POINT_free(tmp);
  52911. tmp = NULL;
  52912. #endif
  52913. #endif
  52914. /* test BN_mod_add */
  52915. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  52916. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  52917. 1);
  52918. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  52919. /* cleanup */
  52920. BN_free(X);
  52921. BN_free(Y);
  52922. BN_free(k);
  52923. BN_free(set_point_bn);
  52924. EC_POINT_free(infinity);
  52925. EC_POINT_free(new_point);
  52926. EC_POINT_free(set_point);
  52927. EC_POINT_clear_free(Gxy);
  52928. #ifndef HAVE_ECC_BRAINPOOL
  52929. EC_GROUP_free(group2);
  52930. #endif
  52931. EC_GROUP_free(group);
  52932. BN_CTX_free(ctx);
  52933. #endif
  52934. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  52935. return EXPECT_RESULT();
  52936. }
  52937. static int test_wolfSSL_SPAKE(void)
  52938. {
  52939. EXPECT_DECLS;
  52940. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  52941. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  52942. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52943. BIGNUM* x = NULL; /* kdc priv */
  52944. BIGNUM* y = NULL; /* client priv */
  52945. BIGNUM* w = NULL; /* shared value */
  52946. byte M_bytes[] = {
  52947. /* uncompressed */
  52948. 0x04,
  52949. /* x */
  52950. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  52951. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  52952. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  52953. /* y */
  52954. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  52955. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  52956. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  52957. };
  52958. EC_POINT* M = NULL; /* shared value */
  52959. byte N_bytes[] = {
  52960. /* uncompressed */
  52961. 0x04,
  52962. /* x */
  52963. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  52964. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  52965. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  52966. /* y */
  52967. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  52968. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  52969. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  52970. };
  52971. EC_POINT* N = NULL; /* shared value */
  52972. EC_POINT* T = NULL; /* kdc pub */
  52973. EC_POINT* tmp1 = NULL; /* kdc pub */
  52974. EC_POINT* tmp2 = NULL; /* kdc pub */
  52975. EC_POINT* S = NULL; /* client pub */
  52976. EC_POINT* client_secret = NULL;
  52977. EC_POINT* kdc_secret = NULL;
  52978. EC_GROUP* group = NULL;
  52979. BN_CTX* bn_ctx = NULL;
  52980. /* Values taken from a test run of Kerberos 5 */
  52981. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52982. ExpectNotNull(bn_ctx = BN_CTX_new());
  52983. ExpectNotNull(M = EC_POINT_new(group));
  52984. ExpectNotNull(N = EC_POINT_new(group));
  52985. ExpectNotNull(T = EC_POINT_new(group));
  52986. ExpectNotNull(tmp1 = EC_POINT_new(group));
  52987. ExpectNotNull(tmp2 = EC_POINT_new(group));
  52988. ExpectNotNull(S = EC_POINT_new(group));
  52989. ExpectNotNull(client_secret = EC_POINT_new(group));
  52990. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  52991. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  52992. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  52993. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  52994. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  52995. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  52996. "506AB395478F0AAB647752CF117B36250"), 1);
  52997. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  52998. 1);
  52999. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  53000. 1);
  53001. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  53002. /* kdc */
  53003. /* T=x*P+w*M */
  53004. /* All in one function call */
  53005. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  53006. /* Spread into separate calls */
  53007. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  53008. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  53009. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  53010. 1);
  53011. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  53012. /* client */
  53013. /* S=y*P+w*N */
  53014. /* All in one function call */
  53015. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  53016. /* Spread into separate calls */
  53017. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  53018. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  53019. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  53020. 1);
  53021. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  53022. /* K=y*(T-w*M) */
  53023. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  53024. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  53025. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  53026. 1);
  53027. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  53028. bn_ctx), 1);
  53029. /* kdc */
  53030. /* K=x*(S-w*N) */
  53031. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  53032. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  53033. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  53034. 1);
  53035. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  53036. 1);
  53037. /* kdc_secret == client_secret */
  53038. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  53039. BN_free(x);
  53040. BN_free(y);
  53041. BN_free(w);
  53042. EC_POINT_free(M);
  53043. EC_POINT_free(N);
  53044. EC_POINT_free(T);
  53045. EC_POINT_free(tmp1);
  53046. EC_POINT_free(tmp2);
  53047. EC_POINT_free(S);
  53048. EC_POINT_free(client_secret);
  53049. EC_POINT_free(kdc_secret);
  53050. EC_GROUP_free(group);
  53051. BN_CTX_free(bn_ctx);
  53052. #endif
  53053. return EXPECT_RESULT();
  53054. }
  53055. static int test_wolfSSL_EC_KEY_generate(void)
  53056. {
  53057. EXPECT_DECLS;
  53058. #ifdef OPENSSL_EXTRA
  53059. WOLFSSL_EC_KEY* key = NULL;
  53060. #ifndef HAVE_ECC_BRAINPOOL
  53061. WOLFSSL_EC_GROUP* group = NULL;
  53062. #endif
  53063. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53064. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  53065. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  53066. wolfSSL_EC_KEY_free(key);
  53067. key = NULL;
  53068. #ifndef HAVE_ECC_BRAINPOOL
  53069. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  53070. NID_brainpoolP256r1));
  53071. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  53072. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  53073. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  53074. wolfSSL_EC_KEY_free(key);
  53075. wolfSSL_EC_GROUP_free(group);
  53076. #endif
  53077. #endif
  53078. return EXPECT_RESULT();
  53079. }
  53080. static int test_EC_i2d(void)
  53081. {
  53082. EXPECT_DECLS;
  53083. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  53084. EC_KEY *key = NULL;
  53085. EC_KEY *copy = NULL;
  53086. int len = 0;
  53087. unsigned char *buf = NULL;
  53088. unsigned char *p = NULL;
  53089. const unsigned char *tmp = NULL;
  53090. const unsigned char octBad[] = {
  53091. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53092. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53093. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53094. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53095. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53096. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53097. };
  53098. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53099. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  53100. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  53101. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53102. DYNAMIC_TYPE_TMP_BUFFER));
  53103. p = buf;
  53104. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  53105. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  53106. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  53107. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  53108. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  53109. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  53110. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  53111. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  53112. tmp = buf;
  53113. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  53114. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  53115. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  53116. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  53117. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  53118. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  53119. tmp = buf;
  53120. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  53121. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  53122. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  53123. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  53124. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  53125. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  53126. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  53127. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  53128. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  53129. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  53130. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  53131. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  53132. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  53133. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53134. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  53135. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53136. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  53137. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53138. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  53139. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53140. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  53141. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  53142. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  53143. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  53144. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  53145. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  53146. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53147. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53148. buf = NULL;
  53149. buf = NULL;
  53150. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  53151. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53152. DYNAMIC_TYPE_TMP_BUFFER));
  53153. p = buf;
  53154. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  53155. p = NULL;
  53156. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  53157. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53158. p = NULL;
  53159. /* Bad point is also an invalid private key. */
  53160. tmp = octBad;
  53161. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  53162. tmp = buf;
  53163. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  53164. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53165. buf = NULL;
  53166. buf = NULL;
  53167. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  53168. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53169. DYNAMIC_TYPE_TMP_BUFFER));
  53170. p = buf;
  53171. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  53172. p = NULL;
  53173. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  53174. tmp = buf;
  53175. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  53176. tmp = octBad;
  53177. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  53178. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  53179. ExpectIntEQ(EC_KEY_check_key(key), 1);
  53180. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  53181. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  53182. EC_KEY_free(key);
  53183. EC_KEY_free(copy);
  53184. #endif
  53185. return EXPECT_RESULT();
  53186. }
  53187. static int test_wolfSSL_EC_curve(void)
  53188. {
  53189. EXPECT_DECLS;
  53190. #if defined(OPENSSL_EXTRA)
  53191. int nid = NID_secp160k1;
  53192. const char* nid_name = NULL;
  53193. ExpectNull(EC_curve_nid2nist(NID_sha256));
  53194. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  53195. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  53196. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  53197. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  53198. #endif
  53199. return EXPECT_RESULT();
  53200. }
  53201. static int test_wolfSSL_EC_KEY_dup(void)
  53202. {
  53203. EXPECT_DECLS;
  53204. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  53205. WOLFSSL_EC_KEY* ecKey = NULL;
  53206. WOLFSSL_EC_KEY* dupKey = NULL;
  53207. ecc_key* srcKey = NULL;
  53208. ecc_key* destKey = NULL;
  53209. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53210. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53211. /* Valid cases */
  53212. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53213. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  53214. /* Compare pubkey */
  53215. if (ecKey != NULL) {
  53216. srcKey = (ecc_key*)ecKey->internal;
  53217. }
  53218. if (dupKey != NULL) {
  53219. destKey = (ecc_key*)dupKey->internal;
  53220. }
  53221. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  53222. /* compare EC_GROUP */
  53223. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  53224. /* compare EC_POINT */
  53225. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  53226. dupKey->pub_key, NULL), MP_EQ);
  53227. /* compare BIGNUM */
  53228. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  53229. wolfSSL_EC_KEY_free(dupKey);
  53230. dupKey = NULL;
  53231. /* Invalid cases */
  53232. /* NULL key */
  53233. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  53234. /* NULL ecc_key */
  53235. if (ecKey != NULL) {
  53236. wc_ecc_free((ecc_key*)ecKey->internal);
  53237. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  53238. ecKey->internal = NULL; /* Set ecc_key to NULL */
  53239. }
  53240. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53241. wolfSSL_EC_KEY_free(ecKey);
  53242. ecKey = NULL;
  53243. wolfSSL_EC_KEY_free(dupKey);
  53244. dupKey = NULL;
  53245. /* NULL Group */
  53246. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53247. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53248. if (ecKey != NULL) {
  53249. wolfSSL_EC_GROUP_free(ecKey->group);
  53250. ecKey->group = NULL; /* Set group to NULL */
  53251. }
  53252. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53253. wolfSSL_EC_KEY_free(ecKey);
  53254. ecKey = NULL;
  53255. wolfSSL_EC_KEY_free(dupKey);
  53256. dupKey = NULL;
  53257. /* NULL public key */
  53258. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53259. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53260. if (ecKey != NULL) {
  53261. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  53262. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  53263. }
  53264. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53265. if (ecKey != NULL) {
  53266. wolfSSL_EC_POINT_free(ecKey->pub_key);
  53267. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  53268. }
  53269. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53270. wolfSSL_EC_KEY_free(ecKey);
  53271. ecKey = NULL;
  53272. wolfSSL_EC_KEY_free(dupKey);
  53273. dupKey = NULL;
  53274. /* NULL private key */
  53275. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53276. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53277. if (ecKey != NULL) {
  53278. wolfSSL_BN_free(ecKey->priv_key);
  53279. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  53280. }
  53281. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53282. wolfSSL_EC_KEY_free(ecKey);
  53283. ecKey = NULL;
  53284. wolfSSL_EC_KEY_free(dupKey);
  53285. dupKey = NULL;
  53286. /* Test EC_KEY_up_ref */
  53287. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53288. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  53289. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  53290. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  53291. /* reference count doesn't follow duplicate */
  53292. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53293. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  53294. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  53295. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  53296. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  53297. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  53298. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  53299. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  53300. #endif
  53301. return EXPECT_RESULT();
  53302. }
  53303. static int test_wolfSSL_EC_KEY_set_group(void)
  53304. {
  53305. EXPECT_DECLS;
  53306. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  53307. defined(OPENSSL_EXTRA)
  53308. EC_KEY *key = NULL;
  53309. EC_GROUP *group = NULL;
  53310. const EC_GROUP *group2 = NULL;
  53311. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53312. ExpectNotNull(key = EC_KEY_new());
  53313. ExpectNull(EC_KEY_get0_group(NULL));
  53314. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  53315. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  53316. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  53317. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  53318. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  53319. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  53320. EC_GROUP_free(group);
  53321. EC_KEY_free(key);
  53322. #endif
  53323. return EXPECT_RESULT();
  53324. }
  53325. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  53326. {
  53327. EXPECT_DECLS;
  53328. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53329. BIO* bio = NULL;
  53330. EC_KEY* key = NULL;
  53331. /* Error condition: NULL key. */
  53332. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  53333. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  53334. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  53335. /* Conversion form defaults to uncompressed. */
  53336. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53337. #ifdef HAVE_COMP_KEY
  53338. /* Explicitly set to compressed. */
  53339. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  53340. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  53341. #else
  53342. /* Will still work just won't change anything. */
  53343. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  53344. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53345. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  53346. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53347. #endif
  53348. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  53349. BIO_free(bio);
  53350. EC_KEY_free(key);
  53351. #endif
  53352. return EXPECT_RESULT();
  53353. }
  53354. static int test_wolfSSL_EC_KEY_private_key(void)
  53355. {
  53356. EXPECT_DECLS;
  53357. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53358. WOLFSSL_EC_KEY* key = NULL;
  53359. WOLFSSL_BIGNUM* priv = NULL;
  53360. WOLFSSL_BIGNUM* priv2 = NULL;
  53361. WOLFSSL_BIGNUM* bn;
  53362. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53363. ExpectNotNull(priv = wolfSSL_BN_new());
  53364. ExpectNotNull(priv2 = wolfSSL_BN_new());
  53365. ExpectIntNE(BN_set_word(priv, 2), 0);
  53366. ExpectIntNE(BN_set_word(priv2, 2), 0);
  53367. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  53368. /* No private key set. */
  53369. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  53370. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  53371. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  53372. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  53373. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  53374. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53375. ExpectPtrNE(bn, priv);
  53376. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  53377. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53378. ExpectPtrNE(bn, priv2);
  53379. wolfSSL_BN_free(priv2);
  53380. wolfSSL_BN_free(priv);
  53381. wolfSSL_EC_KEY_free(key);
  53382. #endif
  53383. return EXPECT_RESULT();
  53384. }
  53385. static int test_wolfSSL_EC_KEY_public_key(void)
  53386. {
  53387. EXPECT_DECLS;
  53388. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53389. WOLFSSL_EC_KEY* key = NULL;
  53390. WOLFSSL_EC_POINT* pub = NULL;
  53391. WOLFSSL_EC_POINT* point = NULL;
  53392. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53393. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  53394. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  53395. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  53396. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  53397. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  53398. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  53399. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  53400. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  53401. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  53402. ExpectPtrEq(point, pub);
  53403. wolfSSL_EC_KEY_free(key);
  53404. #endif
  53405. return EXPECT_RESULT();
  53406. }
  53407. static int test_wolfSSL_EC_KEY_print_fp(void)
  53408. {
  53409. EXPECT_DECLS;
  53410. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  53411. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  53412. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  53413. !defined(NO_STDIO_FILESYSTEM)
  53414. EC_KEY* key = NULL;
  53415. /* Bad file pointer. */
  53416. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  53417. /* NULL key. */
  53418. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  53419. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  53420. /* Negative indent. */
  53421. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  53422. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53423. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  53424. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53425. wolfSSL_EC_KEY_free(key);
  53426. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  53427. NID_X9_62_prime256v1)));
  53428. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  53429. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53430. wolfSSL_EC_KEY_free(key);
  53431. #endif
  53432. return EXPECT_RESULT();
  53433. }
  53434. static int test_wolfSSL_EC_get_builtin_curves(void)
  53435. {
  53436. EXPECT_DECLS;
  53437. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  53438. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53439. EC_builtin_curve* curves = NULL;
  53440. size_t crv_len = 0;
  53441. size_t i = 0;
  53442. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  53443. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  53444. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  53445. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  53446. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  53447. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  53448. if (curves[i].comment != NULL) {
  53449. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  53450. }
  53451. }
  53452. if (crv_len > 1) {
  53453. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  53454. }
  53455. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53456. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53457. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  53458. return EXPECT_RESULT();
  53459. }
  53460. static int test_wolfSSL_ECDSA_SIG(void)
  53461. {
  53462. EXPECT_DECLS;
  53463. #ifdef OPENSSL_EXTRA
  53464. WOLFSSL_ECDSA_SIG* sig = NULL;
  53465. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  53466. WOLFSSL_BIGNUM* r = NULL;
  53467. WOLFSSL_BIGNUM* s = NULL;
  53468. const WOLFSSL_BIGNUM* r2 = NULL;
  53469. const WOLFSSL_BIGNUM* s2 = NULL;
  53470. const unsigned char* cp = NULL;
  53471. unsigned char* p = NULL;
  53472. unsigned char outSig[8];
  53473. unsigned char sigData[8] =
  53474. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  53475. unsigned char sigDataBad[8] =
  53476. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  53477. wolfSSL_ECDSA_SIG_free(NULL);
  53478. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  53479. ExpectNotNull(r = wolfSSL_BN_new());
  53480. ExpectNotNull(s = wolfSSL_BN_new());
  53481. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  53482. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  53483. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  53484. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  53485. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  53486. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  53487. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  53488. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  53489. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  53490. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  53491. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  53492. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  53493. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  53494. r2 = NULL;
  53495. s2 = NULL;
  53496. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  53497. ExpectNull(r2);
  53498. ExpectNull(s2);
  53499. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  53500. if (EXPECT_FAIL()) {
  53501. wolfSSL_BN_free(r);
  53502. wolfSSL_BN_free(s);
  53503. }
  53504. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  53505. ExpectPtrEq(r2, r);
  53506. ExpectPtrEq(s2, s);
  53507. r2 = NULL;
  53508. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  53509. ExpectPtrEq(r2, r);
  53510. s2 = NULL;
  53511. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  53512. ExpectPtrEq(s2, s);
  53513. /* r and s are freed when sig is freed. */
  53514. wolfSSL_ECDSA_SIG_free(sig);
  53515. sig = NULL;
  53516. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  53517. cp = sigDataBad;
  53518. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  53519. cp = sigData;
  53520. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  53521. ExpectIntEQ((cp == sigData + 8), 1);
  53522. cp = sigData;
  53523. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  53524. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  53525. ExpectIntEQ((sig == sig2), 1);
  53526. cp = outSig;
  53527. p = outSig;
  53528. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  53529. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  53530. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  53531. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  53532. ExpectIntEQ((p == outSig + 8), 1);
  53533. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  53534. wolfSSL_ECDSA_SIG_free(sig);
  53535. #endif
  53536. return EXPECT_RESULT();
  53537. }
  53538. static int test_ECDSA_size_sign(void)
  53539. {
  53540. EXPECT_DECLS;
  53541. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  53542. EC_KEY* key = NULL;
  53543. ECDSA_SIG* ecdsaSig = NULL;
  53544. int id;
  53545. byte hash[WC_MAX_DIGEST_SIZE];
  53546. byte hash2[WC_MAX_DIGEST_SIZE];
  53547. byte sig[ECC_MAX_SIG_SIZE];
  53548. unsigned int sigSz = sizeof(sig);
  53549. XMEMSET(hash, 123, sizeof(hash));
  53550. XMEMSET(hash2, 234, sizeof(hash2));
  53551. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  53552. ExpectIntEQ(id, ECC_SECP256R1);
  53553. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53554. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  53555. ExpectIntGE(ECDSA_size(NULL), 0);
  53556. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  53557. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  53558. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  53559. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  53560. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  53561. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  53562. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  53563. ExpectIntGE(ECDSA_size(key), sigSz);
  53564. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  53565. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  53566. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  53567. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  53568. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  53569. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  53570. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  53571. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  53572. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  53573. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  53574. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  53575. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  53576. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  53577. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  53578. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  53579. ECDSA_SIG_free(ecdsaSig);
  53580. EC_KEY_free(key);
  53581. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  53582. return EXPECT_RESULT();
  53583. }
  53584. static int test_ECDH_compute_key(void)
  53585. {
  53586. EXPECT_DECLS;
  53587. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  53588. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53589. EC_KEY* key1 = NULL;
  53590. EC_KEY* key2 = NULL;
  53591. EC_POINT* pub1 = NULL;
  53592. EC_POINT* pub2 = NULL;
  53593. byte secret1[32];
  53594. byte secret2[32];
  53595. int i;
  53596. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53597. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  53598. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  53599. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53600. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  53601. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  53602. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  53603. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  53604. 0);
  53605. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  53606. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  53607. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  53608. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  53609. 0);
  53610. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  53611. 0);
  53612. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  53613. NULL), 0);
  53614. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  53615. sizeof(secret1));
  53616. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  53617. sizeof(secret2));
  53618. for (i = 0; i < (int)sizeof(secret1); i++) {
  53619. ExpectIntEQ(secret1[i], secret2[i]);
  53620. }
  53621. EC_KEY_free(key2);
  53622. EC_KEY_free(key1);
  53623. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  53624. * !WOLF_CRYPTO_CB_ONLY_ECC */
  53625. return EXPECT_RESULT();
  53626. }
  53627. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  53628. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  53629. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  53630. !defined(NO_ASN_TIME)
  53631. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  53632. int expectedDerSz)
  53633. {
  53634. EXPECT_DECLS;
  53635. X509* x509 = NULL;
  53636. BIGNUM* serial_number = NULL;
  53637. X509_NAME* name = NULL;
  53638. time_t epoch_off = 0;
  53639. ASN1_INTEGER* asn1_serial_number;
  53640. long not_before, not_after;
  53641. int derSz;
  53642. ExpectNotNull(x509 = X509_new());
  53643. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  53644. ExpectNotNull(serial_number = BN_new());
  53645. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  53646. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  53647. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  53648. /* version 3 */
  53649. ExpectIntNE(X509_set_version(x509, 2L), 0);
  53650. ExpectNotNull(name = X509_NAME_new());
  53651. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  53652. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  53653. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  53654. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  53655. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  53656. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  53657. not_before = (long)wc_Time(NULL);
  53658. not_after = not_before + (365 * 24 * 60 * 60);
  53659. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  53660. &epoch_off));
  53661. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  53662. &epoch_off));
  53663. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  53664. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  53665. ExpectIntGE(derSz, expectedDerSz);
  53666. BN_free(serial_number);
  53667. X509_NAME_free(name);
  53668. X509_free(x509);
  53669. return EXPECT_RESULT();
  53670. }
  53671. #endif
  53672. static int test_openssl_generate_key_and_cert(void)
  53673. {
  53674. EXPECT_DECLS;
  53675. #if defined(OPENSSL_EXTRA)
  53676. int expectedDerSz;
  53677. EVP_PKEY* pkey = NULL;
  53678. #ifdef HAVE_ECC
  53679. EC_KEY* ec_key = NULL;
  53680. #endif
  53681. #if !defined(NO_RSA)
  53682. int key_length = 2048;
  53683. BIGNUM* exponent = NULL;
  53684. RSA* rsa = NULL;
  53685. ExpectNotNull(pkey = EVP_PKEY_new());
  53686. ExpectNotNull(exponent = BN_new());
  53687. ExpectNotNull(rsa = RSA_new());
  53688. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  53689. #ifndef WOLFSSL_KEY_GEN
  53690. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  53691. #if defined(USE_CERT_BUFFERS_1024)
  53692. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  53693. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  53694. key_length = 1024;
  53695. #elif defined(USE_CERT_BUFFERS_2048)
  53696. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  53697. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  53698. #else
  53699. RSA_free(rsa);
  53700. rsa = NULL;
  53701. #endif
  53702. #else
  53703. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  53704. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  53705. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  53706. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  53707. #endif
  53708. if (rsa) {
  53709. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  53710. if (EXPECT_FAIL()) {
  53711. RSA_free(rsa);
  53712. }
  53713. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  53714. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  53715. expectedDerSz = 743;
  53716. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  53717. expectedDerSz), TEST_SUCCESS);
  53718. #endif
  53719. }
  53720. EVP_PKEY_free(pkey);
  53721. pkey = NULL;
  53722. BN_free(exponent);
  53723. #endif /* !NO_RSA */
  53724. #ifdef HAVE_ECC
  53725. ExpectNotNull(pkey = EVP_PKEY_new());
  53726. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53727. #ifndef NO_WOLFSSL_STUB
  53728. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  53729. #endif
  53730. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  53731. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  53732. if (EXPECT_FAIL()) {
  53733. EC_KEY_free(ec_key);
  53734. }
  53735. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  53736. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  53737. expectedDerSz = 344;
  53738. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  53739. TEST_SUCCESS);
  53740. #endif
  53741. EVP_PKEY_free(pkey);
  53742. #endif /* HAVE_ECC */
  53743. (void)pkey;
  53744. (void)expectedDerSz;
  53745. #endif /* OPENSSL_EXTRA */
  53746. return EXPECT_RESULT();
  53747. }
  53748. static int test_stubs_are_stubs(void)
  53749. {
  53750. EXPECT_DECLS;
  53751. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  53752. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  53753. WOLFSSL_CTX* ctx = NULL;
  53754. WOLFSSL_CTX* ctxN = NULL;
  53755. #ifndef NO_WOLFSSL_CLIENT
  53756. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53757. #elif !defined(NO_WOLFSSL_SERVER)
  53758. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  53759. #endif
  53760. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  53761. ExpectIntEQ((int) x(z), 0)
  53762. /* test logic, all stubs return same result regardless of ctx being NULL
  53763. * as there are no sanity checks, it's just a stub! If at some
  53764. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  53765. * if invalid inputs are supplied. Test calling both
  53766. * with and without valid inputs, if a stub functionality remains unchanged.
  53767. */
  53768. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  53769. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  53770. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  53771. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  53772. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  53773. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  53774. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  53775. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  53776. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  53777. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  53778. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  53779. wolfSSL_CTX_free(ctx);
  53780. ctx = NULL;
  53781. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  53782. * !NO_WOLFSSL_SERVER) */
  53783. return EXPECT_RESULT();
  53784. }
  53785. static int test_CONF_modules_xxx(void)
  53786. {
  53787. int res = TEST_SKIPPED;
  53788. #if defined(OPENSSL_EXTRA)
  53789. CONF_modules_free();
  53790. CONF_modules_unload(0);
  53791. CONF_modules_unload(1);
  53792. CONF_modules_unload(-1);
  53793. res = TEST_SUCCESS;
  53794. #endif /* OPENSSL_EXTRA */
  53795. return res;
  53796. }
  53797. static int test_CRYPTO_set_dynlock_xxx(void)
  53798. {
  53799. int res = TEST_SKIPPED;
  53800. #if defined(OPENSSL_EXTRA)
  53801. CRYPTO_set_dynlock_create_callback(
  53802. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  53803. CRYPTO_set_dynlock_create_callback(
  53804. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  53805. CRYPTO_set_dynlock_destroy_callback(
  53806. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  53807. CRYPTO_set_dynlock_destroy_callback(
  53808. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  53809. CRYPTO_set_dynlock_lock_callback(
  53810. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  53811. CRYPTO_set_dynlock_lock_callback(
  53812. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  53813. res = TEST_SUCCESS;
  53814. #endif /* OPENSSL_EXTRA */
  53815. return res;
  53816. }
  53817. static int test_CRYPTO_THREADID_xxx(void)
  53818. {
  53819. EXPECT_DECLS;
  53820. #if defined(OPENSSL_EXTRA)
  53821. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  53822. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  53823. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  53824. #endif /* OPENSSL_EXTRA */
  53825. return EXPECT_RESULT();
  53826. }
  53827. static int test_ENGINE_cleanup(void)
  53828. {
  53829. int res = TEST_SKIPPED;
  53830. #if defined(OPENSSL_EXTRA)
  53831. ENGINE_cleanup();
  53832. res = TEST_SUCCESS;
  53833. #endif /* OPENSSL_EXTRA */
  53834. return res;
  53835. }
  53836. static int test_wolfSSL_CTX_LoadCRL(void)
  53837. {
  53838. EXPECT_DECLS;
  53839. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  53840. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  53841. WOLFSSL_CTX* ctx = NULL;
  53842. WOLFSSL* ssl = NULL;
  53843. const char* badPath = "dummypath";
  53844. const char* validPath = "./certs/crl";
  53845. const char* validFilePath = "./certs/crl/cliCrl.pem";
  53846. const char* issuerCert = "./certs/client-cert.pem";
  53847. int derType = WOLFSSL_FILETYPE_ASN1;
  53848. int pemType = WOLFSSL_FILETYPE_PEM;
  53849. #ifdef HAVE_CRL_MONITOR
  53850. int monitor = WOLFSSL_CRL_MONITOR;
  53851. #else
  53852. int monitor = 0;
  53853. #endif
  53854. WOLFSSL_CERT_MANAGER* cm = NULL;
  53855. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53856. BAD_FUNC_ARG)
  53857. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53858. NOT_COMPILED_IN)
  53859. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53860. WOLFSSL_SUCCESS)
  53861. #ifndef NO_WOLFSSL_CLIENT
  53862. #define NEW_CTX(ctx) ExpectNotNull( \
  53863. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  53864. #elif !defined(NO_WOLFSSL_SERVER)
  53865. #define NEW_CTX(ctx) ExpectNotNull( \
  53866. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  53867. #else
  53868. #define NEW_CTX(ctx) return
  53869. #endif
  53870. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53871. NEW_CTX(ctx);
  53872. #ifndef HAVE_CRL_MONITOR
  53873. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  53874. wolfSSL_CTX_free(ctx);
  53875. NEW_CTX(ctx);
  53876. #endif
  53877. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53878. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  53879. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  53880. wolfSSL_CTX_free(ctx);
  53881. ctx = NULL;
  53882. NEW_CTX(ctx);
  53883. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53884. WOLFSSL_SUCCESS);
  53885. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  53886. wolfSSL_CTX_free(ctx);
  53887. ctx = NULL;
  53888. NEW_CTX(ctx);
  53889. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53890. WOLFSSL_SUCCESS);
  53891. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53892. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  53893. wolfSSL_free(ssl);
  53894. ssl = NULL;
  53895. wolfSSL_CTX_free(ctx);
  53896. ctx = NULL;
  53897. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53898. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  53899. WOLFSSL_SUCCESS);
  53900. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  53901. WOLFSSL_SUCCESS);
  53902. wolfSSL_CertManagerFree(cm);
  53903. #endif
  53904. return EXPECT_RESULT();
  53905. }
  53906. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  53907. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  53908. {
  53909. EXPECT_DECLS;
  53910. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  53911. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  53912. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53913. return EXPECT_RESULT();
  53914. }
  53915. #endif
  53916. static int test_multiple_crls_same_issuer(void)
  53917. {
  53918. EXPECT_DECLS;
  53919. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  53920. test_ssl_cbf client_cbs, server_cbs;
  53921. struct {
  53922. const char* server_cert;
  53923. const char* server_key;
  53924. } test_params[] = {
  53925. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  53926. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  53927. };
  53928. size_t i;
  53929. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  53930. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53931. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53932. server_cbs.certPemFile = test_params[i].server_cert;
  53933. server_cbs.keyPemFile = test_params[i].server_key;
  53934. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  53935. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  53936. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  53937. &server_cbs, NULL), TEST_FAIL);
  53938. }
  53939. #endif
  53940. return EXPECT_RESULT();
  53941. }
  53942. static int test_SetTmpEC_DHE_Sz(void)
  53943. {
  53944. EXPECT_DECLS;
  53945. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  53946. WOLFSSL_CTX *ctx = NULL;
  53947. WOLFSSL *ssl = NULL;
  53948. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53949. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  53950. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53951. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  53952. wolfSSL_free(ssl);
  53953. wolfSSL_CTX_free(ctx);
  53954. #endif
  53955. return EXPECT_RESULT();
  53956. }
  53957. static int test_wolfSSL_CTX_get0_privatekey(void)
  53958. {
  53959. EXPECT_DECLS;
  53960. #ifdef OPENSSL_ALL
  53961. WOLFSSL_CTX* ctx = NULL;
  53962. (void)ctx;
  53963. #ifndef NO_RSA
  53964. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53965. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53966. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53967. WOLFSSL_FILETYPE_PEM));
  53968. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53969. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53970. WOLFSSL_FILETYPE_PEM));
  53971. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53972. wolfSSL_CTX_free(ctx);
  53973. ctx = NULL;
  53974. #endif
  53975. #ifdef HAVE_ECC
  53976. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53977. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53978. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  53979. WOLFSSL_FILETYPE_PEM));
  53980. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53981. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  53982. WOLFSSL_FILETYPE_PEM));
  53983. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53984. wolfSSL_CTX_free(ctx);
  53985. #endif
  53986. #endif
  53987. return EXPECT_RESULT();
  53988. }
  53989. static int test_wolfSSL_dtls_set_mtu(void)
  53990. {
  53991. EXPECT_DECLS;
  53992. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  53993. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  53994. !defined(WOLFSSL_NO_TLS12)
  53995. WOLFSSL_CTX* ctx = NULL;
  53996. WOLFSSL* ssl = NULL;
  53997. const char* testCertFile;
  53998. const char* testKeyFile;
  53999. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  54000. #ifndef NO_RSA
  54001. testCertFile = svrCertFile;
  54002. testKeyFile = svrKeyFile;
  54003. #elif defined(HAVE_ECC)
  54004. testCertFile = eccCertFile;
  54005. testKeyFile = eccKeyFile;
  54006. #endif
  54007. if (testCertFile != NULL && testKeyFile != NULL) {
  54008. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  54009. WOLFSSL_FILETYPE_PEM));
  54010. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54011. WOLFSSL_FILETYPE_PEM));
  54012. }
  54013. ExpectNotNull(ssl = wolfSSL_new(ctx));
  54014. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  54015. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  54016. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  54017. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  54018. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  54019. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  54020. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  54021. wolfSSL_free(ssl);
  54022. wolfSSL_CTX_free(ctx);
  54023. #endif
  54024. return EXPECT_RESULT();
  54025. }
  54026. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  54027. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  54028. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  54029. enum HandShakeType hsType, word16 length)
  54030. {
  54031. size_t idx = 0;
  54032. byte* l;
  54033. /* record layer */
  54034. /* handshake type */
  54035. out[idx++] = handshake;
  54036. /* protocol version */
  54037. out[idx++] = 0xfe;
  54038. out[idx++] = 0xfd; /* DTLS 1.2 */
  54039. /* epoch 0 */
  54040. XMEMSET(out + idx, 0, 2);
  54041. idx += 2;
  54042. /* sequence number */
  54043. XMEMSET(out + idx, 0, 6);
  54044. c32toa(seq, out + idx + 2);
  54045. idx += 6;
  54046. /* length in BE */
  54047. if (length)
  54048. c16toa(length, out + idx);
  54049. else
  54050. c16toa(outSz - idx - 2, out + idx);
  54051. idx += 2;
  54052. /* handshake layer */
  54053. /* handshake type */
  54054. out[idx++] = (byte)hsType;
  54055. /* length */
  54056. l = out + idx;
  54057. idx += 3;
  54058. /* message seq */
  54059. c16toa(0, out + idx);
  54060. idx += 2;
  54061. /* frag offset */
  54062. c32to24(0, out + idx);
  54063. idx += 3;
  54064. /* frag length */
  54065. c32to24((word32)outSz - (word32)idx - 3, l);
  54066. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  54067. idx += 3;
  54068. XMEMSET(out + idx, 0, outSz - idx);
  54069. }
  54070. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  54071. {
  54072. byte msg[] = "This is a msg for the client";
  54073. byte reply[40];
  54074. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  54075. reply[sizeof(reply) - 1] = '\0';
  54076. fprintf(stderr, "Client message: %s\n", reply);
  54077. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  54078. }
  54079. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  54080. {
  54081. byte ch[50];
  54082. int fd = wolfSSL_get_fd(ssl);
  54083. byte msg[] = "This is a msg for the server";
  54084. byte reply[40];
  54085. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  54086. /* Server should ignore this datagram */
  54087. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  54088. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  54089. /* Server should ignore this datagram */
  54090. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  54091. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  54092. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  54093. reply[sizeof(reply) - 1] = '\0';
  54094. fprintf(stderr, "Server response: %s\n", reply);
  54095. }
  54096. static int test_wolfSSL_dtls_plaintext(void)
  54097. {
  54098. callback_functions func_cb_client;
  54099. callback_functions func_cb_server;
  54100. size_t i;
  54101. struct test_params {
  54102. method_provider client_meth;
  54103. method_provider server_meth;
  54104. ssl_callback on_result_server;
  54105. ssl_callback on_result_client;
  54106. } params[] = {
  54107. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54108. test_wolfSSL_dtls_plaintext_server,
  54109. test_wolfSSL_dtls_plaintext_client},
  54110. };
  54111. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54112. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54113. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54114. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54115. func_cb_server.method = params[i].server_meth;
  54116. func_cb_client.method = params[i].client_meth;
  54117. func_cb_client.on_result = params[i].on_result_client;
  54118. func_cb_server.on_result = params[i].on_result_server;
  54119. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54120. if (!func_cb_client.return_code)
  54121. return TEST_FAIL;
  54122. if (!func_cb_server.return_code)
  54123. return TEST_FAIL;
  54124. }
  54125. return TEST_RES_CHECK(1);
  54126. }
  54127. #else
  54128. static int test_wolfSSL_dtls_plaintext(void) {
  54129. return TEST_SKIPPED;
  54130. }
  54131. #endif
  54132. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  54133. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  54134. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  54135. {
  54136. byte b[1100]; /* buffer for the messages to send */
  54137. size_t idx = 0;
  54138. size_t seq_offset = 0;
  54139. size_t msg_offset = 0;
  54140. int i;
  54141. int fd = wolfSSL_get_fd(ssl);
  54142. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  54143. word32 seq_number = 100; /* start high so server definitely reads this */
  54144. word16 msg_number = 50; /* start high so server has to buffer this */
  54145. AssertIntEQ(ret, 1);
  54146. /* Now let's start spamming the peer with fragments it needs to store */
  54147. XMEMSET(b, -1, sizeof(b));
  54148. /* record layer */
  54149. /* handshake type */
  54150. b[idx++] = 22;
  54151. /* protocol version */
  54152. b[idx++] = 0xfe;
  54153. b[idx++] = 0xfd; /* DTLS 1.2 */
  54154. /* epoch 0 */
  54155. XMEMSET(b + idx, 0, 2);
  54156. idx += 2;
  54157. /* sequence number */
  54158. XMEMSET(b + idx, 0, 6);
  54159. seq_offset = idx + 2; /* increment only the low 32 bits */
  54160. idx += 6;
  54161. /* static length in BE */
  54162. c16toa(42, b + idx);
  54163. idx += 2;
  54164. /* handshake layer */
  54165. /* cert type */
  54166. b[idx++] = 11;
  54167. /* length */
  54168. c32to24(1000, b + idx);
  54169. idx += 3;
  54170. /* message seq */
  54171. c16toa(0, b + idx);
  54172. msg_offset = idx;
  54173. idx += 2;
  54174. /* frag offset */
  54175. c32to24(500, b + idx);
  54176. idx += 3;
  54177. /* frag length */
  54178. c32to24(30, b + idx);
  54179. idx += 3;
  54180. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  54181. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  54182. seq_number++, msg_number++, i++) {
  54183. struct timespec delay;
  54184. XMEMSET(&delay, 0, sizeof(delay));
  54185. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  54186. c32toa(seq_number, b + seq_offset);
  54187. c16toa(msg_number, b + msg_offset);
  54188. ret = (int)send(fd, b, 55, 0);
  54189. nanosleep(&delay, NULL);
  54190. }
  54191. }
  54192. #ifdef WOLFSSL_DTLS13
  54193. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  54194. {
  54195. const word16 sendCountMax = 100;
  54196. byte b[150]; /* buffer for the messages to send */
  54197. size_t idx = 0;
  54198. size_t msg_offset = 0;
  54199. int fd = wolfSSL_get_fd(ssl);
  54200. word16 msg_number = 10; /* start high so server has to buffer this */
  54201. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  54202. AssertIntEQ(ret, 1);
  54203. /* Now let's start spamming the peer with fragments it needs to store */
  54204. XMEMSET(b, -1, sizeof(b));
  54205. /* handshake type */
  54206. b[idx++] = 11;
  54207. /* length */
  54208. c32to24(10000, b + idx);
  54209. idx += 3;
  54210. /* message_seq */
  54211. msg_offset = idx;
  54212. idx += 2;
  54213. /* fragment_offset */
  54214. c32to24(5000, b + idx);
  54215. idx += 3;
  54216. /* fragment_length */
  54217. c32to24(100, b + idx);
  54218. idx += 3;
  54219. /* fragment contents */
  54220. idx += 100;
  54221. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  54222. byte sendBuf[150];
  54223. int sendSz = sizeof(sendBuf);
  54224. struct timespec delay;
  54225. XMEMSET(&delay, 0, sizeof(delay));
  54226. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  54227. c16toa(msg_number, b + msg_offset);
  54228. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  54229. (int)idx, handshake, 0, 0, 0);
  54230. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  54231. nanosleep(&delay, NULL);
  54232. }
  54233. }
  54234. #endif
  54235. static int test_wolfSSL_dtls_fragments(void)
  54236. {
  54237. EXPECT_DECLS;
  54238. callback_functions func_cb_client;
  54239. callback_functions func_cb_server;
  54240. size_t i;
  54241. struct test_params {
  54242. method_provider client_meth;
  54243. method_provider server_meth;
  54244. ssl_callback spammer;
  54245. } params[] = {
  54246. #if !defined(WOLFSSL_NO_TLS12)
  54247. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54248. test_wolfSSL_dtls12_fragments_spammer},
  54249. #endif
  54250. #ifdef WOLFSSL_DTLS13
  54251. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54252. test_wolfSSL_dtls13_fragments_spammer},
  54253. #endif
  54254. };
  54255. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54256. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54257. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54258. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54259. func_cb_server.method = params[i].server_meth;
  54260. func_cb_client.method = params[i].client_meth;
  54261. func_cb_client.ssl_ready = params[i].spammer;
  54262. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54263. ExpectFalse(func_cb_client.return_code);
  54264. ExpectFalse(func_cb_server.return_code);
  54265. /* The socket should be closed by the server resulting in a
  54266. * socket error, fatal error or reading a close notify alert */
  54267. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  54268. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  54269. func_cb_client.last_err != FATAL_ERROR) {
  54270. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  54271. }
  54272. /* Check the server returned an error indicating the msg buffer
  54273. * was full */
  54274. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  54275. if (EXPECT_FAIL())
  54276. break;
  54277. }
  54278. return EXPECT_RESULT();
  54279. }
  54280. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  54281. {
  54282. int fd, ret;
  54283. byte alert_msg[] = {
  54284. 0x15, /* alert type */
  54285. 0xfe, 0xfd, /* version */
  54286. 0x00, 0x00, /* epoch */
  54287. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  54288. 0x00, 0x02, /* length */
  54289. 0x02, /* level: fatal */
  54290. 0x46 /* protocol version */
  54291. };
  54292. fd = wolfSSL_get_fd(ssl);
  54293. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  54294. AssertIntGT(ret, 0);
  54295. }
  54296. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  54297. {
  54298. callback_functions client_cbs, server_cbs;
  54299. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54300. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54301. client_cbs.doUdp = server_cbs.doUdp = 1;
  54302. if (version12) {
  54303. #if !defined(WOLFSSL_NO_TLS12)
  54304. client_cbs.method = wolfDTLSv1_2_client_method;
  54305. server_cbs.method = wolfDTLSv1_2_server_method;
  54306. #else
  54307. return TEST_SKIPPED;
  54308. #endif
  54309. }
  54310. else
  54311. {
  54312. #ifdef WOLFSSL_DTLS13
  54313. client_cbs.method = wolfDTLSv1_3_client_method;
  54314. server_cbs.method = wolfDTLSv1_3_server_method;
  54315. #else
  54316. return TEST_SKIPPED;
  54317. #endif /* WOLFSSL_DTLS13 */
  54318. }
  54319. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  54320. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54321. if (!client_cbs.return_code)
  54322. return TEST_FAIL;
  54323. if (!server_cbs.return_code)
  54324. return TEST_FAIL;
  54325. return TEST_SUCCESS;
  54326. }
  54327. static int test_wolfSSL_ignore_alert_before_cookie(void)
  54328. {
  54329. int ret;
  54330. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  54331. if (ret != 0)
  54332. return ret;
  54333. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  54334. if (ret != 0)
  54335. return ret;
  54336. return 0;
  54337. }
  54338. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  54339. {
  54340. int ret;
  54341. int fd;
  54342. byte bad_msg[] = {
  54343. 0x17, /* app data */
  54344. 0xaa, 0xfd, /* bad version */
  54345. 0x00, 0x01, /* epoch 1 */
  54346. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  54347. 0x00, 0x26, /* length: 38 bytes */
  54348. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  54349. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  54350. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  54351. 0x03, 0x18, 0x72
  54352. };
  54353. fd = wolfSSL_get_fd(ssl);
  54354. AssertIntGE(fd, 0);
  54355. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  54356. AssertIntEQ(ret, sizeof(bad_msg));
  54357. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  54358. AssertIntEQ(ret, sizeof("badrecordtest"));
  54359. }
  54360. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  54361. {
  54362. byte buf[100];
  54363. int ret;
  54364. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  54365. AssertIntGT(ret, 0);
  54366. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  54367. }
  54368. static int _test_wolfSSL_dtls_bad_record(
  54369. method_provider client_method, method_provider server_method)
  54370. {
  54371. callback_functions client_cbs, server_cbs;
  54372. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54373. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54374. client_cbs.doUdp = server_cbs.doUdp = 1;
  54375. client_cbs.method = client_method;
  54376. server_cbs.method = server_method;
  54377. client_cbs.on_result = test_wolfSSL_send_bad_record;
  54378. server_cbs.on_result = test_wolfSSL_read_string;
  54379. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54380. if (!client_cbs.return_code)
  54381. return TEST_FAIL;
  54382. if (!server_cbs.return_code)
  54383. return TEST_FAIL;
  54384. return TEST_SUCCESS;
  54385. }
  54386. static int test_wolfSSL_dtls_bad_record(void)
  54387. {
  54388. int ret = TEST_SUCCESS;
  54389. #if !defined(WOLFSSL_NO_TLS12)
  54390. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  54391. wolfDTLSv1_2_server_method);
  54392. #endif
  54393. #ifdef WOLFSSL_DTLS13
  54394. if (ret == TEST_SUCCESS) {
  54395. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  54396. wolfDTLSv1_3_server_method);
  54397. }
  54398. #endif /* WOLFSSL_DTLS13 */
  54399. return ret;
  54400. }
  54401. #else
  54402. static int test_wolfSSL_dtls_fragments(void) {
  54403. return TEST_SKIPPED;
  54404. }
  54405. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  54406. return TEST_SKIPPED;
  54407. }
  54408. static int test_wolfSSL_dtls_bad_record(void) {
  54409. return TEST_SKIPPED;
  54410. }
  54411. #endif
  54412. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  54413. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  54414. defined(HAVE_IO_TESTS_DEPENDENCIES)
  54415. static byte test_AEAD_fail_decryption = 0;
  54416. static byte test_AEAD_seq_num = 0;
  54417. static byte test_AEAD_done = 0;
  54418. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  54419. {
  54420. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  54421. if (ret > 0) {
  54422. if (test_AEAD_fail_decryption) {
  54423. /* Modify the packet to trigger a decryption failure */
  54424. buf[ret/2] ^= 0xFF;
  54425. if (test_AEAD_fail_decryption == 1)
  54426. test_AEAD_fail_decryption = 0;
  54427. }
  54428. }
  54429. (void)ctx;
  54430. return ret;
  54431. }
  54432. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  54433. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  54434. {
  54435. if (sendLimit)
  54436. w64Zero(sendLimit);
  54437. switch (ssl->specs.bulk_cipher_algorithm) {
  54438. case wolfssl_aes_gcm:
  54439. if (sendLimit)
  54440. *sendLimit = AEAD_AES_LIMIT;
  54441. FALL_THROUGH;
  54442. case wolfssl_chacha:
  54443. if (hardLimit)
  54444. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  54445. if (keyUpdateLimit)
  54446. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  54447. break;
  54448. case wolfssl_aes_ccm:
  54449. if (sendLimit)
  54450. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  54451. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  54452. if (hardLimit)
  54453. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  54454. if (keyUpdateLimit)
  54455. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  54456. }
  54457. else {
  54458. if (hardLimit)
  54459. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  54460. if (keyUpdateLimit)
  54461. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  54462. }
  54463. break;
  54464. default:
  54465. fprintf(stderr, "Unrecognized bulk cipher");
  54466. AssertFalse(1);
  54467. break;
  54468. }
  54469. }
  54470. static void test_AEAD_limit_client(WOLFSSL* ssl)
  54471. {
  54472. int ret;
  54473. int i;
  54474. int didReKey = 0;
  54475. char msgBuf[20];
  54476. w64wrapper hardLimit;
  54477. w64wrapper keyUpdateLimit;
  54478. w64wrapper counter;
  54479. w64wrapper sendLimit;
  54480. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  54481. w64Zero(&counter);
  54482. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  54483. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  54484. for (i = 0; i < 10; i++) {
  54485. /* Test some failed decryptions */
  54486. test_AEAD_fail_decryption = 1;
  54487. w64Increment(&counter);
  54488. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54489. /* Should succeed since decryption failures are dropped */
  54490. AssertIntGT(ret, 0);
  54491. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  54492. }
  54493. test_AEAD_fail_decryption = 1;
  54494. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  54495. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  54496. /* 100 read calls should be enough to complete the key update */
  54497. w64Zero(&counter);
  54498. for (i = 0; i < 100; i++) {
  54499. /* Key update should be sent and negotiated */
  54500. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54501. AssertIntGT(ret, 0);
  54502. /* Epoch after one key update is 4 */
  54503. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  54504. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  54505. didReKey = 1;
  54506. break;
  54507. }
  54508. }
  54509. AssertTrue(didReKey);
  54510. if (!w64IsZero(sendLimit)) {
  54511. /* Test the sending limit for AEAD ciphers */
  54512. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  54513. test_AEAD_seq_num = 1;
  54514. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  54515. AssertIntGT(ret, 0);
  54516. didReKey = 0;
  54517. w64Zero(&counter);
  54518. /* 100 read calls should be enough to complete the key update */
  54519. for (i = 0; i < 100; i++) {
  54520. /* Key update should be sent and negotiated */
  54521. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54522. AssertIntGT(ret, 0);
  54523. /* Epoch after another key update is 5 */
  54524. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  54525. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  54526. didReKey = 1;
  54527. break;
  54528. }
  54529. }
  54530. AssertTrue(didReKey);
  54531. }
  54532. test_AEAD_fail_decryption = 2;
  54533. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  54534. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  54535. /* Connection should fail with a DECRYPT_ERROR */
  54536. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54537. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  54538. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  54539. test_AEAD_done = 1;
  54540. }
  54541. int counter = 0;
  54542. static void test_AEAD_limit_server(WOLFSSL* ssl)
  54543. {
  54544. char msgBuf[] = "Sending data";
  54545. int ret = WOLFSSL_SUCCESS;
  54546. w64wrapper sendLimit;
  54547. SOCKET_T fd = wolfSSL_get_fd(ssl);
  54548. struct timespec delay;
  54549. XMEMSET(&delay, 0, sizeof(delay));
  54550. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  54551. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  54552. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  54553. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  54554. while (!test_AEAD_done && ret > 0) {
  54555. counter++;
  54556. if (test_AEAD_seq_num) {
  54557. /* We need to update the seq number so that we can understand the
  54558. * peer. Otherwise we will incorrectly interpret the seq number. */
  54559. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  54560. AssertNotNull(e);
  54561. e->nextPeerSeqNumber = sendLimit;
  54562. test_AEAD_seq_num = 0;
  54563. }
  54564. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54565. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  54566. nanosleep(&delay, NULL);
  54567. }
  54568. }
  54569. static int test_wolfSSL_dtls_AEAD_limit(void)
  54570. {
  54571. callback_functions func_cb_client;
  54572. callback_functions func_cb_server;
  54573. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54574. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54575. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54576. func_cb_server.method = wolfDTLSv1_3_server_method;
  54577. func_cb_client.method = wolfDTLSv1_3_client_method;
  54578. func_cb_server.on_result = test_AEAD_limit_server;
  54579. func_cb_client.on_result = test_AEAD_limit_client;
  54580. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54581. if (!func_cb_client.return_code)
  54582. return TEST_FAIL;
  54583. if (!func_cb_server.return_code)
  54584. return TEST_FAIL;
  54585. return TEST_SUCCESS;
  54586. }
  54587. #else
  54588. static int test_wolfSSL_dtls_AEAD_limit(void)
  54589. {
  54590. return TEST_SKIPPED;
  54591. }
  54592. #endif
  54593. #if defined(WOLFSSL_DTLS) && \
  54594. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  54595. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  54596. {
  54597. int fd, ret;
  54598. byte ch_msg[] = {
  54599. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  54600. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  54601. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  54602. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  54603. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  54604. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  54605. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  54606. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  54607. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  54608. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  54609. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  54610. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  54611. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  54612. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  54613. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  54614. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  54615. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  54616. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  54617. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  54618. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  54619. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  54620. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  54621. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  54622. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  54623. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  54624. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  54625. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  54626. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  54627. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  54628. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  54629. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  54630. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  54631. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  54632. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  54633. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  54634. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  54635. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  54636. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  54637. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  54638. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  54639. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  54640. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  54641. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  54642. 0x31, 0x68, 0x4c
  54643. };
  54644. fd = wolfSSL_get_fd(ssl);
  54645. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  54646. AssertIntGT(ret, 0);
  54647. /* consume the HRR otherwise handshake will fail */
  54648. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  54649. AssertIntGT(ret, 0);
  54650. }
  54651. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54652. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  54653. {
  54654. int fd, ret;
  54655. byte ch_msh_invalid_cookie[] = {
  54656. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  54657. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  54658. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  54659. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  54660. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  54661. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  54662. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  54663. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  54664. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  54665. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  54666. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  54667. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  54668. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  54669. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  54670. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54671. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54672. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54673. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54674. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54675. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  54676. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  54677. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  54678. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  54679. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  54680. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  54681. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  54682. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  54683. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  54684. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  54685. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  54686. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  54687. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  54688. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  54689. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  54690. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  54691. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  54692. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  54693. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  54694. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  54695. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  54696. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  54697. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  54698. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  54699. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  54700. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  54701. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  54702. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  54703. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  54704. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  54705. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  54706. 0x23, 0x00, 0x00
  54707. };
  54708. byte alert_reply[50];
  54709. byte expected_alert_reply[] = {
  54710. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  54711. 0x02, 0x02, 0x2f
  54712. };
  54713. fd = wolfSSL_get_fd(ssl);
  54714. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  54715. AssertIntGT(ret, 0);
  54716. /* should reply with an illegal_parameter reply */
  54717. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  54718. AssertIntEQ(ret, sizeof(expected_alert_reply));
  54719. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  54720. }
  54721. #endif
  54722. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  54723. {
  54724. #ifndef NO_MD5
  54725. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  54726. #elif !defined(NO_SHA)
  54727. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  54728. #elif !defined(NO_SHA256)
  54729. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  54730. #else
  54731. #error "We need a digest to hash the WOLFSSL object"
  54732. #endif
  54733. byte hashBuf[WC_MAX_DIGEST_SIZE];
  54734. wc_HashAlg hash;
  54735. const TLSX* exts = ssl->extensions;
  54736. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  54737. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  54738. * InitHandshakeHashes */
  54739. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  54740. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  54741. /* Following fields are not important to compare */
  54742. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  54743. sslCopy.buffers.inputBuffer.buffer = NULL;
  54744. sslCopy.buffers.inputBuffer.bufferSize = 0;
  54745. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  54746. sslCopy.buffers.inputBuffer.offset = 0;
  54747. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  54748. sslCopy.buffers.outputBuffer.buffer = NULL;
  54749. sslCopy.buffers.outputBuffer.bufferSize = 0;
  54750. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  54751. sslCopy.buffers.outputBuffer.offset = 0;
  54752. sslCopy.error = 0;
  54753. sslCopy.curSize = 0;
  54754. sslCopy.curStartIdx = 0;
  54755. sslCopy.keys.curSeq_lo = 0;
  54756. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  54757. #ifdef WOLFSSL_DTLS13
  54758. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  54759. sslCopy.dtls13FastTimeout = 0;
  54760. #endif
  54761. sslCopy.keys.dtls_peer_handshake_number = 0;
  54762. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  54763. sslCopy.hsHashes = NULL;
  54764. #ifdef WOLFSSL_ASYNC_IO
  54765. #ifdef WOLFSSL_ASYNC_CRYPT
  54766. sslCopy.asyncDev = NULL;
  54767. #endif
  54768. sslCopy.async = NULL;
  54769. #endif
  54770. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  54771. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  54772. /* hash extension list */
  54773. while (exts != NULL) {
  54774. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  54775. exts = exts->next;
  54776. }
  54777. /* Hash suites */
  54778. if (sslCopy.suites != NULL) {
  54779. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  54780. sizeof(struct Suites)), 0);
  54781. }
  54782. /* Hash hsHashes */
  54783. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  54784. sizeof(*hsHashes)), 0);
  54785. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  54786. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  54787. return MakeWordFromHash(hashBuf);
  54788. }
  54789. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  54790. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  54791. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  54792. char *buf, int sz, void *ctx)
  54793. {
  54794. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  54795. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  54796. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  54797. }
  54798. else {
  54799. return WOLFSSL_CBIO_ERR_WANT_READ;
  54800. }
  54801. }
  54802. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  54803. {
  54804. /* Compare the ssl object before and after one ClientHello msg */
  54805. SOCKET_T fd = wolfSSL_get_fd(ssl);
  54806. int res;
  54807. int err;
  54808. word32 initHash;
  54809. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  54810. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  54811. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  54812. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  54813. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  54814. (void)initHash;
  54815. res = tcp_select(fd, 5);
  54816. /* We are expecting a msg. A timeout indicates failure. */
  54817. AssertIntEQ(res, TEST_RECV_READY);
  54818. res = wolfSSL_accept(ssl);
  54819. err = wolfSSL_get_error(ssl, res);
  54820. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  54821. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  54822. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  54823. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  54824. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  54825. }
  54826. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54827. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  54828. {
  54829. int ret;
  54830. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  54831. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  54832. test_wolfSSL_dtls_compare_stateless(ssl);
  54833. }
  54834. #endif
  54835. static int test_wolfSSL_dtls_stateless(void)
  54836. {
  54837. callback_functions client_cbs, server_cbs;
  54838. size_t i;
  54839. struct {
  54840. method_provider client_meth;
  54841. method_provider server_meth;
  54842. ssl_callback client_ssl_ready;
  54843. ssl_callback server_ssl_ready;
  54844. } test_params[] = {
  54845. #if !defined(WOLFSSL_NO_TLS12)
  54846. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54847. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  54848. #endif
  54849. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54850. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54851. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  54852. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54853. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  54854. #endif
  54855. };
  54856. if (0 == sizeof(test_params)){
  54857. return TEST_SKIPPED;
  54858. }
  54859. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  54860. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54861. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54862. client_cbs.doUdp = server_cbs.doUdp = 1;
  54863. client_cbs.method = test_params[i].client_meth;
  54864. server_cbs.method = test_params[i].server_meth;
  54865. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  54866. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  54867. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54868. if (!client_cbs.return_code)
  54869. return TEST_FAIL;
  54870. if (!server_cbs.return_code)
  54871. return TEST_FAIL;
  54872. }
  54873. return TEST_SUCCESS;
  54874. }
  54875. #else
  54876. static int test_wolfSSL_dtls_stateless(void)
  54877. {
  54878. return TEST_SKIPPED;
  54879. }
  54880. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  54881. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  54882. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  54883. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  54884. !defined(WOLFSSL_NO_CLIENT_AUTH))
  54885. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54886. {
  54887. int ret;
  54888. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  54889. fprintf(stderr, "loading cert %s failed\n", certA);
  54890. fprintf(stderr, "Error: (%d): %s\n", ret,
  54891. wolfSSL_ERR_reason_error_string(ret));
  54892. return -1;
  54893. }
  54894. return 0;
  54895. }
  54896. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54897. {
  54898. int ret;
  54899. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  54900. != WOLFSSL_SUCCESS) {
  54901. fprintf(stderr, "could not verify the cert: %s\n", certA);
  54902. fprintf(stderr, "Error: (%d): %s\n", ret,
  54903. wolfSSL_ERR_reason_error_string(ret));
  54904. return -1;
  54905. }
  54906. else {
  54907. fprintf(stderr, "successfully verified: %s\n", certA);
  54908. }
  54909. return 0;
  54910. }
  54911. #define LOAD_ONE_CA(a, b, c, d) \
  54912. do { \
  54913. (a) = load_ca_into_cm(c, d); \
  54914. if ((a) != 0) \
  54915. return (b); \
  54916. else \
  54917. (b)--; \
  54918. } while(0)
  54919. #define VERIFY_ONE_CERT(a, b, c, d) \
  54920. do { \
  54921. (a) = verify_cert_with_cm(c, d);\
  54922. if ((a) != 0) \
  54923. return (b); \
  54924. else \
  54925. (b)--; \
  54926. } while(0)
  54927. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  54928. {
  54929. int ret;
  54930. int i = -1;
  54931. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  54932. char chainGArr[9][50] = {"certs/ca-cert.pem",
  54933. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  54934. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  54935. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  54936. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  54937. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  54938. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  54939. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  54940. "certs/test-pathlen/chainG-entity.pem"};
  54941. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  54942. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  54943. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  54944. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  54945. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  54946. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  54947. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  54948. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  54949. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  54950. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  54951. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  54952. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  54953. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  54954. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  54955. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  54956. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  54957. /* test validating the entity twice, should have no effect on pathLen since
  54958. * entity/leaf cert */
  54959. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  54960. return ret;
  54961. }
  54962. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  54963. {
  54964. int ret;
  54965. int i = -1;
  54966. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  54967. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  54968. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  54969. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  54970. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  54971. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54972. */
  54973. char chainHArr[6][50] = {"certs/ca-cert.pem",
  54974. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  54975. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  54976. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  54977. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  54978. "certs/test-pathlen/chainH-entity.pem"};
  54979. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  54980. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  54981. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  54982. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  54983. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  54984. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  54985. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  54986. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  54987. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  54988. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  54989. return ret;
  54990. }
  54991. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  54992. {
  54993. int ret;
  54994. int i = -1;
  54995. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  54996. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  54997. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  54998. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  54999. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  55000. */
  55001. char chainIArr[5][50] = {"certs/ca-cert.pem",
  55002. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  55003. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  55004. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  55005. "certs/test-pathlen/chainI-entity.pem"};
  55006. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  55007. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  55008. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  55009. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  55010. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  55011. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  55012. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  55013. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  55014. return ret;
  55015. }
  55016. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  55017. {
  55018. int ret;
  55019. int i = -1;
  55020. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  55021. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  55022. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  55023. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  55024. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  55025. */
  55026. char chainJArr[6][50] = {"certs/ca-cert.pem",
  55027. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  55028. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  55029. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  55030. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  55031. "certs/test-pathlen/chainJ-entity.pem"};
  55032. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  55033. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  55034. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  55035. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  55036. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  55037. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  55038. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  55039. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  55040. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  55041. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  55042. return ret;
  55043. }
  55044. static int test_various_pathlen_chains(void)
  55045. {
  55046. EXPECT_DECLS;
  55047. WOLFSSL_CERT_MANAGER* cm = NULL;
  55048. /* Test chain G (large chain with varying pathLens) */
  55049. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55050. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  55051. ExpectIntEQ(test_chainG(cm), -1);
  55052. #else
  55053. ExpectIntEQ(test_chainG(cm), 0);
  55054. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  55055. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55056. wolfSSL_CertManagerFree(cm);
  55057. /* end test chain G */
  55058. /* Test chain H (5 chain with same pathLens) */
  55059. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55060. ExpectIntLT(test_chainH(cm), 0);
  55061. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55062. wolfSSL_CertManagerFree(cm);
  55063. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55064. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55065. wolfSSL_CertManagerFree(cm);
  55066. /* end test chain H */
  55067. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  55068. * followed by 2 ICA's, should pass) */
  55069. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55070. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  55071. ExpectIntEQ(test_chainI(cm), -1);
  55072. #else
  55073. ExpectIntEQ(test_chainI(cm), 0);
  55074. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  55075. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55076. wolfSSL_CertManagerFree(cm);
  55077. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55078. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55079. wolfSSL_CertManagerFree(cm);
  55080. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  55081. * this time followed by 3 ICA's, should fail */
  55082. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55083. ExpectIntLT(test_chainJ(cm), 0);
  55084. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55085. wolfSSL_CertManagerFree(cm);
  55086. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55087. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55088. wolfSSL_CertManagerFree(cm);
  55089. return EXPECT_RESULT();
  55090. }
  55091. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  55092. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  55093. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  55094. {
  55095. EXPECT_DECLS;
  55096. byte ekm[100] = {0};
  55097. (void)ctx;
  55098. /* Success Cases */
  55099. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55100. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  55101. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55102. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  55103. /* Use some random context */
  55104. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55105. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  55106. /* Failure cases */
  55107. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55108. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  55109. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55110. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  55111. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55112. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  55113. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55114. "extended master secret", XSTR_SIZEOF("extended master secret"),
  55115. NULL, 0, 0), 0);
  55116. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55117. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  55118. return EXPECT_RESULT();
  55119. }
  55120. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  55121. {
  55122. wolfSSL_KeepArrays(ssl);
  55123. return TEST_SUCCESS;
  55124. }
  55125. static int test_export_keying_material(void)
  55126. {
  55127. EXPECT_DECLS;
  55128. test_ssl_cbf serverCb;
  55129. test_ssl_cbf clientCb;
  55130. XMEMSET(&serverCb, 0, sizeof(serverCb));
  55131. XMEMSET(&clientCb, 0, sizeof(clientCb));
  55132. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  55133. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  55134. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  55135. return EXPECT_RESULT();
  55136. }
  55137. #endif /* HAVE_KEYING_MATERIAL */
  55138. static int test_wolfSSL_THREADID_hash(void)
  55139. {
  55140. EXPECT_DECLS;
  55141. #if defined(OPENSSL_EXTRA)
  55142. CRYPTO_THREADID id;
  55143. CRYPTO_THREADID_current(NULL);
  55144. /* Hash result is unsigned long. */
  55145. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  55146. XMEMSET(&id, 0, sizeof(id));
  55147. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  55148. #endif /* OPENSSL_EXTRA */
  55149. return EXPECT_RESULT();
  55150. }
  55151. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  55152. {
  55153. EXPECT_DECLS;
  55154. #if defined(OPENSSL_EXTRA)
  55155. WOLFSSL_CTX* ctx = NULL;
  55156. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  55157. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  55158. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  55159. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  55160. #endif /* OPENSSL_EXTRA */
  55161. return EXPECT_RESULT();
  55162. }
  55163. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  55164. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  55165. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  55166. {
  55167. EXPECT_DECLS;
  55168. callback_functions* callbacks = NULL;
  55169. WOLFSSL_CTX* ctx = NULL;
  55170. WOLFSSL* ssl = NULL;
  55171. SOCKET_T sfd = 0;
  55172. SOCKET_T cfd = 0;
  55173. word16 port;
  55174. char msg[] = "I hear you fa shizzle!";
  55175. int len = (int) XSTRLEN(msg);
  55176. char input[1024];
  55177. int ret = 0;
  55178. int err = 0;
  55179. if (!args)
  55180. WOLFSSL_RETURN_FROM_THREAD(0);
  55181. ((func_args*)args)->return_code = TEST_FAIL;
  55182. callbacks = ((func_args*)args)->callbacks;
  55183. ctx = wolfSSL_CTX_new(callbacks->method());
  55184. #if defined(USE_WINDOWS_API)
  55185. port = ((func_args*)args)->signal->port;
  55186. #else
  55187. /* Let tcp_listen assign port */
  55188. port = 0;
  55189. #endif
  55190. #ifdef WOLFSSL_TIRTOS
  55191. fdOpenSession(Task_self());
  55192. #endif
  55193. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  55194. caCertFile, 0));
  55195. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  55196. svrCertFile, WOLFSSL_FILETYPE_PEM));
  55197. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  55198. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  55199. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  55200. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  55201. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55202. #elif !defined(NO_DH)
  55203. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  55204. #endif
  55205. if (callbacks->ctx_ready)
  55206. callbacks->ctx_ready(ctx);
  55207. ssl = wolfSSL_new(ctx);
  55208. ExpectNotNull(ssl);
  55209. /* listen and accept */
  55210. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  55211. CloseSocket(sfd);
  55212. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  55213. if (callbacks->ssl_ready)
  55214. callbacks->ssl_ready(ssl);
  55215. if (EXPECT_SUCCESS()) {
  55216. do {
  55217. err = 0; /* Reset error */
  55218. ret = wolfSSL_accept(ssl);
  55219. if (ret != WOLFSSL_SUCCESS) {
  55220. err = wolfSSL_get_error(ssl, 0);
  55221. }
  55222. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  55223. }
  55224. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55225. /* read and write data */
  55226. XMEMSET(input, 0, sizeof(input));
  55227. while (EXPECT_SUCCESS()) {
  55228. ret = wolfSSL_read(ssl, input, sizeof(input));
  55229. if (ret > 0) {
  55230. break;
  55231. }
  55232. else {
  55233. err = wolfSSL_get_error(ssl,ret);
  55234. if (err == WOLFSSL_ERROR_WANT_READ) {
  55235. continue;
  55236. }
  55237. break;
  55238. }
  55239. }
  55240. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  55241. do {
  55242. ret = wolfSSL_write(ssl, msg, len);
  55243. if (ret > 0) {
  55244. break;
  55245. }
  55246. } while (ret < 0);
  55247. }
  55248. /* bidirectional shutdown */
  55249. while (EXPECT_SUCCESS()) {
  55250. ret = wolfSSL_shutdown(ssl);
  55251. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  55252. if (ret == WOLFSSL_SUCCESS) {
  55253. break;
  55254. }
  55255. }
  55256. if (EXPECT_SUCCESS()) {
  55257. /* wait for the peer to disconnect the tcp connection */
  55258. do {
  55259. ret = wolfSSL_read(ssl, input, sizeof(input));
  55260. err = wolfSSL_get_error(ssl, ret);
  55261. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  55262. }
  55263. /* detect TCP disconnect */
  55264. ExpectIntLE(ret,WOLFSSL_FAILURE);
  55265. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  55266. ((func_args*)args)->return_code = EXPECT_RESULT();
  55267. wolfSSL_free(ssl);
  55268. wolfSSL_CTX_free(ctx);
  55269. CloseSocket(cfd);
  55270. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  55271. wc_ecc_fp_free(); /* free per thread cache */
  55272. #endif
  55273. WOLFSSL_RETURN_FROM_THREAD(0);
  55274. }
  55275. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  55276. {
  55277. EXPECT_DECLS;
  55278. callback_functions* callbacks = NULL;
  55279. WOLFSSL_CTX* ctx = NULL;
  55280. WOLFSSL* ssl = NULL;
  55281. SOCKET_T sfd = 0;
  55282. char msg[] = "hello wolfssl server!";
  55283. int len = (int) XSTRLEN(msg);
  55284. char input[1024];
  55285. int idx;
  55286. int ret, err;
  55287. if (!args)
  55288. WOLFSSL_RETURN_FROM_THREAD(0);
  55289. ((func_args*)args)->return_code = TEST_FAIL;
  55290. callbacks = ((func_args*)args)->callbacks;
  55291. ctx = wolfSSL_CTX_new(callbacks->method());
  55292. #ifdef WOLFSSL_TIRTOS
  55293. fdOpenSession(Task_self());
  55294. #endif
  55295. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  55296. caCertFile, 0));
  55297. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  55298. cliCertFile, WOLFSSL_FILETYPE_PEM));
  55299. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  55300. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  55301. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  55302. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  55303. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  55304. if (EXPECT_SUCCESS()) {
  55305. do {
  55306. err = 0; /* Reset error */
  55307. ret = wolfSSL_connect(ssl);
  55308. if (ret != WOLFSSL_SUCCESS) {
  55309. err = wolfSSL_get_error(ssl, 0);
  55310. }
  55311. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  55312. }
  55313. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  55314. if (EXPECT_SUCCESS()) {
  55315. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  55316. input[idx] = 0;
  55317. }
  55318. }
  55319. if (EXPECT_SUCCESS()) {
  55320. ret = wolfSSL_shutdown(ssl);
  55321. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  55322. ret = wolfSSL_shutdown(ssl);
  55323. }
  55324. }
  55325. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55326. ((func_args*)args)->return_code = EXPECT_RESULT();
  55327. wolfSSL_free(ssl);
  55328. wolfSSL_CTX_free(ctx);
  55329. CloseSocket(sfd);
  55330. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  55331. wc_ecc_fp_free(); /* free per thread cache */
  55332. #endif
  55333. WOLFSSL_RETURN_FROM_THREAD(0);
  55334. }
  55335. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  55336. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  55337. /* This test is to check wolfSSL_read behaves as same as
  55338. * openSSL when it is called after SSL_shutdown completes.
  55339. */
  55340. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  55341. {
  55342. EXPECT_DECLS;
  55343. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  55344. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  55345. tcp_ready ready;
  55346. func_args client_args;
  55347. func_args server_args;
  55348. THREAD_TYPE serverThread;
  55349. THREAD_TYPE clientThread;
  55350. callback_functions server_cbf;
  55351. callback_functions client_cbf;
  55352. #ifdef WOLFSSL_TIRTOS
  55353. fdOpenSession(Task_self());
  55354. #endif
  55355. StartTCP();
  55356. InitTcpReady(&ready);
  55357. #if defined(USE_WINDOWS_API)
  55358. /* use RNG to get random port if using windows */
  55359. ready.port = GetRandomPort();
  55360. #endif
  55361. XMEMSET(&client_args, 0, sizeof(func_args));
  55362. XMEMSET(&server_args, 0, sizeof(func_args));
  55363. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  55364. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  55365. server_cbf.method = wolfTLSv1_2_server_method;
  55366. client_cbf.method = wolfTLSv1_2_client_method;
  55367. server_args.callbacks = &server_cbf;
  55368. client_args.callbacks = &client_cbf;
  55369. server_args.signal = &ready;
  55370. client_args.signal = &ready;
  55371. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  55372. wait_tcp_ready(&server_args);
  55373. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  55374. join_thread(clientThread);
  55375. join_thread(serverThread);
  55376. ExpectTrue(client_args.return_code);
  55377. ExpectTrue(server_args.return_code);
  55378. FreeTcpReady(&ready);
  55379. #endif
  55380. return EXPECT_RESULT();
  55381. }
  55382. static int test_wolfSSL_CTX_get_min_proto_version(void)
  55383. {
  55384. EXPECT_DECLS;
  55385. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  55386. WOLFSSL_CTX *ctx = NULL;
  55387. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55388. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  55389. WOLFSSL_SUCCESS);
  55390. #ifdef WOLFSSL_ALLOW_SSLV3
  55391. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  55392. #else
  55393. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  55394. #endif
  55395. wolfSSL_CTX_free(ctx);
  55396. ctx = NULL;
  55397. #ifdef WOLFSSL_ALLOW_TLSV10
  55398. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  55399. #else
  55400. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55401. #endif
  55402. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  55403. WOLFSSL_SUCCESS);
  55404. #ifdef WOLFSSL_ALLOW_TLSV10
  55405. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  55406. #else
  55407. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  55408. #endif
  55409. wolfSSL_CTX_free(ctx);
  55410. ctx = NULL;
  55411. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55412. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  55413. WOLFSSL_SUCCESS);
  55414. #ifndef NO_OLD_TLS
  55415. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  55416. #else
  55417. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  55418. #endif
  55419. wolfSSL_CTX_free(ctx);
  55420. ctx = NULL;
  55421. #ifndef WOLFSSL_NO_TLS12
  55422. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  55423. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  55424. WOLFSSL_SUCCESS);
  55425. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  55426. wolfSSL_CTX_free(ctx);
  55427. ctx = NULL;
  55428. #endif
  55429. #ifdef WOLFSSL_TLS13
  55430. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  55431. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  55432. WOLFSSL_SUCCESS);
  55433. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  55434. wolfSSL_CTX_free(ctx);
  55435. ctx = NULL;
  55436. #endif
  55437. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  55438. return EXPECT_RESULT();
  55439. }
  55440. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55441. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55442. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55443. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  55444. static int test_wolfSSL_set_SSL_CTX(void)
  55445. {
  55446. EXPECT_DECLS;
  55447. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  55448. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  55449. !defined(NO_RSA)
  55450. WOLFSSL_CTX *ctx1 = NULL;
  55451. WOLFSSL_CTX *ctx2 = NULL;
  55452. WOLFSSL *ssl = NULL;
  55453. const byte *session_id1 = (const byte *)"CTX1";
  55454. const byte *session_id2 = (const byte *)"CTX2";
  55455. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  55456. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  55457. WOLFSSL_FILETYPE_PEM));
  55458. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  55459. WOLFSSL_FILETYPE_PEM));
  55460. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  55461. WOLFSSL_SUCCESS);
  55462. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  55463. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  55464. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  55465. WOLFSSL_SUCCESS);
  55466. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  55467. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  55468. WOLFSSL_FILETYPE_PEM));
  55469. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  55470. WOLFSSL_FILETYPE_PEM));
  55471. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  55472. WOLFSSL_SUCCESS);
  55473. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  55474. WOLFSSL_SUCCESS);
  55475. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  55476. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  55477. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  55478. WOLFSSL_SUCCESS);
  55479. #ifdef HAVE_SESSION_TICKET
  55480. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  55481. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  55482. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  55483. #endif
  55484. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  55485. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  55486. #ifdef WOLFSSL_INT_H
  55487. #ifdef WOLFSSL_SESSION_ID_CTX
  55488. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  55489. #endif
  55490. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  55491. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  55492. #endif
  55493. #ifdef HAVE_SESSION_TICKET
  55494. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  55495. #endif
  55496. /* Set the ctx1 that has TLSv1.3 as max proto version */
  55497. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  55498. /* MUST not change proto versions of ssl */
  55499. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  55500. #ifdef HAVE_SESSION_TICKET
  55501. /* MUST not change */
  55502. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  55503. #endif
  55504. /* MUST change */
  55505. #ifdef WOLFSSL_INT_H
  55506. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  55507. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  55508. #ifdef WOLFSSL_SESSION_ID_CTX
  55509. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  55510. #endif
  55511. #endif
  55512. wolfSSL_free(ssl);
  55513. wolfSSL_CTX_free(ctx1);
  55514. wolfSSL_CTX_free(ctx2);
  55515. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  55516. return EXPECT_RESULT();
  55517. }
  55518. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55519. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55520. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55521. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  55522. static int test_wolfSSL_security_level(void)
  55523. {
  55524. EXPECT_DECLS;
  55525. #if defined(OPENSSL_EXTRA)
  55526. SSL_CTX *ctx = NULL;
  55527. #ifdef WOLFSSL_TLS13
  55528. #ifdef NO_WOLFSSL_SERVER
  55529. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55530. #else
  55531. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55532. #endif
  55533. SSL_CTX_set_security_level(NULL, 1);
  55534. SSL_CTX_set_security_level(ctx, 1);
  55535. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  55536. /* Stub so nothing happens. */
  55537. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  55538. SSL_CTX_free(ctx);
  55539. #else
  55540. (void)ctx;
  55541. #endif
  55542. #endif
  55543. return EXPECT_RESULT();
  55544. }
  55545. static int test_wolfSSL_SSL_in_init(void)
  55546. {
  55547. EXPECT_DECLS;
  55548. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  55549. SSL_CTX* ctx = NULL;
  55550. SSL* ssl = NULL;
  55551. const char* testCertFile;
  55552. const char* testKeyFile;
  55553. #ifdef WOLFSSL_TLS13
  55554. #ifdef NO_WOLFSSL_SERVER
  55555. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55556. #else
  55557. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55558. #endif
  55559. #else
  55560. #ifdef NO_WOLFSSL_SERVER
  55561. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55562. #else
  55563. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55564. #endif
  55565. #endif
  55566. #ifndef NO_RSA
  55567. testCertFile = svrCertFile;
  55568. testKeyFile = svrKeyFile;
  55569. #elif defined(HAVE_ECC)
  55570. testCertFile = eccCertFile;
  55571. testKeyFile = eccKeyFile;
  55572. #else
  55573. testCertFile = NULL;
  55574. testKeyFile = NULL;
  55575. #endif
  55576. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  55577. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55578. SSL_FILETYPE_PEM));
  55579. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55580. SSL_FILETYPE_PEM));
  55581. }
  55582. ExpectNotNull(ssl = SSL_new(ctx));
  55583. ExpectIntEQ(SSL_in_init(ssl), 1);
  55584. SSL_CTX_free(ctx);
  55585. SSL_free(ssl);
  55586. #endif
  55587. return EXPECT_RESULT();
  55588. }
  55589. static int test_wolfSSL_CTX_set_timeout(void)
  55590. {
  55591. EXPECT_DECLS;
  55592. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  55593. int timeout;
  55594. WOLFSSL_CTX* ctx = NULL;
  55595. (void)timeout;
  55596. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55597. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  55598. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  55599. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  55600. */
  55601. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  55602. /* giving 0 as timeout value sets default timeout */
  55603. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  55604. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  55605. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  55606. #else
  55607. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  55608. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  55609. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  55610. #endif
  55611. wolfSSL_CTX_free(ctx);
  55612. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  55613. return EXPECT_RESULT();
  55614. }
  55615. static int test_wolfSSL_OpenSSL_version(void)
  55616. {
  55617. EXPECT_DECLS;
  55618. #if defined(OPENSSL_EXTRA)
  55619. const char* ver;
  55620. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  55621. ExpectNotNull(ver = OpenSSL_version(0));
  55622. #else
  55623. ExpectNotNull(ver = OpenSSL_version());
  55624. #endif
  55625. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  55626. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  55627. #endif
  55628. return EXPECT_RESULT();
  55629. }
  55630. static int test_CONF_CTX_CMDLINE(void)
  55631. {
  55632. EXPECT_DECLS;
  55633. #if defined(OPENSSL_ALL)
  55634. SSL_CTX* ctx = NULL;
  55635. SSL_CONF_CTX* cctx = NULL;
  55636. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  55637. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55638. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  55639. /* set flags */
  55640. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  55641. WOLFSSL_CONF_FLAG_CMDLINE);
  55642. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  55643. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  55644. /* cmd invalid command */
  55645. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  55646. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  55647. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  55648. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  55649. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  55650. /* cmd Certificate and Private Key*/
  55651. {
  55652. #if !defined(NO_CERTS) && !defined(NO_RSA)
  55653. const char* ourCert = svrCertFile;
  55654. const char* ourKey = svrKeyFile;
  55655. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  55656. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  55657. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  55658. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  55659. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55660. #endif
  55661. }
  55662. /* cmd curves */
  55663. {
  55664. #if defined(HAVE_ECC)
  55665. const char* curve = "secp256r1";
  55666. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  55667. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  55668. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55669. #endif
  55670. }
  55671. /* cmd CipherString */
  55672. {
  55673. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  55674. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  55675. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  55676. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55677. }
  55678. /* cmd DH parameter */
  55679. {
  55680. #if !defined(NO_DH) && !defined(NO_BIO)
  55681. const char* ourdhcert = "./certs/dh2048.pem";
  55682. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  55683. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  55684. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55685. #endif
  55686. }
  55687. SSL_CTX_free(ctx);
  55688. SSL_CONF_CTX_free(cctx);
  55689. #endif /* OPENSSL_EXTRA */
  55690. return EXPECT_RESULT();
  55691. }
  55692. static int test_CONF_CTX_FILE(void)
  55693. {
  55694. EXPECT_DECLS;
  55695. #if defined(OPENSSL_ALL)
  55696. SSL_CTX* ctx = NULL;
  55697. SSL_CONF_CTX* cctx = NULL;
  55698. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  55699. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55700. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  55701. /* set flags */
  55702. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  55703. WOLFSSL_CONF_FLAG_FILE);
  55704. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  55705. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  55706. /* sanity check */
  55707. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  55708. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  55709. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  55710. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  55711. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  55712. /* cmd Certificate and Private Key*/
  55713. {
  55714. #if !defined(NO_CERTS) && !defined(NO_RSA)
  55715. const char* ourCert = svrCertFile;
  55716. const char* ourKey = svrKeyFile;
  55717. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  55718. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  55719. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  55720. WOLFSSL_SUCCESS);
  55721. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  55722. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55723. #endif
  55724. }
  55725. /* cmd curves */
  55726. {
  55727. #if defined(HAVE_ECC)
  55728. const char* curve = "secp256r1";
  55729. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  55730. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  55731. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55732. #endif
  55733. }
  55734. /* cmd CipherString */
  55735. {
  55736. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  55737. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  55738. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  55739. WOLFSSL_SUCCESS);
  55740. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55741. }
  55742. /* cmd DH parameter */
  55743. {
  55744. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  55745. const char* ourdhcert = "./certs/dh3072.pem";
  55746. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  55747. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  55748. WOLFSSL_SUCCESS);
  55749. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55750. #endif
  55751. }
  55752. SSL_CTX_free(ctx);
  55753. SSL_CONF_CTX_free(cctx);
  55754. #endif /* OPENSSL_EXTRA */
  55755. return EXPECT_RESULT();
  55756. }
  55757. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  55758. {
  55759. EXPECT_DECLS;
  55760. #ifdef HAVE_EX_DATA
  55761. int idx1, idx2;
  55762. /* test for unsupported class index */
  55763. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  55764. 0,NULL, NULL, NULL, NULL ), -1);
  55765. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  55766. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  55767. 0,NULL, NULL, NULL, NULL ), -1);
  55768. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  55769. 0,NULL, NULL, NULL, NULL ), -1);
  55770. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  55771. 0,NULL, NULL, NULL, NULL ), -1);
  55772. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  55773. 0,NULL, NULL, NULL, NULL ), -1);
  55774. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  55775. 0,NULL, NULL, NULL, NULL ), -1);
  55776. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  55777. 0,NULL, NULL, NULL, NULL ), -1);
  55778. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  55779. 0,NULL, NULL, NULL, NULL ), -1);
  55780. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  55781. 0,NULL, NULL, NULL, NULL ), -1);
  55782. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  55783. 0,NULL, NULL, NULL, NULL ), -1);
  55784. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  55785. 0,NULL, NULL, NULL, NULL ), -1);
  55786. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  55787. 0,NULL, NULL, NULL, NULL ), -1);
  55788. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  55789. 0,NULL, NULL, NULL, NULL ), -1);
  55790. /* test for supported class index */
  55791. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  55792. 0,NULL, NULL, NULL, NULL );
  55793. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  55794. 0,NULL, NULL, NULL, NULL );
  55795. ExpectIntNE(idx1, -1);
  55796. ExpectIntNE(idx2, -1);
  55797. ExpectIntNE(idx1, idx2);
  55798. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  55799. 0,NULL, NULL, NULL, NULL );
  55800. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  55801. 0,NULL, NULL, NULL, NULL );
  55802. ExpectIntNE(idx1, -1);
  55803. ExpectIntNE(idx2, -1);
  55804. ExpectIntNE(idx1, idx2);
  55805. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  55806. 0,NULL, NULL, NULL, NULL );
  55807. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  55808. 0,NULL, NULL, NULL, NULL );
  55809. ExpectIntNE(idx1, -1);
  55810. ExpectIntNE(idx2, -1);
  55811. ExpectIntNE(idx1, idx2);
  55812. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  55813. 0,NULL, NULL, NULL, NULL );
  55814. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  55815. 0,NULL, NULL, NULL, NULL );
  55816. ExpectIntNE(idx1, -1);
  55817. ExpectIntNE(idx2, -1);
  55818. ExpectIntNE(idx1, idx2);
  55819. #endif /* HAVE_EX_DATA */
  55820. return EXPECT_RESULT();
  55821. }
  55822. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  55823. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55824. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55825. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55826. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  55827. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  55828. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  55829. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  55830. #define SESSION_NEW_IDX_PTR "Testing"
  55831. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  55832. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55833. {
  55834. AssertNotNull(p);
  55835. AssertNull(ptr);
  55836. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  55837. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55838. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  55839. }
  55840. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  55841. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  55842. void* arg)
  55843. {
  55844. EXPECT_DECLS;
  55845. ExpectNotNull(out);
  55846. ExpectNotNull(in);
  55847. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  55848. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  55849. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  55850. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  55851. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  55852. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  55853. if (EXPECT_SUCCESS()) {
  55854. return SSL_SUCCESS;
  55855. }
  55856. else {
  55857. return SSL_FAILURE;
  55858. }
  55859. }
  55860. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  55861. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  55862. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55863. {
  55864. EXPECT_DECLS;
  55865. ExpectNotNull(p);
  55866. ExpectNull(ptr);
  55867. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  55868. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55869. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  55870. if (EXPECT_SUCCESS()) {
  55871. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  55872. }
  55873. }
  55874. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55875. {
  55876. EXPECT_DECLS;
  55877. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  55878. (void*)SESSION_NEW_IDX_PTR,
  55879. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  55880. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  55881. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  55882. SSL_SESSION* s = SSL_SESSION_new();
  55883. SSL_SESSION* d = NULL;
  55884. ExpectNotNull(s);
  55885. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  55886. ExpectNotNull(d = SSL_SESSION_dup(s));
  55887. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  55888. SSL_SESSION_free(s);
  55889. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  55890. SSL_SESSION_free(d);
  55891. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  55892. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  55893. crypto_ex_cb_ctx_session = NULL;
  55894. return EXPECT_RESULT();
  55895. }
  55896. #else
  55897. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55898. {
  55899. return TEST_SKIPPED;
  55900. }
  55901. #endif
  55902. static int test_wolfSSL_set_psk_use_session_callback(void)
  55903. {
  55904. EXPECT_DECLS;
  55905. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  55906. SSL_CTX* ctx = NULL;
  55907. SSL* ssl = NULL;
  55908. const char* testCertFile;
  55909. const char* testKeyFile;
  55910. #ifdef WOLFSSL_TLS13
  55911. #ifdef NO_WOLFSSL_SERVER
  55912. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55913. #else
  55914. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55915. #endif
  55916. #else
  55917. #ifdef NO_WOLFSSL_SERVER
  55918. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55919. #else
  55920. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55921. #endif
  55922. #endif
  55923. #ifndef NO_RSA
  55924. testCertFile = svrCertFile;
  55925. testKeyFile = svrKeyFile;
  55926. #elif defined(HAVE_ECC)
  55927. testCertFile = eccCertFile;
  55928. testKeyFile = eccKeyFile;
  55929. #else
  55930. testCertFile = NULL;
  55931. testKeyFile = NULL;
  55932. #endif
  55933. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  55934. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55935. SSL_FILETYPE_PEM));
  55936. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55937. SSL_FILETYPE_PEM));
  55938. }
  55939. ExpectNotNull(ssl = SSL_new(ctx));
  55940. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  55941. SSL_CTX_free(ctx);
  55942. SSL_free(ssl);
  55943. #endif
  55944. return EXPECT_RESULT();
  55945. }
  55946. static int test_wolfSSL_ERR_strings(void)
  55947. {
  55948. EXPECT_DECLS;
  55949. #if !defined(NO_ERROR_STRINGS)
  55950. const char* err1 = "unsupported cipher suite";
  55951. const char* err2 = "wolfSSL PEM routines";
  55952. const char* err = NULL;
  55953. (void)err;
  55954. (void)err1;
  55955. (void)err2;
  55956. #if defined(OPENSSL_EXTRA)
  55957. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  55958. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55959. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  55960. ExpectIntEQ((*err == '\0'), 1);
  55961. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  55962. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  55963. #else
  55964. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  55965. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55966. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  55967. ExpectIntEQ((*err == '\0'), 1);
  55968. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  55969. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  55970. ExpectIntEQ((*err == '\0'), 1);
  55971. #endif
  55972. #endif
  55973. return EXPECT_RESULT();
  55974. }
  55975. static int test_wolfSSL_EVP_shake128(void)
  55976. {
  55977. EXPECT_DECLS;
  55978. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55979. defined(WOLFSSL_SHAKE128)
  55980. const EVP_MD* md = NULL;
  55981. ExpectNotNull(md = EVP_shake128());
  55982. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  55983. #endif
  55984. return EXPECT_RESULT();
  55985. }
  55986. static int test_wolfSSL_EVP_shake256(void)
  55987. {
  55988. EXPECT_DECLS;
  55989. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55990. defined(WOLFSSL_SHAKE256)
  55991. const EVP_MD* md = NULL;
  55992. ExpectNotNull(md = EVP_shake256());
  55993. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  55994. #endif
  55995. return EXPECT_RESULT();
  55996. }
  55997. /*
  55998. * Testing EVP digest API with SM3
  55999. */
  56000. static int test_wolfSSL_EVP_sm3(void)
  56001. {
  56002. int res = TEST_SKIPPED;
  56003. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  56004. EXPECT_DECLS;
  56005. const EVP_MD* md = NULL;
  56006. EVP_MD_CTX* mdCtx = NULL;
  56007. byte data[WC_SM3_BLOCK_SIZE * 4];
  56008. byte hash[WC_SM3_DIGEST_SIZE];
  56009. byte calcHash[WC_SM3_DIGEST_SIZE];
  56010. byte expHash[WC_SM3_DIGEST_SIZE] = {
  56011. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  56012. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  56013. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  56014. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  56015. };
  56016. word32 chunk;
  56017. word32 i;
  56018. unsigned int sz;
  56019. int ret;
  56020. XMEMSET(data, 0, sizeof(data));
  56021. md = EVP_sm3();
  56022. ExpectTrue(md != NULL);
  56023. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  56024. mdCtx = EVP_MD_CTX_new();
  56025. ExpectTrue(mdCtx != NULL);
  56026. /* Invalid Parameters */
  56027. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  56028. /* Valid Parameters */
  56029. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  56030. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  56031. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  56032. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  56033. /* Valid Parameters */
  56034. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  56035. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  56036. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  56037. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  56038. WOLFSSL_SUCCESS);
  56039. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  56040. WOLFSSL_SUCCESS);
  56041. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  56042. WOLFSSL_SUCCESS);
  56043. /* Ensure too many bytes for lengths. */
  56044. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  56045. WOLFSSL_SUCCESS);
  56046. /* Invalid Parameters */
  56047. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  56048. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  56049. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  56050. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  56051. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  56052. /* Valid Parameters */
  56053. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  56054. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  56055. /* Chunk tests. */
  56056. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  56057. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  56058. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  56059. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  56060. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  56061. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  56062. WOLFSSL_SUCCESS);
  56063. }
  56064. if (i < (word32)sizeof(data)) {
  56065. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  56066. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  56067. }
  56068. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  56069. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  56070. }
  56071. /* Not testing when the low 32-bit length overflows. */
  56072. ret = EVP_MD_CTX_cleanup(mdCtx);
  56073. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56074. wolfSSL_EVP_MD_CTX_free(mdCtx);
  56075. res = EXPECT_RESULT();
  56076. #endif
  56077. return res;
  56078. } /* END test_EVP_sm3 */
  56079. static int test_EVP_blake2(void)
  56080. {
  56081. EXPECT_DECLS;
  56082. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  56083. const EVP_MD* md = NULL;
  56084. (void)md;
  56085. #if defined(HAVE_BLAKE2)
  56086. ExpectNotNull(md = EVP_blake2b512());
  56087. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  56088. #endif
  56089. #if defined(HAVE_BLAKE2S)
  56090. ExpectNotNull(md = EVP_blake2s256());
  56091. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  56092. #endif
  56093. #endif
  56094. return EXPECT_RESULT();
  56095. }
  56096. #if defined(OPENSSL_EXTRA)
  56097. static void list_md_fn(const EVP_MD* m, const char* from,
  56098. const char* to, void* arg)
  56099. {
  56100. const char* mn;
  56101. BIO *bio;
  56102. (void) from;
  56103. (void) to;
  56104. (void) arg;
  56105. (void) mn;
  56106. (void) bio;
  56107. if (!m) {
  56108. /* alias */
  56109. AssertNull(m);
  56110. AssertNotNull(to);
  56111. }
  56112. else {
  56113. AssertNotNull(m);
  56114. AssertNull(to);
  56115. }
  56116. AssertNotNull(from);
  56117. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  56118. mn = EVP_get_digestbyname(from);
  56119. /* print to stderr */
  56120. AssertNotNull(arg);
  56121. bio = BIO_new(BIO_s_file());
  56122. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  56123. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  56124. BIO_free(bio);
  56125. #endif
  56126. }
  56127. #endif
  56128. static int test_EVP_MD_do_all(void)
  56129. {
  56130. int res = TEST_SKIPPED;
  56131. #if defined(OPENSSL_EXTRA)
  56132. EVP_MD_do_all(NULL, stderr);
  56133. EVP_MD_do_all(list_md_fn, stderr);
  56134. res = TEST_SUCCESS;
  56135. #endif
  56136. return res;
  56137. }
  56138. #if defined(OPENSSL_EXTRA)
  56139. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  56140. {
  56141. (void)arg;
  56142. (void)nm;
  56143. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  56144. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  56145. /* print to stderr */
  56146. AssertNotNull(arg);
  56147. BIO *bio = BIO_new(BIO_s_file());
  56148. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  56149. BIO_printf(bio, "%s\n", nm);
  56150. BIO_free(bio);
  56151. #endif
  56152. }
  56153. #endif
  56154. static int test_OBJ_NAME_do_all(void)
  56155. {
  56156. int res = TEST_SKIPPED;
  56157. #if defined(OPENSSL_EXTRA)
  56158. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  56159. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  56160. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  56161. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  56162. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  56163. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  56164. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  56165. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  56166. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  56167. res = TEST_SUCCESS;
  56168. #endif
  56169. return res;
  56170. }
  56171. static int test_SSL_CIPHER_get_xxx(void)
  56172. {
  56173. EXPECT_DECLS;
  56174. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  56175. !defined(NO_FILESYSTEM)
  56176. const SSL_CIPHER* cipher = NULL;
  56177. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  56178. int i, numCiphers = 0;
  56179. SSL_CTX* ctx = NULL;
  56180. SSL* ssl = NULL;
  56181. const char* testCertFile;
  56182. const char* testKeyFile;
  56183. char buf[256] = {0};
  56184. const char* cipher_id = NULL;
  56185. int expect_nid1 = NID_undef;
  56186. int expect_nid2 = NID_undef;
  56187. int expect_nid3 = NID_undef;
  56188. int expect_nid4 = NID_undef;
  56189. int expect_nid5 = 0;
  56190. const char* cipher_id2 = NULL;
  56191. int expect_nid21 = NID_undef;
  56192. int expect_nid22 = NID_undef;
  56193. int expect_nid23 = NID_undef;
  56194. int expect_nid24 = NID_undef;
  56195. int expect_nid25 = 0;
  56196. (void)cipher;
  56197. (void)supportedCiphers;
  56198. (void)i;
  56199. (void)numCiphers;
  56200. (void)ctx;
  56201. (void)ssl;
  56202. (void)testCertFile;
  56203. (void)testKeyFile;
  56204. #if defined(WOLFSSL_TLS13)
  56205. cipher_id = "TLS13-AES128-GCM-SHA256";
  56206. expect_nid1 = NID_auth_rsa;
  56207. expect_nid2 = NID_aes_128_gcm;
  56208. expect_nid3 = NID_sha256;
  56209. expect_nid4 = NID_kx_any;
  56210. expect_nid5 = 1;
  56211. #if !defined(WOLFSSL_NO_TLS12)
  56212. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  56213. expect_nid21 = NID_auth_rsa;
  56214. expect_nid22 = NID_aes_256_gcm;
  56215. expect_nid23 = NID_sha384;
  56216. expect_nid24 = NID_kx_ecdhe;
  56217. expect_nid25 = 1;
  56218. #endif
  56219. #endif
  56220. #ifdef NO_WOLFSSL_SERVER
  56221. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56222. #else
  56223. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56224. #endif
  56225. if (cipher_id) {
  56226. #ifndef NO_RSA
  56227. testCertFile = svrCertFile;
  56228. testKeyFile = svrKeyFile;
  56229. #elif defined(HAVE_ECC)
  56230. testCertFile = eccCertFile;
  56231. testKeyFile = eccKeyFile;
  56232. #else
  56233. testCertFile = NULL;
  56234. testKeyFile = NULL;
  56235. #endif
  56236. if (testCertFile != NULL && testKeyFile != NULL) {
  56237. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56238. SSL_FILETYPE_PEM));
  56239. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56240. SSL_FILETYPE_PEM));
  56241. }
  56242. ExpectNotNull(ssl = SSL_new(ctx));
  56243. ExpectIntEQ(SSL_in_init(ssl), 1);
  56244. supportedCiphers = SSL_get_ciphers(ssl);
  56245. numCiphers = sk_num(supportedCiphers);
  56246. for (i = 0; i < numCiphers; ++i) {
  56247. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  56248. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  56249. }
  56250. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  56251. break;
  56252. }
  56253. }
  56254. /* test case for */
  56255. if (i != numCiphers) {
  56256. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  56257. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  56258. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  56259. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  56260. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  56261. }
  56262. if (cipher_id2) {
  56263. for (i = 0; i < numCiphers; ++i) {
  56264. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  56265. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  56266. }
  56267. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  56268. break;
  56269. }
  56270. }
  56271. /* test case for */
  56272. if (i != numCiphers) {
  56273. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  56274. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  56275. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  56276. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  56277. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  56278. }
  56279. }
  56280. }
  56281. SSL_CTX_free(ctx);
  56282. SSL_free(ssl);
  56283. #endif
  56284. return EXPECT_RESULT();
  56285. }
  56286. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56287. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  56288. int* keyFormat)
  56289. {
  56290. int ret;
  56291. byte* key_buf = NULL;
  56292. size_t key_sz = 0;
  56293. EncryptedInfo encInfo;
  56294. XMEMSET(&encInfo, 0, sizeof(encInfo));
  56295. ret = load_file(privKeyFile, &key_buf, &key_sz);
  56296. if (ret == 0) {
  56297. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  56298. NULL, &encInfo, keyFormat);
  56299. }
  56300. if (key_buf != NULL) {
  56301. free(key_buf); key_buf = NULL;
  56302. }
  56303. (void)encInfo; /* not used in this test */
  56304. #ifdef DEBUG_WOLFSSL
  56305. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  56306. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  56307. (*pDer)->length);
  56308. #endif
  56309. return ret;
  56310. }
  56311. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  56312. {
  56313. int ret = CRYPTOCB_UNAVAILABLE;
  56314. const char* privKeyFile = (const char*)ctx;
  56315. DerBuffer* pDer = NULL;
  56316. int keyFormat = 0;
  56317. if (info->algo_type == WC_ALGO_TYPE_PK) {
  56318. #ifdef DEBUG_WOLFSSL
  56319. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  56320. #endif
  56321. #ifndef NO_RSA
  56322. if (info->pk.type == WC_PK_TYPE_RSA) {
  56323. switch (info->pk.rsa.type) {
  56324. case RSA_PUBLIC_ENCRYPT:
  56325. case RSA_PUBLIC_DECRYPT:
  56326. /* perform software based RSA public op */
  56327. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  56328. break;
  56329. case RSA_PRIVATE_ENCRYPT:
  56330. case RSA_PRIVATE_DECRYPT:
  56331. {
  56332. RsaKey key;
  56333. /* perform software based RSA private op */
  56334. #ifdef DEBUG_WOLFSSL
  56335. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  56336. #endif
  56337. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  56338. &keyFormat);
  56339. if (ret != 0) {
  56340. return ret;
  56341. }
  56342. ret = wc_InitRsaKey(&key, HEAP_HINT);
  56343. if (ret == 0) {
  56344. word32 keyIdx = 0;
  56345. /* load RSA private key and perform private transform */
  56346. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  56347. &key, pDer->length);
  56348. if (ret == 0) {
  56349. ret = wc_RsaFunction(
  56350. info->pk.rsa.in, info->pk.rsa.inLen,
  56351. info->pk.rsa.out, info->pk.rsa.outLen,
  56352. info->pk.rsa.type, &key, info->pk.rsa.rng);
  56353. }
  56354. else {
  56355. /* if decode fails, then fall-back to software based crypto */
  56356. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  56357. "key decode failed %d, falling back to "
  56358. "software\n", ret);
  56359. ret = CRYPTOCB_UNAVAILABLE;
  56360. }
  56361. wc_FreeRsaKey(&key);
  56362. }
  56363. wc_FreeDer(&pDer); pDer = NULL;
  56364. break;
  56365. }
  56366. }
  56367. #ifdef DEBUG_WOLFSSL
  56368. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  56369. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  56370. #endif
  56371. }
  56372. #endif /* !NO_RSA */
  56373. #ifdef HAVE_ECC
  56374. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  56375. /* mark this key as ephemeral */
  56376. if (info->pk.eckg.key != NULL) {
  56377. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  56378. sizeof(info->pk.eckg.key->label));
  56379. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  56380. }
  56381. }
  56382. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  56383. ecc_key key;
  56384. /* perform software based ECC sign */
  56385. #ifdef DEBUG_WOLFSSL
  56386. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  56387. #endif
  56388. if (info->pk.eccsign.key != NULL &&
  56389. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  56390. /* this is an empheral key */
  56391. #ifdef DEBUG_WOLFSSL
  56392. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  56393. "ephemeral key\n");
  56394. #endif
  56395. return CRYPTOCB_UNAVAILABLE;
  56396. }
  56397. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  56398. if (ret != 0) {
  56399. return ret;
  56400. }
  56401. ret = wc_ecc_init(&key);
  56402. if (ret == 0) {
  56403. word32 keyIdx = 0;
  56404. /* load ECC private key and perform private transform */
  56405. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  56406. &key, pDer->length);
  56407. if (ret == 0) {
  56408. ret = wc_ecc_sign_hash(
  56409. info->pk.eccsign.in, info->pk.eccsign.inlen,
  56410. info->pk.eccsign.out, info->pk.eccsign.outlen,
  56411. info->pk.eccsign.rng, &key);
  56412. }
  56413. else {
  56414. /* if decode fails, then fall-back to software based crypto */
  56415. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  56416. "decode failed %d, falling back to software\n", ret);
  56417. ret = CRYPTOCB_UNAVAILABLE;
  56418. }
  56419. wc_ecc_free(&key);
  56420. }
  56421. wc_FreeDer(&pDer); pDer = NULL;
  56422. #ifdef DEBUG_WOLFSSL
  56423. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  56424. ret, *info->pk.eccsign.outlen);
  56425. #endif
  56426. }
  56427. #endif /* HAVE_ECC */
  56428. #ifdef HAVE_ED25519
  56429. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  56430. ed25519_key key;
  56431. /* perform software based ED25519 sign */
  56432. #ifdef DEBUG_WOLFSSL
  56433. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  56434. #endif
  56435. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  56436. if (ret != 0) {
  56437. return ret;
  56438. }
  56439. ret = wc_ed25519_init(&key);
  56440. if (ret == 0) {
  56441. word32 keyIdx = 0;
  56442. /* load ED25519 private key and perform private transform */
  56443. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  56444. &key, pDer->length);
  56445. if (ret == 0) {
  56446. /* calculate public key */
  56447. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  56448. if (ret == 0) {
  56449. key.pubKeySet = 1;
  56450. ret = wc_ed25519_sign_msg_ex(
  56451. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  56452. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  56453. &key, info->pk.ed25519sign.type,
  56454. info->pk.ed25519sign.context,
  56455. info->pk.ed25519sign.contextLen);
  56456. }
  56457. }
  56458. else {
  56459. /* if decode fails, then fall-back to software based crypto */
  56460. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  56461. "decode failed %d, falling back to software\n", ret);
  56462. ret = CRYPTOCB_UNAVAILABLE;
  56463. }
  56464. wc_ed25519_free(&key);
  56465. }
  56466. wc_FreeDer(&pDer); pDer = NULL;
  56467. #ifdef DEBUG_WOLFSSL
  56468. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  56469. ret, *info->pk.ed25519sign.outLen);
  56470. #endif
  56471. }
  56472. #endif /* HAVE_ED25519 */
  56473. }
  56474. (void)thisDevId;
  56475. (void)keyFormat;
  56476. return ret;
  56477. }
  56478. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  56479. static int test_wc_CryptoCb_TLS(int tlsVer,
  56480. const char* cliCaPemFile, const char* cliCertPemFile,
  56481. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  56482. const char* svrCaPemFile, const char* svrCertPemFile,
  56483. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  56484. {
  56485. EXPECT_DECLS;
  56486. callback_functions client_cbf;
  56487. callback_functions server_cbf;
  56488. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  56489. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  56490. if (tlsVer == WOLFSSL_TLSV1_3) {
  56491. #ifdef WOLFSSL_TLS13
  56492. server_cbf.method = wolfTLSv1_3_server_method;
  56493. client_cbf.method = wolfTLSv1_3_client_method;
  56494. #endif
  56495. }
  56496. else if (tlsVer == WOLFSSL_TLSV1_2) {
  56497. #ifndef WOLFSSL_NO_TLS12
  56498. server_cbf.method = wolfTLSv1_2_server_method;
  56499. client_cbf.method = wolfTLSv1_2_client_method;
  56500. #endif
  56501. }
  56502. else if (tlsVer == WOLFSSL_TLSV1_1) {
  56503. #ifndef NO_OLD_TLS
  56504. server_cbf.method = wolfTLSv1_1_server_method;
  56505. client_cbf.method = wolfTLSv1_1_client_method;
  56506. #endif
  56507. }
  56508. else if (tlsVer == WOLFSSL_TLSV1) {
  56509. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  56510. server_cbf.method = wolfTLSv1_server_method;
  56511. client_cbf.method = wolfTLSv1_client_method;
  56512. #endif
  56513. }
  56514. else if (tlsVer == WOLFSSL_SSLV3) {
  56515. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  56516. defined(WOLFSSL_STATIC_RSA)
  56517. server_cbf.method = wolfSSLv3_server_method;
  56518. client_cbf.method = wolfSSLv3_client_method;
  56519. #endif
  56520. }
  56521. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  56522. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56523. server_cbf.method = wolfDTLSv1_2_server_method;
  56524. client_cbf.method = wolfDTLSv1_2_client_method;
  56525. #endif
  56526. }
  56527. else if (tlsVer == WOLFSSL_DTLSV1) {
  56528. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  56529. server_cbf.method = wolfDTLSv1_server_method;
  56530. client_cbf.method = wolfDTLSv1_client_method;
  56531. #endif
  56532. }
  56533. if (server_cbf.method == NULL) {
  56534. /* not enabled */
  56535. return TEST_SUCCESS;
  56536. }
  56537. /* Setup the keys for the TLS test */
  56538. client_cbf.certPemFile = cliCertPemFile;
  56539. client_cbf.keyPemFile = cliPubKeyPemFile;
  56540. client_cbf.caPemFile = cliCaPemFile;
  56541. server_cbf.certPemFile = svrCertPemFile;
  56542. server_cbf.keyPemFile = svrPubKeyPemFile;
  56543. server_cbf.caPemFile = svrCaPemFile;
  56544. /* Setup a crypto callback with pointer to private key file for testing */
  56545. client_cbf.devId = 1;
  56546. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  56547. (void*)cliPrivKeyPemFile);
  56548. server_cbf.devId = 2;
  56549. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  56550. (void*)svrPrivKeyPemFile);
  56551. /* Perform TLS server and client test */
  56552. /* First test is at WOLFSSL_CTX level */
  56553. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56554. /* Check for success */
  56555. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56556. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56557. if (EXPECT_SUCCESS()) {
  56558. /* Second test is a WOLFSSL object level */
  56559. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  56560. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56561. }
  56562. /* Check for success */
  56563. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56564. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56565. /* Un register the devId's */
  56566. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  56567. client_cbf.devId = INVALID_DEVID;
  56568. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  56569. server_cbf.devId = INVALID_DEVID;
  56570. return EXPECT_RESULT();
  56571. }
  56572. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  56573. static int test_wc_CryptoCb(void)
  56574. {
  56575. EXPECT_DECLS;
  56576. #ifdef WOLF_CRYPTO_CB
  56577. /* TODO: Add crypto callback API tests */
  56578. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56579. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  56580. int tlsVer;
  56581. #endif
  56582. #ifndef NO_RSA
  56583. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56584. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  56585. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  56586. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  56587. TEST_SUCCESS);
  56588. }
  56589. #endif
  56590. #ifdef HAVE_ECC
  56591. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56592. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  56593. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  56594. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  56595. TEST_SUCCESS);
  56596. }
  56597. #endif
  56598. #ifdef HAVE_ED25519
  56599. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  56600. if (tlsVer == WOLFSSL_DTLSV1) continue;
  56601. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  56602. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  56603. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  56604. TEST_SUCCESS);
  56605. }
  56606. #endif
  56607. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  56608. #endif /* WOLF_CRYPTO_CB */
  56609. return EXPECT_RESULT();
  56610. }
  56611. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56612. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  56613. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  56614. const char* cliCaPemFile, const char* cliCertPemFile,
  56615. const char* cliPrivKeyPemFile,
  56616. const char* svrCaPemFile, const char* svrCertPemFile,
  56617. const char* svrPrivKeyPemFile,
  56618. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  56619. {
  56620. EXPECT_DECLS;
  56621. callback_functions client_cbf;
  56622. callback_functions server_cbf;
  56623. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  56624. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  56625. if (tlsVer == WOLFSSL_TLSV1_3) {
  56626. #ifdef WOLFSSL_TLS13
  56627. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  56628. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  56629. #endif
  56630. }
  56631. else if (tlsVer == WOLFSSL_TLSV1_2) {
  56632. #ifndef WOLFSSL_NO_TLS12
  56633. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  56634. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  56635. #endif
  56636. }
  56637. else if (tlsVer == WOLFSSL_TLSV1_1) {
  56638. #ifndef NO_OLD_TLS
  56639. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  56640. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  56641. #endif
  56642. }
  56643. else if (tlsVer == WOLFSSL_TLSV1) {
  56644. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  56645. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  56646. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  56647. #endif
  56648. }
  56649. else if (tlsVer == WOLFSSL_SSLV3) {
  56650. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  56651. defined(WOLFSSL_STATIC_RSA)
  56652. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  56653. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  56654. #endif
  56655. }
  56656. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  56657. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56658. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  56659. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  56660. #endif
  56661. }
  56662. else if (tlsVer == WOLFSSL_DTLSV1) {
  56663. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  56664. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  56665. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  56666. #endif
  56667. }
  56668. if (server_cbf.method_ex == NULL) {
  56669. /* not enabled */
  56670. return TEST_SUCCESS;
  56671. }
  56672. /* Setup the keys for the TLS test */
  56673. client_cbf.certPemFile = cliCertPemFile;
  56674. client_cbf.keyPemFile = cliPrivKeyPemFile;
  56675. client_cbf.caPemFile = cliCaPemFile;
  56676. server_cbf.certPemFile = svrCertPemFile;
  56677. server_cbf.keyPemFile = svrPrivKeyPemFile;
  56678. server_cbf.caPemFile = svrCaPemFile;
  56679. client_cbf.mem = cliMem;
  56680. client_cbf.memSz = cliMemSz;
  56681. server_cbf.mem = svrMem;
  56682. server_cbf.memSz = svrMemSz;
  56683. client_cbf.devId = INVALID_DEVID;
  56684. server_cbf.devId = INVALID_DEVID;
  56685. /* Perform TLS server and client test */
  56686. /* First test is at WOLFSSL_CTX level */
  56687. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56688. /* Check for success */
  56689. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56690. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56691. if (EXPECT_SUCCESS()) {
  56692. /* Second test is a WOLFSSL object level */
  56693. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  56694. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56695. }
  56696. /* Check for success */
  56697. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56698. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56699. return EXPECT_RESULT();
  56700. }
  56701. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  56702. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  56703. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  56704. {
  56705. EXPECT_DECLS;
  56706. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  56707. WOLFSSL_MEM_STATS mem_stats;
  56708. WOLFSSL_MEM_CONN_STATS ssl_stats;
  56709. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  56710. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  56711. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56712. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  56713. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56714. #endif
  56715. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  56716. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  56717. /* this should fail because kMaxCtxClients == 2 */
  56718. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  56719. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  56720. #ifdef DEBUG_WOLFSSL
  56721. wolfSSL_PrintStatsConn(&ssl_stats);
  56722. #endif
  56723. (void)ssl_stats;
  56724. }
  56725. /* display collected statistics */
  56726. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  56727. #ifdef DEBUG_WOLFSSL
  56728. wolfSSL_PrintStats(&mem_stats);
  56729. #endif
  56730. (void)mem_stats;
  56731. }
  56732. wolfSSL_free(ssl1);
  56733. wolfSSL_free(ssl2);
  56734. return EXPECT_RESULT();
  56735. }
  56736. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  56737. static int test_wolfSSL_CTX_StaticMemory(void)
  56738. {
  56739. EXPECT_DECLS;
  56740. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  56741. wolfSSL_method_func method_func;
  56742. WOLFSSL_CTX* ctx;
  56743. const int kMaxCtxClients = 2;
  56744. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56745. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  56746. int tlsVer;
  56747. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  56748. #endif
  56749. #endif
  56750. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  56751. #ifndef NO_WOLFSSL_SERVER
  56752. #ifndef WOLFSSL_NO_TLS12
  56753. method_func = wolfTLSv1_2_server_method_ex;
  56754. #else
  56755. method_func = wolfTLSv1_3_server_method_ex;
  56756. #endif
  56757. #else
  56758. #ifndef WOLFSSL_NO_TLS12
  56759. method_func = wolfTLSv1_2_client_method_ex;
  56760. #else
  56761. method_func = wolfTLSv1_3_client_method_ex;
  56762. #endif
  56763. #endif
  56764. /* Test creating CTX directly from static memory pool */
  56765. ctx = NULL;
  56766. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  56767. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  56768. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  56769. wolfSSL_CTX_free(ctx);
  56770. ctx = NULL;
  56771. /* Test for heap allocated CTX, then assigning static pool to it */
  56772. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  56773. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  56774. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  56775. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  56776. wolfSSL_CTX_free(ctx);
  56777. /* TLS Level Tests using static memory */
  56778. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56779. #ifndef NO_RSA
  56780. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56781. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56782. svrCertFile, cliCertFile, cliKeyFile,
  56783. cliCertFile, svrCertFile, svrKeyFile,
  56784. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  56785. TEST_SUCCESS);
  56786. }
  56787. #endif
  56788. #ifdef HAVE_ECC
  56789. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56790. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56791. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  56792. cliEccCertFile, eccCertFile, eccKeyFile,
  56793. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  56794. TEST_SUCCESS);
  56795. }
  56796. #endif
  56797. #ifdef HAVE_ED25519
  56798. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  56799. if (tlsVer == WOLFSSL_DTLSV1) continue;
  56800. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56801. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  56802. cliEdCertFile, edCertFile, edKeyFile,
  56803. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  56804. TEST_SUCCESS);
  56805. }
  56806. #endif
  56807. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  56808. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  56809. return EXPECT_RESULT();
  56810. }
  56811. static int test_openssl_FIPS_drbg(void)
  56812. {
  56813. EXPECT_DECLS;
  56814. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  56815. DRBG_CTX* dctx = NULL;
  56816. byte data1[32], data2[32], zeroData[32];
  56817. byte testSeed[16];
  56818. size_t dlen = sizeof(data1);
  56819. int i;
  56820. XMEMSET(data1, 0, dlen);
  56821. XMEMSET(data2, 0, dlen);
  56822. XMEMSET(zeroData, 0, sizeof(zeroData));
  56823. for (i = 0; i < (int)sizeof(testSeed); i++) {
  56824. testSeed[i] = (byte)i;
  56825. }
  56826. ExpectNotNull(dctx = FIPS_get_default_drbg());
  56827. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  56828. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  56829. WOLFSSL_SUCCESS);
  56830. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  56831. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  56832. WOLFSSL_SUCCESS);
  56833. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  56834. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  56835. WOLFSSL_SUCCESS);
  56836. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  56837. WOLFSSL_SUCCESS);
  56838. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  56839. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  56840. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  56841. #ifndef HAVE_GLOBAL_RNG
  56842. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  56843. wolfSSL_FIPS_drbg_free(dctx);
  56844. #endif
  56845. #endif
  56846. return EXPECT_RESULT();
  56847. }
  56848. static int test_wolfSSL_FIPS_mode(void)
  56849. {
  56850. EXPECT_DECLS;
  56851. #if defined(OPENSSL_ALL)
  56852. #ifdef HAVE_FIPS
  56853. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  56854. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  56855. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  56856. #else
  56857. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  56858. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  56859. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  56860. #endif
  56861. #endif
  56862. return EXPECT_RESULT();
  56863. }
  56864. #ifdef WOLFSSL_DTLS
  56865. /* Prints out the current window */
  56866. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  56867. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56868. int i;
  56869. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  56870. word32 b = w[i];
  56871. int j;
  56872. /* Prints out a 32 bit binary number in big endian order */
  56873. for (j = 0; j < 32; j++, b <<= 1) {
  56874. if (b & (((word32)1) << 31))
  56875. fprintf(stderr, "1");
  56876. else
  56877. fprintf(stderr, "0");
  56878. }
  56879. fprintf(stderr, " ");
  56880. }
  56881. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  56882. #else
  56883. (void)h;
  56884. (void)l;
  56885. (void)w;
  56886. #endif
  56887. }
  56888. /* a - cur_hi
  56889. * b - cur_lo
  56890. * c - next_hi
  56891. * d - next_lo
  56892. * e - window
  56893. * f - expected next_hi
  56894. * g - expected next_lo
  56895. * h - expected window[1]
  56896. * i - expected window[0]
  56897. */
  56898. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  56899. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  56900. DUW_TEST_print_window_binary((a), (b), (e)); \
  56901. ExpectIntEQ((c), (f)); \
  56902. ExpectIntEQ((d), (g)); \
  56903. ExpectIntEQ((e)[1], (h)); \
  56904. ExpectIntEQ((e)[0], (i)); \
  56905. } while (0)
  56906. static int test_wolfSSL_DtlsUpdateWindow(void)
  56907. {
  56908. EXPECT_DECLS;
  56909. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  56910. word32 next_lo = 0;
  56911. word16 next_hi = 0;
  56912. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56913. fprintf(stderr, "\n");
  56914. #endif
  56915. XMEMSET(window, 0, sizeof window);
  56916. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  56917. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  56918. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  56919. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  56920. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  56921. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  56922. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  56923. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  56924. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  56925. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  56926. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  56927. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  56928. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  56929. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  56930. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  56931. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  56932. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  56933. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  56934. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  56935. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  56936. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  56937. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56938. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56939. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  56940. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  56941. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56942. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56943. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56944. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56945. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56946. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  56947. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  56948. return EXPECT_RESULT();
  56949. }
  56950. #endif /* WOLFSSL_DTLS */
  56951. #ifdef WOLFSSL_DTLS
  56952. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  56953. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  56954. {
  56955. DtlsMsg* cur;
  56956. static byte msg[100];
  56957. static byte msgInit = 0;
  56958. if (!msgInit) {
  56959. int i;
  56960. for (i = 0; i < 100; i++)
  56961. msg[i] = i + 1;
  56962. msgInit = 1;
  56963. }
  56964. /* Sanitize test parameters */
  56965. if (len > sizeof(msg))
  56966. return -1;
  56967. if (f_offset + f_len > sizeof(msg))
  56968. return -1;
  56969. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  56970. if (ssl->dtls_rx_msg_list == NULL)
  56971. return -100;
  56972. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  56973. return -200;
  56974. if (cur->fragBucketListCount != f_count)
  56975. return -300;
  56976. if (cur->ready != ready)
  56977. return -400;
  56978. if (cur->bytesReceived != bytesReceived)
  56979. return -500;
  56980. if (ready) {
  56981. if (cur->fragBucketList != NULL)
  56982. return -600;
  56983. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  56984. return -700;
  56985. }
  56986. else {
  56987. DtlsFragBucket* fb;
  56988. if (cur->fragBucketList == NULL)
  56989. return -800;
  56990. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  56991. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  56992. return -900;
  56993. }
  56994. }
  56995. return 0;
  56996. }
  56997. static int test_wolfSSL_DTLS_fragment_buckets(void)
  56998. {
  56999. EXPECT_DECLS;
  57000. WOLFSSL ssl[1];
  57001. XMEMSET(ssl, 0, sizeof(*ssl));
  57002. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  57003. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57004. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  57005. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  57006. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  57007. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  57008. /* Test all permutations of 3 regions */
  57009. /* 1 2 3 */
  57010. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57011. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  57012. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  57013. /* 1 3 2 */
  57014. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57015. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  57016. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  57017. /* 2 1 3 */
  57018. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  57019. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  57020. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  57021. /* 2 3 1 */
  57022. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  57023. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  57024. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  57025. /* 3 1 2 */
  57026. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  57027. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  57028. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  57029. /* 3 2 1 */
  57030. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  57031. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  57032. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  57033. /* Test overlapping regions */
  57034. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57035. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  57036. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  57037. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  57038. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  57039. /* Test overlapping multiple regions */
  57040. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57041. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  57042. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  57043. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  57044. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  57045. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  57046. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  57047. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  57048. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  57049. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  57050. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57051. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  57052. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  57053. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  57054. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  57055. ssl->dtls_rx_msg_list = NULL;
  57056. ssl->dtls_rx_msg_list_sz = 0;
  57057. return EXPECT_RESULT();
  57058. }
  57059. #endif
  57060. #if !defined(NO_FILESYSTEM) && \
  57061. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57062. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57063. static int test_wolfSSL_dtls_stateless2(void)
  57064. {
  57065. EXPECT_DECLS;
  57066. WOLFSSL *ssl_c = NULL;
  57067. WOLFSSL *ssl_c2 = NULL;
  57068. WOLFSSL *ssl_s = NULL;
  57069. struct test_memio_ctx test_ctx;
  57070. WOLFSSL_CTX *ctx_c = NULL;
  57071. WOLFSSL_CTX *ctx_s = NULL;
  57072. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57073. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57074. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57075. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  57076. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57077. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57078. /* send CH */
  57079. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57080. (ssl_c2->error == WANT_READ));
  57081. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57082. (ssl_s->error == WANT_READ));
  57083. ExpectIntNE(test_ctx.c_len, 0);
  57084. /* consume HRR */
  57085. test_ctx.c_len = 0;
  57086. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57087. wolfSSL_free(ssl_c2);
  57088. wolfSSL_free(ssl_c);
  57089. wolfSSL_free(ssl_s);
  57090. wolfSSL_CTX_free(ctx_c);
  57091. wolfSSL_CTX_free(ctx_s);
  57092. return EXPECT_RESULT();
  57093. }
  57094. #ifdef HAVE_MAX_FRAGMENT
  57095. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  57096. {
  57097. EXPECT_DECLS;
  57098. WOLFSSL *ssl_c = NULL;
  57099. WOLFSSL *ssl_c2 = NULL;
  57100. WOLFSSL *ssl_s = NULL;
  57101. struct test_memio_ctx test_ctx;
  57102. WOLFSSL_CTX *ctx_c = NULL;
  57103. WOLFSSL_CTX *ctx_s = NULL;
  57104. word16 max_fragment = 0;
  57105. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57106. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57107. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57108. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  57109. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  57110. WOLFSSL_SUCCESS);
  57111. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57112. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57113. if (ssl_s != NULL) {
  57114. max_fragment = ssl_s->max_fragment;
  57115. }
  57116. /* send CH */
  57117. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57118. (ssl_c2->error == WANT_READ));
  57119. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57120. (ssl_s->error == WANT_READ));
  57121. /* CH without cookie shouldn't change state */
  57122. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  57123. ExpectIntNE(test_ctx.c_len, 0);
  57124. /* consume HRR from buffer */
  57125. test_ctx.c_len = 0;
  57126. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57127. wolfSSL_free(ssl_c2);
  57128. wolfSSL_free(ssl_c);
  57129. wolfSSL_free(ssl_s);
  57130. wolfSSL_CTX_free(ctx_c);
  57131. wolfSSL_CTX_free(ctx_s);
  57132. return EXPECT_RESULT();
  57133. }
  57134. #endif /* HAVE_MAX_FRAGMENT */
  57135. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  57136. #define ROUNDS_WITH_HVR 4
  57137. #define ROUNDS_WITHOUT_HVR 2
  57138. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  57139. static int buf_is_hvr(const byte *data, int len)
  57140. {
  57141. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  57142. return 0;
  57143. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  57144. }
  57145. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  57146. {
  57147. EXPECT_DECLS;
  57148. struct test_memio_ctx test_ctx;
  57149. WOLFSSL_CTX *ctx_c = NULL;
  57150. WOLFSSL_CTX *ctx_s = NULL;
  57151. WOLFSSL *ssl_c = NULL;
  57152. WOLFSSL *ssl_s = NULL;
  57153. WOLFSSL_SESSION *sess = NULL;
  57154. int round_trips;
  57155. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57156. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  57157. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57158. #ifdef HAVE_SESSION_TICKET
  57159. if (useticket) {
  57160. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  57161. }
  57162. #endif
  57163. round_trips = ROUNDS_WITH_HVR;
  57164. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  57165. &round_trips), 0);
  57166. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  57167. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57168. wolfSSL_shutdown(ssl_c);
  57169. wolfSSL_shutdown(ssl_s);
  57170. wolfSSL_free(ssl_c);
  57171. ssl_c = NULL;
  57172. wolfSSL_free(ssl_s);
  57173. ssl_s = NULL;
  57174. test_ctx.c_len = test_ctx.s_len = 0;
  57175. /* make resumption invalid */
  57176. if (bad && (sess != NULL)) {
  57177. if (useticket) {
  57178. #ifdef HAVE_SESSION_TICKET
  57179. if (sess->ticket != NULL) {
  57180. sess->ticket[0] = !sess->ticket[0];
  57181. }
  57182. #endif /* HAVE_SESSION_TICKET */
  57183. }
  57184. else {
  57185. sess->sessionID[0] = !sess->sessionID[0];
  57186. }
  57187. }
  57188. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  57189. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  57190. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  57191. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  57192. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  57193. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  57194. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  57195. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57196. (ssl_c->error == WANT_READ));
  57197. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57198. (ssl_s->error == WANT_READ));
  57199. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  57200. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  57201. if (!useticket) {
  57202. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  57203. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  57204. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  57205. }
  57206. wolfSSL_SESSION_free(sess);
  57207. wolfSSL_free(ssl_c);
  57208. wolfSSL_free(ssl_s);
  57209. wolfSSL_CTX_free(ctx_c);
  57210. wolfSSL_CTX_free(ctx_s);
  57211. return EXPECT_RESULT();
  57212. }
  57213. static int test_wolfSSL_dtls_stateless_resume(void)
  57214. {
  57215. EXPECT_DECLS;
  57216. #ifdef HAVE_SESSION_TICKET
  57217. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  57218. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  57219. #endif /* HAVE_SESION_TICKET */
  57220. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  57221. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  57222. return EXPECT_RESULT();
  57223. }
  57224. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  57225. #if !defined(NO_OLD_TLS)
  57226. static int test_wolfSSL_dtls_stateless_downgrade(void)
  57227. {
  57228. EXPECT_DECLS;
  57229. WOLFSSL_CTX *ctx_c = NULL;
  57230. WOLFSSL_CTX *ctx_c2 = NULL;
  57231. WOLFSSL_CTX *ctx_s = NULL;
  57232. WOLFSSL *ssl_c = NULL;
  57233. WOLFSSL *ssl_c2 = NULL;
  57234. WOLFSSL *ssl_s = NULL;
  57235. struct test_memio_ctx test_ctx;
  57236. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57237. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57238. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57239. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  57240. WOLFSSL_SUCCESS);
  57241. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  57242. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  57243. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  57244. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  57245. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57246. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57247. /* send CH */
  57248. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57249. (ssl_c2->error == WANT_READ));
  57250. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57251. (ssl_s->error == WANT_READ));
  57252. ExpectIntNE(test_ctx.c_len, 0);
  57253. /* consume HRR */
  57254. test_ctx.c_len = 0;
  57255. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57256. wolfSSL_free(ssl_c2);
  57257. wolfSSL_free(ssl_c);
  57258. wolfSSL_free(ssl_s);
  57259. wolfSSL_CTX_free(ctx_c);
  57260. wolfSSL_CTX_free(ctx_c2);
  57261. wolfSSL_CTX_free(ctx_s);
  57262. return EXPECT_RESULT();
  57263. }
  57264. #endif /* !defined(NO_OLD_TLS) */
  57265. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57266. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  57267. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57268. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57269. static int test_WOLFSSL_dtls_version_alert(void)
  57270. {
  57271. EXPECT_DECLS;
  57272. struct test_memio_ctx test_ctx;
  57273. WOLFSSL_CTX *ctx_c = NULL;
  57274. WOLFSSL_CTX *ctx_s = NULL;
  57275. WOLFSSL *ssl_c = NULL;
  57276. WOLFSSL *ssl_s = NULL;
  57277. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57278. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57279. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  57280. /* client hello */
  57281. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57282. (ssl_c->error == WANT_READ));
  57283. /* hrr */
  57284. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57285. (ssl_s->error == WANT_READ));
  57286. /* client hello 1 */
  57287. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57288. (ssl_c->error == WANT_READ));
  57289. /* server hello */
  57290. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57291. (ssl_s->error == WANT_READ));
  57292. /* should fail */
  57293. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57294. (ssl_c->error == VERSION_ERROR));
  57295. /* shuould fail */
  57296. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57297. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  57298. wolfSSL_free(ssl_c);
  57299. wolfSSL_free(ssl_s);
  57300. wolfSSL_CTX_free(ctx_c);
  57301. wolfSSL_CTX_free(ctx_s);
  57302. return EXPECT_RESULT();
  57303. }
  57304. #else
  57305. static int test_WOLFSSL_dtls_version_alert(void)
  57306. {
  57307. return TEST_SKIPPED;
  57308. }
  57309. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  57310. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  57311. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  57312. */
  57313. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  57314. && defined(WOLFSSL_TLS13) && \
  57315. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  57316. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57317. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  57318. {
  57319. struct test_memio_ctx *test_ctx;
  57320. byte buf[2048];
  57321. int idx, sz;
  57322. word32 tmp;
  57323. int ret;
  57324. idx = 5; /* space for record header */
  57325. buf[idx] = session_ticket; /* type */
  57326. idx++;
  57327. tmp = OPAQUE32_LEN +
  57328. OPAQUE32_LEN +
  57329. OPAQUE8_LEN + nonceLength +
  57330. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  57331. c32to24(tmp, buf + idx);
  57332. idx += OPAQUE24_LEN;
  57333. c32toa((word32)12345, buf+idx); /* lifetime */
  57334. idx += OPAQUE32_LEN;
  57335. c32toa((word32)12345, buf+idx); /* add */
  57336. idx += OPAQUE32_LEN;
  57337. buf[idx] = nonceLength; /* nonce length */
  57338. idx++;
  57339. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  57340. idx += nonceLength;
  57341. tmp = 1; /* ticket len */
  57342. c16toa((word16)tmp, buf+idx);
  57343. idx += 2;
  57344. buf[idx] = 0xFF; /* ticket */
  57345. idx++;
  57346. tmp = 0; /* ext len */
  57347. c16toa((word16)tmp, buf+idx);
  57348. idx += 2;
  57349. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  57350. handshake, 0, 0, 0);
  57351. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  57352. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  57353. return !(ret == sz);
  57354. }
  57355. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  57356. {
  57357. int ret = 0;
  57358. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  57359. ret = -1;
  57360. }
  57361. else {
  57362. int i;
  57363. for (i = 0; i < len; i++) {
  57364. if (sess->ticketNonce.data[i] != len) {
  57365. ret = -1;
  57366. break;
  57367. }
  57368. }
  57369. }
  57370. return ret;
  57371. }
  57372. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57373. {
  57374. EXPECT_DECLS;
  57375. char *buf[1024];
  57376. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  57377. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  57378. (ssl_c->error == WANT_READ));
  57379. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  57380. return EXPECT_RESULT();
  57381. }
  57382. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57383. {
  57384. EXPECT_DECLS;
  57385. WOLFSSL_SESSION *sess = NULL;
  57386. WOLFSSL_SESSION *cached = NULL;
  57387. WOLFSSL_CTX *ctx = ssl_c->ctx;
  57388. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  57389. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57390. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  57391. NULL, ssl_c->options.side, 1,NULL), 0);
  57392. ExpectNotNull(cached = wolfSSL_SESSION_new());
  57393. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  57394. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  57395. wolfSSL_SESSION_free(cached);
  57396. wolfSSL_SESSION_free(sess);
  57397. return EXPECT_RESULT();
  57398. }
  57399. static int test_ticket_nonce_malloc(void)
  57400. {
  57401. EXPECT_DECLS;
  57402. struct test_memio_ctx test_ctx;
  57403. WOLFSSL_CTX *ctx_c = NULL;
  57404. WOLFSSL_CTX *ctx_s = NULL;
  57405. WOLFSSL *ssl_c = NULL;
  57406. WOLFSSL *ssl_s = NULL;
  57407. byte small;
  57408. byte medium;
  57409. byte big;
  57410. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57411. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57412. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  57413. /* will send ticket manually */
  57414. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  57415. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  57416. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  57417. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  57418. (ssl_s->options.handShakeDone == 0)) {
  57419. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  57420. (ssl_c->error == WANT_READ));
  57421. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  57422. (ssl_s->error == WANT_READ));
  57423. }
  57424. small = TLS13_TICKET_NONCE_STATIC_SZ;
  57425. medium = small + 20 <= 255 ? small + 20 : 255;
  57426. big = medium + 20 <= 255 ? small + 20 : 255;
  57427. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  57428. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  57429. ssl_c->session->ticketNonce.dataStatic);
  57430. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  57431. TEST_SUCCESS);
  57432. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  57433. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  57434. TEST_SUCCESS);
  57435. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  57436. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  57437. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  57438. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  57439. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  57440. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  57441. wolfSSL_free(ssl_c);
  57442. wolfSSL_free(ssl_s);
  57443. wolfSSL_CTX_free(ctx_c);
  57444. wolfSSL_CTX_free(ctx_s);
  57445. return EXPECT_RESULT();
  57446. }
  57447. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  57448. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  57449. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  57450. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57451. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  57452. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57453. static int test_ticket_ret_create(void)
  57454. {
  57455. EXPECT_DECLS;
  57456. WOLFSSL_CTX *ctx_c = NULL;
  57457. WOLFSSL_CTX *ctx_s = NULL;
  57458. WOLFSSL *ssl_c = NULL;
  57459. WOLFSSL *ssl_s = NULL;
  57460. byte ticket[SESSION_TICKET_LEN];
  57461. struct test_memio_ctx test_ctx;
  57462. WOLFSSL_SESSION *sess = NULL;
  57463. word16 ticketLen = 0;
  57464. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57465. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57466. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57467. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  57468. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  57469. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  57470. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57471. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57472. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  57473. if (sess != NULL) {
  57474. ticketLen = sess->ticketLen;
  57475. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  57476. }
  57477. wolfSSL_free(ssl_c);
  57478. ssl_c = NULL;
  57479. wolfSSL_free(ssl_s);
  57480. ssl_s = NULL;
  57481. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  57482. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  57483. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  57484. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  57485. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  57486. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  57487. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  57488. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57489. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  57490. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  57491. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  57492. wolfSSL_SESSION_free(sess);
  57493. wolfSSL_free(ssl_c);
  57494. wolfSSL_free(ssl_s);
  57495. wolfSSL_CTX_free(ctx_c);
  57496. wolfSSL_CTX_free(ctx_s);
  57497. return EXPECT_RESULT();
  57498. }
  57499. #else
  57500. static int test_ticket_ret_create(void)
  57501. {
  57502. return TEST_SKIPPED;
  57503. }
  57504. #endif
  57505. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  57506. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  57507. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  57508. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57509. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  57510. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  57511. {
  57512. int ret;
  57513. WOLFSSL_SESSION* session = NULL;
  57514. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  57515. if (!wolfSSL_is_server(ssl)) {
  57516. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  57517. AssertNotNull(session);
  57518. }
  57519. do {
  57520. ret = wolfSSL_shutdown(ssl);
  57521. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  57522. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  57523. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  57524. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  57525. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  57526. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  57527. #endif
  57528. if (!wolfSSL_is_server(ssl)) {
  57529. /* client */
  57530. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57531. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57532. wolfSSL_set_session(ssl, session);
  57533. wolfSSL_SESSION_free(session);
  57534. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  57535. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  57536. }
  57537. else {
  57538. /* server */
  57539. /* Different ciphersuite so that the ticket will be invalidated based on
  57540. * the ciphersuite */
  57541. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  57542. WOLFSSL_SUCCESS);
  57543. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  57544. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  57545. }
  57546. }
  57547. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  57548. {
  57549. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  57550. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57551. WOLFSSL_SUCCESS);
  57552. }
  57553. static int test_ticket_and_psk_mixing(void)
  57554. {
  57555. EXPECT_DECLS;
  57556. /* Test mixing tickets and regular PSK */
  57557. callback_functions client_cbs, server_cbs;
  57558. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57559. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57560. client_cbs.method = wolfTLSv1_3_client_method;
  57561. server_cbs.method = wolfTLSv1_3_server_method;
  57562. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  57563. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  57564. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  57565. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57566. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  57567. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  57568. return EXPECT_RESULT();
  57569. }
  57570. #else
  57571. static int test_ticket_and_psk_mixing(void)
  57572. {
  57573. return TEST_SKIPPED;
  57574. }
  57575. #endif
  57576. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  57577. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  57578. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57579. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  57580. static int test_prioritize_psk_cb_called = FALSE;
  57581. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  57582. const char* identity, unsigned char* key, unsigned int key_max_len,
  57583. const char** ciphersuite)
  57584. {
  57585. test_prioritize_psk_cb_called = TRUE;
  57586. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  57587. }
  57588. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  57589. {
  57590. int ret;
  57591. WOLFSSL_SESSION* session = NULL;
  57592. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  57593. if (!wolfSSL_is_server(ssl)) {
  57594. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  57595. AssertNotNull(session);
  57596. }
  57597. do {
  57598. ret = wolfSSL_shutdown(ssl);
  57599. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  57600. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  57601. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  57602. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  57603. * important. */
  57604. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57605. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57606. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  57607. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  57608. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  57609. #endif
  57610. if (!wolfSSL_is_server(ssl)) {
  57611. /* client */
  57612. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  57613. wolfSSL_set_session(ssl, session);
  57614. wolfSSL_SESSION_free(session);
  57615. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  57616. }
  57617. else {
  57618. /* server */
  57619. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  57620. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  57621. #ifdef WOLFSSL_PRIORITIZE_PSK
  57622. /* The ticket should be first tried with all ciphersuites and chosen */
  57623. AssertFalse(test_prioritize_psk_cb_called);
  57624. #else
  57625. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  57626. * callback that sets this var. */
  57627. AssertTrue(test_prioritize_psk_cb_called);
  57628. #endif
  57629. }
  57630. }
  57631. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  57632. {
  57633. if (!wolfSSL_is_server(ssl))
  57634. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57635. WOLFSSL_SUCCESS);
  57636. else
  57637. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57638. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57639. }
  57640. static int test_prioritize_psk(void)
  57641. {
  57642. EXPECT_DECLS;
  57643. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  57644. * of the PSK's will be followed instead of the ciphersuite. */
  57645. callback_functions client_cbs, server_cbs;
  57646. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57647. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57648. client_cbs.method = wolfTLSv1_3_client_method;
  57649. server_cbs.method = wolfTLSv1_3_server_method;
  57650. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  57651. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  57652. client_cbs.on_result = test_prioritize_psk_on_result;
  57653. server_cbs.on_result = test_prioritize_psk_on_result;
  57654. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57655. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  57656. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  57657. return EXPECT_RESULT();
  57658. }
  57659. #else
  57660. static int test_prioritize_psk(void)
  57661. {
  57662. return TEST_SKIPPED;
  57663. }
  57664. #endif
  57665. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  57666. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  57667. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57668. !defined(WOLFSSL_NO_TLS12)
  57669. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  57670. {
  57671. EXPECT_DECLS;
  57672. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  57673. /* Set TLS 1.3 specific suite */
  57674. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  57675. return EXPECT_RESULT();
  57676. }
  57677. static int test_wolfSSL_CTX_set_ciphersuites(void)
  57678. {
  57679. EXPECT_DECLS;
  57680. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  57681. * do a 1.2 connection. */
  57682. test_ssl_cbf client_cbs, server_cbs;
  57683. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57684. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57685. client_cbs.method = wolfTLSv1_2_client_method;
  57686. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  57687. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  57688. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57689. &server_cbs, NULL), TEST_SUCCESS);
  57690. return EXPECT_RESULT();
  57691. }
  57692. #else
  57693. static int test_wolfSSL_CTX_set_ciphersuites(void)
  57694. {
  57695. return TEST_SKIPPED;
  57696. }
  57697. #endif
  57698. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  57699. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57700. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  57701. {
  57702. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57703. return TEST_SUCCESS;
  57704. }
  57705. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  57706. {
  57707. EXPECT_DECLS;
  57708. WOLFSSL_ALERT_HISTORY h;
  57709. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  57710. ExpectIntEQ(h.last_rx.level, alert_fatal);
  57711. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  57712. return EXPECT_RESULT();
  57713. }
  57714. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  57715. {
  57716. EXPECT_DECLS;
  57717. test_ssl_cbf client_cbs, server_cbs;
  57718. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57719. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57720. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  57721. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  57722. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  57723. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  57724. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  57725. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57726. &server_cbs, NULL), TEST_FAIL);
  57727. return EXPECT_RESULT();
  57728. }
  57729. #else
  57730. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  57731. {
  57732. return TEST_SKIPPED;
  57733. }
  57734. #endif
  57735. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  57736. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  57737. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57738. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  57739. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  57740. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  57741. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  57742. static int test_TLS_13_ticket_different_ciphers_run = 0;
  57743. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  57744. {
  57745. EXPECT_DECLS;
  57746. switch (test_TLS_13_ticket_different_ciphers_run) {
  57747. case 0:
  57748. /* First run */
  57749. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57750. WOLFSSL_SUCCESS);
  57751. if (wolfSSL_is_server(ssl)) {
  57752. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  57753. wolfSSL_get_SSL_CTX(ssl));
  57754. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  57755. test_TLS_13_ticket_different_ciphers_ctx));
  57756. }
  57757. break;
  57758. case 1:
  57759. /* Second run */
  57760. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57761. "TLS13-AES128-GCM-SHA256"),
  57762. WOLFSSL_SUCCESS);
  57763. if (!wolfSSL_is_server(ssl)) {
  57764. ExpectIntEQ(wolfSSL_set_session(ssl,
  57765. test_TLS_13_ticket_different_ciphers_session),
  57766. WOLFSSL_SUCCESS);
  57767. }
  57768. break;
  57769. default:
  57770. /* Bad state? */
  57771. Fail(("Should not enter here"), ("Should not enter here"));
  57772. }
  57773. return EXPECT_RESULT();
  57774. }
  57775. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  57776. {
  57777. EXPECT_DECLS;
  57778. switch (test_TLS_13_ticket_different_ciphers_run) {
  57779. case 0:
  57780. /* First run */
  57781. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  57782. wolfSSL_get1_session(ssl));
  57783. break;
  57784. case 1:
  57785. /* Second run */
  57786. ExpectTrue(wolfSSL_session_reused(ssl));
  57787. break;
  57788. default:
  57789. /* Bad state? */
  57790. Fail(("Should not enter here"), ("Should not enter here"));
  57791. }
  57792. return EXPECT_RESULT();
  57793. }
  57794. static int test_TLS_13_ticket_different_ciphers(void)
  57795. {
  57796. EXPECT_DECLS;
  57797. /* Check that we handle the connection when the ticket doesn't match
  57798. * the first ciphersuite. */
  57799. test_ssl_cbf client_cbs, server_cbs;
  57800. struct test_params {
  57801. method_provider client_meth;
  57802. method_provider server_meth;
  57803. int doUdp;
  57804. } params[] = {
  57805. #ifdef WOLFSSL_DTLS13
  57806. /* Test that the stateless code handles sessions correctly */
  57807. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  57808. #endif
  57809. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  57810. };
  57811. size_t i;
  57812. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57813. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57814. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57815. test_TLS_13_ticket_different_ciphers_run = 0;
  57816. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  57817. client_cbs.method = params[i].client_meth;
  57818. server_cbs.method = params[i].server_meth;
  57819. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  57820. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  57821. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  57822. server_cbs.ticNoInit = 1;
  57823. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57824. &server_cbs, NULL), TEST_SUCCESS);
  57825. test_TLS_13_ticket_different_ciphers_run++;
  57826. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  57827. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57828. &server_cbs, NULL), TEST_SUCCESS);
  57829. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  57830. test_TLS_13_ticket_different_ciphers_session = NULL;
  57831. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  57832. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  57833. }
  57834. return EXPECT_RESULT();
  57835. }
  57836. #else
  57837. static int test_TLS_13_ticket_different_ciphers(void)
  57838. {
  57839. return TEST_SKIPPED;
  57840. }
  57841. #endif
  57842. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  57843. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57844. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  57845. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  57846. byte test_extra_alerts_wrong_cs_sh[] = {
  57847. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  57848. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  57849. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  57850. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  57851. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  57852. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  57853. 0xbb, 0x07, 0x54, 0x1c,
  57854. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  57855. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  57856. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  57857. };
  57858. static int test_extra_alerts_wrong_cs(void)
  57859. {
  57860. EXPECT_DECLS;
  57861. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  57862. struct test_memio_ctx test_ctx;
  57863. WOLFSSL_CTX *ctx_c = NULL;
  57864. WOLFSSL_ALERT_HISTORY h;
  57865. WOLFSSL *ssl_c = NULL;
  57866. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57867. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  57868. wolfTLSv1_2_client_method, NULL), 0);
  57869. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  57870. WOLFSSL_SUCCESS);
  57871. /* CH */
  57872. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57873. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57874. WOLFSSL_ERROR_WANT_READ);
  57875. /* consume CH */
  57876. test_ctx.s_len = 0;
  57877. /* inject SH */
  57878. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  57879. sizeof(test_extra_alerts_wrong_cs_sh));
  57880. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  57881. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57882. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57883. WOLFSSL_ERROR_WANT_READ);
  57884. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57885. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  57886. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57887. wolfSSL_free(ssl_c);
  57888. wolfSSL_CTX_free(ctx_c);
  57889. #endif
  57890. return EXPECT_RESULT();
  57891. }
  57892. #else
  57893. static int test_extra_alerts_wrong_cs(void)
  57894. {
  57895. return TEST_SKIPPED;
  57896. }
  57897. #endif
  57898. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  57899. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  57900. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  57901. {
  57902. tail_len -= msg_length;
  57903. XMEMMOVE(msg, msg + msg_length, tail_len);
  57904. *len = *len - msg_length;
  57905. }
  57906. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  57907. byte *found)
  57908. {
  57909. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  57910. const unsigned int _RECORD_HEADER_SZ = 5;
  57911. const int _change_cipher_hs = 55;
  57912. const int _change_cipher = 20;
  57913. const int _handshake = 22;
  57914. unsigned int tail_len;
  57915. byte *idx, *curr;
  57916. word8 currType;
  57917. word16 rLength;
  57918. word32 hLength;
  57919. idx = buf;
  57920. tail_len = *len;
  57921. *found = 0;
  57922. while (tail_len > _RECORD_HEADER_SZ) {
  57923. curr = idx;
  57924. currType = *idx;
  57925. ato16(idx + 3, &rLength);
  57926. idx += _RECORD_HEADER_SZ;
  57927. tail_len -= _RECORD_HEADER_SZ;
  57928. if (tail_len < rLength)
  57929. return -1;
  57930. if (type == _change_cipher_hs && currType == _change_cipher) {
  57931. if (rLength != 1)
  57932. return -1;
  57933. /* match */
  57934. test_remove_msg(curr, *len - (int)(curr - buf),
  57935. len, _RECORD_HEADER_SZ + 1);
  57936. *found = 1;
  57937. return 0;
  57938. }
  57939. if (currType != _handshake) {
  57940. idx += rLength;
  57941. tail_len -= rLength;
  57942. continue;
  57943. }
  57944. if (rLength < _HANDSHAKE_HEADER_SZ)
  57945. return -1;
  57946. currType = *idx;
  57947. ato24(idx+1, &hLength);
  57948. hLength += _HANDSHAKE_HEADER_SZ;
  57949. if (tail_len < hLength)
  57950. return -1;
  57951. if (currType != type) {
  57952. idx += hLength;
  57953. tail_len -= hLength;
  57954. continue;
  57955. }
  57956. /* match */
  57957. test_remove_msg(curr, *len - (int)(curr - buf), len,
  57958. hLength + _RECORD_HEADER_SZ);
  57959. *found = 1;
  57960. return 0;
  57961. }
  57962. /* not found */
  57963. return 0;
  57964. }
  57965. static int test_remove_hs_message(byte hs_message_type,
  57966. int extra_round, byte alert_type)
  57967. {
  57968. EXPECT_DECLS;
  57969. WOLFSSL_CTX *ctx_c = NULL;
  57970. WOLFSSL_CTX *ctx_s = NULL;
  57971. WOLFSSL *ssl_c = NULL;
  57972. WOLFSSL *ssl_s = NULL;
  57973. struct test_memio_ctx test_ctx;
  57974. WOLFSSL_ALERT_HISTORY h;
  57975. byte found = 0;
  57976. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57977. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57978. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57979. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57980. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57981. WOLFSSL_ERROR_WANT_READ);
  57982. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57983. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57984. WOLFSSL_ERROR_WANT_READ);
  57985. if (extra_round) {
  57986. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57987. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57988. WOLFSSL_ERROR_WANT_READ);
  57989. /* this will complete handshake from server side */
  57990. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57991. }
  57992. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  57993. &test_ctx.c_len, hs_message_type, &found), 0);
  57994. if (!found) {
  57995. wolfSSL_free(ssl_c);
  57996. wolfSSL_CTX_free(ctx_c);
  57997. wolfSSL_free(ssl_s);
  57998. wolfSSL_CTX_free(ctx_s);
  57999. return TEST_SKIPPED;
  58000. }
  58001. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58002. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58003. WOLFSSL_ERROR_WANT_READ);
  58004. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58005. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  58006. ExpectIntEQ(h.last_tx.level, alert_fatal);
  58007. wolfSSL_free(ssl_c);
  58008. wolfSSL_CTX_free(ctx_c);
  58009. wolfSSL_free(ssl_s);
  58010. wolfSSL_CTX_free(ctx_s);
  58011. return EXPECT_RESULT();
  58012. }
  58013. static int test_extra_alerts_skip_hs(void)
  58014. {
  58015. EXPECT_DECLS;
  58016. const byte _server_key_exchange = 12;
  58017. const byte _server_hello = 2;
  58018. const byte _certificate = 11;
  58019. /* server_hello */
  58020. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  58021. unexpected_message), TEST_FAIL);
  58022. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  58023. 0xff), TEST_FAIL);
  58024. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  58025. unexpected_message), TEST_FAIL);
  58026. return EXPECT_RESULT();
  58027. }
  58028. #else
  58029. static int test_extra_alerts_skip_hs(void)
  58030. {
  58031. return TEST_SKIPPED;
  58032. }
  58033. #endif
  58034. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58035. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  58036. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  58037. unsigned char* key, unsigned int key_max_len)
  58038. {
  58039. (void)ssl;
  58040. (void)id;
  58041. (void)key_max_len;
  58042. /* zero means error */
  58043. key[0] = 0x10;
  58044. return 1;
  58045. }
  58046. static int test_extra_alerts_bad_psk(void)
  58047. {
  58048. EXPECT_DECLS;
  58049. WOLFSSL_CTX *ctx_c = NULL;
  58050. WOLFSSL_CTX *ctx_s = NULL;
  58051. WOLFSSL *ssl_c = NULL;
  58052. WOLFSSL *ssl_s = NULL;
  58053. struct test_memio_ctx test_ctx;
  58054. WOLFSSL_ALERT_HISTORY h;
  58055. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58056. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58057. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58058. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  58059. WOLFSSL_SUCCESS);
  58060. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  58061. WOLFSSL_SUCCESS);
  58062. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  58063. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58064. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58065. WOLFSSL_ERROR_WANT_READ);
  58066. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58067. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58068. WOLFSSL_ERROR_WANT_READ);
  58069. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58070. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58071. WOLFSSL_ERROR_WANT_READ);
  58072. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58073. ExpectIntEQ(h.last_tx.code, handshake_failure);
  58074. ExpectIntEQ(h.last_tx.level, alert_fatal);
  58075. wolfSSL_free(ssl_c);
  58076. wolfSSL_CTX_free(ctx_c);
  58077. wolfSSL_free(ssl_s);
  58078. wolfSSL_CTX_free(ctx_s);
  58079. return EXPECT_RESULT();
  58080. }
  58081. #else
  58082. static int test_extra_alerts_bad_psk(void)
  58083. {
  58084. return TEST_SKIPPED;
  58085. }
  58086. #endif
  58087. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58088. && !defined(NO_PSK)
  58089. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  58090. const char* hint, char* identity, unsigned int id_max_len,
  58091. unsigned char* key, unsigned int key_max_len)
  58092. {
  58093. (void)ssl;
  58094. (void)hint;
  58095. (void)key_max_len;
  58096. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  58097. XSTRNCPY(identity, "Client_identity", id_max_len);
  58098. key[0] = 0x20;
  58099. return 1;
  58100. }
  58101. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  58102. const char* id, unsigned char* key, unsigned int key_max_len)
  58103. {
  58104. (void)ssl;
  58105. (void)id;
  58106. (void)key_max_len;
  58107. /* zero means error */
  58108. key[0] = 0x10;
  58109. return 1;
  58110. }
  58111. #endif
  58112. static int test_tls13_bad_psk_binder(void)
  58113. {
  58114. EXPECT_DECLS;
  58115. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58116. && !defined(NO_PSK)
  58117. WOLFSSL_CTX *ctx_c = NULL;
  58118. WOLFSSL_CTX *ctx_s = NULL;
  58119. WOLFSSL *ssl_c = NULL;
  58120. WOLFSSL *ssl_s = NULL;
  58121. struct test_memio_ctx test_ctx;
  58122. WOLFSSL_ALERT_HISTORY h;
  58123. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58124. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58125. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58126. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  58127. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  58128. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58129. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58130. WOLFSSL_ERROR_WANT_READ);
  58131. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58132. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58133. BAD_BINDER);
  58134. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58135. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58136. FATAL_ERROR);
  58137. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58138. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  58139. ExpectIntEQ(h.last_rx.level, alert_fatal);
  58140. wolfSSL_free(ssl_c);
  58141. wolfSSL_CTX_free(ctx_c);
  58142. wolfSSL_free(ssl_s);
  58143. wolfSSL_CTX_free(ctx_s);
  58144. #endif
  58145. return EXPECT_RESULT();
  58146. }
  58147. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  58148. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58149. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  58150. int sz, void *ctx)
  58151. {
  58152. static int sentServerHello = FALSE;
  58153. if (!sentServerHello) {
  58154. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  58155. size_t i;
  58156. if (sz < (int)sizeof(renegExt))
  58157. return WOLFSSL_CBIO_ERR_GENERAL;
  58158. /* Remove SCR from ServerHello */
  58159. for (i = 0; i < sz - sizeof(renegExt); i++) {
  58160. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  58161. /* Found the extension. Change it to something unrecognized. */
  58162. buf[i+1] = 0x11;
  58163. break;
  58164. }
  58165. }
  58166. sentServerHello = TRUE;
  58167. }
  58168. return EmbedSend(ssl, buf, sz, ctx);
  58169. }
  58170. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  58171. {
  58172. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  58173. }
  58174. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  58175. {
  58176. WOLFSSL_ALERT_HISTORY h;
  58177. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  58178. AssertIntEQ(h.last_rx.code, handshake_failure);
  58179. AssertIntEQ(h.last_rx.level, alert_fatal);
  58180. }
  58181. static int test_harden_no_secure_renegotiation(void)
  58182. {
  58183. EXPECT_DECLS;
  58184. callback_functions client_cbs, server_cbs;
  58185. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58186. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58187. client_cbs.method = wolfTLSv1_2_client_method;
  58188. server_cbs.method = wolfTLSv1_2_server_method;
  58189. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  58190. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  58191. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58192. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  58193. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  58194. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  58195. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  58196. server_cbs.last_err == FATAL_ERROR);
  58197. return EXPECT_RESULT();
  58198. }
  58199. #else
  58200. static int test_harden_no_secure_renegotiation(void)
  58201. {
  58202. return TEST_SKIPPED;
  58203. }
  58204. #endif
  58205. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58206. static int test_override_alt_cert_chain_cert_cb(int preverify,
  58207. WOLFSSL_X509_STORE_CTX* store)
  58208. {
  58209. fprintf(stderr, "preverify: %d\n", preverify);
  58210. fprintf(stderr, "store->error: %d\n", store->error);
  58211. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  58212. if (store->error == OCSP_INVALID_STATUS) {
  58213. fprintf(stderr, "Overriding OCSP error\n");
  58214. return 1;
  58215. }
  58216. #ifndef WOLFSSL_ALT_CERT_CHAINS
  58217. else if ((store->error == ASN_NO_SIGNER_E ||
  58218. store->error == ASN_SELF_SIGNED_E
  58219. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  58220. defined(HAVE_WEBSERVER)
  58221. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  58222. #endif
  58223. ) && store->error_depth == store->totalCerts - 1) {
  58224. fprintf(stderr, "Overriding no signer error only for root cert\n");
  58225. return 1;
  58226. }
  58227. #endif
  58228. else
  58229. return preverify;
  58230. }
  58231. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  58232. int urlSz, unsigned char* request, int requestSz,
  58233. unsigned char** response)
  58234. {
  58235. (void)ioCtx;
  58236. (void)url;
  58237. (void)urlSz;
  58238. (void)request;
  58239. (void)requestSz;
  58240. (void)response;
  58241. return -1;
  58242. }
  58243. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  58244. {
  58245. EXPECT_DECLS;
  58246. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  58247. test_override_alt_cert_chain_cert_cb);
  58248. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  58249. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  58250. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  58251. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  58252. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  58253. WOLFSSL_SUCCESS);
  58254. return EXPECT_RESULT();
  58255. }
  58256. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  58257. {
  58258. EXPECT_DECLS;
  58259. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  58260. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  58261. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  58262. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  58263. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  58264. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  58265. WOLFSSL_SUCCESS);
  58266. return EXPECT_RESULT();
  58267. }
  58268. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  58269. {
  58270. EXPECT_DECLS;
  58271. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  58272. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  58273. return EXPECT_RESULT();
  58274. }
  58275. static int test_override_alt_cert_chain(void)
  58276. {
  58277. EXPECT_DECLS;
  58278. size_t i;
  58279. struct test_params {
  58280. ctx_cb client_ctx_cb;
  58281. ctx_cb server_ctx_cb;
  58282. int result;
  58283. } params[] = {
  58284. {test_override_alt_cert_chain_client_ctx_ready,
  58285. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  58286. {test_override_alt_cert_chain_client_ctx_ready2,
  58287. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  58288. };
  58289. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  58290. test_ssl_cbf client_cbs, server_cbs;
  58291. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58292. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58293. fprintf(stderr, "test config: %d\n", (int)i);
  58294. client_cbs.ctx_ready = params[i].client_ctx_cb;
  58295. server_cbs.ctx_ready = params[i].server_ctx_cb;
  58296. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58297. &server_cbs, NULL), params[i].result);
  58298. ExpectIntEQ(client_cbs.return_code, params[i].result);
  58299. ExpectIntEQ(server_cbs.return_code, params[i].result);
  58300. }
  58301. return EXPECT_RESULT();
  58302. }
  58303. #else
  58304. static int test_override_alt_cert_chain(void)
  58305. {
  58306. return TEST_SKIPPED;
  58307. }
  58308. #endif
  58309. #if defined(HAVE_RPK)
  58310. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  58311. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  58312. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  58313. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  58314. {
  58315. int ret = WOLFSSL_SUCCESS;
  58316. (void)mode;
  58317. (void)strctx;
  58318. WOLFSSL_ENTER("MyRpkVerifyCb");
  58319. return ret;
  58320. }
  58321. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  58322. static WC_INLINE int test_rpk_memio_setup(
  58323. struct test_memio_ctx *ctx,
  58324. WOLFSSL_CTX **ctx_c,
  58325. WOLFSSL_CTX **ctx_s,
  58326. WOLFSSL **ssl_c,
  58327. WOLFSSL **ssl_s,
  58328. method_provider method_c,
  58329. method_provider method_s,
  58330. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  58331. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  58332. const char* pkey_c, int fmt_kc, /* client private key and format */
  58333. const char* pkey_s, int fmt_ks /* server private key and format */
  58334. )
  58335. {
  58336. int ret;
  58337. if (ctx_c != NULL && *ctx_c == NULL) {
  58338. *ctx_c = wolfSSL_CTX_new(method_c());
  58339. if (*ctx_c == NULL) {
  58340. return -1;
  58341. }
  58342. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  58343. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  58344. if (ret != WOLFSSL_SUCCESS) {
  58345. return -1;
  58346. }
  58347. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  58348. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  58349. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  58350. if (ret != WOLFSSL_SUCCESS) {
  58351. return -1;
  58352. }
  58353. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  58354. if (ret != WOLFSSL_SUCCESS) {
  58355. return -1;
  58356. }
  58357. }
  58358. if (ctx_s != NULL && *ctx_s == NULL) {
  58359. *ctx_s = wolfSSL_CTX_new(method_s());
  58360. if (*ctx_s == NULL) {
  58361. return -1;
  58362. }
  58363. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  58364. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  58365. if (ret != WOLFSSL_SUCCESS) {
  58366. return -1;
  58367. }
  58368. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  58369. if (ret != WOLFSSL_SUCCESS) {
  58370. return -1;
  58371. }
  58372. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  58373. if (ret != WOLFSSL_SUCCESS) {
  58374. return -1;
  58375. }
  58376. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  58377. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  58378. if (ctx->s_ciphers != NULL) {
  58379. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  58380. if (ret != WOLFSSL_SUCCESS) {
  58381. return -1;
  58382. }
  58383. }
  58384. }
  58385. if (ctx_c != NULL && ssl_c != NULL) {
  58386. *ssl_c = wolfSSL_new(*ctx_c);
  58387. if (*ssl_c == NULL) {
  58388. return -1;
  58389. }
  58390. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  58391. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  58392. }
  58393. if (ctx_s != NULL && ssl_s != NULL) {
  58394. *ssl_s = wolfSSL_new(*ctx_s);
  58395. if (*ssl_s == NULL) {
  58396. return -1;
  58397. }
  58398. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  58399. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  58400. #if !defined(NO_DH)
  58401. SetDH(*ssl_s);
  58402. #endif
  58403. }
  58404. return 0;
  58405. }
  58406. #endif /* HAVE_RPK */
  58407. static int test_rpk_set_xxx_cert_type(void)
  58408. {
  58409. EXPECT_DECLS;
  58410. #if defined(HAVE_RPK)
  58411. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  58412. WOLFSSL_CTX* ctx = NULL;
  58413. WOLFSSL* ssl = NULL;
  58414. int tp;
  58415. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  58416. ExpectNotNull(ctx);
  58417. ssl = wolfSSL_new(ctx);
  58418. ExpectNotNull(ssl);
  58419. /*--------------------------------------------*/
  58420. /* tests for wolfSSL_CTX_set_client_cert_type */
  58421. /*--------------------------------------------*/
  58422. /* illegal parameter test caces */
  58423. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  58424. MAX_CLIENT_CERT_TYPE_CNT),
  58425. BAD_FUNC_ARG);
  58426. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58427. sizeof(ctype)),
  58428. BAD_FUNC_ARG);
  58429. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  58430. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  58431. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58432. MAX_CLIENT_CERT_TYPE_CNT),
  58433. BAD_FUNC_ARG);
  58434. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  58435. ctype[1] = 10; /* set unknown cert type */
  58436. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58437. MAX_CLIENT_CERT_TYPE_CNT),
  58438. BAD_FUNC_ARG);
  58439. /* pass larger type count */
  58440. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  58441. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  58442. ctype[2] = 1; /* pass unacceptable type count */
  58443. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58444. MAX_CLIENT_CERT_TYPE_CNT + 1),
  58445. BAD_FUNC_ARG);
  58446. /* should accept NULL for type buffer */
  58447. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  58448. MAX_CLIENT_CERT_TYPE_CNT),
  58449. WOLFSSL_SUCCESS);
  58450. /* should accept zero for type count */
  58451. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58452. 0),
  58453. WOLFSSL_SUCCESS);
  58454. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58455. MAX_CLIENT_CERT_TYPE_CNT),
  58456. WOLFSSL_SUCCESS);
  58457. /*--------------------------------------------*/
  58458. /* tests for wolfSSL_CTX_set_server_cert_type */
  58459. /*--------------------------------------------*/
  58460. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  58461. MAX_SERVER_CERT_TYPE_CNT),
  58462. BAD_FUNC_ARG);
  58463. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58464. sizeof(ctype)),
  58465. BAD_FUNC_ARG);
  58466. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  58467. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  58468. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58469. MAX_SERVER_CERT_TYPE_CNT),
  58470. BAD_FUNC_ARG);
  58471. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  58472. ctype[1] = 10; /* set unknown cert type */
  58473. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58474. MAX_SERVER_CERT_TYPE_CNT),
  58475. BAD_FUNC_ARG);
  58476. /* pass larger type count */
  58477. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  58478. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  58479. ctype[2] = 1; /* pass unacceptable type count */
  58480. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58481. MAX_SERVER_CERT_TYPE_CNT + 1),
  58482. BAD_FUNC_ARG);
  58483. /* should accept NULL for type buffer */
  58484. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  58485. MAX_SERVER_CERT_TYPE_CNT),
  58486. WOLFSSL_SUCCESS);
  58487. /* should accept zero for type count */
  58488. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58489. 0),
  58490. WOLFSSL_SUCCESS);
  58491. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58492. MAX_CLIENT_CERT_TYPE_CNT),
  58493. WOLFSSL_SUCCESS);
  58494. /*--------------------------------------------*/
  58495. /* tests for wolfSSL_set_client_cert_type */
  58496. /*--------------------------------------------*/
  58497. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  58498. MAX_CLIENT_CERT_TYPE_CNT),
  58499. BAD_FUNC_ARG);
  58500. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58501. sizeof(ctype)),
  58502. BAD_FUNC_ARG);
  58503. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  58504. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  58505. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58506. MAX_CLIENT_CERT_TYPE_CNT),
  58507. BAD_FUNC_ARG);
  58508. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  58509. ctype[1] = 10; /* set unknown cert type */
  58510. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58511. MAX_CLIENT_CERT_TYPE_CNT),
  58512. BAD_FUNC_ARG);
  58513. /* pass larger type count */
  58514. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  58515. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  58516. ctype[2] = 1; /* pass unacceptable type count */
  58517. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58518. MAX_CLIENT_CERT_TYPE_CNT + 1),
  58519. BAD_FUNC_ARG);
  58520. /* should accept NULL for type buffer */
  58521. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  58522. MAX_CLIENT_CERT_TYPE_CNT),
  58523. WOLFSSL_SUCCESS);
  58524. /* should accept zero for type count */
  58525. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58526. 0),
  58527. WOLFSSL_SUCCESS);
  58528. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58529. MAX_CLIENT_CERT_TYPE_CNT),
  58530. WOLFSSL_SUCCESS);
  58531. /*--------------------------------------------*/
  58532. /* tests for wolfSSL_CTX_set_server_cert_type */
  58533. /*--------------------------------------------*/
  58534. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  58535. MAX_SERVER_CERT_TYPE_CNT),
  58536. BAD_FUNC_ARG);
  58537. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58538. sizeof(ctype)),
  58539. BAD_FUNC_ARG);
  58540. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  58541. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  58542. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58543. MAX_SERVER_CERT_TYPE_CNT),
  58544. BAD_FUNC_ARG);
  58545. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  58546. ctype[1] = 10; /* set unknown cert type */
  58547. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58548. MAX_SERVER_CERT_TYPE_CNT),
  58549. BAD_FUNC_ARG);
  58550. /* pass larger type count */
  58551. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  58552. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  58553. ctype[2] = 1; /* pass unacceptable type count */
  58554. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58555. MAX_SERVER_CERT_TYPE_CNT + 1),
  58556. BAD_FUNC_ARG);
  58557. /* should accept NULL for type buffer */
  58558. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  58559. MAX_SERVER_CERT_TYPE_CNT),
  58560. WOLFSSL_SUCCESS);
  58561. /* should accept zero for type count */
  58562. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58563. 0),
  58564. WOLFSSL_SUCCESS);
  58565. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58566. MAX_SERVER_CERT_TYPE_CNT),
  58567. WOLFSSL_SUCCESS);
  58568. /*------------------------------------------------*/
  58569. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  58570. /*------------------------------------------------*/
  58571. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  58572. BAD_FUNC_ARG);
  58573. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  58574. BAD_FUNC_ARG);
  58575. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  58576. BAD_FUNC_ARG);
  58577. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  58578. BAD_FUNC_ARG);
  58579. /* clean up */
  58580. wolfSSL_free(ssl);
  58581. wolfSSL_CTX_free(ctx);
  58582. #endif
  58583. return EXPECT_RESULT();
  58584. }
  58585. static int test_tls13_rpk_handshake(void)
  58586. {
  58587. EXPECT_DECLS;
  58588. #if defined(HAVE_RPK)
  58589. int ret = 0;
  58590. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  58591. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  58592. struct test_memio_ctx test_ctx;
  58593. int err;
  58594. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  58595. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  58596. int typeCnt_c;
  58597. int typeCnt_s;
  58598. int tp;
  58599. (void)err;
  58600. (void)typeCnt_c;
  58601. (void)typeCnt_s;
  58602. (void)certType_c;
  58603. (void)certType_s;
  58604. /* TLS1.2
  58605. * Both client and server load x509 cert and start handshaking.
  58606. * Check no negotiation occurred.
  58607. */
  58608. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58609. ExpectIntEQ(
  58610. test_rpk_memio_setup(
  58611. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58612. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  58613. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58614. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58615. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58616. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  58617. , 0);
  58618. /* set client certificate type in client end */
  58619. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58620. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58621. typeCnt_c = 2;
  58622. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58623. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58624. typeCnt_s = 2;
  58625. /* both clien and server do not call client/server_cert_type APIs,
  58626. * expecting default settings works and no negotiation performed.
  58627. */
  58628. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58629. return TEST_FAIL;
  58630. /* confirm no negotiation occurred */
  58631. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58632. WOLFSSL_SUCCESS);
  58633. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58634. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58635. WOLFSSL_SUCCESS);
  58636. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58637. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58638. WOLFSSL_SUCCESS);
  58639. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58640. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58641. WOLFSSL_SUCCESS);
  58642. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58643. wolfSSL_free(ssl_c);
  58644. wolfSSL_CTX_free(ctx_c);
  58645. wolfSSL_free(ssl_s);
  58646. wolfSSL_CTX_free(ctx_s);
  58647. ssl_c = ssl_s = NULL;
  58648. ctx_c = ctx_s = NULL;
  58649. /* Both client and server load x509 cert and start handshaking.
  58650. * Check no negotiation occurred.
  58651. */
  58652. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58653. ExpectIntEQ(
  58654. test_rpk_memio_setup(
  58655. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58656. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58657. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58658. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58659. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58660. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58661. , 0);
  58662. /* set client certificate type in client end */
  58663. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58664. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58665. typeCnt_c = 2;
  58666. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58667. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58668. typeCnt_s = 2;
  58669. /* both clien and server do not call client/server_cert_type APIs,
  58670. * expecting default settings works and no negotiation performed.
  58671. */
  58672. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58673. return TEST_FAIL;
  58674. /* confirm no negotiation occurred */
  58675. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58676. WOLFSSL_SUCCESS);
  58677. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58678. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58679. WOLFSSL_SUCCESS);
  58680. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58681. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58682. WOLFSSL_SUCCESS);
  58683. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58684. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58685. WOLFSSL_SUCCESS);
  58686. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58687. wolfSSL_free(ssl_c);
  58688. wolfSSL_CTX_free(ctx_c);
  58689. wolfSSL_free(ssl_s);
  58690. wolfSSL_CTX_free(ctx_s);
  58691. ssl_c = ssl_s = NULL;
  58692. ctx_c = ctx_s = NULL;
  58693. /* Both client and server load RPK cert and start handshaking.
  58694. * Confirm negotiated cert types match as expected.
  58695. */
  58696. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58697. ExpectIntEQ(
  58698. test_rpk_memio_setup(
  58699. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58700. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58701. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58702. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58703. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58704. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58705. , 0);
  58706. /* set client certificate type in client end */
  58707. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58708. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58709. typeCnt_c = 2;
  58710. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58711. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58712. typeCnt_s = 2;
  58713. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58714. WOLFSSL_SUCCESS);
  58715. /* set server certificate type in client end */
  58716. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58717. WOLFSSL_SUCCESS);
  58718. /* set client certificate type in server end */
  58719. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58720. WOLFSSL_SUCCESS);
  58721. /* set server certificate type in server end */
  58722. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58723. WOLFSSL_SUCCESS);
  58724. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58725. return TEST_FAIL;
  58726. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58727. WOLFSSL_SUCCESS);
  58728. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58729. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58730. WOLFSSL_SUCCESS);
  58731. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58732. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58733. WOLFSSL_SUCCESS);
  58734. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58735. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58736. WOLFSSL_SUCCESS);
  58737. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58738. wolfSSL_free(ssl_c);
  58739. wolfSSL_CTX_free(ctx_c);
  58740. wolfSSL_free(ssl_s);
  58741. wolfSSL_CTX_free(ctx_s);
  58742. ssl_c = ssl_s = NULL;
  58743. ctx_c = ctx_s = NULL;
  58744. /* TLS1.2
  58745. * Both client and server load RPK cert and start handshaking.
  58746. * Confirm negotiated cert types match as expected.
  58747. */
  58748. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58749. ExpectIntEQ(
  58750. test_rpk_memio_setup(
  58751. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58752. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  58753. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58754. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58755. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58756. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58757. , 0);
  58758. /* set client certificate type in client end */
  58759. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58760. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58761. typeCnt_c = 2;
  58762. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58763. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58764. typeCnt_s = 2;
  58765. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58766. WOLFSSL_SUCCESS);
  58767. /* set server certificate type in client end */
  58768. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58769. WOLFSSL_SUCCESS);
  58770. /* set client certificate type in server end */
  58771. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58772. WOLFSSL_SUCCESS);
  58773. /* set server certificate type in server end */
  58774. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58775. WOLFSSL_SUCCESS);
  58776. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58777. return TEST_FAIL;
  58778. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58779. WOLFSSL_SUCCESS);
  58780. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58781. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58782. WOLFSSL_SUCCESS);
  58783. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58784. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58785. WOLFSSL_SUCCESS);
  58786. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58787. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58788. WOLFSSL_SUCCESS);
  58789. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58790. wolfSSL_free(ssl_c);
  58791. wolfSSL_CTX_free(ctx_c);
  58792. wolfSSL_free(ssl_s);
  58793. wolfSSL_CTX_free(ctx_s);
  58794. ssl_c = ssl_s = NULL;
  58795. ctx_c = ctx_s = NULL;
  58796. /* Both client and server load x509 cert.
  58797. * Have client call set_client_cert_type with both RPK and x509.
  58798. * This doesn't makes client add client cert type extension to ClientHello,
  58799. * since it does not load RPK cert actually.
  58800. */
  58801. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58802. ExpectIntEQ(
  58803. test_rpk_memio_setup(
  58804. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58805. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58806. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58807. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58808. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58809. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58810. , 0);
  58811. /* set client certificate type in client end */
  58812. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58813. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58814. typeCnt_c = 2;
  58815. /* client indicates both RPK and x509 certs are available but loaded RPK
  58816. * cert only. It does not have client add client-cert-type extension in CH.
  58817. */
  58818. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58819. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58820. typeCnt_c = 2;
  58821. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58822. WOLFSSL_SUCCESS);
  58823. /* client indicates both RPK and x509 certs are acceptable */
  58824. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58825. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58826. typeCnt_s = 2;
  58827. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58828. WOLFSSL_SUCCESS);
  58829. /* server indicates both RPK and x509 certs are acceptable */
  58830. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58831. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58832. typeCnt_c = 2;
  58833. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58834. WOLFSSL_SUCCESS);
  58835. /* server should indicate only RPK cert is available */
  58836. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58837. certType_s[1] = -1;
  58838. typeCnt_s = 1;
  58839. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58840. WOLFSSL_SUCCESS);
  58841. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58842. return TEST_FAIL;
  58843. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  58844. * be returned as cert type.
  58845. */
  58846. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58847. WOLFSSL_SUCCESS);
  58848. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58849. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58850. WOLFSSL_SUCCESS);
  58851. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58852. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58853. WOLFSSL_SUCCESS);
  58854. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58855. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58856. WOLFSSL_SUCCESS);
  58857. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58858. wolfSSL_free(ssl_c);
  58859. wolfSSL_CTX_free(ctx_c);
  58860. wolfSSL_free(ssl_s);
  58861. wolfSSL_CTX_free(ctx_s);
  58862. ssl_c = ssl_s = NULL;
  58863. ctx_c = ctx_s = NULL;
  58864. /* Have client load RPK cert and have server load x509 cert.
  58865. * Check the negotiation result from both ends.
  58866. */
  58867. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58868. ExpectIntEQ(
  58869. test_rpk_memio_setup(
  58870. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58871. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58872. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58873. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58874. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58875. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58876. , 0);
  58877. /* have client tell to use RPK cert */
  58878. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58879. certType_c[1] = -1;
  58880. typeCnt_c = 1;
  58881. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58882. WOLFSSL_SUCCESS);
  58883. /* have client tell to accept both RPK and x509 cert */
  58884. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58885. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58886. typeCnt_s = 2;
  58887. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58888. WOLFSSL_SUCCESS);
  58889. /* have server accept to both RPK and x509 cert */
  58890. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58891. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58892. typeCnt_c = 2;
  58893. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58894. WOLFSSL_SUCCESS);
  58895. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  58896. * end, expecting the default setting works.
  58897. */
  58898. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58899. return TEST_FAIL;
  58900. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58901. WOLFSSL_SUCCESS);
  58902. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58903. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58904. WOLFSSL_SUCCESS);
  58905. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58906. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58907. WOLFSSL_SUCCESS);
  58908. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58909. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58910. WOLFSSL_SUCCESS);
  58911. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58912. wolfSSL_free(ssl_c);
  58913. wolfSSL_CTX_free(ctx_c);
  58914. wolfSSL_free(ssl_s);
  58915. wolfSSL_CTX_free(ctx_s);
  58916. ssl_c = ssl_s = NULL;
  58917. ctx_c = ctx_s = NULL;
  58918. /* Have both client and server load RPK cert, however, have server
  58919. * indicate its cert type x509.
  58920. * Client is expected to detect the cert type mismatch then to send alert
  58921. * with "unsupported_certificate".
  58922. */
  58923. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58924. ExpectIntEQ(
  58925. test_rpk_memio_setup(
  58926. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58927. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58928. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58929. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  58930. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58931. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58932. , 0);
  58933. /* have client tell to use RPK cert */
  58934. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58935. certType_c[1] = -1;
  58936. typeCnt_c = 1;
  58937. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58938. WOLFSSL_SUCCESS);
  58939. /* have client tell to accept both RPK and x509 cert */
  58940. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58941. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58942. typeCnt_s = 2;
  58943. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58944. WOLFSSL_SUCCESS);
  58945. /* have server accept to both RPK and x509 cert */
  58946. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58947. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58948. typeCnt_c = 2;
  58949. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58950. WOLFSSL_SUCCESS);
  58951. /* have server tell to use x509 cert intentionally. This will bring
  58952. * certificate type mismatch in client side.
  58953. */
  58954. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58955. certType_s[1] = -1;
  58956. typeCnt_s = 1;
  58957. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58958. WOLFSSL_SUCCESS);
  58959. /* expect client detect cert type mismatch then send Alert */
  58960. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58961. if (ret != -1)
  58962. return TEST_FAIL;
  58963. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  58964. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58965. WOLFSSL_SUCCESS);
  58966. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58967. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58968. WOLFSSL_SUCCESS);
  58969. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58970. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58971. WOLFSSL_SUCCESS);
  58972. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58973. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58974. WOLFSSL_SUCCESS);
  58975. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58976. wolfSSL_free(ssl_c);
  58977. wolfSSL_CTX_free(ctx_c);
  58978. wolfSSL_free(ssl_s);
  58979. wolfSSL_CTX_free(ctx_s);
  58980. ssl_c = ssl_s = NULL;
  58981. ctx_c = ctx_s = NULL;
  58982. /* Have client load x509 cert and server load RPK cert,
  58983. * however, have client indicate its cert type RPK.
  58984. * Server is expected to detect the cert type mismatch then to send alert
  58985. * with "unsupported_certificate".
  58986. */
  58987. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58988. ExpectIntEQ(
  58989. test_rpk_memio_setup(
  58990. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58991. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58992. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58993. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58994. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58995. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58996. , 0);
  58997. /* have client tell to use RPK cert intentionally */
  58998. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58999. certType_c[1] = -1;
  59000. typeCnt_c = 1;
  59001. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59002. WOLFSSL_SUCCESS);
  59003. /* have client tell to accept both RPK and x509 cert */
  59004. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59005. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  59006. typeCnt_s = 2;
  59007. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59008. WOLFSSL_SUCCESS);
  59009. /* have server accept to both RPK and x509 cert */
  59010. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  59011. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  59012. typeCnt_c = 2;
  59013. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59014. WOLFSSL_SUCCESS);
  59015. /* have server tell to use x509 cert intentionally. This will bring
  59016. * certificate type mismatch in client side.
  59017. */
  59018. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59019. certType_s[1] = -1;
  59020. typeCnt_s = 1;
  59021. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59022. WOLFSSL_SUCCESS);
  59023. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  59024. /* expect server detect cert type mismatch then send Alert */
  59025. ExpectIntNE(ret, 0);
  59026. err = wolfSSL_get_error(ssl_c, ret);
  59027. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  59028. /* client did not load RPK cert actually, so negotiation did not happen */
  59029. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59030. WOLFSSL_SUCCESS);
  59031. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59032. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59033. WOLFSSL_SUCCESS);
  59034. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59035. /* client did not load RPK cert actually, so negotiation did not happen */
  59036. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59037. WOLFSSL_SUCCESS);
  59038. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59039. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59040. WOLFSSL_SUCCESS);
  59041. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59042. wolfSSL_free(ssl_c);
  59043. wolfSSL_CTX_free(ctx_c);
  59044. wolfSSL_free(ssl_s);
  59045. wolfSSL_CTX_free(ctx_s);
  59046. ssl_c = ssl_s = NULL;
  59047. ctx_c = ctx_s = NULL;
  59048. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59049. /* Both client and server load RPK cert and set certificate verify
  59050. * callbacks then start handshaking.
  59051. * Confirm both side can refer the peer's cert.
  59052. */
  59053. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59054. ExpectIntEQ(
  59055. test_rpk_memio_setup(
  59056. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59057. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59058. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59059. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59060. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59061. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59062. , 0);
  59063. /* set client certificate type in client end */
  59064. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59065. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59066. typeCnt_c = 2;
  59067. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59068. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59069. typeCnt_s = 2;
  59070. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59071. WOLFSSL_SUCCESS);
  59072. /* set server certificate type in client end */
  59073. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59074. WOLFSSL_SUCCESS);
  59075. /* set client certificate type in server end */
  59076. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59077. WOLFSSL_SUCCESS);
  59078. /* set server certificate type in server end */
  59079. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59080. WOLFSSL_SUCCESS);
  59081. /* set certificate verify callback to both client and server */
  59082. int isServer = 0;
  59083. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  59084. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  59085. isServer = 1;
  59086. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  59087. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  59088. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  59089. if (ret != 0)
  59090. return TEST_FAIL;
  59091. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59092. WOLFSSL_SUCCESS);
  59093. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59094. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59095. WOLFSSL_SUCCESS);
  59096. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59097. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59098. WOLFSSL_SUCCESS);
  59099. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59100. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59101. WOLFSSL_SUCCESS);
  59102. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59103. wolfSSL_free(ssl_c);
  59104. wolfSSL_CTX_free(ctx_c);
  59105. wolfSSL_free(ssl_s);
  59106. wolfSSL_CTX_free(ctx_s);
  59107. ssl_c = ssl_s = NULL;
  59108. ctx_c = ctx_s = NULL;
  59109. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  59110. #endif /* HAVE_RPK */
  59111. return EXPECT_RESULT();
  59112. }
  59113. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59114. static int test_dtls13_bad_epoch_ch(void)
  59115. {
  59116. EXPECT_DECLS;
  59117. WOLFSSL_CTX *ctx_c = NULL;
  59118. WOLFSSL_CTX *ctx_s = NULL;
  59119. WOLFSSL *ssl_c = NULL;
  59120. WOLFSSL *ssl_s = NULL;
  59121. struct test_memio_ctx test_ctx;
  59122. const int EPOCH_OFF = 3;
  59123. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59124. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59125. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59126. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  59127. * with just one message */
  59128. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  59129. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59130. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59131. WOLFSSL_ERROR_WANT_READ);
  59132. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  59133. /* first CH should use epoch 0x0 */
  59134. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  59135. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  59136. /* change epoch to 2 */
  59137. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  59138. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59139. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59140. WOLFSSL_ERROR_WANT_READ);
  59141. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  59142. /* resend the CH */
  59143. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59144. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59145. wolfSSL_free(ssl_c);
  59146. wolfSSL_CTX_free(ctx_c);
  59147. wolfSSL_free(ssl_s);
  59148. wolfSSL_CTX_free(ctx_s);
  59149. return EXPECT_RESULT();
  59150. }
  59151. #else
  59152. static int test_dtls13_bad_epoch_ch(void)
  59153. {
  59154. return TEST_SKIPPED;
  59155. }
  59156. #endif
  59157. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  59158. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  59159. {
  59160. EXPECT_DECLS;
  59161. WOLFSSL_SESSION *sess = NULL;
  59162. /* Setup the session to avoid errors */
  59163. ssl->session->timeout = -1;
  59164. ssl->session->side = WOLFSSL_CLIENT_END;
  59165. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  59166. defined(HAVE_SESSION_TICKET))
  59167. ssl->session->version = ssl->version;
  59168. #endif
  59169. /* Force a short session ID to be sent */
  59170. ssl->session->sessionIDSz = 4;
  59171. #ifndef NO_SESSION_CACHE_REF
  59172. /* Allow the client cache to be used */
  59173. ssl->session->idLen = 4;
  59174. #endif
  59175. ssl->session->isSetup = 1;
  59176. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  59177. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  59178. return EXPECT_RESULT();
  59179. }
  59180. static int test_short_session_id(void)
  59181. {
  59182. EXPECT_DECLS;
  59183. test_ssl_cbf client_cbf;
  59184. test_ssl_cbf server_cbf;
  59185. size_t i;
  59186. struct {
  59187. method_provider client_meth;
  59188. method_provider server_meth;
  59189. const char* tls_version;
  59190. } params[] = {
  59191. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  59192. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  59193. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  59194. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  59195. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  59196. #ifdef WOLFSSL_DTLS13
  59197. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  59198. #endif
  59199. #endif
  59200. #ifndef WOLFSSL_NO_TLS12
  59201. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  59202. #ifdef WOLFSSL_DTLS
  59203. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  59204. #endif
  59205. #endif
  59206. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  59207. !defined(NO_DES3))
  59208. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  59209. #ifdef WOLFSSL_DTLS
  59210. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  59211. #endif
  59212. #endif
  59213. };
  59214. fprintf(stderr, "\n");
  59215. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  59216. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  59217. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  59218. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  59219. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  59220. client_cbf.method = params[i].client_meth;
  59221. server_cbf.method = params[i].server_meth;
  59222. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  59223. &server_cbf, NULL), TEST_SUCCESS);
  59224. }
  59225. return EXPECT_RESULT();
  59226. }
  59227. #else
  59228. static int test_short_session_id(void)
  59229. {
  59230. return TEST_SKIPPED;
  59231. }
  59232. #endif
  59233. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  59234. && defined(WOLFSSL_DTLS13)
  59235. static byte* test_find_string(const char *string,
  59236. byte *buf, int buf_size)
  59237. {
  59238. int string_size, i;
  59239. string_size = (int)XSTRLEN(string);
  59240. for (i = 0; i < buf_size - string_size - 1; i++) {
  59241. if (XSTRCMP((char*)&buf[i], string) == 0)
  59242. return &buf[i];
  59243. }
  59244. return NULL;
  59245. }
  59246. static int test_wolfSSL_dtls13_null_cipher(void)
  59247. {
  59248. EXPECT_DECLS;
  59249. WOLFSSL_CTX *ctx_c = NULL;
  59250. WOLFSSL_CTX *ctx_s = NULL;
  59251. WOLFSSL *ssl_c = NULL;
  59252. WOLFSSL *ssl_s = NULL;
  59253. struct test_memio_ctx test_ctx;
  59254. const char *test_str = "test";
  59255. int test_str_size;
  59256. byte buf[255], *ptr = NULL;
  59257. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59258. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  59259. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59260. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59261. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59262. test_str_size = XSTRLEN("test") + 1;
  59263. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59264. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  59265. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  59266. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59267. /* check that the packet was sent cleartext */
  59268. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  59269. test_ctx.s_len));
  59270. if (ptr != NULL) {
  59271. /* modify the message */
  59272. *ptr = 'H';
  59273. /* bad messages should be ignored in DTLS */
  59274. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  59275. ExpectIntEQ(ssl_s->error, WANT_READ);
  59276. }
  59277. wolfSSL_free(ssl_c);
  59278. wolfSSL_free(ssl_s);
  59279. wolfSSL_CTX_free(ctx_c);
  59280. wolfSSL_CTX_free(ctx_s);
  59281. return TEST_SUCCESS;
  59282. }
  59283. #else
  59284. static int test_wolfSSL_dtls13_null_cipher(void)
  59285. {
  59286. return TEST_SKIPPED;
  59287. }
  59288. #endif
  59289. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59290. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59291. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  59292. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  59293. {
  59294. SOCKADDR_S peer;
  59295. XSOCKLENT len;
  59296. int ret;
  59297. XMEMSET((byte*)&peer, 0, sizeof(peer));
  59298. len = sizeof(peer);
  59299. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  59300. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  59301. return -1;
  59302. switch (peer.ss_family) {
  59303. #ifdef WOLFSSL_IPV6
  59304. case WOLFSSL_IP6: {
  59305. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  59306. break;
  59307. }
  59308. #endif /* WOLFSSL_IPV6 */
  59309. case WOLFSSL_IP4:
  59310. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  59311. break;
  59312. default:
  59313. return -1;
  59314. }
  59315. return 0;
  59316. }
  59317. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  59318. {
  59319. char buf[1] = {'t'};
  59320. SOCKADDR_IN_T addr;
  59321. int sock_fd;
  59322. word16 port;
  59323. int err;
  59324. (void)ssl;
  59325. (void)ctx;
  59326. if (ssl == NULL)
  59327. return -1;
  59328. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  59329. if (err != 0)
  59330. return -1;
  59331. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  59332. if (sock_fd == -1)
  59333. return -1;
  59334. build_addr(&addr, wolfSSLIP, port, 1, 0);
  59335. /* send a packet to the server. Being another socket, the kernel will ensure
  59336. * the source port will be different. */
  59337. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  59338. sizeof(addr));
  59339. close(sock_fd);
  59340. if (err == -1)
  59341. return -1;
  59342. return 0;
  59343. }
  59344. /* setup a SSL session but just after the handshake send a packet to the server
  59345. * with a source address different than the one of the connected client. The I/O
  59346. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  59347. * is done in test_dtls_msg_from_other_peer_cb */
  59348. static int test_dtls_msg_from_other_peer(void)
  59349. {
  59350. EXPECT_DECLS;
  59351. callback_functions client_cbs;
  59352. callback_functions server_cbs;
  59353. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  59354. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  59355. client_cbs.method = wolfDTLSv1_2_client_method;
  59356. server_cbs.method = wolfDTLSv1_2_server_method;
  59357. client_cbs.doUdp = 1;
  59358. server_cbs.doUdp = 1;
  59359. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  59360. test_dtls_msg_from_other_peer_cb);
  59361. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  59362. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  59363. return EXPECT_RESULT();
  59364. }
  59365. #else
  59366. static int test_dtls_msg_from_other_peer(void)
  59367. {
  59368. return TEST_SKIPPED;
  59369. }
  59370. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59371. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59372. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  59373. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  59374. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59375. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  59376. static int test_dtls_ipv6_check(void)
  59377. {
  59378. EXPECT_DECLS;
  59379. WOLFSSL_CTX *ctx_c = NULL;
  59380. WOLFSSL_CTX *ctx_s = NULL;
  59381. WOLFSSL *ssl_c = NULL;
  59382. WOLFSSL *ssl_s = NULL;
  59383. SOCKADDR_IN fake_addr6;
  59384. int sockfd = -1;
  59385. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  59386. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  59387. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  59388. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  59389. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59390. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  59391. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59392. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  59393. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  59394. /* mimic a sockaddr_in6 struct, this way we can't test without
  59395. * WOLFSSL_IPV6 */
  59396. fake_addr6.sin_family = WOLFSSL_IP6;
  59397. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  59398. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  59399. /* can't return error here, as the peer is opaque for wolfssl library at
  59400. * this point */
  59401. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  59402. WOLFSSL_SUCCESS);
  59403. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  59404. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  59405. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59406. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  59407. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  59408. WOLFSSL_SUCCESS);
  59409. /* reuse the socket */
  59410. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  59411. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  59412. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59413. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  59414. if (sockfd != -1)
  59415. close(sockfd);
  59416. wolfSSL_free(ssl_c);
  59417. wolfSSL_CTX_free(ctx_c);
  59418. wolfSSL_free(ssl_s);
  59419. wolfSSL_CTX_free(ctx_s);
  59420. return EXPECT_RESULT();
  59421. }
  59422. #else
  59423. static int test_dtls_ipv6_check(void)
  59424. {
  59425. return TEST_SKIPPED;
  59426. }
  59427. #endif
  59428. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59429. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  59430. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  59431. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  59432. {
  59433. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  59434. }
  59435. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  59436. {
  59437. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  59438. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  59439. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  59440. }
  59441. else {
  59442. char testMsg[] = "Message after SCR";
  59443. char msgBuf[sizeof(testMsg)];
  59444. int ret;
  59445. if (!wolfSSL_is_server(ssl)) {
  59446. AssertIntEQ(WOLFSSL_SUCCESS,
  59447. wolfSSL_set_session(ssl,
  59448. test_wolfSSL_SCR_after_resumption_session));
  59449. }
  59450. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  59451. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59452. sizeof(testMsg));
  59453. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  59454. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  59455. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  59456. AssertIntEQ(ret, sizeof(msgBuf));
  59457. }
  59458. }
  59459. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  59460. {
  59461. AssertIntEQ(WOLFSSL_SUCCESS,
  59462. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  59463. }
  59464. static int test_wolfSSL_SCR_after_resumption(void)
  59465. {
  59466. EXPECT_DECLS;
  59467. callback_functions func_cb_client;
  59468. callback_functions func_cb_server;
  59469. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  59470. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  59471. func_cb_client.method = wolfTLSv1_2_client_method;
  59472. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  59473. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  59474. func_cb_server.method = wolfTLSv1_2_server_method;
  59475. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  59476. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59477. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59478. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59479. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  59480. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  59481. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59482. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59483. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59484. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  59485. return EXPECT_RESULT();
  59486. }
  59487. #else
  59488. static int test_wolfSSL_SCR_after_resumption(void)
  59489. {
  59490. return TEST_SKIPPED;
  59491. }
  59492. #endif
  59493. static int test_wolfSSL_configure_args(void)
  59494. {
  59495. EXPECT_DECLS;
  59496. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  59497. ExpectNotNull(wolfSSL_configure_args());
  59498. #endif
  59499. return EXPECT_RESULT();
  59500. }
  59501. static int test_dtls_no_extensions(void)
  59502. {
  59503. EXPECT_DECLS;
  59504. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  59505. !defined(WOLFSSL_NO_TLS12)
  59506. WOLFSSL *ssl_s = NULL;
  59507. WOLFSSL_CTX *ctx_s = NULL;
  59508. struct test_memio_ctx test_ctx;
  59509. const byte chNoExtensions[] = {
  59510. /* Handshake type */
  59511. 0x16,
  59512. /* Version */
  59513. 0xfe, 0xff,
  59514. /* Epoch */
  59515. 0x00, 0x00,
  59516. /* Seq number */
  59517. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59518. /* Length */
  59519. 0x00, 0x40,
  59520. /* CH type */
  59521. 0x01,
  59522. /* Length */
  59523. 0x00, 0x00, 0x34,
  59524. /* Msg Seq */
  59525. 0x00, 0x00,
  59526. /* Frag offset */
  59527. 0x00, 0x00, 0x00,
  59528. /* Frag length */
  59529. 0x00, 0x00, 0x34,
  59530. /* Version */
  59531. 0xfe, 0xff,
  59532. /* Random */
  59533. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  59534. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  59535. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  59536. /* Session Length */
  59537. 0x00,
  59538. /* Cookie Length */
  59539. 0x00,
  59540. /* CS Length */
  59541. 0x00, 0x0c,
  59542. /* CS */
  59543. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  59544. /* Comp Meths Length */
  59545. 0x01,
  59546. /* Comp Meths */
  59547. 0x00
  59548. /* And finally... no extensions */
  59549. };
  59550. int i;
  59551. #ifdef OPENSSL_EXTRA
  59552. int repeats = 2;
  59553. #else
  59554. int repeats = 1;
  59555. #endif
  59556. for (i = 0; i < repeats; i++) {
  59557. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59558. ssl_s = NULL;
  59559. ctx_s = NULL;
  59560. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  59561. NULL, wolfDTLS_server_method), 0);
  59562. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  59563. test_ctx.s_len = sizeof(chNoExtensions);
  59564. #ifdef OPENSSL_EXTRA
  59565. if (i > 0) {
  59566. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  59567. WOLFSSL_SUCCESS);
  59568. }
  59569. #endif
  59570. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  59571. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59572. /* Expecting a handshake msg. Either HVR or SH. */
  59573. ExpectIntGT(test_ctx.c_len, 0);
  59574. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  59575. wolfSSL_free(ssl_s);
  59576. wolfSSL_CTX_free(ctx_s);
  59577. }
  59578. #endif
  59579. return EXPECT_RESULT();
  59580. }
  59581. static int test_TLSX_CA_NAMES_bad_extension(void)
  59582. {
  59583. EXPECT_DECLS;
  59584. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  59585. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  59586. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  59587. defined(HAVE_NULL_CIPHER)
  59588. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  59589. * parse the CA Names extension. Otherwise it will return other non-related
  59590. * errors. If CA Names will be parsed in more configurations, that should
  59591. * be reflected in the macro guard above. */
  59592. WOLFSSL *ssl_c = NULL;
  59593. WOLFSSL_CTX *ctx_c = NULL;
  59594. struct test_memio_ctx test_ctx;
  59595. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  59596. const byte shBadCaNamesExt[] = {
  59597. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  59598. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  59599. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  59600. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  59601. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  59602. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  59603. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  59604. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  59605. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  59606. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  59607. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  59608. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  59609. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  59610. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  59611. };
  59612. const byte shBadCaNamesExt2[] = {
  59613. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  59614. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  59615. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  59616. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  59617. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  59618. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  59619. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  59620. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  59621. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  59622. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  59623. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  59624. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  59625. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  59626. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  59627. };
  59628. int i = 0;
  59629. for (i = 0; i < 2; i++) {
  59630. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59631. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59632. wolfTLSv1_3_client_method, NULL), 0);
  59633. switch (i) {
  59634. case 0:
  59635. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  59636. sizeof(shBadCaNamesExt));
  59637. test_ctx.c_len = sizeof(shBadCaNamesExt);
  59638. break;
  59639. case 1:
  59640. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  59641. sizeof(shBadCaNamesExt2));
  59642. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  59643. break;
  59644. }
  59645. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59646. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  59647. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  59648. #else
  59649. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  59650. #endif
  59651. wolfSSL_free(ssl_c);
  59652. ssl_c = NULL;
  59653. wolfSSL_CTX_free(ctx_c);
  59654. ctx_c = NULL;
  59655. }
  59656. #endif
  59657. return EXPECT_RESULT();
  59658. }
  59659. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59660. defined(HAVE_IO_TESTS_DEPENDENCIES)
  59661. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  59662. {
  59663. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  59664. WOLFSSL_SUCCESS);
  59665. }
  59666. static int test_dtls_1_0_hvr_downgrade(void)
  59667. {
  59668. EXPECT_DECLS;
  59669. callback_functions func_cb_client;
  59670. callback_functions func_cb_server;
  59671. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59672. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59673. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  59674. func_cb_client.method = wolfDTLS_client_method;
  59675. func_cb_server.method = wolfDTLSv1_2_server_method;
  59676. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  59677. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59678. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59679. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59680. return EXPECT_RESULT();
  59681. }
  59682. #else
  59683. static int test_dtls_1_0_hvr_downgrade(void)
  59684. {
  59685. EXPECT_DECLS;
  59686. return EXPECT_RESULT();
  59687. }
  59688. #endif
  59689. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  59690. defined(HAVE_SESSION_TICKET)
  59691. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  59692. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  59693. {
  59694. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  59695. AssertNotNull(test_session_ticket_no_id_session);
  59696. }
  59697. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  59698. {
  59699. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  59700. }
  59701. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  59702. {
  59703. test_session_ticket_no_id_session->sessionIDSz = 0;
  59704. AssertIntEQ(WOLFSSL_SUCCESS,
  59705. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  59706. }
  59707. static int test_session_ticket_no_id(void)
  59708. {
  59709. /* We are testing an expired (invalid crypto context in out case since the
  59710. * ctx changes) session ticket being sent with the session ID being 0
  59711. * length. */
  59712. EXPECT_DECLS;
  59713. callback_functions func_cb_client;
  59714. callback_functions func_cb_server;
  59715. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  59716. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  59717. func_cb_client.method = wolfTLSv1_2_client_method;
  59718. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  59719. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  59720. func_cb_server.method = wolfTLSv1_2_server_method;
  59721. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  59722. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59723. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59724. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59725. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  59726. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  59727. func_cb_client.method = wolfTLSv1_2_client_method;
  59728. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  59729. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  59730. func_cb_server.method = wolfTLSv1_2_server_method;
  59731. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  59732. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59733. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59734. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59735. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  59736. return EXPECT_RESULT();
  59737. }
  59738. #else
  59739. static int test_session_ticket_no_id(void)
  59740. {
  59741. EXPECT_DECLS;
  59742. return EXPECT_RESULT();
  59743. }
  59744. #endif
  59745. static int test_session_ticket_hs_update(void)
  59746. {
  59747. EXPECT_DECLS;
  59748. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  59749. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  59750. struct test_memio_ctx test_ctx;
  59751. struct test_memio_ctx test_ctx2;
  59752. struct test_memio_ctx test_ctx3;
  59753. WOLFSSL_CTX *ctx_c = NULL;
  59754. WOLFSSL_CTX *ctx_s = NULL;
  59755. WOLFSSL *ssl_c = NULL;
  59756. WOLFSSL *ssl_c2 = NULL;
  59757. WOLFSSL *ssl_c3 = NULL;
  59758. WOLFSSL *ssl_s = NULL;
  59759. WOLFSSL *ssl_s2 = NULL;
  59760. WOLFSSL *ssl_s3 = NULL;
  59761. WOLFSSL_SESSION *sess = NULL;
  59762. byte read_data[1];
  59763. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59764. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  59765. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  59766. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59767. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59768. /* Generate tickets */
  59769. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59770. wolfSSL_SetLoggingPrefix("client");
  59771. /* Read the ticket msg */
  59772. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  59773. WOLFSSL_FATAL_ERROR);
  59774. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59775. WOLFSSL_ERROR_WANT_READ);
  59776. wolfSSL_SetLoggingPrefix(NULL);
  59777. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  59778. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59779. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  59780. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59781. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59782. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  59783. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  59784. wolfSSL_SetLoggingPrefix("client");
  59785. /* Exchange initial flights for the second connection */
  59786. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  59787. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  59788. WOLFSSL_ERROR_WANT_READ);
  59789. wolfSSL_SetLoggingPrefix(NULL);
  59790. wolfSSL_SetLoggingPrefix("server");
  59791. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  59792. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  59793. WOLFSSL_ERROR_WANT_READ);
  59794. wolfSSL_SetLoggingPrefix(NULL);
  59795. /* Complete third connection so that new tickets are exchanged */
  59796. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  59797. /* Read the ticket msg */
  59798. wolfSSL_SetLoggingPrefix("client");
  59799. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  59800. WOLFSSL_FATAL_ERROR);
  59801. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  59802. WOLFSSL_ERROR_WANT_READ);
  59803. wolfSSL_SetLoggingPrefix(NULL);
  59804. /* Complete second connection */
  59805. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  59806. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  59807. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  59808. wolfSSL_free(ssl_c);
  59809. wolfSSL_free(ssl_c2);
  59810. wolfSSL_free(ssl_c3);
  59811. wolfSSL_free(ssl_s);
  59812. wolfSSL_free(ssl_s2);
  59813. wolfSSL_free(ssl_s3);
  59814. wolfSSL_CTX_free(ctx_c);
  59815. wolfSSL_CTX_free(ctx_s);
  59816. wolfSSL_SESSION_free(sess);
  59817. #endif
  59818. return EXPECT_RESULT();
  59819. }
  59820. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59821. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  59822. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  59823. {
  59824. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  59825. WOLFSSL_SUCCESS);
  59826. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  59827. }
  59828. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  59829. {
  59830. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  59831. }
  59832. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  59833. {
  59834. char testMsg[] = "Message after SCR";
  59835. char msgBuf[sizeof(testMsg)];
  59836. if (wolfSSL_is_server(ssl)) {
  59837. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  59838. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  59839. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59840. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  59841. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59842. sizeof(testMsg));
  59843. }
  59844. else {
  59845. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59846. sizeof(testMsg));
  59847. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59848. }
  59849. }
  59850. static int test_dtls_downgrade_scr_server(void)
  59851. {
  59852. EXPECT_DECLS;
  59853. callback_functions func_cb_client;
  59854. callback_functions func_cb_server;
  59855. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59856. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59857. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  59858. func_cb_client.method = wolfDTLSv1_2_client_method;
  59859. func_cb_server.method = wolfDTLS_server_method;
  59860. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  59861. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  59862. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  59863. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  59864. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59865. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59866. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59867. return EXPECT_RESULT();
  59868. }
  59869. #else
  59870. static int test_dtls_downgrade_scr_server(void)
  59871. {
  59872. EXPECT_DECLS;
  59873. return EXPECT_RESULT();
  59874. }
  59875. #endif
  59876. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59877. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  59878. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  59879. {
  59880. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  59881. WOLFSSL_SUCCESS);
  59882. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  59883. }
  59884. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  59885. {
  59886. char testMsg[] = "Message after SCR";
  59887. char msgBuf[sizeof(testMsg)];
  59888. if (wolfSSL_is_server(ssl)) {
  59889. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  59890. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  59891. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59892. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  59893. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59894. sizeof(testMsg));
  59895. }
  59896. else {
  59897. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59898. sizeof(testMsg));
  59899. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59900. }
  59901. }
  59902. static int test_dtls_downgrade_scr(void)
  59903. {
  59904. EXPECT_DECLS;
  59905. callback_functions func_cb_client;
  59906. callback_functions func_cb_server;
  59907. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59908. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59909. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  59910. func_cb_client.method = wolfDTLS_client_method;
  59911. func_cb_server.method = wolfDTLSv1_2_server_method;
  59912. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  59913. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  59914. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  59915. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59916. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59917. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59918. return EXPECT_RESULT();
  59919. }
  59920. #else
  59921. static int test_dtls_downgrade_scr(void)
  59922. {
  59923. EXPECT_DECLS;
  59924. return EXPECT_RESULT();
  59925. }
  59926. #endif
  59927. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59928. && !defined(WOLFSSL_NO_TLS12)
  59929. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  59930. char *data, int sz, void *ctx)
  59931. {
  59932. static int call_counter = 0;
  59933. call_counter++;
  59934. (void)ssl;
  59935. (void)data;
  59936. (void)sz;
  59937. (void)ctx;
  59938. switch (call_counter) {
  59939. case 1:
  59940. case 2:
  59941. return WOLFSSL_CBIO_ERR_TIMEOUT;
  59942. case 3:
  59943. return WOLFSSL_CBIO_ERR_WANT_READ;
  59944. default:
  59945. AssertIntLE(call_counter, 3);
  59946. return -1;
  59947. }
  59948. }
  59949. #endif
  59950. /* Make sure we don't send acks before getting a server hello */
  59951. static int test_dtls_client_hello_timeout_downgrade(void)
  59952. {
  59953. EXPECT_DECLS;
  59954. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59955. && !defined(WOLFSSL_NO_TLS12)
  59956. WOLFSSL_CTX *ctx_c = NULL;
  59957. WOLFSSL_CTX *ctx_s = NULL;
  59958. WOLFSSL *ssl_c = NULL;
  59959. WOLFSSL *ssl_s = NULL;
  59960. struct test_memio_ctx test_ctx;
  59961. DtlsRecordLayerHeader* dtlsRH;
  59962. size_t len;
  59963. byte sequence_number[8];
  59964. int i;
  59965. for (i = 0; i < 2; i++) {
  59966. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59967. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59968. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  59969. if (i == 0) {
  59970. /* First time simulate timeout in IO layer */
  59971. /* CH1 */
  59972. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59973. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59974. /* HVR */
  59975. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59976. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59977. /* CH2 */
  59978. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59979. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59980. /* SH flight */
  59981. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59982. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59983. /* Drop the SH */
  59984. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  59985. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59986. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59987. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59988. (sizeof(DtlsRecordLayerHeader) + len));
  59989. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59990. /* Read the remainder of the flight */
  59991. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59992. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59993. wolfSSL_SSLSetIORecv(ssl_c,
  59994. test_dtls_client_hello_timeout_downgrade_read_cb);
  59995. /* CH3 */
  59996. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59997. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59998. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  59999. }
  60000. else {
  60001. /* Second time call wolfSSL_dtls_got_timeout */
  60002. /* CH1 */
  60003. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60004. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60005. /* HVR */
  60006. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60007. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60008. /* CH2 */
  60009. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60010. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60011. /* SH flight */
  60012. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60013. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60014. /* Drop the SH */
  60015. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  60016. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60017. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  60018. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  60019. (sizeof(DtlsRecordLayerHeader) + len));
  60020. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  60021. /* Read the remainder of the flight */
  60022. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60023. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60024. /* Quick timeout should be set as we received at least one msg */
  60025. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  60026. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60027. /* Quick timeout should be cleared after a quick timeout */
  60028. /* CH3 */
  60029. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  60030. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60031. }
  60032. /* Parse out to make sure we got exactly one ClientHello message */
  60033. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60034. /* Second ClientHello after HVR */
  60035. sequence_number[7] = 2;
  60036. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  60037. ExpectIntEQ(dtlsRH->type, handshake);
  60038. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60039. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60040. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60041. sizeof(sequence_number)), 0);
  60042. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60043. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60044. /* Connection should be able to continue */
  60045. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60046. wolfSSL_free(ssl_c);
  60047. wolfSSL_free(ssl_s);
  60048. wolfSSL_CTX_free(ctx_c);
  60049. wolfSSL_CTX_free(ctx_s);
  60050. ssl_c = NULL;
  60051. ssl_s = NULL;
  60052. ctx_c = NULL;
  60053. ctx_s = NULL;
  60054. if (!EXPECT_SUCCESS())
  60055. break;
  60056. }
  60057. #endif
  60058. return EXPECT_RESULT();
  60059. }
  60060. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60061. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  60062. int sz, void *ctx)
  60063. {
  60064. static int call_counter = 0;
  60065. call_counter++;
  60066. (void)ssl;
  60067. (void)data;
  60068. (void)sz;
  60069. (void)ctx;
  60070. switch (call_counter) {
  60071. case 1:
  60072. return WOLFSSL_CBIO_ERR_TIMEOUT;
  60073. case 2:
  60074. return WOLFSSL_CBIO_ERR_WANT_READ;
  60075. default:
  60076. AssertIntLE(call_counter, 2);
  60077. return -1;
  60078. }
  60079. }
  60080. #endif
  60081. /* Make sure we don't send acks before getting a server hello */
  60082. static int test_dtls_client_hello_timeout(void)
  60083. {
  60084. EXPECT_DECLS;
  60085. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60086. WOLFSSL *ssl_c = NULL;
  60087. WOLFSSL_CTX *ctx_c = NULL;
  60088. struct test_memio_ctx test_ctx;
  60089. DtlsRecordLayerHeader* dtlsRH;
  60090. size_t idx;
  60091. size_t len;
  60092. byte sequence_number[8];
  60093. int i;
  60094. for (i = 0; i < 2; i++) {
  60095. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60096. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60097. wolfDTLSv1_3_client_method, NULL), 0);
  60098. if (i == 0) {
  60099. /* First time simulate timeout in IO layer */
  60100. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  60101. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60102. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60103. }
  60104. else {
  60105. /* Second time call wolfSSL_dtls_got_timeout */
  60106. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60107. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60108. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60109. }
  60110. /* Parse out to make sure we got exactly two ClientHello messages */
  60111. idx = 0;
  60112. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60113. /* First ClientHello */
  60114. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  60115. ExpectIntEQ(dtlsRH->type, handshake);
  60116. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60117. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60118. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60119. sizeof(sequence_number)), 0);
  60120. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60121. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60122. idx += sizeof(DtlsRecordLayerHeader) + len;
  60123. /* Second ClientHello */
  60124. sequence_number[7] = 1;
  60125. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  60126. ExpectIntEQ(dtlsRH->type, handshake);
  60127. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60128. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60129. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60130. sizeof(sequence_number)), 0);
  60131. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60132. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60133. wolfSSL_free(ssl_c);
  60134. wolfSSL_CTX_free(ctx_c);
  60135. ssl_c = NULL;
  60136. ctx_c = NULL;
  60137. if (!EXPECT_SUCCESS())
  60138. break;
  60139. }
  60140. #endif
  60141. return EXPECT_RESULT();
  60142. }
  60143. /* DTLS test when dropping the changed cipher spec message */
  60144. static int test_dtls_dropped_ccs(void)
  60145. {
  60146. EXPECT_DECLS;
  60147. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60148. && !defined(WOLFSSL_NO_TLS12)
  60149. WOLFSSL_CTX *ctx_c = NULL;
  60150. WOLFSSL_CTX *ctx_s = NULL;
  60151. WOLFSSL *ssl_c = NULL;
  60152. WOLFSSL *ssl_s = NULL;
  60153. struct test_memio_ctx test_ctx;
  60154. DtlsRecordLayerHeader* dtlsRH;
  60155. size_t len;
  60156. byte data[1];
  60157. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60158. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60159. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60160. /* CH1 */
  60161. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60162. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60163. /* HVR */
  60164. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60165. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60166. /* CH2 */
  60167. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60168. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60169. /* Server first flight */
  60170. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60171. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60172. /* Client flight */
  60173. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60174. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60175. /* Server ccs + finished */
  60176. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  60177. /* Drop the ccs */
  60178. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  60179. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60180. ExpectIntEQ(len, 1);
  60181. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  60182. if (EXPECT_SUCCESS()) {
  60183. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  60184. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  60185. (sizeof(DtlsRecordLayerHeader) + len));
  60186. }
  60187. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  60188. /* Client rtx flight */
  60189. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60190. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60191. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60192. /* Server ccs + finished rtx */
  60193. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  60194. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60195. /* Client processes finished */
  60196. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  60197. wolfSSL_free(ssl_c);
  60198. wolfSSL_free(ssl_s);
  60199. wolfSSL_CTX_free(ctx_c);
  60200. wolfSSL_CTX_free(ctx_s);
  60201. #endif
  60202. return EXPECT_RESULT();
  60203. }
  60204. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60205. && !defined(WOLFSSL_NO_TLS12)
  60206. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  60207. byte seq_num)
  60208. {
  60209. EXPECT_DECLS;
  60210. DtlsRecordLayerHeader* dtlsRH;
  60211. byte sequence_number[8];
  60212. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60213. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  60214. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  60215. ExpectIntEQ(dtlsRH->type, handshake);
  60216. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60217. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60218. sequence_number[7] = seq_num;
  60219. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60220. sizeof(sequence_number)), 0);
  60221. return EXPECT_RESULT();
  60222. }
  60223. #endif
  60224. /*
  60225. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  60226. * and after a HelloRetryRequest. This is testing the server side as it is
  60227. * operating statelessly and should copy the sequence number of the ClientHello.
  60228. */
  60229. static int test_dtls_seq_num_downgrade(void)
  60230. {
  60231. EXPECT_DECLS;
  60232. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60233. && !defined(WOLFSSL_NO_TLS12)
  60234. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60235. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60236. struct test_memio_ctx test_ctx;
  60237. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60238. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60239. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  60240. /* CH1 */
  60241. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60242. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60243. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  60244. test_ctx.s_len, 0), TEST_SUCCESS);
  60245. /* HVR */
  60246. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60247. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60248. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  60249. test_ctx.c_len, 0), TEST_SUCCESS);
  60250. /* CH2 */
  60251. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60252. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60253. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  60254. test_ctx.s_len, 1), TEST_SUCCESS);
  60255. /* Server first flight */
  60256. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60257. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60258. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  60259. test_ctx.c_len, 1), TEST_SUCCESS);
  60260. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60261. wolfSSL_free(ssl_c);
  60262. wolfSSL_CTX_free(ctx_c);
  60263. wolfSSL_free(ssl_s);
  60264. wolfSSL_CTX_free(ctx_s);
  60265. #endif
  60266. return EXPECT_RESULT();
  60267. }
  60268. /**
  60269. * Make sure we don't send RSA Signature Hash Algorithms in the
  60270. * CertificateRequest when we don't have any such ciphers set.
  60271. * @return EXPECT_RESULT()
  60272. */
  60273. static int test_certreq_sighash_algos(void)
  60274. {
  60275. EXPECT_DECLS;
  60276. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60277. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  60278. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  60279. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  60280. WOLFSSL_CTX *ctx_c = NULL;
  60281. WOLFSSL_CTX *ctx_s = NULL;
  60282. WOLFSSL *ssl_c = NULL;
  60283. WOLFSSL *ssl_s = NULL;
  60284. struct test_memio_ctx test_ctx;
  60285. int idx = 0;
  60286. int maxIdx = 0;
  60287. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60288. test_ctx.c_ciphers = test_ctx.s_ciphers =
  60289. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  60290. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60291. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  60292. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  60293. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  60294. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  60295. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  60296. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60297. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  60298. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60299. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  60300. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60301. WOLFSSL_ERROR_WANT_READ);
  60302. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  60303. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60304. WOLFSSL_ERROR_WANT_READ);
  60305. /* Find the CertificateRequest message */
  60306. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  60307. word16 len;
  60308. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  60309. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  60310. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  60311. ato16(test_ctx.c_buff + idx, &len);
  60312. idx += OPAQUE16_LEN;
  60313. if (test_ctx.c_buff[idx] == certificate_request) {
  60314. idx++;
  60315. /* length */
  60316. idx += OPAQUE24_LEN;
  60317. /* cert types */
  60318. idx += 1 + test_ctx.c_buff[idx];
  60319. /* Sig algos */
  60320. ato16(test_ctx.c_buff + idx, &len);
  60321. idx += OPAQUE16_LEN;
  60322. maxIdx = idx + (int)len;
  60323. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  60324. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  60325. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  60326. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  60327. else
  60328. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  60329. }
  60330. break;
  60331. }
  60332. else {
  60333. idx += (int)len;
  60334. }
  60335. }
  60336. ExpectIntLT(idx, test_ctx.c_len);
  60337. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60338. wolfSSL_free(ssl_c);
  60339. wolfSSL_free(ssl_s);
  60340. wolfSSL_CTX_free(ctx_c);
  60341. wolfSSL_CTX_free(ctx_s);
  60342. #endif
  60343. return EXPECT_RESULT();
  60344. }
  60345. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60346. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  60347. {
  60348. EXPECT_DECLS;
  60349. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  60350. myVerifyAction = VERIFY_USE_PREVERFIY;
  60351. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60352. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60353. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60354. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60355. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  60356. WOLFSSL_SUCCESS);
  60357. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60358. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  60359. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60360. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60361. "./certs/crl/ca-int.pem",
  60362. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60363. return EXPECT_RESULT();
  60364. }
  60365. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  60366. {
  60367. EXPECT_DECLS;
  60368. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  60369. myVerifyAction = VERIFY_USE_PREVERFIY;
  60370. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60371. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60372. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60373. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60374. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60375. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60376. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  60377. WOLFSSL_SUCCESS);
  60378. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60379. "./certs/crl/ca-int2.pem",
  60380. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60381. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60382. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  60383. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60384. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60385. "./certs/crl/ca-int.pem",
  60386. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60387. return EXPECT_RESULT();
  60388. }
  60389. #endif
  60390. static int test_revoked_loaded_int_cert(void)
  60391. {
  60392. EXPECT_DECLS;
  60393. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60394. test_ssl_cbf client_cbf;
  60395. test_ssl_cbf server_cbf;
  60396. struct {
  60397. const char* certPemFile;
  60398. const char* keyPemFile;
  60399. ctx_cb client_ctx_ready;
  60400. } test_params[] = {
  60401. {"./certs/intermediate/ca-int2-cert.pem",
  60402. "./certs/intermediate/ca-int2-key.pem",
  60403. test_revoked_loaded_int_cert_ctx_ready1},
  60404. {"./certs/intermediate/server-chain.pem",
  60405. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  60406. {"./certs/intermediate/server-chain-short.pem",
  60407. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  60408. };
  60409. size_t i;
  60410. printf("\n");
  60411. for (i = 0; i < XELEM_CNT(test_params); i++) {
  60412. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60413. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60414. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  60415. server_cbf.certPemFile = test_params[i].certPemFile;
  60416. server_cbf.keyPemFile = test_params[i].keyPemFile;
  60417. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  60418. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60419. &server_cbf, NULL), TEST_FAIL);
  60420. #ifndef WOLFSSL_HAPROXY
  60421. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  60422. #else
  60423. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  60424. #endif
  60425. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  60426. if (!EXPECT_SUCCESS())
  60427. break;
  60428. printf("\t%s passed\n", test_params[i].certPemFile);
  60429. }
  60430. #endif
  60431. return EXPECT_RESULT();
  60432. }
  60433. static int test_dtls13_frag_ch_pq(void)
  60434. {
  60435. EXPECT_DECLS;
  60436. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  60437. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  60438. WOLFSSL_CTX *ctx_c = NULL;
  60439. WOLFSSL_CTX *ctx_s = NULL;
  60440. WOLFSSL *ssl_c = NULL;
  60441. WOLFSSL *ssl_s = NULL;
  60442. struct test_memio_ctx test_ctx;
  60443. const char *test_str = "test";
  60444. int test_str_size;
  60445. byte buf[255];
  60446. int group = WOLFSSL_KYBER_LEVEL5;
  60447. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60448. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60449. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60450. /* Add in a large post-quantum key share to make the CH long. */
  60451. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  60452. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  60453. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  60454. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60455. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  60456. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  60457. test_str_size = XSTRLEN("test") + 1;
  60458. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60459. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  60460. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  60461. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60462. wolfSSL_free(ssl_c);
  60463. wolfSSL_free(ssl_s);
  60464. wolfSSL_CTX_free(ctx_c);
  60465. wolfSSL_CTX_free(ctx_s);
  60466. #endif
  60467. return EXPECT_RESULT();
  60468. }
  60469. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60470. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  60471. static int test_dtls_frag_ch_count_records(byte* b, int len)
  60472. {
  60473. DtlsRecordLayerHeader* dtlsRH;
  60474. int records = 0;
  60475. size_t recordLen;
  60476. while (len > 0) {
  60477. records++;
  60478. dtlsRH = (DtlsRecordLayerHeader*)b;
  60479. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  60480. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  60481. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  60482. }
  60483. return records;
  60484. }
  60485. #endif
  60486. static int test_dtls_frag_ch(void)
  60487. {
  60488. EXPECT_DECLS;
  60489. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  60490. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  60491. WOLFSSL_CTX *ctx_c = NULL;
  60492. WOLFSSL_CTX *ctx_s = NULL;
  60493. WOLFSSL *ssl_c = NULL;
  60494. WOLFSSL *ssl_s = NULL;
  60495. struct test_memio_ctx test_ctx;
  60496. static unsigned int DUMMY_MTU = 256;
  60497. unsigned char four_frag_CH[] = {
  60498. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60499. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60500. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  60501. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  60502. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  60503. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  60504. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  60505. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  60506. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  60507. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  60508. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  60509. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  60510. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  60511. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  60512. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  60513. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  60514. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  60515. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  60516. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  60517. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60518. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  60519. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  60520. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  60521. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  60522. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  60523. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  60524. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  60525. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  60526. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  60527. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  60528. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  60529. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  60530. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  60531. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  60532. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  60533. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  60534. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  60535. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  60536. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  60537. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  60538. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  60539. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  60540. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  60541. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  60542. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  60543. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  60544. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  60545. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  60546. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  60547. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  60548. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  60549. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  60550. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  60551. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  60552. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  60553. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  60554. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  60555. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  60556. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  60557. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  60558. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  60559. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  60560. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  60561. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  60562. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  60563. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  60564. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  60565. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  60566. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  60567. 0xa5, 0xcb, 0x68, 0xf3
  60568. };
  60569. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60570. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60571. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60572. /* Fragment msgs */
  60573. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  60574. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  60575. /* Add in some key shares to make the CH long */
  60576. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  60577. WOLFSSL_SUCCESS);
  60578. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  60579. WOLFSSL_SUCCESS);
  60580. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  60581. WOLFSSL_SUCCESS);
  60582. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  60583. WOLFSSL_SUCCESS);
  60584. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  60585. /* Reject fragmented first CH */
  60586. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  60587. sizeof(four_frag_CH)), 4);
  60588. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  60589. test_ctx.s_len = sizeof(four_frag_CH);
  60590. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  60591. int s_len = test_ctx.s_len;
  60592. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60593. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60594. /* Fail if we didn't advance the buffer to avoid infinite loops */
  60595. ExpectIntLT(test_ctx.s_len, s_len);
  60596. }
  60597. /* Expect all fragments read */
  60598. ExpectIntEQ(test_ctx.s_len, 0);
  60599. /* Expect quietly dropping fragmented first CH */
  60600. ExpectIntEQ(test_ctx.c_len, 0);
  60601. /* CH1 */
  60602. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60603. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60604. /* Count records. Expect 1 unfragmented CH */
  60605. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  60606. test_ctx.s_len), 1);
  60607. /* HRR */
  60608. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60609. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60610. /* CH2 */
  60611. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60612. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60613. /* Count records. Expect fragmented CH */
  60614. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  60615. test_ctx.s_len), 1);
  60616. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60617. wolfSSL_free(ssl_c);
  60618. wolfSSL_free(ssl_s);
  60619. wolfSSL_CTX_free(ctx_c);
  60620. wolfSSL_CTX_free(ctx_s);
  60621. ssl_c = ssl_s = NULL;
  60622. ctx_c = ctx_s = NULL;
  60623. #endif
  60624. return EXPECT_RESULT();
  60625. }
  60626. static int test_dtls_empty_keyshare_with_cookie(void)
  60627. {
  60628. EXPECT_DECLS;
  60629. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60630. WOLFSSL_CTX *ctx_s = NULL;
  60631. WOLFSSL *ssl_s = NULL;
  60632. struct test_memio_ctx test_ctx;
  60633. unsigned char ch_empty_keyshare_with_cookie[] = {
  60634. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  60635. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  60636. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  60637. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  60638. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  60639. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  60640. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  60641. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  60642. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  60643. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  60644. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  60645. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  60646. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  60647. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  60648. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  60649. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  60650. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  60651. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  60652. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  60653. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  60654. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  60655. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  60656. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  60657. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  60658. };
  60659. DtlsRecordLayerHeader* dtlsRH;
  60660. byte sequence_number[8];
  60661. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60662. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60663. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  60664. sizeof(ch_empty_keyshare_with_cookie));
  60665. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  60666. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  60667. NULL, wolfDTLSv1_3_server_method), 0);
  60668. /* CH1 */
  60669. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60670. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60671. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  60672. ExpectIntEQ(test_ctx.c_len, 15);
  60673. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  60674. ExpectIntEQ(dtlsRH->type, alert);
  60675. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60676. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60677. sequence_number[7] = 1;
  60678. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60679. sizeof(sequence_number)), 0);
  60680. ExpectIntEQ(dtlsRH->length[0], 0);
  60681. ExpectIntEQ(dtlsRH->length[1], 2);
  60682. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  60683. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  60684. wolfSSL_free(ssl_s);
  60685. wolfSSL_CTX_free(ctx_s);
  60686. #endif
  60687. return EXPECT_RESULT();
  60688. }
  60689. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60690. defined(HAVE_LIBOQS)
  60691. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  60692. {
  60693. int group = WOLFSSL_KYBER_LEVEL5;
  60694. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  60695. }
  60696. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  60697. {
  60698. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  60699. }
  60700. #endif
  60701. static int test_tls13_pq_groups(void)
  60702. {
  60703. EXPECT_DECLS;
  60704. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60705. defined(HAVE_LIBOQS)
  60706. callback_functions func_cb_client;
  60707. callback_functions func_cb_server;
  60708. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60709. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60710. func_cb_client.method = wolfTLSv1_3_client_method;
  60711. func_cb_server.method = wolfTLSv1_3_server_method;
  60712. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  60713. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  60714. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  60715. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60716. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60717. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60718. #endif
  60719. return EXPECT_RESULT();
  60720. }
  60721. static int test_tls13_early_data(void)
  60722. {
  60723. EXPECT_DECLS;
  60724. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60725. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  60726. int written = 0;
  60727. int read = 0;
  60728. size_t i;
  60729. int splitEarlyData;
  60730. char msg[] = "This is early data";
  60731. char msg2[] = "This is client data";
  60732. char msg3[] = "This is server data";
  60733. char msg4[] = "This is server immediate data";
  60734. char msgBuf[50];
  60735. struct {
  60736. method_provider client_meth;
  60737. method_provider server_meth;
  60738. const char* tls_version;
  60739. int isUdp;
  60740. } params[] = {
  60741. #ifdef WOLFSSL_TLS13
  60742. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60743. "TLS 1.3", 0 },
  60744. #endif
  60745. #ifdef WOLFSSL_DTLS13
  60746. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  60747. "DTLS 1.3", 1 },
  60748. #endif
  60749. };
  60750. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  60751. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  60752. struct test_memio_ctx test_ctx;
  60753. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60754. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60755. WOLFSSL_SESSION *sess = NULL;
  60756. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60757. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  60758. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  60759. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  60760. /* Get a ticket so that we can do 0-RTT on the next connection */
  60761. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60762. /* Make sure we read the ticket */
  60763. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  60764. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60765. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  60766. wolfSSL_free(ssl_c);
  60767. ssl_c = NULL;
  60768. wolfSSL_free(ssl_s);
  60769. ssl_s = NULL;
  60770. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60771. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60772. params[i].client_meth, params[i].server_meth), 0);
  60773. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  60774. #ifdef WOLFSSL_DTLS13
  60775. if (params[i].isUdp) {
  60776. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  60777. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  60778. #else
  60779. /* Let's test this but we generally don't recommend turning off the
  60780. * cookie exchange */
  60781. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  60782. #endif
  60783. }
  60784. #endif
  60785. /* Test 0-RTT data */
  60786. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  60787. &written), sizeof(msg));
  60788. ExpectIntEQ(written, sizeof(msg));
  60789. if (splitEarlyData) {
  60790. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  60791. &written), sizeof(msg));
  60792. ExpectIntEQ(written, sizeof(msg));
  60793. }
  60794. /* Read first 0-RTT data (if split otherwise entire data) */
  60795. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  60796. &read), sizeof(msg));
  60797. ExpectIntEQ(read, sizeof(msg));
  60798. ExpectStrEQ(msg, msgBuf);
  60799. /* Test 0.5-RTT data */
  60800. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  60801. if (splitEarlyData) {
  60802. /* Read second 0-RTT data */
  60803. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  60804. &read), sizeof(msg));
  60805. ExpectIntEQ(read, sizeof(msg));
  60806. ExpectStrEQ(msg, msgBuf);
  60807. }
  60808. if (params[i].isUdp) {
  60809. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60810. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  60811. /* Read server 0.5-RTT data */
  60812. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  60813. ExpectStrEQ(msg4, msgBuf);
  60814. /* Complete handshake */
  60815. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60816. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60817. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  60818. * a user would loop until it is true but here we control both sides so we
  60819. * just assert the expected value. wolfSSL_read_early_data does not provide
  60820. * handshake status to us with non-blocking IO and we can't use
  60821. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  60822. * parsing logic. */
  60823. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  60824. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  60825. &read), 0);
  60826. ExpectIntEQ(read, 0);
  60827. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  60828. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60829. }
  60830. else {
  60831. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60832. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  60833. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  60834. &read), 0);
  60835. ExpectIntEQ(read, 0);
  60836. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  60837. /* Read server 0.5-RTT data */
  60838. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  60839. ExpectStrEQ(msg4, msgBuf);
  60840. }
  60841. /* Test bi-directional write */
  60842. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  60843. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  60844. ExpectStrEQ(msg2, msgBuf);
  60845. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  60846. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  60847. ExpectStrEQ(msg3, msgBuf);
  60848. ExpectTrue(wolfSSL_session_reused(ssl_c));
  60849. ExpectTrue(wolfSSL_session_reused(ssl_s));
  60850. wolfSSL_SESSION_free(sess);
  60851. wolfSSL_free(ssl_c);
  60852. wolfSSL_free(ssl_s);
  60853. wolfSSL_CTX_free(ctx_c);
  60854. wolfSSL_CTX_free(ctx_s);
  60855. }
  60856. }
  60857. #endif
  60858. return EXPECT_RESULT();
  60859. }
  60860. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  60861. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  60862. {
  60863. EXPECT_DECLS;
  60864. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  60865. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  60866. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  60867. return EXPECT_RESULT();
  60868. }
  60869. #endif
  60870. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  60871. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  60872. {
  60873. EXPECT_DECLS;
  60874. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  60875. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  60876. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  60877. return EXPECT_RESULT();
  60878. }
  60879. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  60880. {
  60881. EXPECT_DECLS;
  60882. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  60883. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  60884. 0), 1);
  60885. return EXPECT_RESULT();
  60886. }
  60887. #endif
  60888. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  60889. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  60890. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  60891. {
  60892. EXPECT_DECLS;
  60893. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  60894. return EXPECT_RESULT();
  60895. }
  60896. #endif
  60897. static int test_self_signed_stapling(void)
  60898. {
  60899. EXPECT_DECLS;
  60900. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  60901. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  60902. test_ssl_cbf client_cbf;
  60903. test_ssl_cbf server_cbf;
  60904. size_t i;
  60905. struct {
  60906. method_provider client_meth;
  60907. method_provider server_meth;
  60908. ctx_cb client_ctx;
  60909. const char* tls_version;
  60910. } params[] = {
  60911. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  60912. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60913. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  60914. #endif
  60915. #ifndef WOLFSSL_NO_TLS12
  60916. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  60917. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60918. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  60919. #endif
  60920. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  60921. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60922. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  60923. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60924. test_self_signed_stapling_client_v2_multi_ctx_ready,
  60925. "TLSv1_2 v2 multi" },
  60926. #endif
  60927. #endif
  60928. };
  60929. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  60930. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60931. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60932. printf("\nTesting self-signed cert with status request: %s\n",
  60933. params[i].tls_version);
  60934. client_cbf.method = params[i].client_meth;
  60935. client_cbf.ctx_ready = params[i].client_ctx;
  60936. server_cbf.method = params[i].server_meth;
  60937. server_cbf.certPemFile = "certs/ca-cert.pem";
  60938. server_cbf.keyPemFile = "certs/ca-key.pem";
  60939. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  60940. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60941. &server_cbf, NULL), TEST_SUCCESS);
  60942. }
  60943. #endif
  60944. return EXPECT_RESULT();
  60945. }
  60946. static int test_tls_multi_handshakes_one_record(void)
  60947. {
  60948. EXPECT_DECLS;
  60949. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  60950. struct test_memio_ctx test_ctx;
  60951. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60952. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60953. RecordLayerHeader* rh = NULL;
  60954. byte *len ;
  60955. int newRecIdx = RECORD_HEADER_SZ;
  60956. int idx = 0;
  60957. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60958. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60959. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  60960. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60961. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60962. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  60963. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60964. /* Combine server handshake msgs into one record */
  60965. while (idx < test_ctx.c_len) {
  60966. word16 recLen;
  60967. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  60968. len = &rh->length[0];
  60969. ato16((const byte*)len, &recLen);
  60970. idx += RECORD_HEADER_SZ;
  60971. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  60972. (size_t)recLen);
  60973. newRecIdx += recLen;
  60974. idx += recLen;
  60975. }
  60976. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  60977. len = &rh->length[0];
  60978. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  60979. test_ctx.c_len = newRecIdx;
  60980. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60981. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60982. wolfSSL_free(ssl_c);
  60983. wolfSSL_free(ssl_s);
  60984. wolfSSL_CTX_free(ctx_c);
  60985. wolfSSL_CTX_free(ctx_s);
  60986. #endif
  60987. return EXPECT_RESULT();
  60988. }
  60989. static int test_write_dup(void)
  60990. {
  60991. EXPECT_DECLS;
  60992. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  60993. size_t i, j;
  60994. char hiWorld[] = "dup message";
  60995. char readData[sizeof(hiWorld) + 5];
  60996. struct {
  60997. method_provider client_meth;
  60998. method_provider server_meth;
  60999. const char* version_name;
  61000. int version;
  61001. } methods[] = {
  61002. #ifndef WOLFSSL_NO_TLS12
  61003. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  61004. #endif
  61005. #ifdef WOLFSSL_TLS13
  61006. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  61007. #endif
  61008. };
  61009. struct {
  61010. const char* cipher;
  61011. int version;
  61012. } ciphers[] = {
  61013. /* For simplicity the macros are copied from internal.h */
  61014. /* TLS 1.2 */
  61015. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  61016. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  61017. #ifndef NO_RSA
  61018. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  61019. #endif
  61020. #endif
  61021. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  61022. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  61023. #endif
  61024. #endif
  61025. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  61026. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  61027. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61028. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  61029. #endif
  61030. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61031. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  61032. #endif
  61033. #endif
  61034. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  61035. && !defined(NO_TLS) && !defined(NO_AES)
  61036. #ifdef HAVE_AESGCM
  61037. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61038. #ifndef NO_RSA
  61039. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  61040. #endif
  61041. #endif
  61042. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61043. #ifndef NO_RSA
  61044. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  61045. #endif
  61046. #endif
  61047. #endif
  61048. #endif
  61049. /* TLS 1.3 */
  61050. #ifdef WOLFSSL_TLS13
  61051. #ifdef HAVE_AESGCM
  61052. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61053. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  61054. #endif
  61055. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61056. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  61057. #endif
  61058. #endif
  61059. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  61060. #ifndef NO_SHA256
  61061. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  61062. #endif
  61063. #endif
  61064. #ifdef HAVE_AESCCM
  61065. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61066. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  61067. #endif
  61068. #endif
  61069. #endif
  61070. };
  61071. for (i = 0; i < XELEM_CNT(methods); i++) {
  61072. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  61073. struct test_memio_ctx test_ctx;
  61074. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61075. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61076. WOLFSSL *ssl_c2 = NULL;
  61077. if (methods[i].version != ciphers[j].version)
  61078. continue;
  61079. if (i == 0 && j == 0)
  61080. printf("\n");
  61081. printf("Testing %s with %s... ", methods[i].version_name,
  61082. ciphers[j].cipher);
  61083. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61084. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  61085. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61086. methods[i].client_meth, methods[i].server_meth), 0);
  61087. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61088. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  61089. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  61090. WRITE_DUP_WRITE_E);
  61091. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  61092. sizeof(hiWorld));
  61093. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  61094. sizeof(hiWorld));
  61095. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  61096. sizeof(hiWorld));
  61097. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  61098. WRITE_DUP_READ_E);
  61099. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  61100. sizeof(hiWorld));
  61101. if (EXPECT_SUCCESS())
  61102. printf("ok\n");
  61103. else
  61104. printf("failed\n");
  61105. wolfSSL_free(ssl_c);
  61106. wolfSSL_free(ssl_c2);
  61107. wolfSSL_free(ssl_s);
  61108. wolfSSL_CTX_free(ctx_c);
  61109. wolfSSL_CTX_free(ctx_s);
  61110. }
  61111. }
  61112. #endif
  61113. return EXPECT_RESULT();
  61114. }
  61115. static int test_read_write_hs(void)
  61116. {
  61117. EXPECT_DECLS;
  61118. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  61119. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  61120. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  61121. struct test_memio_ctx test_ctx;
  61122. uint8_t test_buffer[16];
  61123. unsigned int test;
  61124. /* test == 0 : client writes, server reads */
  61125. /* test == 1 : server writes, client reads */
  61126. for (test = 0; test < 2; test++) {
  61127. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61128. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61129. wolfTLSv1_2_client_method,
  61130. wolfTLSv1_2_server_method), 0);
  61131. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  61132. /* CH -> */
  61133. if (test == 0) {
  61134. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61135. } else {
  61136. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61137. sizeof(test_buffer)), -1);
  61138. }
  61139. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61140. /* <- SH + SKE + SHD */
  61141. if (test == 0) {
  61142. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61143. sizeof(test_buffer)), -1);
  61144. } else {
  61145. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  61146. }
  61147. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61148. /* -> CKE + CLIENT FINISHED */
  61149. if (test == 0) {
  61150. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61151. } else {
  61152. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61153. sizeof(test_buffer)), -1);
  61154. }
  61155. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61156. /* abide clang static analyzer */
  61157. if (ssl_s != NULL) {
  61158. /* disable group message to separate sending of ChangeCipherspec
  61159. * from Finished */
  61160. ssl_s->options.groupMessages = 0;
  61161. }
  61162. /* allow writing of CS, but not FINISHED */
  61163. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  61164. /* <- CS */
  61165. if (test == 0) {
  61166. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61167. sizeof(test_buffer)), -1);
  61168. } else {
  61169. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  61170. }
  61171. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  61172. /* move CS message where the client can read it */
  61173. memmove(test_ctx.c_buff,
  61174. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  61175. test_ctx.c_len = 6;
  61176. /* read CS */
  61177. if (test == 0) {
  61178. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61179. } else {
  61180. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61181. sizeof(test_buffer)), -1);
  61182. }
  61183. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61184. ExpectIntEQ(test_ctx.c_len, 0);
  61185. if (test == 0) {
  61186. /* send SERVER FINISHED */
  61187. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61188. sizeof(test_buffer)), -1);
  61189. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  61190. WOLFSSL_ERROR_WANT_READ);
  61191. } else {
  61192. /* send SERVER FINISHED + App Data */
  61193. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  61194. }
  61195. ExpectIntGT(test_ctx.c_len, 0);
  61196. /* Send and receive the data */
  61197. if (test == 0) {
  61198. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  61199. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61200. sizeof(test_buffer)), 5);
  61201. } else {
  61202. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61203. sizeof(test_buffer)), 5);
  61204. }
  61205. ExpectBufEQ(test_buffer, "hello", 5);
  61206. wolfSSL_free(ssl_c);
  61207. wolfSSL_free(ssl_s);
  61208. wolfSSL_CTX_free(ctx_c);
  61209. wolfSSL_CTX_free(ctx_s);
  61210. ssl_c = ssl_s = NULL;
  61211. ctx_c = ctx_s = NULL;
  61212. }
  61213. #endif
  61214. return EXPECT_RESULT();
  61215. }
  61216. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  61217. static const char* test_get_signature_nid_siglag;
  61218. static int test_get_signature_nid_sig;
  61219. static int test_get_signature_nid_hash;
  61220. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  61221. {
  61222. EXPECT_DECLS;
  61223. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  61224. if (!wolfSSL_is_server(ssl)) {
  61225. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  61226. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  61227. }
  61228. return EXPECT_RESULT();
  61229. }
  61230. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  61231. {
  61232. EXPECT_DECLS;
  61233. int nid = 0;
  61234. (void)ctx;
  61235. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  61236. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61237. ExpectIntEQ(nid, test_get_signature_nid_sig);
  61238. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61239. ExpectIntEQ(nid, test_get_signature_nid_hash);
  61240. }
  61241. else /* No sigalg info on static ciphersuite */
  61242. return TEST_SUCCESS;
  61243. return EXPECT_RESULT();
  61244. }
  61245. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  61246. {
  61247. EXPECT_DECLS;
  61248. int nid = 0;
  61249. (void)ctx;
  61250. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61251. ExpectIntEQ(nid, test_get_signature_nid_sig);
  61252. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61253. ExpectIntEQ(nid, test_get_signature_nid_hash);
  61254. return EXPECT_RESULT();
  61255. }
  61256. #endif
  61257. static int test_get_signature_nid(void)
  61258. {
  61259. EXPECT_DECLS;
  61260. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  61261. test_ssl_cbf client_cbf;
  61262. test_ssl_cbf server_cbf;
  61263. size_t i;
  61264. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  61265. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  61266. caCertFile }
  61267. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  61268. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  61269. caEccCertFile }
  61270. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  61271. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  61272. caCertFile }
  61273. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  61274. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  61275. caEccCertFile }
  61276. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  61277. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  61278. caEdCertFile }
  61279. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  61280. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  61281. caEd448CertFile }
  61282. struct {
  61283. const char* siglag;
  61284. int sig_nid;
  61285. int hash_nid;
  61286. int tls_ver;
  61287. const char* server_cert;
  61288. const char* server_key;
  61289. const char* client_ca;
  61290. } params[] = {
  61291. #ifndef NO_RSA
  61292. #ifndef NO_SHA256
  61293. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  61294. #ifdef WC_RSA_PSS
  61295. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  61296. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  61297. #endif
  61298. #endif
  61299. #ifdef WOLFSSL_SHA512
  61300. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  61301. #ifdef WC_RSA_PSS
  61302. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  61303. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  61304. #endif
  61305. #endif
  61306. #endif
  61307. #ifdef HAVE_ECC
  61308. #ifndef NO_SHA256
  61309. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  61310. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  61311. #endif
  61312. #endif
  61313. #ifdef HAVE_ED25519
  61314. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  61315. #endif
  61316. #ifdef HAVE_ED448
  61317. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  61318. #endif
  61319. };
  61320. printf("\n");
  61321. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  61322. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61323. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61324. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  61325. params[i].siglag);
  61326. switch (params[i].tls_ver) {
  61327. #ifndef WOLFSSL_NO_TLS12
  61328. case WOLFSSL_TLSV1_2:
  61329. client_cbf.method = wolfTLSv1_2_client_method;
  61330. server_cbf.method = wolfTLSv1_2_server_method;
  61331. break;
  61332. #endif
  61333. #ifdef WOLFSSL_TLS13
  61334. case WOLFSSL_TLSV1_3:
  61335. client_cbf.method = wolfTLSv1_3_client_method;
  61336. server_cbf.method = wolfTLSv1_3_server_method;
  61337. break;
  61338. #endif
  61339. default:
  61340. printf("skipping\n");
  61341. continue;
  61342. }
  61343. test_get_signature_nid_siglag = params[i].siglag;
  61344. test_get_signature_nid_sig = params[i].sig_nid;
  61345. test_get_signature_nid_hash = params[i].hash_nid;
  61346. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  61347. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  61348. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  61349. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  61350. server_cbf.certPemFile = params[i].server_cert;
  61351. server_cbf.keyPemFile = params[i].server_key;
  61352. client_cbf.caPemFile = params[i].client_ca;
  61353. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61354. &server_cbf, NULL), TEST_SUCCESS);
  61355. if (EXPECT_SUCCESS())
  61356. printf("passed\n");
  61357. }
  61358. #endif
  61359. return EXPECT_RESULT();
  61360. }
  61361. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61362. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  61363. {
  61364. #ifndef NO_MD5
  61365. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  61366. #elif !defined(NO_SHA)
  61367. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  61368. #elif !defined(NO_SHA256)
  61369. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  61370. #else
  61371. #error "We need a digest to hash the Signer object"
  61372. #endif
  61373. byte hashBuf[WC_MAX_DIGEST_SIZE];
  61374. wc_HashAlg hash;
  61375. size_t i;
  61376. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  61377. for (i = 0; i < CA_TABLE_SIZE; i++) {
  61378. Signer* cur;
  61379. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  61380. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  61381. sizeof(*cur)), 0);
  61382. }
  61383. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  61384. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  61385. return MakeWordFromHash(hashBuf);
  61386. }
  61387. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  61388. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  61389. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  61390. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  61391. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  61392. {
  61393. EXPECT_DECLS;
  61394. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  61395. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  61396. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  61397. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  61398. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  61399. return EXPECT_RESULT();
  61400. }
  61401. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  61402. {
  61403. EXPECT_DECLS;
  61404. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  61405. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  61406. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  61407. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  61408. return EXPECT_RESULT();
  61409. }
  61410. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  61411. {
  61412. EXPECT_DECLS;
  61413. WOLFSSL_CERT_MANAGER* cm;
  61414. (void)ssl;
  61415. /* WARNING: this approach bypasses the reference counter check in
  61416. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  61417. * cause unexpected behaviour when other active connections try accessing
  61418. * the caTable. */
  61419. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  61420. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  61421. WOLFSSL_SUCCESS);
  61422. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  61423. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  61424. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  61425. return EXPECT_RESULT();
  61426. }
  61427. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  61428. {
  61429. EXPECT_DECLS;
  61430. WOLFSSL_CTX* ctx;
  61431. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  61432. return EXPECT_RESULT();
  61433. }
  61434. #endif
  61435. static int test_tls_cert_store_unchanged(void)
  61436. {
  61437. EXPECT_DECLS;
  61438. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61439. test_ssl_cbf client_cbf;
  61440. test_ssl_cbf server_cbf;
  61441. int i;
  61442. for (i = 0; i < 2; i++) {
  61443. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61444. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61445. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  61446. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  61447. sizeof(test_tls_cert_store_unchanged_before_hashes));
  61448. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  61449. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  61450. sizeof(test_tls_cert_store_unchanged_after_hashes));
  61451. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  61452. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  61453. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  61454. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  61455. switch (i) {
  61456. case 0:
  61457. client_cbf.on_ctx_cleanup =
  61458. test_tls_cert_store_unchanged_ctx_cleanup;
  61459. server_cbf.on_ctx_cleanup =
  61460. test_tls_cert_store_unchanged_ctx_cleanup;
  61461. break;
  61462. case 1:
  61463. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  61464. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  61465. break;
  61466. default:
  61467. Fail(("Should not enter here"), ("Entered here"));
  61468. }
  61469. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  61470. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  61471. server_cbf.caPemFile = caCertFile;
  61472. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61473. &server_cbf, NULL), TEST_SUCCESS);
  61474. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  61475. test_tls_cert_store_unchanged_after_hashes,
  61476. sizeof(test_tls_cert_store_unchanged_after_hashes));
  61477. }
  61478. #endif
  61479. return EXPECT_RESULT();
  61480. }
  61481. /*----------------------------------------------------------------------------*
  61482. | Main
  61483. *----------------------------------------------------------------------------*/
  61484. typedef int (*TEST_FUNC)(void);
  61485. typedef struct {
  61486. const char *name;
  61487. TEST_FUNC func;
  61488. byte run:1;
  61489. byte fail:1;
  61490. } TEST_CASE;
  61491. #define TEST_DECL(func) { #func, func, 0, 0 }
  61492. int testAll = 1;
  61493. TEST_CASE testCases[] = {
  61494. TEST_DECL(test_fileAccess),
  61495. /*********************************
  61496. * wolfcrypt
  61497. *********************************/
  61498. TEST_DECL(test_ForceZero),
  61499. TEST_DECL(test_wolfCrypt_Init),
  61500. /* Locking with Compat Mutex */
  61501. TEST_DECL(test_wc_SetMutexCb),
  61502. TEST_DECL(test_wc_LockMutex_ex),
  61503. /* Digests */
  61504. TEST_DECL(test_wc_InitMd5),
  61505. TEST_DECL(test_wc_Md5Update),
  61506. TEST_DECL(test_wc_Md5Final),
  61507. TEST_DECL(test_wc_InitSha),
  61508. TEST_DECL(test_wc_ShaUpdate),
  61509. TEST_DECL(test_wc_ShaFinal),
  61510. TEST_DECL(test_wc_InitSha256),
  61511. TEST_DECL(test_wc_Sha256Update),
  61512. TEST_DECL(test_wc_Sha256Final),
  61513. TEST_DECL(test_wc_Sha256FinalRaw),
  61514. TEST_DECL(test_wc_Sha256GetFlags),
  61515. TEST_DECL(test_wc_Sha256Free),
  61516. TEST_DECL(test_wc_Sha256GetHash),
  61517. TEST_DECL(test_wc_Sha256Copy),
  61518. TEST_DECL(test_wc_InitSha224),
  61519. TEST_DECL(test_wc_Sha224Update),
  61520. TEST_DECL(test_wc_Sha224Final),
  61521. TEST_DECL(test_wc_Sha224SetFlags),
  61522. TEST_DECL(test_wc_Sha224GetFlags),
  61523. TEST_DECL(test_wc_Sha224Free),
  61524. TEST_DECL(test_wc_Sha224GetHash),
  61525. TEST_DECL(test_wc_Sha224Copy),
  61526. TEST_DECL(test_wc_InitSha512),
  61527. TEST_DECL(test_wc_Sha512Update),
  61528. TEST_DECL(test_wc_Sha512Final),
  61529. TEST_DECL(test_wc_Sha512GetFlags),
  61530. TEST_DECL(test_wc_Sha512FinalRaw),
  61531. TEST_DECL(test_wc_Sha512Free),
  61532. TEST_DECL(test_wc_Sha512GetHash),
  61533. TEST_DECL(test_wc_Sha512Copy),
  61534. TEST_DECL(test_wc_InitSha512_224),
  61535. TEST_DECL(test_wc_Sha512_224Update),
  61536. TEST_DECL(test_wc_Sha512_224Final),
  61537. TEST_DECL(test_wc_Sha512_224GetFlags),
  61538. TEST_DECL(test_wc_Sha512_224FinalRaw),
  61539. TEST_DECL(test_wc_Sha512_224Free),
  61540. TEST_DECL(test_wc_Sha512_224GetHash),
  61541. TEST_DECL(test_wc_Sha512_224Copy),
  61542. TEST_DECL(test_wc_InitSha512_256),
  61543. TEST_DECL(test_wc_Sha512_256Update),
  61544. TEST_DECL(test_wc_Sha512_256Final),
  61545. TEST_DECL(test_wc_Sha512_256GetFlags),
  61546. TEST_DECL(test_wc_Sha512_256FinalRaw),
  61547. TEST_DECL(test_wc_Sha512_256Free),
  61548. TEST_DECL(test_wc_Sha512_256GetHash),
  61549. TEST_DECL(test_wc_Sha512_256Copy),
  61550. TEST_DECL(test_wc_InitSha384),
  61551. TEST_DECL(test_wc_Sha384Update),
  61552. TEST_DECL(test_wc_Sha384Final),
  61553. TEST_DECL(test_wc_Sha384GetFlags),
  61554. TEST_DECL(test_wc_Sha384FinalRaw),
  61555. TEST_DECL(test_wc_Sha384Free),
  61556. TEST_DECL(test_wc_Sha384GetHash),
  61557. TEST_DECL(test_wc_Sha384Copy),
  61558. TEST_DECL(test_wc_InitBlake2b),
  61559. TEST_DECL(test_wc_InitBlake2b_WithKey),
  61560. TEST_DECL(test_wc_InitBlake2s_WithKey),
  61561. TEST_DECL(test_wc_InitRipeMd),
  61562. TEST_DECL(test_wc_RipeMdUpdate),
  61563. TEST_DECL(test_wc_RipeMdFinal),
  61564. TEST_DECL(test_wc_InitSha3),
  61565. TEST_DECL(testing_wc_Sha3_Update),
  61566. TEST_DECL(test_wc_Sha3_224_Final),
  61567. TEST_DECL(test_wc_Sha3_256_Final),
  61568. TEST_DECL(test_wc_Sha3_384_Final),
  61569. TEST_DECL(test_wc_Sha3_512_Final),
  61570. TEST_DECL(test_wc_Sha3_224_Copy),
  61571. TEST_DECL(test_wc_Sha3_256_Copy),
  61572. TEST_DECL(test_wc_Sha3_384_Copy),
  61573. TEST_DECL(test_wc_Sha3_512_Copy),
  61574. TEST_DECL(test_wc_Sha3_GetFlags),
  61575. TEST_DECL(test_wc_InitShake256),
  61576. TEST_DECL(testing_wc_Shake256_Update),
  61577. TEST_DECL(test_wc_Shake256_Final),
  61578. TEST_DECL(test_wc_Shake256_Copy),
  61579. TEST_DECL(test_wc_Shake256Hash),
  61580. /* SM3 Digest */
  61581. TEST_DECL(test_wc_InitSm3Free),
  61582. TEST_DECL(test_wc_Sm3UpdateFinal),
  61583. TEST_DECL(test_wc_Sm3GetHash),
  61584. TEST_DECL(test_wc_Sm3Copy),
  61585. TEST_DECL(test_wc_Sm3FinalRaw),
  61586. TEST_DECL(test_wc_Sm3GetSetFlags),
  61587. TEST_DECL(test_wc_Sm3Hash),
  61588. TEST_DECL(test_wc_HashInit),
  61589. TEST_DECL(test_wc_HashSetFlags),
  61590. TEST_DECL(test_wc_HashGetFlags),
  61591. /* HMAC */
  61592. TEST_DECL(test_wc_Md5HmacSetKey),
  61593. TEST_DECL(test_wc_Md5HmacUpdate),
  61594. TEST_DECL(test_wc_Md5HmacFinal),
  61595. TEST_DECL(test_wc_ShaHmacSetKey),
  61596. TEST_DECL(test_wc_ShaHmacUpdate),
  61597. TEST_DECL(test_wc_ShaHmacFinal),
  61598. TEST_DECL(test_wc_Sha224HmacSetKey),
  61599. TEST_DECL(test_wc_Sha224HmacUpdate),
  61600. TEST_DECL(test_wc_Sha224HmacFinal),
  61601. TEST_DECL(test_wc_Sha256HmacSetKey),
  61602. TEST_DECL(test_wc_Sha256HmacUpdate),
  61603. TEST_DECL(test_wc_Sha256HmacFinal),
  61604. TEST_DECL(test_wc_Sha384HmacSetKey),
  61605. TEST_DECL(test_wc_Sha384HmacUpdate),
  61606. TEST_DECL(test_wc_Sha384HmacFinal),
  61607. /* CMAC */
  61608. TEST_DECL(test_wc_InitCmac),
  61609. TEST_DECL(test_wc_CmacUpdate),
  61610. TEST_DECL(test_wc_CmacFinal),
  61611. TEST_DECL(test_wc_AesCmacGenerate),
  61612. /* Cipher */
  61613. TEST_DECL(test_wc_AesGcmStream),
  61614. TEST_DECL(test_wc_Des3_SetIV),
  61615. TEST_DECL(test_wc_Des3_SetKey),
  61616. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  61617. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  61618. TEST_DECL(test_wc_Des3_EcbEncrypt),
  61619. TEST_DECL(test_wc_Chacha_SetKey),
  61620. TEST_DECL(test_wc_Chacha_Process),
  61621. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  61622. TEST_DECL(test_wc_Poly1305SetKey),
  61623. TEST_DECL(test_wc_CamelliaSetKey),
  61624. TEST_DECL(test_wc_CamelliaSetIV),
  61625. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  61626. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  61627. TEST_DECL(test_wc_Arc4SetKey),
  61628. TEST_DECL(test_wc_Arc4Process),
  61629. TEST_DECL(test_wc_Rc2SetKey),
  61630. TEST_DECL(test_wc_Rc2SetIV),
  61631. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  61632. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  61633. /* AES cipher and GMAC. */
  61634. TEST_DECL(test_wc_AesSetKey),
  61635. TEST_DECL(test_wc_AesSetIV),
  61636. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  61637. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  61638. TEST_DECL(test_wc_AesGcmSetKey),
  61639. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  61640. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  61641. TEST_DECL(test_wc_GmacSetKey),
  61642. TEST_DECL(test_wc_GmacUpdate),
  61643. TEST_DECL(test_wc_AesCcmSetKey),
  61644. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  61645. #if defined(WOLFSSL_AES_EAX) && \
  61646. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  61647. TEST_DECL(test_wc_AesEaxVectors),
  61648. TEST_DECL(test_wc_AesEaxEncryptAuth),
  61649. TEST_DECL(test_wc_AesEaxDecryptAuth),
  61650. #endif /* WOLFSSL_AES_EAX */
  61651. /* SM4 cipher */
  61652. TEST_DECL(test_wc_Sm4),
  61653. TEST_DECL(test_wc_Sm4Ecb),
  61654. TEST_DECL(test_wc_Sm4Cbc),
  61655. TEST_DECL(test_wc_Sm4Ctr),
  61656. TEST_DECL(test_wc_Sm4Gcm),
  61657. TEST_DECL(test_wc_Sm4Ccm),
  61658. /* RNG tests */
  61659. #ifdef HAVE_HASHDRBG
  61660. #ifdef TEST_RESEED_INTERVAL
  61661. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  61662. #endif
  61663. TEST_DECL(test_wc_RNG_GenerateBlock),
  61664. #endif
  61665. TEST_DECL(test_get_rand_digit),
  61666. TEST_DECL(test_wc_InitRngNonce),
  61667. TEST_DECL(test_wc_InitRngNonce_ex),
  61668. /* MP API tests */
  61669. TEST_DECL(test_get_digit_count),
  61670. TEST_DECL(test_mp_cond_copy),
  61671. TEST_DECL(test_mp_rand),
  61672. TEST_DECL(test_get_digit),
  61673. TEST_DECL(test_wc_export_int),
  61674. /* RSA */
  61675. TEST_DECL(test_wc_InitRsaKey),
  61676. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  61677. TEST_DECL(test_wc_RsaPublicKeyDecode),
  61678. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  61679. TEST_DECL(test_wc_MakeRsaKey),
  61680. TEST_DECL(test_wc_CheckProbablePrime),
  61681. TEST_DECL(test_wc_RsaPSS_Verify),
  61682. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  61683. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  61684. TEST_DECL(test_wc_RsaKeyToDer),
  61685. TEST_DECL(test_wc_RsaKeyToPublicDer),
  61686. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  61687. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  61688. TEST_DECL(test_wc_RsaEncryptSize),
  61689. TEST_DECL(test_wc_RsaSSL_SignVerify),
  61690. TEST_DECL(test_wc_RsaFlattenPublicKey),
  61691. TEST_DECL(test_RsaDecryptBoundsCheck),
  61692. /* DSA */
  61693. TEST_DECL(test_wc_InitDsaKey),
  61694. TEST_DECL(test_wc_DsaSignVerify),
  61695. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  61696. TEST_DECL(test_wc_MakeDsaKey),
  61697. TEST_DECL(test_wc_DsaKeyToDer),
  61698. TEST_DECL(test_wc_DsaKeyToPublicDer),
  61699. TEST_DECL(test_wc_DsaImportParamsRaw),
  61700. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  61701. TEST_DECL(test_wc_DsaExportParamsRaw),
  61702. TEST_DECL(test_wc_DsaExportKeyRaw),
  61703. /* DH */
  61704. TEST_DECL(test_wc_DhPublicKeyDecode),
  61705. /* wolfCrypt ECC tests */
  61706. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  61707. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  61708. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  61709. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  61710. !defined(HAVE_SELFTEST) && \
  61711. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  61712. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  61713. #endif
  61714. TEST_DECL(test_wc_ecc_make_key),
  61715. TEST_DECL(test_wc_ecc_init),
  61716. TEST_DECL(test_wc_ecc_check_key),
  61717. TEST_DECL(test_wc_ecc_get_generator),
  61718. TEST_DECL(test_wc_ecc_size),
  61719. TEST_DECL(test_wc_ecc_params),
  61720. TEST_DECL(test_wc_ecc_signVerify_hash),
  61721. TEST_DECL(test_wc_ecc_shared_secret),
  61722. TEST_DECL(test_wc_ecc_export_x963),
  61723. TEST_DECL(test_wc_ecc_export_x963_ex),
  61724. TEST_DECL(test_wc_ecc_import_x963),
  61725. TEST_DECL(test_wc_ecc_import_private_key),
  61726. TEST_DECL(test_wc_ecc_export_private_only),
  61727. TEST_DECL(test_wc_ecc_rs_to_sig),
  61728. TEST_DECL(test_wc_ecc_import_raw),
  61729. TEST_DECL(test_wc_ecc_import_unsigned),
  61730. TEST_DECL(test_wc_ecc_sig_size),
  61731. TEST_DECL(test_wc_ecc_ctx_new),
  61732. TEST_DECL(test_wc_ecc_ctx_reset),
  61733. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  61734. TEST_DECL(test_wc_ecc_ctx_set_info),
  61735. TEST_DECL(test_wc_ecc_encryptDecrypt),
  61736. TEST_DECL(test_wc_ecc_del_point),
  61737. TEST_DECL(test_wc_ecc_pointFns),
  61738. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  61739. TEST_DECL(test_wc_ecc_verify_hash_ex),
  61740. TEST_DECL(test_wc_ecc_mulmod),
  61741. TEST_DECL(test_wc_ecc_is_valid_idx),
  61742. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  61743. TEST_DECL(test_wc_ecc_sig_size_calc),
  61744. TEST_DECL(test_wc_EccPrivateKeyToDer),
  61745. /* SM2 elliptic curve */
  61746. TEST_DECL(test_wc_ecc_sm2_make_key),
  61747. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  61748. TEST_DECL(test_wc_ecc_sm2_create_digest),
  61749. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  61750. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  61751. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  61752. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  61753. /* Curve25519 */
  61754. TEST_DECL(test_wc_curve25519_init),
  61755. TEST_DECL(test_wc_curve25519_size),
  61756. TEST_DECL(test_wc_curve25519_export_key_raw),
  61757. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  61758. TEST_DECL(test_wc_curve25519_make_key),
  61759. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  61760. TEST_DECL(test_wc_curve25519_make_pub),
  61761. TEST_DECL(test_wc_curve25519_export_public_ex),
  61762. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  61763. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  61764. TEST_DECL(test_wc_curve25519_import_private),
  61765. /* ED25519 */
  61766. TEST_DECL(test_wc_ed25519_make_key),
  61767. TEST_DECL(test_wc_ed25519_init),
  61768. TEST_DECL(test_wc_ed25519_sign_msg),
  61769. TEST_DECL(test_wc_ed25519_import_public),
  61770. TEST_DECL(test_wc_ed25519_import_private_key),
  61771. TEST_DECL(test_wc_ed25519_export),
  61772. TEST_DECL(test_wc_ed25519_size),
  61773. TEST_DECL(test_wc_ed25519_exportKey),
  61774. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  61775. TEST_DECL(test_wc_Ed25519KeyToDer),
  61776. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  61777. /* Curve448 */
  61778. TEST_DECL(test_wc_curve448_make_key),
  61779. TEST_DECL(test_wc_curve448_shared_secret_ex),
  61780. TEST_DECL(test_wc_curve448_export_public_ex),
  61781. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  61782. TEST_DECL(test_wc_curve448_export_key_raw),
  61783. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  61784. TEST_DECL(test_wc_curve448_import_private),
  61785. TEST_DECL(test_wc_curve448_init),
  61786. TEST_DECL(test_wc_curve448_size),
  61787. /* Ed448 */
  61788. TEST_DECL(test_wc_ed448_make_key),
  61789. TEST_DECL(test_wc_ed448_init),
  61790. TEST_DECL(test_wc_ed448_sign_msg),
  61791. TEST_DECL(test_wc_ed448_import_public),
  61792. TEST_DECL(test_wc_ed448_import_private_key),
  61793. TEST_DECL(test_wc_ed448_export),
  61794. TEST_DECL(test_wc_ed448_size),
  61795. TEST_DECL(test_wc_ed448_exportKey),
  61796. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  61797. TEST_DECL(test_wc_Ed448KeyToDer),
  61798. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  61799. /* Signature API */
  61800. TEST_DECL(test_wc_SignatureGetSize_ecc),
  61801. TEST_DECL(test_wc_SignatureGetSize_rsa),
  61802. /* PEM and DER APIs. */
  61803. TEST_DECL(test_wc_PemToDer),
  61804. TEST_DECL(test_wc_AllocDer),
  61805. TEST_DECL(test_wc_CertPemToDer),
  61806. TEST_DECL(test_wc_KeyPemToDer),
  61807. TEST_DECL(test_wc_PubKeyPemToDer),
  61808. TEST_DECL(test_wc_PemPubKeyToDer),
  61809. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  61810. TEST_DECL(test_wc_CheckCertSigPubKey),
  61811. /* wolfCrypt ASN tests */
  61812. TEST_DECL(test_ToTraditional),
  61813. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  61814. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  61815. /* Certificate */
  61816. TEST_DECL(test_wc_SetSubjectRaw),
  61817. TEST_DECL(test_wc_GetSubjectRaw),
  61818. TEST_DECL(test_wc_SetIssuerRaw),
  61819. TEST_DECL(test_wc_SetIssueBuffer),
  61820. TEST_DECL(test_wc_SetSubjectKeyId),
  61821. TEST_DECL(test_wc_SetSubject),
  61822. TEST_DECL(test_CheckCertSignature),
  61823. TEST_DECL(test_wc_ParseCert),
  61824. TEST_DECL(test_wc_ParseCert_Error),
  61825. TEST_DECL(test_MakeCertWithPathLen),
  61826. TEST_DECL(test_MakeCertWithCaFalse),
  61827. TEST_DECL(test_wc_SetKeyUsage),
  61828. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  61829. TEST_DECL(test_wc_SetSubjectBuffer),
  61830. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  61831. /* wolfcrypt PKCS#7 */
  61832. TEST_DECL(test_wc_PKCS7_New),
  61833. TEST_DECL(test_wc_PKCS7_Init),
  61834. TEST_DECL(test_wc_PKCS7_InitWithCert),
  61835. TEST_DECL(test_wc_PKCS7_EncodeData),
  61836. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  61837. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  61838. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  61839. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  61840. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  61841. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  61842. TEST_DECL(test_wc_PKCS7_Degenerate),
  61843. TEST_DECL(test_wc_PKCS7_BER),
  61844. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  61845. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  61846. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  61847. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  61848. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  61849. /* wolfCrypt PKCS#12 */
  61850. TEST_DECL(test_wc_i2d_PKCS12),
  61851. /*
  61852. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  61853. * avoid memory leaks.
  61854. */
  61855. TEST_DECL(test_wolfCrypt_Cleanup),
  61856. TEST_DECL(test_wolfSSL_Init),
  61857. TEST_DECL(test_dual_alg_support),
  61858. /*********************************
  61859. * OpenSSL compatibility API tests
  61860. *********************************/
  61861. /* If at some point a stub get implemented this test should fail indicating
  61862. * a need to implement a new test case
  61863. */
  61864. TEST_DECL(test_stubs_are_stubs),
  61865. /* ASN.1 compatibility API tests */
  61866. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  61867. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  61868. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  61869. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  61870. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  61871. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  61872. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  61873. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  61874. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  61875. TEST_DECL(test_wolfSSL_ASN1_get_object),
  61876. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  61877. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  61878. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  61879. TEST_DECL(test_wolfSSL_ASN1_STRING),
  61880. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  61881. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  61882. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  61883. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  61884. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  61885. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  61886. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  61887. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  61888. TEST_DECL(test_wolfSSL_ASN1_TIME),
  61889. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  61890. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  61891. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  61892. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  61893. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  61894. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  61895. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  61896. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  61897. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  61898. TEST_DECL(test_wolfSSL_lhash),
  61899. TEST_DECL(test_wolfSSL_certs),
  61900. TEST_DECL(test_wolfSSL_private_keys),
  61901. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  61902. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  61903. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  61904. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  61905. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  61906. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  61907. #ifndef NO_BIO
  61908. TEST_DECL(test_wolfSSL_BIO),
  61909. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  61910. TEST_DECL(test_wolfSSL_PEM_read_bio),
  61911. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  61912. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  61913. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  61914. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  61915. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  61916. #endif
  61917. /* EVP API testing */
  61918. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  61919. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  61920. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  61921. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  61922. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  61923. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  61924. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  61925. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  61926. TEST_DECL(test_wolfSSL_EVP_shake128),
  61927. TEST_DECL(test_wolfSSL_EVP_shake256),
  61928. TEST_DECL(test_wolfSSL_EVP_sm3),
  61929. TEST_DECL(test_EVP_blake2),
  61930. #ifdef OPENSSL_ALL
  61931. TEST_DECL(test_wolfSSL_EVP_md4),
  61932. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  61933. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  61934. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  61935. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  61936. #endif
  61937. TEST_DECL(test_EVP_MD_do_all),
  61938. TEST_DECL(test_wolfSSL_EVP_MD_size),
  61939. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  61940. TEST_DECL(test_wolfSSL_EVP_Digest),
  61941. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  61942. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  61943. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  61944. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  61945. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  61946. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  61947. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  61948. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  61949. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  61950. TEST_DECL(test_wolfssl_EVP_chacha20),
  61951. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  61952. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  61953. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  61954. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  61955. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  61956. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  61957. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  61958. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  61959. #ifdef OPENSSL_ALL
  61960. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  61961. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  61962. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  61963. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  61964. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  61965. TEST_DECL(test_wolfSSL_EVP_rc4),
  61966. TEST_DECL(test_wolfSSL_EVP_enc_null),
  61967. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  61968. TEST_DECL(test_wolfSSL_EVP_mdc2),
  61969. TEST_DECL(test_evp_cipher_aes_gcm),
  61970. #endif
  61971. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  61972. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  61973. #ifdef OPENSSL_EXTRA
  61974. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  61975. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  61976. #endif
  61977. #ifdef OPENSSL_ALL
  61978. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  61979. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  61980. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  61981. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  61982. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  61983. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  61984. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  61985. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  61986. #endif
  61987. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  61988. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  61989. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  61990. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  61991. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  61992. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  61993. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  61994. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  61995. #ifndef NO_BIO
  61996. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  61997. #endif
  61998. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  61999. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  62000. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  62001. #ifndef NO_BIO
  62002. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  62003. #endif /* !NO_BIO */
  62004. #endif
  62005. #ifdef OPENSSL_ALL
  62006. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  62007. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  62008. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  62009. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  62010. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  62011. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  62012. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  62013. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  62014. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  62015. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  62016. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  62017. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  62018. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  62019. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  62020. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  62021. #endif
  62022. TEST_DECL(test_EVP_PKEY_rsa),
  62023. TEST_DECL(test_EVP_PKEY_ec),
  62024. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  62025. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  62026. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  62027. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  62028. TEST_DECL(test_EVP_PKEY_cmp),
  62029. #ifdef OPENSSL_ALL
  62030. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  62031. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  62032. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  62033. #endif
  62034. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  62035. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  62036. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  62037. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  62038. #endif
  62039. #ifndef NO_BIO
  62040. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  62041. TEST_DECL(test_wolfSSL_GetLoggingCb),
  62042. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  62043. TEST_DECL(test_wc_ERR_remove_state),
  62044. TEST_DECL(test_wc_ERR_print_errors_fp),
  62045. #endif
  62046. TEST_DECL(test_wolfSSL_configure_args),
  62047. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  62048. TEST_DECL(test_wolfSSL_set1_curves_list),
  62049. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  62050. TEST_DECL(test_wolfSSL_OtherName),
  62051. TEST_DECL(test_wolfSSL_FPKI),
  62052. TEST_DECL(test_wolfSSL_URI),
  62053. TEST_DECL(test_wolfSSL_TBS),
  62054. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  62055. TEST_DECL(test_X509_STORE_untrusted),
  62056. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  62057. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  62058. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  62059. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  62060. TEST_DECL(test_wolfSSL_X509_Name_canon),
  62061. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  62062. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  62063. TEST_DECL(test_wolfSSL_X509_NID),
  62064. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  62065. TEST_DECL(test_wolfSSL_get0_param),
  62066. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  62067. TEST_DECL(test_wolfSSL_set1_host),
  62068. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  62069. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  62070. TEST_DECL(test_wolfSSL_X509_STORE),
  62071. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  62072. TEST_DECL(test_X509_STORE_get0_objects),
  62073. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  62074. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  62075. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  62076. TEST_DECL(test_wolfSSL_X509_cmp_time),
  62077. TEST_DECL(test_wolfSSL_X509_time_adj),
  62078. /* X509 tests */
  62079. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  62080. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  62081. TEST_DECL(test_wolfSSL_X509_check_host),
  62082. TEST_DECL(test_wolfSSL_X509_check_email),
  62083. TEST_DECL(test_wolfSSL_X509_check_private_key),
  62084. TEST_DECL(test_wolfSSL_X509),
  62085. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  62086. TEST_DECL(test_wolfSSL_X509_sign),
  62087. TEST_DECL(test_wolfSSL_X509_sign2),
  62088. TEST_DECL(test_wolfSSL_X509_verify),
  62089. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  62090. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  62091. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  62092. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  62093. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  62094. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  62095. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  62096. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  62097. TEST_DECL(test_wolfSSL_X509_NAME),
  62098. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  62099. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  62100. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  62101. TEST_DECL(test_wolfSSL_X509_set_name),
  62102. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  62103. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  62104. TEST_DECL(test_wolfSSL_X509_set_version),
  62105. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  62106. TEST_DECL(test_wolfSSL_X509_CRL),
  62107. TEST_DECL(test_wolfSSL_i2d_X509),
  62108. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  62109. TEST_DECL(test_wolfSSL_PEM_read_X509),
  62110. TEST_DECL(test_wolfSSL_X509_check_ca),
  62111. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  62112. TEST_DECL(test_wolfSSL_make_cert),
  62113. #ifndef NO_BIO
  62114. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  62115. TEST_DECL(test_wolfSSL_X509_INFO),
  62116. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  62117. #endif
  62118. #ifdef OPENSSL_ALL
  62119. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  62120. #endif
  62121. TEST_DECL(test_wolfSSL_X509_CA_num),
  62122. TEST_DECL(test_wolfSSL_X509_get_version),
  62123. #ifndef NO_BIO
  62124. TEST_DECL(test_wolfSSL_X509_print),
  62125. TEST_DECL(test_wolfSSL_X509_CRL_print),
  62126. #endif
  62127. TEST_DECL(test_X509_get_signature_nid),
  62128. /* X509 extension testing. */
  62129. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  62130. TEST_DECL(test_wolfSSL_X509_get_ext),
  62131. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  62132. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  62133. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  62134. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  62135. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  62136. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  62137. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  62138. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  62139. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  62140. TEST_DECL(test_wolfSSL_X509V3_EXT),
  62141. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  62142. TEST_DECL(test_wolfSSL_X509_cmp),
  62143. TEST_DECL(test_GENERAL_NAME_set0_othername),
  62144. TEST_DECL(test_othername_and_SID_ext),
  62145. TEST_DECL(test_wolfSSL_dup_CA_list),
  62146. /* OpenSSL sk_X509 API test */
  62147. TEST_DECL(test_sk_X509),
  62148. /* OpenSSL sk_X509_CRL API test */
  62149. TEST_DECL(test_sk_X509_CRL),
  62150. /* OpenSSL X509 REQ API test */
  62151. TEST_DECL(test_X509_REQ),
  62152. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  62153. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  62154. TEST_DECL(test_X509_LOOKUP_add_dir),
  62155. /* RAND compatibility API */
  62156. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  62157. TEST_DECL(test_wolfSSL_RAND_bytes),
  62158. TEST_DECL(test_wolfSSL_RAND),
  62159. /* BN compatibility API */
  62160. TEST_DECL(test_wolfSSL_BN_CTX),
  62161. TEST_DECL(test_wolfSSL_BN),
  62162. TEST_DECL(test_wolfSSL_BN_init),
  62163. TEST_DECL(test_wolfSSL_BN_enc_dec),
  62164. TEST_DECL(test_wolfSSL_BN_word),
  62165. TEST_DECL(test_wolfSSL_BN_bits),
  62166. TEST_DECL(test_wolfSSL_BN_shift),
  62167. TEST_DECL(test_wolfSSL_BN_math),
  62168. TEST_DECL(test_wolfSSL_BN_math_mod),
  62169. TEST_DECL(test_wolfSSL_BN_math_other),
  62170. TEST_DECL(test_wolfSSL_BN_rand),
  62171. TEST_DECL(test_wolfSSL_BN_prime),
  62172. /* OpenSSL PKCS5 API test */
  62173. TEST_DECL(test_wolfSSL_PKCS5),
  62174. /* OpenSSL PKCS8 API test */
  62175. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  62176. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  62177. /* OpenSSL PKCS7 API test */
  62178. TEST_DECL(test_wolfssl_PKCS7),
  62179. TEST_DECL(test_wolfSSL_PKCS7_certs),
  62180. TEST_DECL(test_wolfSSL_PKCS7_sign),
  62181. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  62182. #ifndef NO_BIO
  62183. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  62184. #ifdef HAVE_SMIME
  62185. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  62186. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  62187. #endif /* HAVE_SMIME */
  62188. #endif /* !NO_BIO */
  62189. /* OpenSSL PKCS12 API test */
  62190. TEST_DECL(test_wolfSSL_PKCS12),
  62191. /* Can't memory test as callbacks use Assert. */
  62192. TEST_DECL(test_error_queue_per_thread),
  62193. TEST_DECL(test_wolfSSL_ERR_put_error),
  62194. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  62195. #ifndef NO_BIO
  62196. TEST_DECL(test_wolfSSL_ERR_print_errors),
  62197. #endif
  62198. TEST_DECL(test_OBJ_NAME_do_all),
  62199. TEST_DECL(test_wolfSSL_OBJ),
  62200. TEST_DECL(test_wolfSSL_OBJ_cmp),
  62201. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  62202. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  62203. #ifdef OPENSSL_ALL
  62204. TEST_DECL(test_wolfSSL_OBJ_ln),
  62205. TEST_DECL(test_wolfSSL_OBJ_sn),
  62206. #endif
  62207. #ifndef NO_BIO
  62208. TEST_DECL(test_wolfSSL_BIO_gets),
  62209. TEST_DECL(test_wolfSSL_BIO_puts),
  62210. TEST_DECL(test_wolfSSL_BIO_dump),
  62211. /* Can't memory test as server hangs. */
  62212. TEST_DECL(test_wolfSSL_BIO_should_retry),
  62213. TEST_DECL(test_wolfSSL_BIO_write),
  62214. TEST_DECL(test_wolfSSL_BIO_printf),
  62215. TEST_DECL(test_wolfSSL_BIO_f_md),
  62216. TEST_DECL(test_wolfSSL_BIO_up_ref),
  62217. TEST_DECL(test_wolfSSL_BIO_reset),
  62218. TEST_DECL(test_wolfSSL_BIO_get_len),
  62219. #endif
  62220. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62221. TEST_DECL(test_wolfSSL_check_domain),
  62222. #endif
  62223. TEST_DECL(test_wolfSSL_cert_cb),
  62224. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  62225. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  62226. TEST_DECL(test_wolfSSL_sigalg_info),
  62227. /* Can't memory test as tcp_connect aborts. */
  62228. TEST_DECL(test_wolfSSL_SESSION),
  62229. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  62230. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  62231. TEST_DECL(test_wolfSSL_ticket_keys),
  62232. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  62233. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  62234. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  62235. TEST_DECL(test_wolfSSL_verify_mode),
  62236. TEST_DECL(test_wolfSSL_verify_depth),
  62237. TEST_DECL(test_wolfSSL_verify_result),
  62238. TEST_DECL(test_wolfSSL_msg_callback),
  62239. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  62240. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  62241. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  62242. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  62243. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  62244. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  62245. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  62246. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  62247. TEST_DECL(test_wolfSSL_PEM_read),
  62248. TEST_DECL(test_wolfSSL_OpenSSL_version),
  62249. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  62250. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  62251. TEST_DECL(test_CONF_modules_xxx),
  62252. #ifdef OPENSSL_ALL
  62253. TEST_DECL(test_wolfSSL_TXT_DB),
  62254. TEST_DECL(test_wolfSSL_NCONF),
  62255. #endif
  62256. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  62257. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  62258. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  62259. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  62260. TEST_DECL(test_CRYPTO_THREADID_xxx),
  62261. TEST_DECL(test_ENGINE_cleanup),
  62262. /* test the no op functions for compatibility */
  62263. TEST_DECL(test_no_op_functions),
  62264. /* OpenSSL error API tests */
  62265. TEST_DECL(test_ERR_load_crypto_strings),
  62266. #ifdef OPENSSL_ALL
  62267. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  62268. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  62269. TEST_DECL(test_wolfSSL_CTX_ctrl),
  62270. #endif /* OPENSSL_ALL */
  62271. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  62272. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  62273. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  62274. /*********************************
  62275. * Crypto API tests
  62276. *********************************/
  62277. TEST_DECL(test_wolfSSL_MD4),
  62278. TEST_DECL(test_wolfSSL_MD5),
  62279. TEST_DECL(test_wolfSSL_MD5_Transform),
  62280. TEST_DECL(test_wolfSSL_SHA),
  62281. TEST_DECL(test_wolfSSL_SHA_Transform),
  62282. TEST_DECL(test_wolfSSL_SHA224),
  62283. TEST_DECL(test_wolfSSL_SHA256),
  62284. TEST_DECL(test_wolfSSL_SHA256_Transform),
  62285. TEST_DECL(test_wolfSSL_SHA512_Transform),
  62286. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  62287. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  62288. TEST_DECL(test_wolfSSL_HMAC_CTX),
  62289. TEST_DECL(test_wolfSSL_HMAC),
  62290. TEST_DECL(test_wolfSSL_CMAC),
  62291. TEST_DECL(test_wolfSSL_DES),
  62292. TEST_DECL(test_wolfSSL_DES_ncbc),
  62293. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  62294. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  62295. TEST_DECL(test_wolfSSL_AES_encrypt),
  62296. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  62297. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  62298. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  62299. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  62300. TEST_DECL(test_wolfSSL_RC4),
  62301. TEST_DECL(test_wolfSSL_RSA),
  62302. TEST_DECL(test_wolfSSL_RSA_DER),
  62303. TEST_DECL(test_wolfSSL_RSA_print),
  62304. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  62305. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  62306. TEST_DECL(test_wolfSSL_RSA_get0_key),
  62307. TEST_DECL(test_wolfSSL_RSA_meth),
  62308. TEST_DECL(test_wolfSSL_RSA_verify),
  62309. TEST_DECL(test_wolfSSL_RSA_sign),
  62310. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  62311. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  62312. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  62313. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  62314. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  62315. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  62316. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  62317. TEST_DECL(test_wolfSSL_RSA_ex_data),
  62318. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  62319. TEST_DECL(test_wolfSSL_RSA_To_Der),
  62320. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  62321. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  62322. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  62323. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  62324. TEST_DECL(test_wolfSSL_DH),
  62325. TEST_DECL(test_wolfSSL_DH_dup),
  62326. TEST_DECL(test_wolfSSL_DH_check),
  62327. TEST_DECL(test_wolfSSL_DH_prime),
  62328. TEST_DECL(test_wolfSSL_DH_1536_prime),
  62329. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  62330. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  62331. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  62332. TEST_DECL(test_wolfSSL_d2i_DHparams),
  62333. TEST_DECL(test_wolfSSL_DH_LoadDer),
  62334. TEST_DECL(test_wolfSSL_i2d_DHparams),
  62335. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  62336. TEST_DECL(test_wolfSSL_EC_GROUP),
  62337. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  62338. TEST_DECL(test_wolfSSL_EC_POINT),
  62339. TEST_DECL(test_wolfSSL_SPAKE),
  62340. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  62341. TEST_DECL(test_EC_i2d),
  62342. TEST_DECL(test_wolfSSL_EC_curve),
  62343. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  62344. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  62345. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  62346. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  62347. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  62348. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  62349. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  62350. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  62351. TEST_DECL(test_ECDSA_size_sign),
  62352. TEST_DECL(test_ECDH_compute_key),
  62353. #endif
  62354. #ifdef OPENSSL_EXTRA
  62355. TEST_DECL(test_ED25519),
  62356. TEST_DECL(test_ED448),
  62357. #endif
  62358. TEST_DECL(test_DSA_do_sign_verify),
  62359. #ifdef OPENSSL_ALL
  62360. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  62361. TEST_DECL(test_wolfSSL_DSA_SIG),
  62362. #endif
  62363. TEST_DECL(test_openssl_generate_key_and_cert),
  62364. TEST_DECL(test_wolfSSL_FIPS_mode),
  62365. TEST_DECL(test_openssl_FIPS_drbg),
  62366. /*********************************
  62367. * CertManager API tests
  62368. *********************************/
  62369. TEST_DECL(test_wolfSSL_CertManagerAPI),
  62370. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  62371. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  62372. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  62373. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  62374. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  62375. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  62376. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  62377. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  62378. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  62379. TEST_DECL(test_wolfSSL_CertManagerCRL),
  62380. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  62381. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  62382. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  62383. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  62384. !defined(WOLFSSL_NO_CLIENT_AUTH))
  62385. TEST_DECL(test_various_pathlen_chains),
  62386. #endif
  62387. /*********************************
  62388. * SSL/TLS API tests
  62389. *********************************/
  62390. TEST_DECL(test_wolfSSL_Method_Allocators),
  62391. #ifndef NO_WOLFSSL_SERVER
  62392. TEST_DECL(test_wolfSSL_CTX_new),
  62393. #endif
  62394. TEST_DECL(test_server_wolfSSL_new),
  62395. TEST_DECL(test_client_wolfSSL_new),
  62396. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  62397. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  62398. TEST_DECL(test_for_double_Free),
  62399. #endif
  62400. TEST_DECL(test_wolfSSL_set_options),
  62401. #ifdef WOLFSSL_TLS13
  62402. /* TLS v1.3 API tests */
  62403. TEST_DECL(test_tls13_apis),
  62404. TEST_DECL(test_tls13_cipher_suites),
  62405. #endif
  62406. TEST_DECL(test_wolfSSL_tmp_dh),
  62407. TEST_DECL(test_wolfSSL_ctrl),
  62408. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  62409. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  62410. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  62411. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  62412. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  62413. #endif
  62414. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  62415. TEST_DECL(test_wolfSSL_security_level),
  62416. TEST_DECL(test_wolfSSL_SSL_in_init),
  62417. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  62418. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  62419. TEST_DECL(test_CONF_CTX_FILE),
  62420. TEST_DECL(test_CONF_CTX_CMDLINE),
  62421. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  62422. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  62423. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  62424. /* Bad certificate signature tests */
  62425. TEST_DECL(test_EccSigFailure_cm),
  62426. TEST_DECL(test_RsaSigFailure_cm),
  62427. #endif /* NO_CERTS */
  62428. /* PKCS8 testing */
  62429. TEST_DECL(test_wolfSSL_no_password_cb),
  62430. TEST_DECL(test_wolfSSL_PKCS8),
  62431. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  62432. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  62433. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  62434. TEST_DECL(test_wolfSSL_get_finished),
  62435. /* Uses Assert in handshake callback. */
  62436. TEST_DECL(test_wolfSSL_CTX_add_session),
  62437. /* Large number of memory allocations. */
  62438. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  62439. /* Large number of memory allocations. */
  62440. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  62441. /* Large number of memory allocations. */
  62442. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  62443. /* Large number of memory allocations. */
  62444. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  62445. /* Large number of memory allocations. */
  62446. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  62447. /* Large number of memory allocations. */
  62448. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  62449. #endif
  62450. TEST_DECL(test_SSL_CIPHER_get_xxx),
  62451. TEST_DECL(test_wolfSSL_ERR_strings),
  62452. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  62453. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  62454. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  62455. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  62456. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  62457. /* Large number of memory allocations. */
  62458. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  62459. TEST_DECL(test_wolfSSL_CertRsaPss),
  62460. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  62461. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  62462. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  62463. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  62464. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  62465. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  62466. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  62467. TEST_DECL(test_multiple_crls_same_issuer),
  62468. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  62469. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  62470. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  62471. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  62472. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  62473. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  62474. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  62475. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  62476. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  62477. TEST_DECL(test_SetTmpEC_DHE_Sz),
  62478. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  62479. #ifdef WOLFSSL_DTLS
  62480. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  62481. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  62482. #endif
  62483. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  62484. /* Uses Assert in handshake callback. */
  62485. TEST_DECL(test_wolfSSL_dtls_plaintext),
  62486. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  62487. defined(HAVE_IO_TESTS_DEPENDENCIES)
  62488. TEST_DECL(test_wolfSSL_read_write),
  62489. /* Can't memory test as server hangs if client fails before second connect.
  62490. */
  62491. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  62492. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  62493. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  62494. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  62495. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  62496. /* Can't memory test as server hangs. */
  62497. TEST_DECL(test_wolfSSL_dtls_export),
  62498. /* Uses Assert in handshake callback. */
  62499. TEST_DECL(test_wolfSSL_tls_export),
  62500. #endif
  62501. TEST_DECL(test_wolfSSL_dtls_export_peers),
  62502. TEST_DECL(test_wolfSSL_SetMinVersion),
  62503. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  62504. /* wolfSSL handshake APIs. */
  62505. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  62506. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  62507. TEST_DECL(test_wolfSSL_BUF),
  62508. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  62509. /* Can't memory test as server hangs. */
  62510. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  62511. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  62512. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  62513. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  62514. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  62515. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  62516. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  62517. /* Can't memory test as server hangs. */
  62518. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  62519. TEST_DECL(test_wolfSSL_Tls13_postauth),
  62520. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  62521. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  62522. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  62523. TEST_DECL(test_wolfSSL_THREADID_hash),
  62524. /* TLS extensions tests */
  62525. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  62526. #ifdef HAVE_SNI
  62527. TEST_DECL(test_wolfSSL_UseSNI_params),
  62528. /* Uses Assert in handshake callback. */
  62529. TEST_DECL(test_wolfSSL_UseSNI_connection),
  62530. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  62531. #endif /* HAVE_SNI */
  62532. #endif
  62533. TEST_DECL(test_wolfSSL_UseTrustedCA),
  62534. TEST_DECL(test_wolfSSL_UseMaxFragment),
  62535. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  62536. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  62537. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  62538. /* Uses Assert in handshake callback. */
  62539. TEST_DECL(test_wolfSSL_UseALPN_connection),
  62540. TEST_DECL(test_wolfSSL_UseALPN_params),
  62541. #endif
  62542. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  62543. /* Uses Assert in handshake callback. */
  62544. TEST_DECL(test_wolfSSL_set_alpn_protos),
  62545. #endif
  62546. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  62547. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  62548. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  62549. TEST_DECL(test_tls_ext_duplicate),
  62550. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  62551. defined(HAVE_IO_TESTS_DEPENDENCIES)
  62552. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  62553. /* Uses Assert in handshake callback. */
  62554. TEST_DECL(test_wolfSSL_Tls13_ECH),
  62555. #endif
  62556. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  62557. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  62558. /* OCSP Stapling */
  62559. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  62560. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  62561. TEST_DECL(test_self_signed_stapling),
  62562. /* Multicast */
  62563. TEST_DECL(test_wolfSSL_mcast),
  62564. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  62565. TEST_DECL(test_wolfSSL_msgCb),
  62566. TEST_DECL(test_wolfSSL_either_side),
  62567. TEST_DECL(test_wolfSSL_DTLS_either_side),
  62568. /* Uses Assert in handshake callback. */
  62569. TEST_DECL(test_wolfSSL_dtls_fragments),
  62570. /* Uses Assert in handshake callback. */
  62571. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  62572. /* Uses Assert in handshake callback. */
  62573. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  62574. /* Uses Assert in handshake callback. */
  62575. TEST_DECL(test_wolfSSL_dtls_bad_record),
  62576. /* Uses Assert in handshake callback. */
  62577. TEST_DECL(test_wolfSSL_dtls_stateless),
  62578. TEST_DECL(test_generate_cookie),
  62579. #ifndef NO_BIO
  62580. /* Can't memory test as server hangs. */
  62581. TEST_DECL(test_wolfSSL_BIO_connect),
  62582. /* Can't memory test as server Asserts in thread. */
  62583. TEST_DECL(test_wolfSSL_BIO_accept),
  62584. TEST_DECL(test_wolfSSL_BIO_tls),
  62585. #endif
  62586. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  62587. TEST_DECL(test_DhCallbacks),
  62588. #endif
  62589. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62590. TEST_DECL(test_export_keying_material),
  62591. #endif
  62592. /* Can't memory test as client/server Asserts in thread. */
  62593. TEST_DECL(test_ticket_and_psk_mixing),
  62594. /* Can't memory test as client/server Asserts in thread. */
  62595. TEST_DECL(test_prioritize_psk),
  62596. /* Can't memory test as client/server hangs. */
  62597. TEST_DECL(test_wc_CryptoCb),
  62598. /* Can't memory test as client/server hangs. */
  62599. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  62600. #if !defined(NO_FILESYSTEM) && \
  62601. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  62602. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  62603. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  62604. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  62605. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  62606. #ifdef HAVE_MAX_FRAGMENT
  62607. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  62608. #endif /* HAVE_MAX_FRAGMENT */
  62609. #ifndef NO_RSA
  62610. TEST_DECL(test_wolfSSL_dtls_stateless2),
  62611. #if !defined(NO_OLD_TLS)
  62612. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  62613. #endif /* !defined(NO_OLD_TLS) */
  62614. #endif /* ! NO_RSA */
  62615. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  62616. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  62617. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  62618. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  62619. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  62620. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  62621. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  62622. && defined(WOLFSSL_TLS13) && \
  62623. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  62624. TEST_DECL(test_ticket_nonce_malloc),
  62625. #endif
  62626. TEST_DECL(test_ticket_ret_create),
  62627. TEST_DECL(test_extra_alerts_wrong_cs),
  62628. TEST_DECL(test_extra_alerts_skip_hs),
  62629. TEST_DECL(test_extra_alerts_bad_psk),
  62630. TEST_DECL(test_tls13_bad_psk_binder),
  62631. /* Can't memory test as client/server Asserts. */
  62632. TEST_DECL(test_harden_no_secure_renegotiation),
  62633. TEST_DECL(test_override_alt_cert_chain),
  62634. TEST_DECL(test_rpk_set_xxx_cert_type),
  62635. TEST_DECL(test_tls13_rpk_handshake),
  62636. TEST_DECL(test_dtls13_bad_epoch_ch),
  62637. TEST_DECL(test_short_session_id),
  62638. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  62639. /* Can't memory test as client/server hangs. */
  62640. TEST_DECL(test_dtls_msg_from_other_peer),
  62641. TEST_DECL(test_dtls_ipv6_check),
  62642. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  62643. TEST_DECL(test_dtls_no_extensions),
  62644. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  62645. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  62646. TEST_DECL(test_session_ticket_no_id),
  62647. TEST_DECL(test_session_ticket_hs_update),
  62648. TEST_DECL(test_dtls_downgrade_scr_server),
  62649. TEST_DECL(test_dtls_downgrade_scr),
  62650. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  62651. TEST_DECL(test_dtls_client_hello_timeout),
  62652. TEST_DECL(test_dtls_dropped_ccs),
  62653. TEST_DECL(test_dtls_seq_num_downgrade),
  62654. TEST_DECL(test_certreq_sighash_algos),
  62655. TEST_DECL(test_revoked_loaded_int_cert),
  62656. TEST_DECL(test_dtls_frag_ch),
  62657. TEST_DECL(test_dtls13_frag_ch_pq),
  62658. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  62659. TEST_DECL(test_tls13_pq_groups),
  62660. TEST_DECL(test_tls13_early_data),
  62661. TEST_DECL(test_tls_multi_handshakes_one_record),
  62662. TEST_DECL(test_write_dup),
  62663. TEST_DECL(test_read_write_hs),
  62664. TEST_DECL(test_get_signature_nid),
  62665. TEST_DECL(test_tls_cert_store_unchanged),
  62666. /* This test needs to stay at the end to clean up any caches allocated. */
  62667. TEST_DECL(test_wolfSSL_Cleanup)
  62668. };
  62669. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  62670. static void TestSetup(void)
  62671. {
  62672. /* Stub, for now. Add common test setup code here. */
  62673. }
  62674. static void TestCleanup(void)
  62675. {
  62676. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  62677. /* Clear any errors added to the error queue during the test run. */
  62678. wolfSSL_ERR_clear_error();
  62679. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  62680. }
  62681. /* Print out all API test cases with numeric identifier.
  62682. */
  62683. void ApiTest_PrintTestCases(void)
  62684. {
  62685. int i;
  62686. printf("All Test Cases:\n");
  62687. for (i = 0; i < TEST_CASE_CNT; i++) {
  62688. printf("%3d: %s\n", i + 1, testCases[i].name);
  62689. }
  62690. }
  62691. /* Add test case with index to the list to run.
  62692. *
  62693. * @param [in] idx Index of test case to run starting at 1.
  62694. * @return 0 on success.
  62695. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  62696. */
  62697. int ApiTest_RunIdx(int idx)
  62698. {
  62699. if (idx < 1 || idx > TEST_CASE_CNT) {
  62700. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  62701. return BAD_FUNC_ARG;
  62702. }
  62703. testAll = 0;
  62704. testCases[idx-1].run = 1;
  62705. return 0;
  62706. }
  62707. /* Add test case with name to the list to run.
  62708. *
  62709. * @param [in] name Name of test case to run.
  62710. * @return 0 on success.
  62711. * @return BAD_FUNC_ARG when name is not a known test case name.
  62712. */
  62713. int ApiTest_RunName(char* name)
  62714. {
  62715. int i;
  62716. for (i = 0; i < TEST_CASE_CNT; i++) {
  62717. if (XSTRCMP(testCases[i].name, name) == 0) {
  62718. testAll = 0;
  62719. testCases[i].run = 1;
  62720. return 0;
  62721. }
  62722. }
  62723. printf("Test case name not found: %s\n", name);
  62724. printf("Use --list to see all test case names.\n");
  62725. return BAD_FUNC_ARG;
  62726. }
  62727. /* Converts the result code to a string.
  62728. *
  62729. * @param [in] res Test result code.
  62730. * @return String describing test result.
  62731. */
  62732. static const char* apitest_res_string(int res)
  62733. {
  62734. const char* str = "invalid result";
  62735. switch (res) {
  62736. case TEST_SUCCESS:
  62737. str = "passed";
  62738. break;
  62739. case TEST_FAIL:
  62740. str = "failed";
  62741. break;
  62742. case TEST_SKIPPED:
  62743. str = "skipped";
  62744. break;
  62745. }
  62746. return str;
  62747. }
  62748. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  62749. static double gettime_secs(void)
  62750. #if defined(_MSC_VER) && defined(_WIN32)
  62751. {
  62752. /* there's no gettimeofday for Windows, so we'll use system time */
  62753. #define EPOCH_DIFF 11644473600LL
  62754. FILETIME currentFileTime;
  62755. GetSystemTimePreciseAsFileTime(&currentFileTime);
  62756. ULARGE_INTEGER uli = { 0, 0 };
  62757. uli.LowPart = currentFileTime.dwLowDateTime;
  62758. uli.HighPart = currentFileTime.dwHighDateTime;
  62759. /* Convert to seconds since Unix epoch */
  62760. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  62761. }
  62762. #else
  62763. {
  62764. struct timeval tv;
  62765. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  62766. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  62767. }
  62768. #endif
  62769. #endif
  62770. int ApiTest(void)
  62771. {
  62772. int i;
  62773. int ret;
  62774. int res = 0;
  62775. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  62776. double timeDiff;
  62777. #endif
  62778. printf(" Begin API Tests\n");
  62779. fflush(stdout);
  62780. /* we must perform init and cleanup if not all tests are running */
  62781. if (!testAll) {
  62782. #ifdef WOLFCRYPT_ONLY
  62783. if (wolfCrypt_Init() != 0) {
  62784. printf("wolfCrypt Initialization failed\n");
  62785. res = 1;
  62786. }
  62787. #else
  62788. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  62789. printf("wolfSSL Initialization failed\n");
  62790. res = 1;
  62791. }
  62792. #endif
  62793. }
  62794. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  62795. if (res == 0) {
  62796. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  62797. printf("failed to create tmp dir\n");
  62798. res = 1;
  62799. }
  62800. else {
  62801. tmpDirNameSet = 1;
  62802. }
  62803. }
  62804. #endif
  62805. if (res == 0) {
  62806. for (i = 0; i < TEST_CASE_CNT; ++i) {
  62807. EXPECT_DECLS;
  62808. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  62809. currentTestName = testCases[i].name;
  62810. #endif
  62811. /* When not testing all cases then skip if not marked for running.
  62812. */
  62813. if (!testAll && !testCases[i].run) {
  62814. continue;
  62815. }
  62816. TestSetup();
  62817. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  62818. fflush(stdout);
  62819. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  62820. timeDiff = gettime_secs();
  62821. #endif
  62822. ret = testCases[i].func();
  62823. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  62824. timeDiff = gettime_secs() - timeDiff;
  62825. #endif
  62826. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  62827. if (ret != TEST_SKIPPED) {
  62828. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  62829. }
  62830. else
  62831. #endif
  62832. {
  62833. printf(" %s\n", apitest_res_string(ret));
  62834. }
  62835. fflush(stdout);
  62836. /* if return code is < 0 and not skipped then assert error */
  62837. Expect((ret > 0 || ret == TEST_SKIPPED),
  62838. ("Test failed\n"),
  62839. ("ret %d", ret));
  62840. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  62841. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  62842. TestCleanup();
  62843. }
  62844. }
  62845. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  62846. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  62847. wc_ecc_fp_free(); /* free per thread cache */
  62848. #endif
  62849. if (!testAll) {
  62850. #ifdef WOLFCRYPT_ONLY
  62851. wolfCrypt_Cleanup();
  62852. #else
  62853. wolfSSL_Cleanup();
  62854. #endif
  62855. }
  62856. (void)testDevId;
  62857. if (res != 0) {
  62858. printf("\nFAILURES:\n");
  62859. for (i = 0; i < TEST_CASE_CNT; ++i) {
  62860. if (testCases[i].fail) {
  62861. printf(" %3d: %s\n", i + 1, testCases[i].name);
  62862. }
  62863. }
  62864. printf("\n");
  62865. fflush(stdout);
  62866. }
  62867. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  62868. if (tmpDirNameSet) {
  62869. printf("\nBinary dumps of the memio streams can be found in the\n"
  62870. "%s directory. This can be imported into\n"
  62871. "Wireshark by transforming the file with\n"
  62872. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  62873. "And then loading test_output.dump.hex into Wireshark using\n"
  62874. "the \"Import from Hex Dump...\" option and selecting the\n"
  62875. "TCP encapsulation option.\n", tmpDirName);
  62876. }
  62877. #endif
  62878. printf(" End API Tests\n");
  62879. fflush(stdout);
  62880. return res;
  62881. }